# How to parallelize code alongside assign()?

**URL:** <https://fenicsproject.discourse.group/t/how-to-parallelize-code-alongside-assign/10326>\
**Category:** Errors\
**Created:** [February 2, 2023, 9:39pm UTC](https://fenicsproject.discourse.group/t/how-to-parallelize-code-alongside-assign/10326 "2023-02-02T21:39:26Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![Biophysman](https://avatars.discourse-cdn.com/v4/letter/b/bc8723/32.png) [@Biophysman](https://fenicsproject.discourse.group/u/Biophysman)\
**Post date:** [February 3, 2023, 3:31pm UTC](https://fenicsproject.discourse.group/t/how-to-parallelize-code-alongside-assign/10326/4 "2023-02-03T15:31:13Z")

</div>

Thanks for your help @dokken! You are correct about the typo.

I’m not sure why you cannot reproduce the error. I’m running the code from the [conda-forge version](https://anaconda.org/conda-forge/fenics) running on a cluster. Could clashes between the conda installation and the cluster’s handling of multithreading or something be going on?

I was able to “fix” things by doing a hacky workaround. From [this question](https://fenicsproject.discourse.group/t/problem-with-assign-in-parallel/989) I learned that `c.assign(a)` has different behavior than `assign(c, a)` when running under `mpirun`. Thus, the following code has the desired behavior for me:

```auto
# Define submesh mesh and function spaces
mesh = BoxMesh(Point(0, 0), Point(1, 1, 0.5),
                     64, 64, 32)

P1 = FiniteElement('CG', mesh.ufl_cell(), 1)
W = FunctionSpace(mesh, P1)

#Define pointwise addition of functions. Returns a numpy array of the pointwise sym of .get_local() values
def Add(functions):
    garrays = []
    for g in functions:
        garrays.append(g.vector().get_local())
    Sum = garrays[0] #initialize the sum variable with the first array in garrays
    
    #successively add each array in garrays together (-1 in loop +1 in index because sum begins with garrays[0])
    for i in range(len(garrays)-1): 
        Sum = Sum+garrays[i + 1]
    result = Function(W)
    result.vector().set_local(Sum)
    return result

Aexp = Expression('2', degree=1)
A = interpolate(Aexp, W)

Bexp = Expression('2', degree=1)
B = interpolate(Bexp, W)

Cexp = Expression('0', degree=1)
C = interpolate(Cexp, W)

for x in range(0,5):
  assign(C, Add([A,B]))
  assign(B, C)  
  print(C.vector().get_local())

```

While this workaround might “work” for now, I certainly don’t understand what’s going on? Bafflingly, this is the output from the print function using `-np 2`:

```auto
[0. 4. 0. ... 4. 4. 0.]
[0. 6. 0. ... 6. 6. 0.]
[0. 8. 0. ... 8. 8. 0.]
[0. 10. 0. ... 10. 10. 0.]
[0. 12. 0. ... 12. 12. 0.]
[4. 4. 4. ... 4. 4. 4.]
[6. 6. 6. ... 6. 6. 6.]
[8. 8. 8. ... 8. 8. 8.]
[10. 10. 10. ... 10. 10. 10.]
[12. 12. 12. ... 12. 12. 12.]

```

One of the processes seems to get it right, while the other doesn’t?

---

_[View the full topic](https://fenicsproject.discourse.group/t/how-to-parallelize-code-alongside-assign/10326)._
