-
Notifications
You must be signed in to change notification settings - Fork 0
/
mandlebrot.py
105 lines (77 loc) · 1.7 KB
/
mandlebrot.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
from numpy import *
from matplotlib import pyplot
import time
'''
# Sequential mandlebrot code
def mandlebrot(x,y,maxit):
c = x + y*1j
z = 0 + 0j
it = 0
while abs(z) < 2 and it < maxit:
z = z**2 + c
it += 1
return it
# print mandlebrot(1,1,127)
start = time.time()
x1,x2 = -2.0,1.0
y1,y2 = -2.0,2.0
nx,ny = 100,150
c = zeros((nx,ny),dtype='i')
dx = (x2-x1)/nx
dy = (y2-y1)/ny
for i in range(nx):
y = y1 + i*dx
for j in range(ny):
x = x1 + j*dy
c[i,j] = mandlebrot(x,y,127)
end = time.time()
print "Total time taken: "+str(end-start)
pyplot.imshow(c, aspect='equal')
pyplot.spectral()
pyplot.show()
'''
# Parallel mandlebrot code
from mpi4py import MPI
comm = MPI.COMM_WORLD
size = comm.Get_size()
rank = comm.Get_rank()
def mandlebrot(x,y,maxit):
c = x + y*1j
z = 0 + 0j
it = 0
while abs(z) < 2 and it < maxit:
z = z**2 + c
it += 1
return it
start = MPI.Wtime()
x1,x2 = -2.0,1.0
y1,y2 = -2.0,2.0
nx,ny = 1000,1500
strip_size = nx/size
dx = (x2-x1)/nx
dy = (y2-y1)/ny
c = zeros((nx,ny),dtype='i')
# without load balancing
# for i in range(rank*strip_size,rank*strip_size+strip_size):
# y = y1 + i*dx
# for j in range(ny):
# x = x1 + j*dy
# c[i,j] = mandlebrot(x,y,127)
# with load balancing by cyclic distribution
i = rank
while i < nx:
y = y1 + i*dx
for j in range(ny):
x = x1 + j*dy
c[i,j] = mandlebrot(x,y,127)
i += size
end_local = MPI.Wtime()
print "Total time taken for local calculation by process "+str(rank)+" : "+str(end_local-start)
comm.Allreduce(MPI.IN_PLACE,c,op=MPI.MAX)
if rank == 0:
# print c
end_comm = time.time()
print "Total time taken for all communications : "+str(end_comm-start)
pyplot.imshow(c, aspect='equal')
pyplot.spectral()
pyplot.show()