1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
#!/usr/bin/env python
from __future__ import with_statement # Python 2.5 and later
# If you want MPE to log MPI calls, you have to add the two lines
# below at the very beginning of your main bootstrap script.
import mpi4py
mpi4py.profile('MPE', logfile='cpilog')
# Import the MPI extension module
from mpi4py import MPI
if 0: # <- use '1' to disable logging of MPI calls
MPI.Pcontrol(0)
# Import the MPE extension module
from mpi4py import MPE
if 1: # <- use '0' to disable user-defined logging
# This has to be explicitly called !
MPE.initLog(logfile='cpilog')
# Set the log file name (note: no extension)
MPE.setLogFileName('cpilog')
# Import the 'array' module
from array import array
# This is just to make the logging
# output a bit more interesting
from time import sleep
# User-defined MPE events
cpi_begin = MPE.newLogEvent("ComputePi-Begin", "yellow")
cpi_end = MPE.newLogEvent("ComputePi-End", "pink")
# User-defined MPE states
synchronization = MPE.newLogState("Synchronize", "orange")
communication = MPE.newLogState("Comunicate", "red")
computation = MPE.newLogState("Compute", "green")
comm = MPI.COMM_WORLD
nprocs = comm.Get_size()
myrank = comm.Get_rank()
n = array('i', [0])
pi = array('d', [0])
mypi = array('d', [0])
def comp_pi(n, myrank=0, nprocs=1):
h = 1.0 / n;
s = 0.0;
for i in range(myrank + 1, n + 1, nprocs):
x = h * (i - 0.5);
s += 4.0 / (1.0 + x**2);
return s * h
with synchronization:
comm.Barrier()
for N in [10000]*10:
with cpi_begin: pass
if myrank == 0:
n[0] = N
with communication:
comm.Bcast([n, MPI.INT], root=0)
with computation:
mypi[0] = comp_pi(n[0], myrank, nprocs)
with communication:
comm.Reduce([mypi, MPI.DOUBLE],
[pi, MPI.DOUBLE],
op=MPI.SUM, root=0)
with cpi_end: pass
with synchronization:
comm.Barrier()
sleep(0.01)
# ----------------------- #
# Python 2.3/2.4 version #
# ----------------------- #
## synchronization.enter()
## comm.Barrier()
## synchronization.exit()
##
## for N in [50000]*10:
##
## cpi_begin.log()
##
## if myrank == 0:
## n[0] = N
##
## communication.enter()
## comm.Bcast([n, MPI.INT], root=0)
## communication.exit()
##
## computation.enter()
## mypi[0] = comp_pi(n[0], myrank, nprocs)
## computation.exit()
##
## communication.enter()
## comm.Reduce([mypi, MPI.DOUBLE],
## [pi, MPI.DOUBLE],
## op=MPI.SUM, root=0)
## communication.exit()
##
## cpi_end.log()
##
## synchronization.enter()
## comm.Barrier()
## synchronization.exit()
##
## sleep(0.01)
|