# Parallelizing Legacy Fenics Code

**URL:** <https://fenicsproject.discourse.group/t/parallelizing-legacy-fenics-code/18472>\
**Category:** Legacy DOLFIN\
**Created:** [December 16, 2025, 7:25am UTC](https://fenicsproject.discourse.group/t/parallelizing-legacy-fenics-code/18472 "2025-12-16T07:25:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![zaccandels](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/zaccandels/32/6204_2.png) [@zaccandels](https://fenicsproject.discourse.group/u/zaccandels)\
**Post date:** [December 16, 2025, 7:25am UTC](https://fenicsproject.discourse.group/t/parallelizing-legacy-fenics-code/18472/1 "2025-12-16T07:25:11Z")

</div>

Here I’m using FEniCS 2019.1.0. For the MWE pasted below, the performance I see upon executing the program using `mpirun -np 4 python3 program.py` is actually _worse_ than when I run the program in serially.

What do I have to change to see a meaningful speedup?

```auto
from __future__ import print_function
import fenics as fe
comm = fe.MPI.comm_world
rank = fe.MPI.rank(comm)

T = 2.0 # final time
num_steps = 1000 # number of time steps
dt = T / num_steps # time step size

# Create mesh and define function space
nx = ny = 30
mesh = fe.RectangleMesh(comm, fe.Point(-2, -2), fe.Point(2, 2), nx, ny)
V = fe.FunctionSpace(mesh, 'P', 1)

# Define boundary condition
def boundary(x, on_boundary):
    return on_boundary

bc = fe.DirichletBC(V, fe.Constant(0), boundary)

# Define initial value
u_0 = fe.Expression('exp(-a*pow(x[0], 2) - a*pow(x[1], 2))',
                 degree=2, a=5)
u_n = fe.interpolate(u_0, V)

# Define variational problem
u = fe.TrialFunction(V)
v = fe.TestFunction(V)
f = fe.Constant(0)

bilin = u*v*fe.dx + dt*fe.dot(fe.grad(u), fe.grad(v))*fe.dx
lin = (u_n + dt*f)*v*fe.dx

A = fe.assemble(bilin)
b = fe.assemble(lin)

solver = fe.KrylovSolver("cg", "hypre_amg")
solver.set_operator(A)

outfile = fe.XDMFFile(comm, "solution.xdmf")
outfile.parameters["flush_output"] = True
outfile.parameters["functions_share_mesh"] = True
outfile.parameters["rewrite_function_mesh"] = False

# Time-stepping
u = fe.Function(V)
t = 0
for n in range(num_steps):

    # Update current time
    t += dt

    # Compute solution
    b = fe.assemble(lin)
    bc.apply(A, b)
    solver.solve(u.vector(), b)

    # Update previous solution
    u_n.assign(u)

    # Save to file 
    outfile.write(u_n, t)

```

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [December 16, 2025, 8:32am UTC](https://fenicsproject.discourse.group/t/parallelizing-legacy-fenics-code/18472/2 "2025-12-16T08:32:40Z")

</div>

Your problem is so small

> [@zaccandels](#):
>
> ```auto
> nx = ny = 30
> mesh = fe.RectangleMesh(comm, fe.Point(-2, -2), fe.Point(2, 2), nx, ny)
> 
> ```

that running in parallel with a partitioned mesh doesn’t make sense.

This has also been discussed at for instance: [Parallel slower runtime - #2 by dokken](https://fenicsproject.discourse.group/t/parallel-slower-runtime/8547/2)

Especially, since your problem is this small, I would probably use a direct solver rather than “cg” and “hypre\_amg”.
