Home > Blockchain >  Numba : parallel flag in @jit does not work in my code with numpy 2D arrays
Numba : parallel flag in @jit does not work in my code with numpy 2D arrays

Time:12-14

First, I asked this question Numba : Why guvectorize is so slow?. Trying many things as I am discovering numba, this more basic question arose

Why this code works (parallel flag set to False):

import numpy as np
from numba import jit, prange

@jit(('float64[:,:], float64[:,:], float64[:]'), nopython=True, parallel=False)
def myFoo(result,par,x):
    lenght=len(x)
    for i in prange(lenght):
        result[:,i:i 1] = (par[:,0:1]*np.exp(par[:,1:2]*x[i]))
         
x=np.array(np.arange(0,20,1.0))
par=np.array([[i*10,i*0.1] for i in range(10)])
result = np.empty([par.shape[0],x.shape[0]], dtype=np.float64)
myFoo(result, par, x)

but when I change parallel flag to True, it does not work. The error message is :

TypingError: Failed in nopython mode pipeline (step: nopython mode backend)
Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function(<intrinsic assert_equiv>) found for signature:
 
 >>> <unknown function>(Literal[str](Sizes of result, $72binary_multiply.31 do not match on /tmp/ipykernel_64043/4263189787.py (13)), float64, int64)
 
There are 2 candidate implementations:
    - Of which 2 did not match due to:
    Intrinsic in function 'assert_equiv': File: numba/parfors/array_analysis.py: Line 127.
      With argument(s): '(unicode_type, float64, int64)':
     Rejected as the implementation raised a specific error:
       AssertionError: 
  raised from ... lib/python3.9/site-packages/numba/parfors/array_analysis.py:143

CodePudding user response:

Not sure wether it is strictly necessary in your project that you allocate the result array outside of the function scope and pass it to the function as argument. Otherwise this code modification runs perfectly fine:

import numpy as np
from numba import jit, prange

@jit(('float64[:,:], float64[:]'), nopython=True, parallel=True)
def myFoo(par,x):
    length=x.shape[0]
    result = np.empty((par.shape[0],x.shape[0]), dtype=np.float64)               
    for i in prange(length):
        result[:,i] = (par[:,0]*np.exp(par[:,1]*x[i]))
    return result

    
x=np.array(np.arange(0,20,1.0))
par=np.array([[i*10,i*0.1] for i in range(10)])
result = myFoo(par, x)
print(result)

Judging from the error message, I assume numba has an issue figuring out the reference of the result array to write into.
Note that the shape specification in allocating result raises a warning if done with a list instead of a tuple.

  • Related