# Parallel for loop in freefem

**URL:** <https://community.freefem.org/t/parallel-for-loop-in-freefem/1204>\
**Category:** General Discussion\
**Created:** [September 14, 2021, 8:20am UTC](https://community.freefem.org/t/parallel-for-loop-in-freefem/1204 "2021-09-14T08:20:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![distractor](https://avatars.discourse-cdn.com/v4/letter/d/57b2e6/32.png) [@distractor](https://community.freefem.org/u/distractor)\
**Post date:** [September 14, 2021, 8:20am UTC](https://community.freefem.org/t/parallel-for-loop-in-freefem/1204/1 "2021-09-14T08:20:08Z")

</div>

Hi,

Is there any easy way to run a `for` loop in parallel in FreeFem++?

For example the majority of code below could be run in parallel:

```auto
int N = 1e6;
real[int] a(N), b(N);

// This loop could be parallel:
for (int i = 0; i < N; i++) {
  a(i) = i;
  b(i) = -i;
}

real[int] c(N);
// Also this could be parallel:
for (int i = 0; i < N; i++) {
  c(i) = a(i) - b(i);
}

```

---

<div class="post-metadata">

**Author:** ![Markov](https://avatars.discourse-cdn.com/v4/letter/m/f1d935/32.png) [@Markov](https://community.freefem.org/u/Markov)\
**Post date:** [September 14, 2021, 11:58am UTC](https://community.freefem.org/t/parallel-for-loop-in-freefem/1204/2 "2021-09-14T11:58:09Z")

</div>

you can use MPI and run each loop on a proc

---

<div class="post-metadata">

**Author:** ![distractor](https://avatars.discourse-cdn.com/v4/letter/d/57b2e6/32.png) [@distractor](https://community.freefem.org/u/distractor)\
**Post date:** [September 14, 2021, 12:02pm UTC](https://community.freefem.org/t/parallel-for-loop-in-freefem/1204/3 "2021-09-14T12:02:40Z")

</div>

Are there any simple examples on that?

---

<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:** [September 15, 2021, 8:27am UTC](https://community.freefem.org/t/parallel-for-loop-in-freefem/1204/4 "2021-09-15T08:27:38Z")

</div>

> [@Solving a lot of independent ODEs in parallel](https://community.freefem.org/t/solving-a-lot-of-independent-odes-in-parallel/518/2):
>
> Use MPI, it’s an embarrassingly parallel job, so trivial to parallelize. int ntask = 200; for(int i = (mpirank \* ntask) / mpisize; i \< ((mpirank + 1) \* ntask) / mpisize; ++i) { cout \<\< "process #" \<\< mpirank \<\< " dealing with task #" \<\< i \<\< endl; } mpiAllreduce(...); // for synchronization

---

<div class="post-metadata">

**Author:** ![Alex6](https://avatars.discourse-cdn.com/v4/letter/a/58956e/32.png) [@Alex6](https://community.freefem.org/u/Alex6)\
**Post date:** [March 5, 2023, 11:46am UTC](https://community.freefem.org/t/parallel-for-loop-in-freefem/1204/5 "2023-03-05T11:46:05Z")

</div>

Hello, distractor. I am a starter and I want to run a for loop in parallel, too! It seems that you have succeeded and would you please show your codes to me? Thanks a lot!

---

<div class="post-metadata">

**Author:** ![marchywka](https://avatars.discourse-cdn.com/v4/letter/m/ee59a6/32.png) [@marchywka](https://community.freefem.org/u/marchywka)\
**Post date:** [March 5, 2023, 1:18pm UTC](https://community.freefem.org/t/parallel-for-loop-in-freefem/1204/6 "2023-03-05T13:18:35Z")

</div>

You can look at the examples, search for “mpirank” but if you do this be sure  
to check the wall clock time. AFAICT, this generates processes and  
communicators etc. For something short you may benefit more from  
thrads communicating with shared memory. In my c+ code I use openmp  
compiler directives and they seem to generate some speed up although  
I have not quantified it ( generally this is a last resort for me as parallel  
has overhead even from threads but mpi processes don’t even have to run  
on the same LAN lol )  
And I’d also note that these things take over cores- at the end all the cores would  
be at 100 percent CPU in spin wait lol.

Maybe someone can comment on thread vs process in FF as I have not looked  
too closely at that yet.
