forked from AdamSimpson/python
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathriemann-parallel.py
62 lines (46 loc) · 1.34 KB
/
riemann-parallel.py
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from mpi4py import MPI
import numpy
import scipy
from scipy import integrate
def func(x) :
y = numpy.sin(x)
return y
def main() :
# MPI variables
size = MPI.COMM_WORLD.Get_size()
rank = MPI.COMM_WORLD.Get_rank()
name = MPI.Get_processor_name()
# Domain start and end
xStart = 0.0
xEnd = 2.0 * scipy.pi
# Number of total samples
samplesPerRank = 10
# Rectangle width
width = (xEnd-xStart) / (samplesPerRank*size)
# Node start and end
rankStart = xStart + width*samplesPerRank*rank
rankEnd = rankStart + width*samplesPerRank
# Create area float
area = numpy.zeros(1)
# Create array on rank 0 to hold all results
if rank == 0:
totalArea = numpy.zeros(1)
else:
totalArea = None
# Fill x,y coordinate pairs and sum area
for i in range(0, samplesPerRank) :
x = rankStart + i*width
y = func(x)
area = area + width*y
# Sum reduce area
MPI.COMM_WORLD.Reduce(area, totalArea, root=0)
# Rank 0 will calculate error
if rank == 0:
# Calculate integral
actualArea = integrate.quad(func, xStart, xEnd)[0]
# Calculate difference
error = actualArea - totalArea
print error
# execute main
if __name__ == "__main__":
main()