subroutine distwork(myid,numprocs,nmax,mystart,myend,my_veclen) ! There are numprocs of MPI processes calling this routine ! Distribute different sections to the processes implicit none ! Input variables integer :: myid,nmax,numprocs ! Output variables integer :: mystart,myend ! Local variables integer :: rest, my_veclen rest = mod(nmax,numprocs) ! if(rest.gt.numprocs/2) then if(rest .ne. 0) then ! If each process takes my_veclen, total will be bigger than nmax my_veclen = (nmax+(numprocs-rest))/numprocs mystart = 1 + myid*my_veclen myend = mystart + my_veclen - 1 if(myid.eq.numprocs-1) myend = nmax else ! If each process takes my_veclen, total will be less than nmax my_veclen = nmax/numprocs mystart = 1 + myid*my_veclen myend = mystart + my_veclen - 1 if(myid.eq.numprocs-1) myend = nmax endif end subroutine distwork