Last active
October 31, 2022 11:07
-
-
Save rvprasad/6582d14b69c2f1fb6fb72b4680443605 to your computer and use it in GitHub Desktop.
Compare map's performance of mpi4py module and Python's multiprocessing module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
localhost slots=9 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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