I am trying to set up a code that in effect should create a matrix A on the first process and then broadcast this matrix to the other processes, then each process solves an Ax=b system where the b will vary for each process.
My question is that I can’t seem to find a way to perform this. If I try to broadcast the matrix directly I am given the error “TypeError: cannot pickle ‘dolfin.cpp.la.Matrix’ object”. An alternative that I suppose could work would be to convert the dolfin Matrix to an array with .array(), but then this gets rid of any sparsity and if I had to then re-convert the array to a dolfin Matrix it seems like this would erase any of the advantage from creating the matrix on one proc and then broadcasting it.
A MWE is included below that shows the problem:
from dolfin import *
mpi_rank = MPI.comm_world.Get_rank()
n = 4
mesh = UnitSquareMesh(MPI.comm_self, n, n)
W = FunctionSpace(mesh, "P1")
u = TrialFunction(W)
v = TestFunction(W)
A = Matrix()
if mpi_rank == 0:
A = assemble(u*v*dx)
MPI.comm_world.barrier()
A = MPI.comm_world.bcast(A,root=0)
print(A)
Would really appreciate any help on how to approach this.