# Can using MPI prevent thread-safe issue?

**URL:** <https://fortran-lang.discourse.group/t/can-using-mpi-prevent-thread-safe-issue/2828>\
**Category:** Uncategorized\
**Created:** [February 20, 2022, 2:18am UTC](https://fortran-lang.discourse.group/t/can-using-mpi-prevent-thread-safe-issue/2828 "2022-02-20T02:18:45Z")\
**Posts on this page:** 5\
**Page:** 1

<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:** [February 20, 2022, 2:18am UTC](https://fortran-lang.discourse.group/t/can-using-mpi-prevent-thread-safe-issue/2828/1 "2022-02-20T02:18:45Z")

</div>

Dear all,

A quick question, as I read the comment from @ivanpribec

> [@Is there a good Fortran package for SDE, DDE and ODE?](https://fortran-lang.discourse.group/t/is-there-a-good-fortran-package-for-sde-dde-and-ode/1756/11):
>
> Just to be on the clear side, I was only quoting part of their conclusion. They do also make some remarks in favor of Julia. But perhaps the rebuttal of that paper can go to a new thread or the [Julia Discourse](https://discourse.julialang.org/). To return to the original question, I have personally used the following ODE solvers: [dop853](https://github.com/jacobwilliams/dop853) (thanks to @jacobwilliams) [ddeabm](https://github.com/jacobwilliams/ddeabm) (also thanks to @jacobwilliams) [RKC: An explicit solver for parabolic PDEs](https://www.sciencedirect.com/science/article/pii/S0377042797002197) (available from netlib) [stiff3](https://github.com/ivan-pi/stiff3) In a distant past I also used LSODE from ODEPACK …

He mentioned many old f77 fortran ode solver code are not thread-safe.  
Uhm, I guess that is probably an issue for openMP, since those old code uses some common block perhaps?  
But I wonder, if I use MPI, does the ‘thread-safe’ issue still matter?

It seems in MPI, each core or thread actually run the program independently, and there will be no communication between the cores or threads if I do not let them communicate.  
So, in MPI, is there still thread-safe issue?

Thanks much in advance!

---

<div class="post-metadata">

**Author:** ![sblionel](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/sblionel/32/853_2.png) [@sblionel](https://fortran-lang.discourse.group/u/sblionel)\
**Post date:** [February 20, 2022, 4:09pm UTC](https://fortran-lang.discourse.group/t/can-using-mpi-prevent-thread-safe-issue/2828/2 "2022-02-20T16:09:03Z")

</div>

Answering your specific question, as you say, each MPI “rank” is an independent process (not a thread) so there’s no issue of thread safety. But adding MPI to an application takes significant work, especially if the code uses global variables for communication. (Coarrays might be a better bet, but still requires effort.) You would be better served to look for solvers that were designed to support parallelism.

---

<div class="post-metadata">

**Author:** ![themos](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/themos/32/521_2.png) [@themos](https://fortran-lang.discourse.group/u/themos)\
**Post date:** [February 20, 2022, 6:17pm UTC](https://fortran-lang.discourse.group/t/can-using-mpi-prevent-thread-safe-issue/2828/3 "2022-02-20T18:17:43Z")

</div>

I may be misunderstanding things, but MPI is a library and if you are building your own library on top of MPI, you will have no control over the execution environment. People could be calling your library routine from multiple threads and if any internal data structures are shared, results can be unexpected.

---

<div class="post-metadata">

**Author:** ![ivanpribec](https://yyz2.discourse-cdn.com/free1/user_avatar/fortran-lang.discourse.group/ivanpribec/32/3290_2.png) [@ivanpribec](https://fortran-lang.discourse.group/u/ivanpribec)\
**Post date:** [February 20, 2022, 7:41pm UTC](https://fortran-lang.discourse.group/t/can-using-mpi-prevent-thread-safe-issue/2828/4 "2022-02-20T19:41:50Z")

</div>

> [@CRquantum](#):
>
> He mentioned many old f77 fortran ode solver code are not thread-safe.  
> Uhm, I guess that is probably an issue for openMP, since those old code uses some common block perhaps?  
> But I wonder, if I use MPI, does the ‘thread-safe’ issue still matter?

It’s not only common blocks, it’s also procedure variables with `save` attribute.

If you need to solve multiple systems of parametrized ODE’s (e.g. for different combinations of parameters or many different initial conditions) then OpenMPI or co-arrays could be an option. Here’s a table ([original source](https://www7320.nrlssc.navy.mil/pubs/2002/wallcraft-2002.pdf)) which highlights some differences between SPMD and co-arrays:

 ![slika](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/1/13769a2d35adc3767cbd672311e8950a8ad51fd5.png)

If you need to solve a single large system of ODEs in parallel I’d recommend taking a look at Sundials. Here’s the list of NVector types available in Sundials for different accelerator/parallel programming paradigms:

 ![slika](https://global.discourse-cdn.com/free1/uploads/fortran_lang/original/2X/c/c692725092ea39ff3ac9908f33851055968e555e.png)

---

<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:** [February 20, 2022, 11:35pm UTC](https://fortran-lang.discourse.group/t/can-using-mpi-prevent-thread-safe-issue/2828/5 "2022-02-20T23:35:09Z")

</div>

Thank you very much Dr. Fortran @sblionel and @ivanpribec , @themos .

I see. I usually wrote MPI code and run on supercomputer and never have the thread-safe issue because each rank run independently in MPI.

Roughly speaking, what I did is like solving ODEs from 1000 different initial conditions then average the results. In this case instead of solving the ODEs on a single core for 1000 loops, I run MPI on 1000 cores each with a different initial condition and then collect the results on rank 0 and average the results. In this case, MPI works just fine and it does not matter if the ODE solver is thread-safe or not, because each rank actually run independently. I am not seeking to solve ODEs parallelly.

But I guess I got what you mean. If I were to really solve the ODEs parallelly, then it is better to use those parallel ODE solver. Otherwise modifying those non-parallel ODE solver to make them parallel is not an easy task.
