import numpy as np from mpi4py import MPI from time import time import matplotlib.pyplot as plt def compute_pi(ns): """ input: ns list of block sizes output: pis len(ns) estimated pi values method: pick n x n random points in a square area [-1,1] surrounding a circle radius 1 probability of hitting the circle is P = (area of circle)/(area of square) = pi/(2*2) = hits/n => pi = 4*hits/n """ pis = np.zeros(len(ns)) for i,n in enumerate(ns): points = 2*np.random.random((n,2)) - 1 # random points in square # count points inside the unit circle; 1 (True) for hits, 0 (False) for misses hits = np.sum(np.linalg.norm(points,axis=-1) < 1) pis[i] = 4*hits/n return pis def taskdivider(N,nb,nproc): """ input: N number of tasks nb number of blocks nproc number of processes used for computation output: ns (nproc,nb) array, N tasks divided as evenly as possible to nproc processes and nb blocks low rank processes may get a few more tasks than the rest """ # // gives integer result, discard remainder ns = np.array([N//nproc//nb]*nb*nproc) # distribute remaining jobs evenly among blocks i = 0 while ns.sum()