From 64ece36dde553c4fe83a0f085ce5d03505ce98c4 Mon Sep 17 00:00:00 2001 From: Neil Gershenfeld <gersh@cba.mit.edu> Date: Thu, 6 Feb 2020 01:17:37 -0500 Subject: [PATCH] start MPI py --- Python/mpipi.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100755 Python/mpipi.py diff --git a/Python/mpipi.py b/Python/mpipi.py new file mode 100755 index 0000000..42c54d9 --- /dev/null +++ b/Python/mpipi.py @@ -0,0 +1,37 @@ +# +# mpipi.py +# Neil Gershenfeld 2/6/20 +# calculation of pi by an MPI Numba sum +# pi = 3.14159265358979323846 +# + +import time +from numba import jit +from mpi4py import MPI + +@jit(nopython=True) +def calc(istart,iend): + sum = 0.0 + for i in range(istart,iend+1): + sum += 0.5/((i-0.75)*(i-0.25)) + return sum + +NPTS = 100000000000 +comm = MPI.COMM_WORLD +rank = comm.Get_rank() +nproc = comm.Get_size() +if (rank == 0): + start_time = time.time() + istart = int(1 + NPTS*((rank+0)/nproc)) + iend = int(NPTS*((rank+1)/nproc)) + pi = calc(istart,iend) + comm.reduce(pi,op=MPI.SUM,root=0) + end_time = time.time() + mflops = NPTS*5.0/(1.0e6*(end_time-start_time)) + print("NPTS = %d, pi = %f"%(NPTS,pi)) + print("time = %f, estimated MFlops = %f"%(end_time-start_time,mflops)) +else: + istart = int(1 + NPTS*((rank+0)/nproc)) + iend = int(NPTS*((rank+1)/nproc)) + pi = calc(istart,iend) + comm.reduce(pi,op=MPI.SUM,root=0) -- GitLab