lunes, 12 de marzo de 2012

Py-par

Pypar is a useful tool that allows programs written in python to run in parallel on multiple processors and communicate using message passing.


Requirements:
  • Python
  • numpy
  • C compiler
  • MPI C library (such as openmpi)
To verify if the installation was correct we must write:

mpirun -np 2 testpypar.py

It must try to run a C/MPI program.

Here is an example program written in pypar

import pypar # The Python-MPI interface                                                                                                                 
numproc = pypar.size()
myid = pypar.rank()
node = pypar.get_processor_name()

print "I am proc %d of %d on node %s" %(myid, numproc, node)

if numproc < 2:
  print "Demo must run on at least 2 processors to continue"
  pypar.abort()

if myid == 0:  
  msg = "MSGP0"

  print 'Processor 0 sending message "%s" to processor %d' %(msg, 1)
  pypar.send(msg, 1)

  msg, status = pypar.receive(numproc-1, return_status=True)
  print 'Processor 0 received message "%s" from processor %d' %(msg, numproc-1)
  print 'Size of msg was %d bytes' %(status.bytes())

else:
  source = myid-1
  destination = (myid+1)%numproc

  msg, status = pypar.receive(source, return_status=True)
  print 'Processor %d received message "%s" from processor %d'\
     %(myid, msg, source)
  print 'Size of msg was %d bytes' %(status.bytes())

  msg = msg + '->P' + str(myid) #Update message                                                                                                            
  print 'Processor %d sending msg "%s" to %d' %(myid, msg, destination)
  pypar.send(msg, destination)

pypar.finalize()

This program sends a message to processor 0. The message is traveling through the processors in the form of a ring, where each one of the processors is adding a bit to the message, then it returns to processor 0.
In order to know what programs are running on the processors we can make the call myid = pypar.rank(). To obtain the total number of processors we write proc = pypar.size()

In order to start running py-par you can write:

mpirun -np 4 demo.py

This command will run 4 different copies of the program in different processors

My nominations of this week are for rafa, cecy and roberto.


Bibliography


Py-par documentation
Py-par: Parallel programming for python



1 comentario:

  1. Good, 5+5. Please try to remember to include in the future in your nominations the reason why you nominate the person.

    ResponderEliminar