# Parallelization with multiple solves

**URL:** <https://fenicsproject.discourse.group/t/parallelization-with-multiple-solves/1818>\
**Category:** dolfin-adjoint\
**Created:** [October 31, 2019, 9:37pm UTC](https://fenicsproject.discourse.group/t/parallelization-with-multiple-solves/1818 "2019-10-31T21:37:56Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Baptista](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/baptista/32/436_2.png) [@Baptista](https://fenicsproject.discourse.group/u/Baptista)\
**Post date:** [October 31, 2019, 9:37pm UTC](https://fenicsproject.discourse.group/t/parallelization-with-multiple-solves/1818/1 "2019-10-31T21:37:56Z")

</div>

Dear all,

I am trying to solve an optimization problem of the type:

\min\limits\_{a} J(a,u\_1, u\_2, \ldots, u\_N) = \sum\_{i=1}^{N} J\_i(a, u\_i)

where a is a field and each J\_i comes from solving an independent PDE for u\_i.

The ideal approach would be to solve these PDEs in parallel, but I am having trouble making dolfin-adjoint generate the tape correctly.

Here is my attempt at producing a minimal working example:

```auto
""" minimal working example: figuring out parallelism in dolfin-adjoint """

from fenics import *
from fenics_adjoint import *

comm = MPI.comm_world

mesh = UnitSquareMesh(MPI.comm_self, 64, 64)
V = FunctionSpace(mesh, 'CG', 1)
v = TestFunction(V)
bc = DirichletBC(V, 0.0, "on_boundary")
d = Expression("2*pow(pi, i)*sin(pi*x[i])",i=0, degree=3)
f = interpolate(Expression('x[0]+x[1]', degree=1), V)

def forward(g, i):
    u = Function(V)
    F = (inner(grad(u), grad(v)) - g * v) * dx
    solve(F == 0, u, bc)
    d.i=i
    J = assemble((0.5*inner(u - d, u - d))*dx + 1e-6*f**2*dx)
    return J

if comm.size == 1:
    J = forward(f, 0) + forward(f, 1)

else:
    if MPI.rank(comm)==0:
        Jp = forward(f, 0)
    elif MPI.rank(comm)==1:
        Jp = forward(f, 1)

    data = comm.gather(Jp, root=0)
    if MPI.rank(comm)==0:
        J = sum(data)

tape = get_working_tape()
tape.visualise()

```

Looking at the tape generated, using 1 processor I get:

 ![graph_large_attrs_key%3D_too_large_attrs%26limit_attr_size%3D1024%26run%3D(2)](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/1X/8d5a36f11554f2226c5cc6aa25e91814ef97bcf3.png)

Using two processors, however, the main problem seems to be that only what comes from one of the processors is “visible” to the tape:

 ![graph_large_attrs_key%3D_too_large_attrs%26limit_attr_size%3D1024%26run%3D(3)](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/1X/c9ee6d57df9af6177fd92cad9f0aef9f07ee3845.png)

So how should I proceed in order to run the optimization with many calls to “solve()” in parallel?

Thank you for the consideration.
