Hello,
I am learning numba and I try to parallelize a code that calculates y=f(x,parameters) for a large number of x values. So x is chunked. The following code is an example that calculates y=a*exp(b*x) for 3 parameters (a,b) and a few x values but there is not parallelization. It is only to be sure that it works.
import numpy as np
from numba import jit, prange
@jit(('float64[:], float64[:,:], int64'), nopython=True, parallel=False)
def myFoo(x, par, numthreads):
result = np.empty((par.shape[0],len(x)), dtype=np.float64)
chunklen = (len(x) + numthreads - 1) // numthreads
for i in range(numthreads):
result[:,i * chunklen:(i + 1) * chunklen] = par[:,0:1]*np.exp(x[i * chunklen:(i + 1) * chunklen]*par[:,0:1])
return result
x=np.array(np.arange(0,10,1.0))
par=np.array([[1.0,0.5],[2.0,3.0],[3.0,5.0]])
myFoo(x, par, 4)
This code works. In my real problem, I have a few parameter sets and a lot ot x values. So I set the parallel flag to True and I replaced range by prange
import numpy as np
from numba import jit, prange
@jit(('float64[:], float64[:,:], int64'), nopython=True, parallel=True)
def myFoo(x, par, numthreads):
result = np.empty((par.shape[0],len(x)), dtype=np.float64)
chunklen = (len(x) + numthreads - 1) // numthreads
for i in prange(numthreads):
result[:,i * chunklen:(i + 1) * chunklen] = par[:,0:1]*np.exp(x[i * chunklen:(i + 1) * chunklen]*par[:,0:1])
return result
x=np.array(np.arange(0,10,1.0))
par=np.array([[1.0,0.5],[2.0,3.0],[3.0,5.0]])
myFoo(x, par, 4)
And I got the following error
AssertionError: Sizes of $80binary_subscr.12, $106binary_subscr.26 do not match on /tmp/ipykernel_17011/815571837.py (9)
How can I solve it ?
Thanks for answer.