My original code looks similar to this toy example. It creates two random matrices, multiplies them and saves the output in a list.
import numpy as np
n = 100
N = 10
out = [1] * N
for i in range(0, N):
A = np.random.rand(n, n)
B = np.random.rand(n, n)
out[i] = A * B
Now I am trying to parallelize this loop using multiprocessing. I define the function g and run_complex_operations in defs.py
import numpy as np
def g(n):
A = np.random.rand(n, n)
B = np.random.rand(n, n)
C = A * B
def run_complex_operations(operation, input, pool):
pool.map(operation, input)
print(input)
and parallelize the code as follows:
import defs
import numpy as np
import multiprocessing as mp
n = 100
N = 10
l = [n] * N
if __name__ == '__main__':
processes_pool = mp.Pool(N)
defs.run_complex_operations(defs.g, range(N), processes_pool)
My question is: How can I save the resulting matrix C in each iteration in the list out, so that I end up with a list of 10 matrices?
Update:
Solved.