title: How to divide work between processes in MPI
body: <p>I have the following code to count the number of primes, I have divided the work in the loop across processors. The problem is that when the subroutine assigns sub-sections of the loop to processors it assigns according to rank and i cant seem to control the order in which they arrive </p>
  
  <p>i.e  i would like then to come as 0,1,2,3... and not like 2,1,0,3..</p>
  
  <p>So that if there were 500 iterations in the loop and 5 processors. </p>
  
  <ul>
  <li>Rank 0 executes [1 - 100]</li>
  <li><p>Rank 1 executes [101-200] etc...</p>
  
  <pre><code>      program main
        implicit none
        include 'mpif.h'
        integer(4), parameter :: n = 36500
        integer(4) :: a(n)
        integer(4) :: i
        integer(4) :: j
        integer(4) :: ista
        integer(4) :: iend
        integer(4) :: sum
        integer(4) :: f=0
        integer(4) :: ssum
        integer(4) :: ierr
        integer(4) :: iproc
        integer(4) :: nproc
        call MPI_INIT(ierr)
        call MPI_COMM_SIZE(MPI_COMM_WORLD, nproc, ierr)
        call MPI_COMM_RANK(MPI_COMM_WORLD, iproc, ierr)
        call loop_range(2, n, nproc, iproc, ista, iend)
        sum = 0.0
        print *,ista,"-",iend,"&gt;",iproc
        do i = ista, iend
          f=0
          do j=2,INT(SQRT(REAL(i)))
         if(MOD(i,j)==0) then
         f=1
         end if
         end do
         if(f==0) then
         sum = sum + 1
        end if  
        end do
        call MPI_REDUCE(sum, ssum, 1, MPI_INTEGER,MPI_SUM, 0,MPI_COMM_WORLD, ierr)
        if ( iproc == 0 ) write(6,*)'Total No of primes=', ssum
        call MPI_FINALIZE(ierr)
      end program main
  
      subroutine para_range(n1, n2, nprocs, irank, ista, iend)
        integer(4) :: n1        ! Lowest value of iteration variable
        integer(4) :: n2        ! Highest value of iteration variable
        integer(4) :: nprocs    ! No of Cores/Processors you want to use
        integer(4) :: irank     ! Process rank
        integer(4) :: ista      ! Start of iterations for rank iproc
        integer(4) :: iend      ! End of iterations for rank iproc
        integer(4) :: iwork1, iwork2
  
        print *,irank
        iwork1 = ( n2 - n1 + 1 )  /  nprocs
        iwork2 = MOD(n2 - n1 + 1, nprocs)
        ista = irank * iwork1 + n1 + MIN(irank, iwork2)
        iend = ista + iwork1 - 1
        if ( iwork2 &gt; irank ) then
             iend = iend + 1
        end if
      end subroutine para_range
  </code></pre></li>
  </ul>
  
  <p>I am using Open MPI.</p>