pyMPI: Examples

Main
Features
FAQ
Examples
Docs
Downloads
Forums
Report Bugs
SourceForge.net Logo
Support This Project

The following are a number of examples illustrating how pyMPI can be used. If you have questions about these examples, or would like to contribute your own examples(or more pythonic versions of these examples), please send us e-mail.

Python(pyMPI) Only

Monte Carlo Calculation of Pi

   1:"""
   2:This example computes PI to certain precision using 
   3:4 processors and a monte carlo simulation.
   4:"""
   5:
   6:import random
   7:import mpi
   8:
   9:def computePi(nsamples):
  10:    rank, size = mpi.rank, mpi.size
  11:    oldpi, pi, mypi = 0.0,0.0,0.0
  12:    
  13:    done = False
  14:    while(not done):
  15:        inside = 0
  16:        for i in xrange(nsamples):
  17:            x = random.random()
  18:            y = random.random()
  19:            if ((x*x)+(y*y)<1):
  20:                inside+=1
  21:        
  22:        oldpi = pi
  23:        mypi = (inside * 1.0)/nsamples
  24:        pi =  (4.0 / mpi.size) * mpi.allreduce(mypi, mpi.SUM) 
  25:        
  26:        delta = abs(pi - oldpi)
  27:        if(mpi.rank==0):
  28:            print "pi:",pi," - delta:",delta
  29:        if(delta < 0.00001):
  30:            done = True
  31:    return pi
  32:
  33:if __name__=="__main__":
  34:    pi = computePi(10000)
  35:    if(mpi.rank==0):
  36:        print "Computed value of pi on",mpi.size,"processors is",pi
  37:    
  38:
			

pyMPI with Extension Code

Very simple extension code

This is a simple example designed to allow you to test and see if your Python+MPI solution allows extension codes to make calls to MPI. This is one of pyMPI's features that other Python+MPI interfaces don't have.

C Extension Code
   1:#include <Python.h>
   2:#include <mpi.h>
   3:
   4:static PyObject *
   5:mpimodule_rank( PyObject *self, PyObject *args );
   6:    
   7:static PyMethodDef MpimoduleMethods[] = {
   8:    {"rank", mpimodule_rank, METH_VARARGS,"Execute MPI_Comm_rank and return an integer rank value."},
   9:     {NULL,NULL,0,NULL}
  10:     };
  11:
  12:static PyObject *MpimoduleError;
  13:
  14:    PyMODINIT_FUNC
  15:initmpimodule(void)
  16:{
  17:    PyObject *m; 
  18:    m = Py_InitModule("mpimodule", MpimoduleMethods);
  19:
  20:    MpimoduleError = PyErr_NewException("mpimodule.error",NULL,NULL);
  21:    Py_INCREF(MpimoduleError);
  22:    PyModule_AddObject(m, "error", MpimoduleError);
  23:}
  24:
  25:static PyObject *
  26:mpimodule_rank( PyObject *self, PyObject *args )
  27:{
  28:    int flag;
  29:    int rank;
  30:    if( MPI_Initialized(&flag) ) // MPI not initialized
  31:        return Py_BuildValue("i",-1);
  32:    if( ! flag ) // Failure to call MPI_Initialized
  33:        return Py_BuildValue("i",-2);
  34:    else
  35:        MPI_Comm_rank(MPI_COMM_WORLD,&rank);
  36:        return Py_BuildValue("i",rank);
  37:}
  38:
  39:
Compiling the Extension

    gcc -shared -I$(PYTHONINCLUDEDIR) mpimodule.c -o $@
PYTHONINCLUDEDIR is usually something like /usr/include/pythonX.Y

Python Code
   1:import mpi
   2:
   3:# My trivial mpi module
   4:import mpimodule
   5:
   6:if __name__=="__main__":
   7:    print "PYMPI: I'm processor",mpi.rank
   8:    r = mpimodule.rank()
   9:    if (r >= 0):
  10:        print "mpimodule:  I'm processor",r
  11:    else:
  12:        print "Extension was unable to call MPI!"

Download examples