How to multiply 3 matrices using shared memory in Python?

231 views Asked by At

I want to multiply 3 matrices (like E = AxBxC) using shared memory in multiprocessing method. I can do it with 2 matrices but when I want to repeat the same procedure for the third matrix, I got stuck and don't know how to handle the shared array. I know I must use the multiprocessing array but don't know how to manage it. Here is the way I used array in my code:

mp_arr_one = multiprocessing.Array(ctypes.c_int, 3*3)

and then in my function:

arr = numpy.frombuffer(mp_arr_one.get_obj(), dtype=ctypes.c_int)
res = arr.reshape((3,3))

Everything's good for first part (D = AxB) but when I want to calculate E = DxC, the code goes wrong and the result is completely incorrect.

Thanks in advance.

1

There are 1 answers

0
rth On

Just use numpy with an optimized BLAS (e.g. OpenBLAS, BLAS ATLAS, MKL) that supports multi-threading.

Matrix multiplication will be parallelized, and because it includes extensive architecture dependent optimizations, this approach will be faster than explicitly managing shared memory in Python with multiprocessing.Array. By the way, you should use an optimized BLAS implementation with numpy, if speed is an issue, even in single thread execution, so there is no way around it.