Python Multiprocessing Data Output Wrong
Solution 1:
The reason your last line print Z[0]
returns [0] instead of [2] is that each of the processes makes an independent copy of Z (or may be Z[j]
- not completely sure about that) before modifying it. Either way, a separate process run will guarantee that your original version will be unchanged.
If you were to use the threading module
instead the last line would indeed return [2] as expected, but that is not multi-processing.
So, you probably want to use multiprocessing.Pool
instead. Going along your experiment purely for illustration, one could do the following:
In [40]: pool = multiprocessing.Pool()
In [41]: def add_func(j):
....: return X[j] + Y[j]
In [42]: pool = multiprocessing.Pool(numThreads)
In [43]: pool.map(add_func, range(numThreads))
Out[43]:
[array([ 2.]),
array([ 2.]),
array([ 2.]),
array([ 2.]),
array([ 2.]),
array([ 2.]),
array([ 2.]),
array([ 2.]),
array([ 2.]),
array([ 2.]),
array([ 2.]),
array([ 2.]),
array([ 2.]),
array([ 2.]),
array([ 2.]),
array([ 2.])]
Have fun!
For your second part to your question, the problem is that the conv() function does not return any value. While the process pool gets a copy of X, B and W for pulling values from, the Y inside conv() is local to each process that is launched. To get the new computed value of Y, you would use something like this:
defconv(idx):
Ylocal_section = X[idx*stride:idx*stride+stride].dot(W) + B[idx*stride:idx*stride+stride]
return Ylocal_section
results = pool.map(conv, range(numThreads)) # then apply each result to Yfor idx inrange(numThreads):
Y[idx*stride:idx*stride+stride] = results[idx]
Parallelism can get complicated really fast, and at this point I would evaluate existing libraries that can perform fast 2D convolution. numpy and scipy libraries can be super efficient and might serve your needs better.
Post a Comment for "Python Multiprocessing Data Output Wrong"