I have a very large matrix (over 100k by 100K) with a calculation logic whereby each row can be calculated distinct from other rows
I want to use multiprocessing to optimize compute time (with the matrix split into 3 slices of 1/3 rows each). However it seems like multiprocessing takes longer than a single call to calculate all rows. I am changing different parts of the matrix in each process- is that the issue?
import multiprocessing, os
import time, pandas as pd, numpy as np
def mat_proc(df):
print("ID of process running worker1: {}".format(os.getpid()))
return(df+3) # simplified version of process
print('done processing')
count=5000
df = pd.DataFrame(np.random.randint(0,10,size=(3*count,3*count)),dtype='int8')
slice1=df.iloc[0:count,]
slice2=df.iloc[count:2*count,]
slice3=df.iloc[2*count:3*count,]
p1=multiprocessing.Process(target=mat_proc,args=(slice1,))
p2=multiprocessing.Process(target=mat_proc,args=(slice2,))
p3=multiprocessing.Process(target=mat_proc,args=(slice3,))
start=time.time()
print('started now')
# this is to compare the multiprocess with a single call to full matrix
#mat_proc(df)
if __name__ == '__main__':
p1.start()
p2.start()
p3.start()
p1.join()
p2.join()
p3.join()
finish=time.time()
print(f'total time taken {round(finish-start,2)}')