Parallelize these nested for loops in python

前端 未结 3 1338
北荒
北荒 2021-01-04 14:18

I have a multidimensional array (result) that should be filled by some nested loops. Function fun() is a complex and time-consuming function. I wan

3条回答
  •  臣服心动
    2021-01-04 14:34

    Here is a version of code that runs fun(i, j, k) in parallel for differend k indices. This is done by running fun in different processes by using https://docs.python.org/2/library/multiprocessing.html

    import numpy as np
    from multiprocessing import Pool
    
    
    def fun(x, y, z):
        # time-consuming computation...
        # ...
    
        return output
    
    
    def fun_wrapper(indices):
        fun(*indices)
    
    if __name__ == '__main__':
        dim1 = 10
        dim2 = 20
        dim3 = 30
    
        result = np.zeros([dim1, dim2, dim3])
    
        pool = Pool(processes=8)
        for i in xrange(dim1):
            for j in xrange(dim2):
                result[i, j] = pool.map(fun_wrapper, [(i, j, k) for k in xrange(dim3)])
    

    This 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

提交回复
热议问题