Parallelise python loop with numpy arrays and shared-memory

前端 未结 3 2079
Happy的楠姐
Happy的楠姐 2020-12-23 12:31

I am aware of several questions and answers on this topic, but haven\'t found a satisfactory answer to this particular problem:

What is the easiest way to do a simpl

3条回答
  •  悲&欢浪女
    2020-12-23 12:40

    Using a mapping operation (in this case multiprocessing.Pool.map()) is more or less the the canonical way to paralellize a loop on a single machine. Unless and until the built-in map() is ever paralellized.

    An overview of the different possibilities can be found here.

    You can use openmp with python (or rather cython), but it doesn't look exactly easy.

    IIRC, the point if only running multiprocessing stuff from __main__ is a neccesity because of compatibility with Windows. Since windows lacks fork(), it starts a new python interpreter and has to import the code in it.

    Edit

    Numpy can paralellize some operations like dot(), vdot() and innerproduct(), when configured with a good multithreading BLAS library like e.g. OpenBLAS. (See also this question.)

    Since numpy array operations are mostly by element it seems possible to parallelize them. But this would involve setting up either a shared memory segment for python objects, or dividing the arrays up into pieces and feeding them to the different processes, not unlike what multiprocessing.Pool does. No matter what approach is taken, it would incur memory and processing overhead to manage all that. One would have to run extensive tests to see for which sizes of arrays this would actually be worth the effort. The outcome of those tests would probably vary considerable per hardware architecture, operating system and amount of RAM.

提交回复
热议问题