An easy way to parallelize this code is to use Pool
processes:
pool = multiprocessing.Pool() results = pool.starmap(get_sub_matrix_C, ((i, other_args) for i in range(10))) for i, res in enumerate(results): C[i*10:(i+1)*10,:10] = res
I used starmap
since the get_sub_matrix_C
function has more than one argument ( starmap(f, [(x1, ..., xN)])
calls f(x1, ..., xN)
).
Please note, however, that serialization / deserialization can take considerable time and space, so you may need to use a lower-level solution to avoid this overhead.
It looks like you are using an outdated version of python. You can replace starmap
with a regular map
, but then you must provide a function that takes one parameter:
def f(args): return get_sub_matrix_C(*args) pool = multiprocessing.Pool() results = pool.map(f, ((i, other_args) for i in range(10))) for i, res in enumerate(results): C[i*10:(i+1)*10,:10] = res
source share