Yes, indeed I was wrong. Lets have a look at this file (my edited version of hello_class.py, which comes with the PMI archive):
if __name__ != 'pmi':
##################################################
## Serial code
##################################################
import pmi
pmi.setup()
pmi.execfile_(__file__)
create a frontend class
class Hello(object):
def __init__(self, name):
self.pmiobj = pmi.create('HelloLocal', name)
def __call__(self):
return pmi.invoke(self.pmiobj, '__call__')
def func(self):
return pmi.invoke(self.pmiobj,'func')
# use the class
hello = Hello('Olaf')
print('\n'.join(hello()))
a = hello.func()
print "Wow, PMI rules!"
else:
##################################################
## Parallel code
##################################################
from mpi4py import MPI
class HelloLocal(object):
def __init__(self, name):
self.name = name
def __call__(self):
return 'Hello %s, this is MPI task %d!' % (self.name, MPI.COMM_WORLD.rank)
def func(self):
if MPI.COMM_WORLD.rank == 0:
return raw_input()
else:
return "Slave node"
Things to pay attention to:
1) First, name object is passed as a parameter from the serial code into the parallel class, and than a separate call to parallel code uses this object. This means that all objects persist between separate calls to parallel code.
2) While calling the func function, only the master node (rank 0) is waiting for user input. However, as opposed to using just mpi, all other nodes are waiting for the master node to complete operation before all nodes join again in the serial code.
3) Notice that "Wow, PMI rules!" is printed out only once, no matter how many nodes you create with mpirun. Now comment out everything else, leave just this print statement, and mpirun this file once again, creating say 4 processes. What will you get?
"Wow, PMI rules!"
"Wow, PMI rules!"
"Wow, PMI rules!"
"Wow, PMI rules!"
Indeed, it does.
No comments:
Post a Comment