# Parallelize this subroutine with Open MPI

**URL:** <https://fortran-lang.discourse.group/t/parallelize-this-subroutine-with-open-mpi/2994>\
**Category:** Help\
**Created:** [March 16, 2022, 11:22pm UTC](https://fortran-lang.discourse.group/t/parallelize-this-subroutine-with-open-mpi/2994 "2022-03-16T23:22:50Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![CRquantum](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/crquantum/32/730_2.png) [@CRquantum](https://fortran-lang.discourse.group/u/CRquantum)\
**Post date:** [March 17, 2022, 3:45am UTC](https://fortran-lang.discourse.group/t/parallelize-this-subroutine-with-open-mpi/2994/4 "2022-03-17T03:45:17Z")

</div>

MPI in my opinion, works best if you can clearly or easily distribute almost equal amount of jobs on each of the cores involved. The communications between each cores should be the fewer the better.

If your job is to solve, like a system of 10^4 ODEs, then perhaps you might want to use parallelized solver like Sundials. If this is what you want, I guess the best option is perhaps to just use Sundials instead as suggested by @nicholaswogan, @ivanpribec etal,

> [@Has anyone used DVODE and is it good?](https://fortran-lang.discourse.group/t/has-anyone-used-dvode-and-is-it-good/2281/5):
>
> I’m sure DVODE is great. But, CVODE is probably the best maintained flavor of VODE, with a bunch of different matrix sparsity options. It’s written in C but has a good Fortran interface.

On the other hand, if you want to solve like 3-ODE system for 10^4 times, then say you have 100 cores, you can distribute 100 of such 3-ODE systems in each of the cores.  
In general, I think you may want to isolate the part you want to parallelize

```auto
do j = 1, n
  XXXXXXXXX # you want to parallelize this part
enddo

```

into an independent subroutine first, call it subroutine AAA.

```auto
do j = 1, n
   call AAA(j,...) # AAA needs to be completely and absolutely an independent subroutine. 
enddo

```

Then in MPI it would be more or less like below I guess,

```auto
do i = 0, nproc()-1 # say 100 cores, so from rank0 to rank99
  if (myrank()==i) then
    jstart_i = ...
    jend_i = ...
    do j =jstart_i, jend_i # distribute corresponding jobs from jstart_i to jend_i on rank i 
      call AAA(j,...)  
    enddo
   # when finished exit the do loop
  endif
enddo

```

Or perhaps you do not need the loop at all. Just directly let each rank do the subroutine AAA with the jobs they are assigned.

---

_[View the full topic](https://fortran-lang.discourse.group/t/parallelize-this-subroutine-with-open-mpi/2994)._
