Broadcast Dolfin Matrix to MPI Processes

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.

Why not just do:

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 = assemble(u*v*dx)

print(A)

Then each process has the same starting matrix?

Well the goal is for a time evolution problem with a large enough matrix and a large number of processes, creating the matrix using one processor at the start of each time step and then sending it to the other processes should be more efficient rather than creating the same matrix on each processor, at least in theory.

Of course I could do it the way you described but then it doesn’t have the behavior I am looking for.