File: cpilog.py

package info (click to toggle)
mpi4py 1.3%2Bhg20120611-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,020 kB
  • sloc: python: 9,503; ansic: 6,296; makefile: 571; f90: 158; sh: 146; cpp: 103
file content (52 lines) | stat: -rw-r--r-- 1,083 bytes parent folder | download
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
#!/usr/bin/env python

# If you want VampirTrace to log MPI calls, you have to add the two
# lines below at the very beginning of your main bootstrap script.
import mpi4py.rc
mpi4py.rc.threaded = False
mpi4py.rc.profile('vt-mpi', logfile='cpilog')

# Import the MPI extension module
from mpi4py import MPI

# Import the 'array' module
from array import array

# This is just to make the logging
# output a bit more interesting
from time import sleep

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

comm.Barrier()

for N in [10000]*10:

    if myrank == 0:
        n[0] = N

    comm.Bcast([n, MPI.INT], root=0)

    mypi[0] = comp_pi(n[0], myrank, nprocs)

    comm.Reduce([mypi, MPI.DOUBLE],
                [pi, MPI.DOUBLE],
                op=MPI.SUM, root=0)

    comm.Barrier()

    sleep(0.01)