0

I'm new to the concept of parallel programming. I'm supposed to use mpi4run for a project, but I don't know how to actually run it. For example, if I want to run this code :

from mpi4py import MPI
import numpy as np

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

if rank == 0:
    numData = 5  
    data = np.linspace(0.0,3.14,numData)  
else:
    numData = None
numData = comm.bcast(numData, root=0)
if rank != 0:    
    data = np.empty(numData, dtype='d')  
comm.Bcast(data, root=0) 
print('Rank: ',rank, ', data received: ',data)

If i run from directly from my IDLE i get:

Rank: 0 , data received: [0. 0.785 1.57 2.355 3.14 ]

But, if I understand correctly, that's not the way we should run it to do the parallel computing, right? On the internet, I find that one runs it with writing

mpirun -n 4 python script.py.

My question is, where do I type that? I have tried on the cmd and the python shell but I get errors, syntaxerrors and so on. I feel there is something I don't understand here. Any tips?

1 Answer 1

1

Python programs that use MPI commands must be run using an MPI interpreter, called mpirun.

The easiest is to create a clean conda environment and install mpi4py there.

If you are on Linux and have Anaconda Python installed, that would look something like this:

$ conda create -n mpi_env
$ source activate mpi_env
(mpi_env) $ conda install mpi4py
(mpi_env) $ which mpirun # test if the MPI interpreter is correctly installed
/anaconda/envs/mpi_env/bin/mpirun
(mpi_env) $ mpirun -n 4 python script.py # run your python script, using 4 processes.

For more info, see this tutorial.

For Windows, you need to install mpiexec. See here for installation instructions.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.