I'm using Python 3 on Windows and trying to construct a toy example, demonstrating how using multiple CPU cores can speed up computation. The toy example is rendering of the Mandelbrot fractal.
So far:
- I have avoided threading, since the Global Interpreter Lock prohibits multicores in this context
- I'm ditching example code that won't work on Windows because it lacks the forking capability of Linux
- Trying to use the "multiprocessing" package. I declare p=Pool(8) (8 is my number of cores) and using p.starmap(..) to delegate work. This is supposed to produce multiple "subprocesses" which windows will automatically delegate to different CPUs
However, I'm unable to demonstrate any speedup, whether due to overhead or no actual multiprocessing. Pointers to toy examples with demonstrable speedup would therefore be very helpful :-)
Edit: Thank you! This pushed me in the right direction and I've now got a working example that demonstrates a doubling of speed on a CPU with 4 cores.
A copy of my code with "lecture notes" here: https://pastebin.com/c9HZ2vAV
I settled on using Pool() but will later try out the "Process" alternative that @16num pointed out. Below is a code example for Pool():
p = Pool(cpu_count())
#Unlike map, starmap only allows 1 input. "partial" provides a workaround
partial_calculatePixel = partial(calculatePixel, dataarray=data)
koord = []
for j in range(height):
for k in range(width):
koord.append((j,k))
#Runs the calls to calculatePixel in a pool. "hmm" collects the output
hmm = p.starmap(partial_calculatePixel,koord)
Processdocs.python.org/2/library/…. Here is an example that creates 10 processes. The example useslockbut it's probably not needed for what you're doing. You will also want to setp.daemon=Truefor each worker. docs.python.org/2/library/… - J'ethreading, to avoid to be closed as "OP hasn't shown what he has already tried / hasn't shown effort". Very interesting topic! - Basj