Skip to content

Instantly share code, notes, and snippets.

@rvprasad
Last active October 31, 2022 11:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rvprasad/6582d14b69c2f1fb6fb72b4680443605 to your computer and use it in GitHub Desktop.
Save rvprasad/6582d14b69c2f1fb6fb72b4680443605 to your computer and use it in GitHub Desktop.
Compare map's performance of mpi4py module and Python's multiprocessing module
localhost slots=9
c = complex(0, 0.65)
def julia(x, y):
z = complex(x, y)
n = 255
while abs(z) < 3 and n > 1:
z = z**2 + c
n -= 1
return n
def julia_line(args):
k, w, h = args
x0, x1 = -2.0, +2.0
y0, y1 = -1.5, +1.5
dx = (x1 - x0) / w
dy = (y1 - y0) / h
line = bytearray(w)
y = y1 - k * dy
for j in range(w):
x = x0 + j * dx
line[j] = julia(x, y)
return line
# Executed via "python3.7 mp.py"
from multiprocessing import Pool
import sys
import time
from julia import julia_line
if __name__ == '__main__':
for scale in range(1, 9):
start_time = time.time()
with Pool(8) as pool:
w = 640*2*scale
h = 480*2*scale
image = pool.map(julia_line, ((i, w, h) for i in range(h)))
with open(f'julia-mp-{scale}.pgm', 'wb') as f:
f.write(b'P5 %d %d %d\n' % (w, h, 255))
for line in image:
f.write(line)
end_time = time.time()
duration = end_time - start_time
print(f"Scale {scale}, Time {duration:>6.6f}s")
# mpi4py version 3.0.2
# Executed via "mpiexec --hostfile hostfile -n 9 python3.7 -m mpi4py.futures mpi.py"
from mpi4py.futures import MPIPoolExecutor
import sys
import time
from julia import julia_line
if __name__ == '__main__':
for scale in range(1, 9):
start_time = time.time()
with MPIPoolExecutor(8) as executor:
w = 640*2*scale
h = 480*2*scale
image = executor.map(julia_line, ((i, w, h) for i in range(h)))
with open(f'julia-mpi-{scale}.pgm', 'wb') as f:
f.write(b'P5 %d %d %d\n' % (w, h, 255))
for line in image:
f.write(line)
end_time = time.time()
duration = end_time - start_time
print(f"Scale {scale}, Time {duration:>6.6f}s")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment