File: cpi-worker.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 (24 lines) | stat: -rw-r--r-- 466 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from mpi4py import MPI
from array import array

master = MPI.Comm.Get_parent()
nprocs = master.Get_size()
myrank = master.Get_rank()

n  = array('i', [0])
master.Bcast([n, MPI.INT], root=0)
n = n[0]

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)
pi = s * h

pi = array('d', [pi])
master.Reduce(sendbuf=[pi, MPI.DOUBLE],
              recvbuf=None,
              op=MPI.SUM, root=0)

master.Disconnect()