This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

Attribute multiple processes to independent solve (2-layer of parallelism)

+1 vote

Hello there,

Consider the following code (fenics 2016.1), that allow to solve two probem in parallel, attributing a MPI communicator for each mesh:
(https://fenicsproject.org/qa/2682/solve-independent-equations-on-different-cores)

with mpirun -n 2 python program.py:

from dolfin import *
from mpi4py import MPI

# Communicator
mpi_comm = MPI.COMM_WORLD
# Rank 
rank_process = mpi_comm.rank

# Create mesh: one for each process
# create a mesh with a given MPI communicator is the key
if rank_process==0:
    mesh = UnitSquareMesh(mpi_comm_self(), 10, 10)
if rank_process==1:
    mesh = UnitSquareMesh(mpi_comm_self(), 20, 20)

# Plot mesh
plot_title= 'MESH for the process: {value}'.format(value=rank_process)
plot(mesh, title=plot_title, interactive=True)

# Function space and functions
V = FunctionSpace(mesh, "CG", 1)
u = TrialFunction(V)
v = TestFunction(V)
u_ = Function(V)

# Set source
if rank_process==0:
    source = Constant(1)
if rank_process==1: 
    source = Constant(-1)   

# Solve
solve(inner(grad(u), grad(v))*dx == source*v*dx, u_, bcs=DirichletBC(V, 0, "on_boundary"))

# Plot
plot(u_, title="Solution of the Proces {}".format(rank_process))
interactive()

Now, i would like that each problem (let's call them problem 1 and problem 2) use several process:

pseudo-code: of what i want to acheive:

mpirun - n NP python program.py

# Get number of DOF for each problem
DOF of probem 1
DOF of probem 2

# Attribute the number of process proportionally to the number of DOF
NP_for_problem1 = DOF of probem 1 / (DOF of probem 1 + DOF of probem 2) * NP 
NP_for_problem2 = DOF of probem 2 / (DOF of probem 1 + DOF of probem 2) * NP 

# Create mesh
# How to attribute multiple process to each mesh?
mesh_problem_1 = UnitSquareMesh(  ??? ,10, 10)  
mesh_problem_1 = UnitSquareMesh(  ??? , 20, 20)

etc. until solve

asked Apr 14, 2017 by fussegli FEniCS Novice (700 points)

Do you need the solution for something else in your code? If not, have you tried setting the script to receive a parameter? You could use something like

mpirun -n 2 python main.py 1
mpirun -n 2 python main.py 2

and put that in a bash script (or windows or mac).

That's another way to do indeed.
However, later in the code these two independent calculations are analyzed together. So, i would prefer to stick to one unique program if possible.
Any hints how to do it?

No clue on how to do it in a single code. For it, I would export the solution in each code and then use another script for the analysis. Regards!

Just a guess, but maybe the python multiprocessing module could help you, instead of using MPI (apparently no communication is needed between processes?).
See here: https://docs.python.org/3/library/multiprocessing.html

...