I have few different equations, that i need to solve. Every equation takes approximately 1 minute to solve, but N equations take N minutes to solve if I will solve their sequential. It is very long for me and I want to solve their parallel. I use python multiprocessing module, but it throws me a error:
TypeError: can’t pickle dolfin.cpp.generation.UnitSquareMesh objects
I write my code in class - based style (I think, it’s the source of problem). On the example of poisson equation, it looks something like this:
class PoissonSolver:
class Boundary(SubDomain):
def inside(self, x, on_boundary):
return on_boundary
def run_solver(self):
self.__define_mesh()
self.__define_bc()
self.__define_functions()
self.__run_processes()
def __define_mesh(self):
self.mesh = UnitSquareMesh(30, 30)
self.V = FunctionSpace(self.mesh, "Lagrange", 1)
def __define_bc(self):
self.u_D = Expression('1 + x[0] * x[0] + 2 * x[1] * x[1]', degree=2)
self.bc = DirichletBC(self.V, self.u_D, self.Boundary())
def __define_functions(self):
self.u = TrialFunction(self.V)
self.v = TestFunction(self.V)
self.a = dot(grad(self.u), grad(self.v)) * dx
self.f = [Constant(-i) for i in range(10)]
self.L = [fun * self.v * dx for fun in self.f]
def __run_processes(self):
pool = multiprocessing.Pool(processes=4)
pool.map(self.solve_eq, range(10))
def solve_eq(self, i):
u = Function(self.V)
solve(self.a == self.L[i], u, self.bc)
File(self.mesh.mpi_comm(), 'foo%g.xml' % i) << u
if __name__ == "__main__":
solver = PoissonSolver()
solver.run_solver()
I know that the multiprocessing module use pickle to share data between processes, but how can I get around it and solve my problem? Thank you for answers.
Ubuntu, dolfin-version: 2018.1.0.