I am trying to use joblib in python to speed up the processing of some data, but I am having problems trying to determine how to assign the output to the required format. I tried to create, possibly too simplified code that shows the problems I am facing:
from joblib import Parallel, delayed
import numpy as np
def main():
print "Nested loop array assignment:"
regular()
print "Parallel nested loop assignment using a single process:"
par2(1)
print "Parallel nested loop assignment using multiple process:"
par2(2)
def regular():
a = [0,1,2,3,4]
b = [0,1,2,3,4]
global ab
ab = np.zeros((2,np.size(a),np.size(b)))
for i in range(0,np.size(a)):
for j in range(0,np.size(b)):
func(i,j,a,b)
print ab
def par2(process):
a2 = [0,1,2,3,4]
b2 = [0,1,2,3,4]
global ab2
ab2 = np.zeros((2,np.size(a2),np.size(b2)))
Parallel(n_jobs=process)(delayed(func2)(i,j,a2,b2) for i in xrange(0,np.size(a2)) for j in xrange(0,np.size(b2)))
print ab2
def func(i,j,a,b):
ab[0,i,j] = a[i]+b[j]
ab[1,i,j] = a[i]*b[j]
def func2(i,j,a2,b2):
ab2[0,i,j] = a2[i]+b2[j]
ab2[1,i,j] = a2[i]*b2[j]
main()
This conclusion is as follows:
Nested loop array assignment:
[[[ 0. 1. 2. 3. 4.]
[ 1. 2. 3. 4. 5.]
[ 2. 3. 4. 5. 6.]
[ 3. 4. 5. 6. 7.]
[ 4. 5. 6. 7. 8.]]
[[ 0. 0. 0. 0. 0.]
[ 0. 1. 2. 3. 4.]
[ 0. 2. 4. 6. 8.]
[ 0. 3. 6. 9. 12.]
[ 0. 4. 8. 12. 16.]]]
Parallel nested loop assignment using a single process:
[[[ 0. 1. 2. 3. 4.]
[ 1. 2. 3. 4. 5.]
[ 2. 3. 4. 5. 6.]
[ 3. 4. 5. 6. 7.]
[ 4. 5. 6. 7. 8.]]
[[ 0. 0. 0. 0. 0.]
[ 0. 1. 2. 3. 4.]
[ 0. 2. 4. 6. 8.]
[ 0. 3. 6. 9. 12.]
[ 0. 4. 8. 12. 16.]]]
Parallel nested loop assignment using multiple process:
[[[ 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]]
[[ 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]]]
From the Google search function and StackOverflow, it appears that when using joblib, the global array is not shared between each subprocess. I'm not sure if this is a joblib limitation or if there is a way around this?
script , , (4, x, x), x ( 100 ). , 2 x = 2400.
joblib ( ), , , . python 2.7.3 joblib 0.7.1.