0
votes

I'm currently facing a problem and I need your help! So I have a code that will call a Linux Terminal command from a python code. But, I also have some part of the code that needs to be run in parallel to accelerate the computation.

Roughly, the code (say, the name is code.py) will looks like this:

import parallel_func, cli_func

parallel_func() #this part is supposed to run in parallel when using mpirun 
cli_func() #this part is supposed to run without mpi

The problem is, if I run mpirun -np 4 python code.py, the parallel_func will do well, but the cli_func will run multiple cli_program that will overburden the cpu.

So, are there any way I can do to isolate that cli_func part from the MPI?

Thank you for your help!

edit: I've did the solution that tobias replied below (using mpi rank selection) but it results in running one rank with the cli_func with the rest of the cpu still running, limitting the usable processor for the cli program.

edit: I found the makeshift solution using this mpi4py: substantial slowdown by idle cores That fixed it all!

1

1 Answers

2
votes

First off, whenever you run a program using MPI, you will always have n instances of your program running in parallel. Hence, what you probably want to achieve is that only one of those n programs running simultaneously executed the cli_func() part.

The n programs running in parallel all have a rank, a number between 0 and n-1, so that you can distinguish the programs running. You can use that rank to make only one program perform a certain action. If you use the mpi4py library, this looks like so:

import parallel_func, cli_func

from mpi4py import MPI

comm = MPI.COMM_WORLD
rank = comm.Get_rank()

parallel_func()
if rank == 0:
    cli_func()

The condition rank == 0 makes sure that only the instance with rank 0 executes the respective code. But you can also choose any other rank, of course, or have different instances do different things.

if rank == 0:
    print("First")
elif rank == 1:
    print("Second!")

You did not specify in your question what MPI Python package you are using. Depending on this, the exact syntax might differ, although the idea will still be the same.