File: cpi-worker.py

package info (click to toggle)
mpi4py 4.1.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,540 kB
  • sloc: python: 34,465; ansic: 16,475; makefile: 614; sh: 325; cpp: 193; f90: 178
file content (28 lines) | stat: -rw-r--r-- 461 bytes parent folder | download | duplicates (2)
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
from array import array

from mpi4py import MPI

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()