Nested parallelism in Python

前端 未结 2 927
失恋的感觉
失恋的感觉 2020-12-30 02:43

I am trying out multiprocessor programming with Python. Take a divide and conquer algorithm like Fibonacci for example. The program flow of execution would bran

2条回答
  •  余生分开走
    2020-12-30 02:48

    1. What am I missing here; why shouldn't a Pool be shared between processes?

    You generally can't share OS threads between processes at all, regardless of language.

    You can arrange to share access to the pool manager with worker processes, but that's probably not a good solution to any problem; see below.

    2. What is a pattern for implementing nested parallelism in Python? If possible, maintaining a recursive structure, and not trading it for iteration.

    This depends a lot on your data.

    On CPython, the general answer is to use a data structure that implements efficient parallel operations. A good example of this is NumPy's optimized array types: here is an example of using them to split a big array operation across multiple processor cores.

    The Fibonacci function implemented using blocking recursion is a particularly pessimal fit for any worker-pool-based approach, though: fib(N) will spend much of its time just tying up N workers doing nothing but waiting for other workers. There are many other ways to approach the Fibonacci function specifically, (e.g. using CPS to eliminate the blocking and fill a constant number of workers), but it's probably better to decide your strategy based on the actual problems you will be solving, rather than examples like this.

提交回复
热议问题