!***********************************************************************
! ISPACK FORTRAN SUBROUTINE LIBRARY FOR SCIENTIFIC COMPUTING
! Copyright (C) 1998--2019 Keiichi Ishioka <ishioka@gfd-dennou.org>
!
! This library is free software; you can redistribute it and/or
! modify it under the terms of the GNU Lesser General Public
! License as published by the Free Software Foundation; either
! version 2.1 of the License, or (at your option) any later version.
!
! This library is distributed in the hope that it will be useful,
! but WITHOUT ANY WARRANTY; without even the implied warranty of
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
! Lesser General Public License for more details.
! 
! You should have received a copy of the GNU Lesser General Public
! License along with this library; if not, write to the Free Software
! Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
! 02110-1301 USA.
!***********************************************************************
SUBROUTINE SXCSMY(MM,NT,S,SG,C,MI)

  IMPLICIT NONE
  INTEGER(8) :: MM,NT,M,N,NS,NSG,NC,MI
  REAL(8) :: S((MM/MI+1)*(2*(NT+1)-MM/MI*MI))
  REAL(8) :: SG((MM/MI+1)*(2*(NT+2)-MM/MI*MI))
  REAL(8) :: C((MM/MI+1)*(2*(NT+1)-MM/MI*MI))
  INTEGER(8) :: NPMAX
  INTEGER(8) :: MN,M1,N1,K
  
  CALL MXGOMP(NPMAX)  

  N1=MM/MI+1
  M1=N1*MI
  MN=N1
  IF(MM/MI-M1/MI.GE.0) THEN
     MN=MN+1
  END IF

  !$omp parallel private(NS,NSG,NC,M) num_threads(NPMAX)
  !$omp do schedule(dynamic)
  DO K=0,MN-1
     M=K*MI
     IF(M.EQ.0) THEN
        CALL LXCSZY(NT,S,SG,C)        
     ELSE
        NS=K*(2*(NT+1)-(K-1)*MI)+1
        NSG=K*(2*(NT+2)-(K-1)*MI)+1
        NC=K*(2*(NT+1)-(K-1)*MI)+1
        CALL LXCSWY(NT,M,S(NS),SG(NSG),C(NC))
     END IF
  END DO
  !$omp end do
  !$omp end parallel

END SUBROUTINE SXCSMY
