# Automatic parallel

**URL:** <https://community.freefem.org/t/automatic-parallel/951>\
**Category:** General Discussion\
**Created:** [April 28, 2021, 4:37am UTC](https://community.freefem.org/t/automatic-parallel/951 "2021-04-28T04:37:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![xiaodizhang29](https://avatars.discourse-cdn.com/v4/letter/x/e99b99/32.png) [@xiaodizhang29](https://community.freefem.org/u/xiaodizhang29)\
**Post date:** [April 28, 2021, 4:37am UTC](https://community.freefem.org/t/automatic-parallel/951/1 "2021-04-28T04:37:35Z")

</div>

Hello, everyone! Is there some tricks on FreeFEM such that I write codes in serial, but it works in parallel.

---

<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:** [April 28, 2021, 6:55am UTC](https://community.freefem.org/t/automatic-parallel/951/2 "2021-04-28T06:55:55Z")

</div>

There is very little to be done to get a sequential script working in parallel using the PETSc interface.  
Here is an example for the Poisson equation.

```auto
mesh Th = square(100, 100);
fespace Vh(Th, P1);
varf vPb(u, v) = int2d(Th)(dx(u)*dx(v) + dy(u)*dy(v)) + int2d(Th)(v) + on(1, u = 0);
matrix A;
A = vPb(Vh, Vh);
real[int] rhs = vPb(0, Vh);
Vh sol;
sol[] = A^-1 * rhs;
plot(sol, cmm = "Sequential solution");

```

And in parallel.

```auto
load "PETSc"
include "macro_ddm.idp"
mesh Th = square(100, 100);
fespace Vh(Th, P1);
varf vPb(u, v) = int2d(Th)(dx(u)*dx(v) + dy(u)*dy(v)) + int2d(Th)(v) + on(1, u = 0);
Mat A;
createMat(Th, A, P1);
real[int] rhs = vPb(0, Vh);
A = vPb(Vh, Vh);
Vh sol;
sol[] = A^-1 * rhs;
plotD(Th, sol, cmm = "Parallel solution");

```

If you want to understand this better, you could have a look [here](https://www.youtube.com/watch?v=-Aw2O46V2bo) and [there](https://www.youtube.com/watch?v=Hx57gxTOnvE).

---

<div class="post-metadata">

**Author:** ![xiaodizhang29](https://avatars.discourse-cdn.com/v4/letter/x/e99b99/32.png) [@xiaodizhang29](https://community.freefem.org/u/xiaodizhang29)\
**Post date:** [April 28, 2021, 2:02pm UTC](https://community.freefem.org/t/automatic-parallel/951/3 "2021-04-28T14:02:04Z")

</div>

Thanks for your reply. How about multiple variables such as Stokes equations, and do we have to use a matrix?

---

<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:** [April 28, 2021, 2:04pm UTC](https://community.freefem.org/t/automatic-parallel/951/4 "2021-04-28T14:04:32Z")

</div>

It works the same, see [FreeFem-sources/stokes-2d-PETSc.edp at develop · FreeFem/FreeFem-sources · GitHub](https://github.com/FreeFem/FreeFem-sources/blob/develop/examples/hpddm/stokes-2d-PETSc.edp). By “do we have to use a matrix?”, are you asking if it works with the keyword `solve` instead of `varf`? If that is the case, then no, it does not work with `solve` and you have to assemble `varf` instead. Note that any `solve` can be converted into a `varf`.

---

<div class="post-metadata">

**Author:** ![xiaodizhang29](https://avatars.discourse-cdn.com/v4/letter/x/e99b99/32.png) [@xiaodizhang29](https://community.freefem.org/u/xiaodizhang29)\
**Post date:** [April 28, 2021, 2:55pm UTC](https://community.freefem.org/t/automatic-parallel/951/5 "2021-04-28T14:55:44Z")

</div>

Thanks for your kindness!
