MPI
Install mpi4py
downlowd openmpi
tar -zxf openmpi-X.X.X tar.gz
cd openmpi-X.X.X
./configure --prefix=/usr/local/openmpi
make all
make install
easy_install mpi4py
		
Overview
  • MPI.Comm, base class of communicators
  • MPI.COMM_SELF, MPI.COMM_WORLD
  • MPI.Comm.Get_size(), the number of processes in a communicator
  • MPI.Comm.Get_rank(), the calling process rank
  • MPI.Comm.Get_group(), returns an instance of the MPI.Group class
  • Point-to-Point Communications
  • Blocking Communications
  • Nonblocking Communications
  • Persistent Communications
  • Collective Communications
  • Dynamic Process Management
  • Parallel Input/Output
  • Timers
  • Tutorials
    from mpi4py import MPI
    
    comm = MPI.COMM_WORLD
    rank = comm.Get_rank()
    
    if rank == 0:
        print('%d of %d, Create data ...' % (rank, comm.Get_size()))
        data = {'a':7, 'b':3.14}
        comm.send(data, dest=1, tag=11)
    elif rank == 1:
        data = comm.recv(source=0, tag=11)
        print('%d of %d, Received data ...' % (rank, comm.Get_size()))
        print(data, type(data))
    else:
        print('%d of %d, Not use ...' % (rank, comm.Get_size()))
    		
    from mpi4py import MPI
    
    comm = MPI.COMM_WORLD
    rank = comm.Get_rank()
    size = comm.Get_size()
    
    if rank == 0:
        # initial data
        data = {'a':7, 'b':3.14}
        comm.send(data, dest=rank+1, tag=11)
    elif rank == size-1:
        # print out data in the last thread
        data = comm.recv(source=rank-1, tag=11)
        print('%d of %d, Received data ...' % (rank, comm.Get_size()))
        print(data, type(data))
    else:
        # relay data
        data = comm.recv(source=rank-1, tag=11)
        comm.send(data, dest=rank+1, tag=11)
    		
    from mpi4py import MPI
    import numpy
    
    comm = MPI.COMM_WORLD
    rank = comm.Get_rank()
    
    if rank == 0:
        data = numpy.arange(100, dtype=numpy.float64)
        comm.Send(data, dest=1, tag=13)
        print(data)
    elif rank == 1:
        data = numpy.empty(100, dtype=numpy.float64)
        comm.Recv(data, source=0, tag=13)
        print('%d of %d, Received data ...' % (rank, comm.Get_size()))
        print(data)
    		
    from mpi4py import MPI
    
    comm = MPI.COMM_WORLD
    rank = comm.Get_rank()
    
    if rank == 0:
        print('%d of %d, Create data ...' % (rank, comm.Get_size()))
        data = {'a':7, 'b':3.14}
    else:
        data = None
    
    data = comm.bcast(data, root=0)
    print('%d of %d, Received data ...' % (rank, comm.Get_size()))
    print(data, type(data))
    		
    Reference
  • MPI broadcasting tutorial with Python, mpi4py, and bcast
  • MPI for Python Documentation