I'm trying to implement a function that uses python multiprocessing
in order to speed-up a calculation. I'm trying to create a pairwise distance matrix but the implementation with for loops takes more than 8 hours.
This code seems to work faster but when I print the matrix is full of zeros. When I print the rows in the function it seems to work. I think is a scope problem but I cannot understand how to deal with it.
import multiprocessing
import time
import numpy as np
def MultiProcessedFunc(i,x):
for j in range(i,len(x)):
time.sleep(0.08)
M[i,j] = (x[i]+x[j])/2
print(M[i,:]) # Check if the operation works
print('')
processes = []
v = [x+1 for x in range(8000)]
M = np.zeros((len(v),len(v)))
for i in range(len(v)):
p = multiprocessing.Process(target = MultiProcessedFunc, args =(i,v))
processes.append(p)
p.start()
for process in processes:
process.join()
end = time.time()
print('Multiprocessing: {}'.format(end-start))
print(M)