Skip to content

Commit eea9bf4

Browse files
author
Jonathan Rocher
committed
Added a non-trivial example of MPI.
1 parent 73c2de0 commit eea9bf4

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# -*- coding: utf-8 -*-
2+
from mpi4py import MPI
3+
import math
4+
5+
def compute_pi(n, start=0, step=1):
6+
""" Pure python integration of 4/(1+x**2) which gives pi
7+
"""
8+
h = 1.0 / n
9+
s = 0.0
10+
for i in range(start, n, step):
11+
x = h * (i + 0.5)
12+
s += 4.0 / (1.0 + x**2)
13+
return s * h
14+
15+
comm = MPI.COMM_WORLD
16+
nprocs = comm.Get_size()
17+
myrank = comm.Get_rank()
18+
19+
if myrank == 0:
20+
n = 10
21+
else:
22+
n = None
23+
24+
n = comm.bcast(n, root=0)
25+
26+
mypi = compute_pi(n, myrank, nprocs)
27+
28+
pi = comm.reduce(mypi, op=MPI.SUM, root=0)
29+
30+
if myrank == 0:
31+
error = abs(pi - math.pi)
32+
print ("pi is approximately %.16f, "
33+
"error is %.16f" % (pi, error))

pickle/pickle.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ def recover_from_file(filename):
2424
filename = "myinfo.pkl"
2525
save2file(info,filename)
2626
new_obj = recover_from_file(filename)
27-
print "object recovered is", new_obj
27+
print "Object recovered is identical?", new_obj.__dict__ == info.__dict__

0 commit comments

Comments
 (0)