title: Using MPI in subroutines
body: <p>I'm working on code that serially calls a subroutine (which in turn performs iterations) many times. I wish to parallelize the iterations inside the subroutine. The problem with mpi is that I'm only allowed to initialize it once. Hence, I cannot initialize it in my subroutine, which gets called multiple times. Can anyone suggest a way out of this?</p>
  
  <p>My problem is roughly as outlined below:</p>
  
  <pre><code>program p
  
  ...
  
  do i=1,10000
      call subroutine s(i)
  end do
  
  end program p
  
  subroutine s(j)  
  
  ...
  
  do i=1,10000
      ...
  end do
  
  end subroutine s
  </code></pre>
  
  <p>I wish to parallelize this process. </p>
  
  <p>Thanks a lot. That helped! But let me re frame my question, 
  Within the iterations of the main program, along with the subroutine s, I have to call another subroutine s2, (which doesn't need to be parallelized). I thought, it could be done this way:</p>
  
  <pre><code>  !initialize mpi
  
    do i=1:1000
  
    if rank!=0
  
    call s
  
    else call s2
  
    end if
  
    end do
  
    !finalize mpi
  </code></pre>
  
  <p>But the main problem here is, while the rest of the processes proceed slowly, process 0 will proceed quickly. (Something not desirable).So, is it possible to make process 0 wait after each iteration till the other process complete their iteration? </p>