File tree Expand file tree Collapse file tree 2 files changed +34
-1
lines changed
Expand file tree Collapse file tree 2 files changed +34
-1
lines changed Original file line number Diff line number Diff line change 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 ))
Original file line number Diff line number Diff 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__
You can’t perform that action at this time.
0 commit comments