Created
September 16, 2016 20:56
-
-
Save tkarabela/c022df3428743fa5bbb070d3268fb39e to your computer and use it in GitHub Desktop.
Paralelni vypocet dat pro matplotlib pomoci multiprocessing.Pool()
This file contains hidden or 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
| """ | |
| Paralelni vypocet dat pro matplotlib pomoci multiprocessing.Pool() | |
| """ | |
| from __future__ import print_function | |
| import multiprocessing | |
| import os | |
| from timeit import default_timer as timer | |
| import time | |
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| def f(a, b, c, d, e): | |
| time.sleep(0.001) # ... komplikovany vypocet ... | |
| return a + b + c + d + e | |
| def main(): | |
| # vykresli konturu f(...) | |
| # a, b - promenne | |
| # c, d, e - konstanty | |
| c = 1 | |
| d = 2 | |
| e = 3 | |
| delta = 0.1 | |
| a_values = np.arange(-3.0, 3.0, delta) | |
| b_values = np.arange(0, 10.0, delta) | |
| X, Y = np.meshgrid(a_values, b_values) # osy X, Y = a, b | |
| # varianta 1 - paralelne | |
| Z1 = [] | |
| N = os.cpu_count() # pocet procesu - tohle je default | |
| t0 = timer() | |
| with multiprocessing.Pool(N) as pool: | |
| for a_row, b_row in zip(X, Y): | |
| args = [(a, b, c, d, e) for a, b in zip(a_row, b_row)] | |
| values = pool.starmap(f, args) # -> [f(*args[0]), f(*args[1]), ...] | |
| Z1.append(values) | |
| print("multiprocessing.Pool({}) took {:.3f} s".format(N, timer() - t0)) | |
| # varianta 2 - bez paralelizace | |
| Z2 = [] | |
| t0 = timer() | |
| for a_row, b_row in zip(X, Y): | |
| args = [(a, b, c, d, e) for a, b in zip(a_row, b_row)] | |
| values = [f(*args[i]) for i in range(len(args))] | |
| Z2.append(values) | |
| print("serial execution took {:.3f} s".format(timer() - t0)) | |
| assert Z1 == Z2 | |
| plt.figure() | |
| plt.contour(X, Y, Z1) | |
| plt.show() | |
| # toto je pri pouzivani `multiprocessing` nutne | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment