I have a multidimensional array (result
) that should be filled by some nested loops. Function fun()
is a complex and time-consuming function. I want to fill my array elements in a parallel manner, so I can use all my system's processing power.
Here's the code:
import numpy as np
def fun(x, y, z):
# time-consuming computation...
# ...
return output
dim1 = 10
dim2 = 20
dim3 = 30
result = np.zeros([dim1, dim2, dim3])
for i in xrange(dim1):
for j in xrange(dim2):
for k in xrange(dim3):
result[i, j, k] = fun(i, j, k)
My question is that "Can I parallelize this code or not? if yes, How?"
I'm using Windows 10 64-bit and python 2.7.
Please provide your solution by changing my code if you can. Thanks!
If you want a more general solution, taking advantage of fully parallel execution, then why not use something like this:
Note I'm using
multiprocess
instead ofmultiprocessing
, but that's only to get the ability to work in the interpreter.I didn't use a
numpy.array
, but if you had to... you could just dump the output fromp.map
directly into anumpy.array
and then modify theshape
attribute to beshape = (dim1, dim2, dim3)
, or you could do something like this:A simple approach could be to divide the array in sections and create some threads to operate throught these sections. For example one section from (0,0,0) to (5,10,15) and other one from (5,10,16) to (10,20,30).
You can use threading module and do something like this
You can define these sections through some algorithm and determine the number of threads dynamically. Hope it helps you or at least give you some ideas.
Here is a version of code that runs
fun(i, j, k)
in parallel for differendk
indices. This is done by runningfun
in different processes by using https://docs.python.org/2/library/multiprocessing.htmlThis is not the most elegant solution but you may start with it. And you will get a speed up only if
fun
contains time-consuming computation