# Enquiry about MPI

**URL:** https://community.freefem.org/t/enquiry-about-mpi/1000
**Category:** General Discussion
**Created:** [May 28, 2021, 8:51am UTC](https://community.freefem.org/t/enquiry-about-mpi/1000 "2021-05-28T08:51:28Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![ligax](https://avatars.discourse-cdn.com/v4/letter/l/dec6dc/32.png) [@ligax](https://community.freefem.org/u/ligax)
#### Post date: [May 28, 2021, 8:51am UTC](https://community.freefem.org/t/enquiry-about-mpi/1000/1 "2021-05-28T08:51:28Z")

</div>

Dear all,

I am new to FreeFEM and have met some problems about MPI recently.  
My algorithm is an iterative one, and in each iteration, there are many sub-problems that are independent with each other, hence I want to solve them in parallel. However, it seems that every step of the whole algorithm is parallelized together, which is not what I want. Would you please tell me whether I can parallelize the program just in each iteration part? Or if not, is there any solution to this problem? Thanks a lot!  
Sincerely,

Liga

---

<div class="post-metadata">

### Author: ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)
#### Post date: [May 28, 2021, 8:56am UTC](https://community.freefem.org/t/enquiry-about-mpi/1000/2 "2021-05-28T08:56:59Z")

</div>

Please look at this very similar post: [Solving a lot of independent ODEs in parallel](https://community.freefem.org/t/solving-a-lot-of-independent-odes-in-parallel/518).

---

<div class="post-metadata">

### Author: ![ligax](https://avatars.discourse-cdn.com/v4/letter/l/dec6dc/32.png) [@ligax](https://community.freefem.org/u/ligax)
#### Post date: [June 1, 2021, 12:32am UTC](https://community.freefem.org/t/enquiry-about-mpi/1000/3 "2021-06-01T00:32:47Z")

</div>

Thank you so much for your reply. I will try it.

---

<div class="post-metadata">

### Author: ![ligax](https://avatars.discourse-cdn.com/v4/letter/l/dec6dc/32.png) [@ligax](https://community.freefem.org/u/ligax)
#### Post date: [June 9, 2021, 2:11am UTC](https://community.freefem.org/t/enquiry-about-mpi/1000/4 "2021-06-09T02:11:52Z")

</div>

Thanks for your reply before. I still have some problems on this topic. My code is of this form:

Stage 1: not in parallel

Stage 2: in parallel

Stage 3: not in parallel

In addition, Stage2 and Stage3 should be included in iterations. Would you please tell me whether I can just parallelize Stage2 or are there any code examples like this? Thanks!

Sincerely yours,  
Liga

---

<div class="post-metadata">

### Author: ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)
#### Post date: [June 9, 2021, 10:12am UTC](https://community.freefem.org/t/enquiry-about-mpi/1000/5 "2021-06-09T10:12:27Z")

</div>

You could simply surround stage 1 and stage 3 with `if(mpirank == 0) { // do stage 1 or 3 }`, but that is extremely wasteful since all other processes will idle. Why are stage 1 and stage 3 not parallel?

---

<div class="post-metadata">

### Author: ![ligax](https://avatars.discourse-cdn.com/v4/letter/l/dec6dc/32.png) [@ligax](https://community.freefem.org/u/ligax)
#### Post date: [June 9, 2021, 10:33am UTC](https://community.freefem.org/t/enquiry-about-mpi/1000/6 "2021-06-09T10:33:10Z")

</div>

Thanks for your reply. Stage 1 produces one solution, which will be used in stage 2. And stage 2 consists of many independent sub-problems, hence it can be parallelized. And stage 3 employs all the solutions of stage 2.

---

<div class="post-metadata">

### Author: ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)
#### Post date: [June 9, 2021, 11:04am UTC](https://community.freefem.org/t/enquiry-about-mpi/1000/7 "2021-06-09T11:04:00Z")

</div>

> Stage 1 produces one solution

Why can’t this be done in parallel?

> Stage 3 employs all the solutions of stage 2

Again, what makes this stage not runnable in parallel?

---

<div class="post-metadata">

### Author: ![ligax](https://avatars.discourse-cdn.com/v4/letter/l/dec6dc/32.png) [@ligax](https://community.freefem.org/u/ligax)
#### Post date: [June 9, 2021, 11:19am UTC](https://community.freefem.org/t/enquiry-about-mpi/1000/8 "2021-06-09T11:19:45Z")

</div>

Sorry. It definitely could be runnable in parallel and could derive the right solution in all processors. I am a begginer of MPI and I am just wondering whether we can parallelize only part of the code.

---

<div class="post-metadata">

### Author: ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)
#### Post date: [June 9, 2021, 11:53am UTC](https://community.freefem.org/t/enquiry-about-mpi/1000/9 "2021-06-09T11:53:11Z")

</div>

I see. It’s actually very easy to parallelize the solution phase using PETSc, you just need to add a couple of keywords. But if you just want to parallelize part of the script, you need to use `if` conditions with `mpirank`, as I wrote earlier. But again, that is a rather bad idea, best to use three different scripts (for stage 1 and stage 3 in sequential, and for stage 2 in parallel).

---

<div class="post-metadata">

### Author: ![ligax](https://avatars.discourse-cdn.com/v4/letter/l/dec6dc/32.png) [@ligax](https://community.freefem.org/u/ligax)
#### Post date: [June 9, 2021, 12:05pm UTC](https://community.freefem.org/t/enquiry-about-mpi/1000/10 "2021-06-09T12:05:04Z")

</div>

That is a nice idea. Thank you so much for your help and patience.
