# Assembling in Parallel and Solving in Serial

**URL:** <https://fenicsproject.discourse.group/t/assembling-in-parallel-and-solving-in-serial/951>\
**Category:** Linear Algebra\
**Created:** [June 6, 2019, 5:05pm UTC](https://fenicsproject.discourse.group/t/assembling-in-parallel-and-solving-in-serial/951 "2019-06-06T17:05:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jared\_C](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/jared_c/32/90_2.png) [@Jared\_C](https://fenicsproject.discourse.group/u/Jared_C)\
**Post date:** [June 6, 2019, 5:05pm UTC](https://fenicsproject.discourse.group/t/assembling-in-parallel-and-solving-in-serial/951/1 "2019-06-06T17:05:25Z")

</div>

I am working with a problem where it takes a long time to assemble the vector u in Ax==u. Running the assemble in parallel does a good job with decreasing the time it takes to assemble, but doing so increases the time it takes to solve the problem. Is it possible for me to assemble u vector in parallel but then solve it with one core or is there something else I should be trying?

---

<div class="post-metadata">

**Author:** ![kamensky](https://avatars.discourse-cdn.com/v4/letter/k/e95f7d/32.png) [@kamensky](https://fenicsproject.discourse.group/u/kamensky)\
**Post date:** [June 6, 2019, 5:42pm UTC](https://fenicsproject.discourse.group/t/assembling-in-parallel-and-solving-in-serial/951/2 "2019-06-06T17:42:58Z")

</div>

Does the solution from this thread help?

> [@Mumps in parallel slower than in serial](https://fenicsproject.discourse.group/t/mumps-in-parallel-slower-than-in-serial/662/2):
>
> Hey, I had a similar problem which is due to petsc (or in particular its mumps solver) using thread parallelism via OpenMP. Let us say your PC has n threads available. Then, when you run your program in serial it already uses n threads (afaik this is the default). Using mpirun with m instances now lets each of these create another n openmp threads, so you have in total m\*n openmp threads, allthough your system can only support n. Therefore, you are slower. You can fix this behaviour setting …

---

<div class="post-metadata">

**Author:** ![Jared\_C](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/jared_c/32/90_2.png) [@Jared\_C](https://fenicsproject.discourse.group/u/Jared_C)\
**Post date:** [June 6, 2019, 7:17pm UTC](https://fenicsproject.discourse.group/t/assembling-in-parallel-and-solving-in-serial/951/3 "2019-06-06T19:17:38Z")

</div>

That did help with the solver not increasing as much, although it still does increase by a little bit. This is very helpful but I still wonder if there is a way to assemble in parallel and solve in serial?

---

<div class="post-metadata">

**Author:** ![KoenFranse](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/koenfranse/32/198_2.png) [@KoenFranse](https://fenicsproject.discourse.group/u/KoenFranse)\
**Post date:** [October 16, 2019, 2:14pm UTC](https://fenicsproject.discourse.group/t/assembling-in-parallel-and-solving-in-serial/951/4 "2019-10-16T14:14:45Z")

</div>

I have the same problem, only I want to assemble in serial and solve in parallel. I assume that for doing something in parallel you always have to run your script with mpirun, but is there any way to perform some of the commands in your script in serial?

---

<div class="post-metadata">

**Author:** ![plugged](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/plugged/32/5115_2.png) [@plugged](https://fenicsproject.discourse.group/u/plugged)\
**Post date:** [October 16, 2019, 2:26pm UTC](https://fenicsproject.discourse.group/t/assembling-in-parallel-and-solving-in-serial/951/5 "2019-10-16T14:26:54Z")

</div>

For some commands you can do this, e.g., by using

```
rank = fenics.MPI.comm_world.Get_rank()

if rank == 0:
    % only processor 0 does this

% the other commands are executed in parallel

```

However, this will not help with your problem directly. What you can do is to use the multithreading ability of PETSc to solve your linear systems in parallel even though you assembled them in serial. For example, just running a script as

```
python scripty.py

```

will execute the assembly in serial, and PETSC will use openmp to parallelize the solve of the linear system, for example with mumps. Sadly, I don’t know which of PETSc’s solvers support openmp (except for mumps), so this might not work for iterative solvers.

For MPI I am not sure if what you want is possible, I suspect that it is not as you might need a domain decomposition for that kind of solver and I guess you only get this by also assembling in parallel.
