Mpi4py

From campisano.org
Jump to navigation Jump to search

Simple multi-process hello world

  • helloworld.py:
#!/usr/bin/env python

# chmod +x hellompi.py
# mpiexec -n 10 hellompi.py

from mpi4py import MPI

size = MPI.COMM_WORLD.Get_size()
rank = MPI.COMM_WORLD.Get_rank()
name = MPI.Get_processor_name()

print "Hello from process " + str(rank + 1) + " of " + str(size) + " on " + str(name)

run with

mpiexec -n 10 hellompi.py


Better spawned and reduced sum example

  • master.py:
#!/usr/bin/env python

from mpi4py import MPI
from array import array

def run(debug = False):
    worker = MPI.COMM_SELF.Spawn("worker.py", None, 3)

    data_size = 1000000

    try:
        data = array('i')
        for i in range(0, data_size):
            data.append(1)

        print "Broadcasting input data len: ", len(data), " content: ", data[0]
        worker.Bcast([data, MPI.INT], root = MPI.ROOT)

        result = array('i', [0])
        worker.Reduce(sendbuf = None, recvbuf = [result, MPI.INT], op = MPI.SUM, root = MPI.ROOT)
        result = result[0] # strange thing (Reduce logic)

        print "Calculated value of sum is", result

    except Exception as ex:
        print "Unexpected error:"
        print ex

    worker.Disconnect()

def __main__():
    run()

__main__()


  • worker.py:
#!/usr/bin/env python

from mpi4py import MPI
from array import array

def run(debug = False):
    master = MPI.Comm.Get_parent()
    my_rank = master.Get_rank()
    n_procs = master.Get_size()

    data_size = 10241024

    try:
        data = array('i')
        for i in range(0, data_size):
            data.append(0)

        master.Bcast([data, MPI.INT], root = 0)
        #print "# Broadcast finished and data on rank ", my_rank, " is len ", len(data), " data: ", data

        work_size = int(data_size / n_procs)

        work_start = my_rank * work_size
        work_limit = int(0)

        # if is the last proc, work_limit is the end of data
        if my_rank == (n_procs - 1):
            work_limit = data_size
        else:
            work_limit = work_start + work_size

        result = int(0)

        for i in range(work_start, work_limit):
            result += data[i]

        print "my_rank: ", my_rank, " work_start: ", work_start, " work_limit: ", work_limit, "result = ", result

        result = array('i', [result]) # strange thing (Reduce logic)
        master.Reduce(sendbuf = [result, MPI.INT], recvbuf = None, op = MPI.SUM, root = 0)

    except Exception as ex:
        print "# Unexpected error:"
        print ex

    #print "# Disconnecting from rank %d" % my_rank
    master.Disconnect()

def __main__():
    run()

__main__()


run the code with

python master.py


Depends

apt-get install libopenmpi-dev python-mpi4py



References