ipcmagic
with MPI4Py¶Points to look at:
%ipcluster start -n 12 --mpi
starts the cluster and it works. On the terminal the following command should look like this:> ps -u $USER | grep ip
1270 pts/8 00:00:01 ipcontroller
1278 pts/8 00:00:00 ipcontroller
1279 pts/8 00:00:00 ipcontroller
1282 pts/8 00:00:00 ipcontroller
1285 pts/8 00:00:00 ipcontroller
1334 ? 00:00:01 ipengine
1335 ? 00:00:01 ipengine
1336 ? 00:00:00 ipengine
1337 ? 00:00:00 ipengine
1338 ? 00:00:00 ipengine
1339 ? 00:00:00 ipengine
1340 ? 00:00:00 ipengine
1341 ? 00:00:00 ipengine
1342 ? 00:00:00 ipengine
1343 ? 00:00:00 ipengine
1344 ? 00:00:00 ipengine
1345 ? 00:00:00 ipengine
31037 ? 00:00:00 slurm_script
%ipcluster stop
stops the cluster. Check with the same command that all the process shown above no longer exist.%ipcluster start -n 12 --mpi
again.%ipcluster start <...>
while the cluster is active, should give IPCluster is already running
.%ipcluster stop
whould give IPCluster not running
.import numpy as np
import ipcmagic
import ipyparallel as ipp
%ipcluster --help
%ipcluster --version
# start a cluster of 12 engines
%ipcluster start -n 12 --mpi
c = ipp.Client()
c.ids
%%px
import socket
from mpi4py import MPI
%%px
print("Hello World from rank %s of %s on nid %s" % (MPI.COMM_WORLD.rank,
MPI.COMM_WORLD.size,
socket.gethostname()))
view = c[:]
view.activate()
Write the file psum.py
the defines the function psum
.
%%bash
echo 'from mpi4py import MPI' > psum.py
echo 'import numpy as np' >> psum.py
echo '' >> psum.py
echo 'def psum(a):' >> psum.py
echo ' locsum = np.sum(a)' >> psum.py
echo " rcvBuf = np.array(0.0,'d')" >> psum.py
echo ' MPI.COMM_WORLD.Allreduce([locsum, MPI.DOUBLE],' >> psum.py
echo ' [rcvBuf, MPI.DOUBLE],' >> psum.py
echo ' op=MPI.SUM)' >> psum.py
echo ' return rcvBuf' >> psum.py
# run the contents of the file on each engine:
view.run('psum.py')
view.scatter('a',np.arange(16,dtype='float'))
view['a']
%px totalsum = psum(a)
view['totalsum']
# Stop the cluster
%ipcluster stop