Universal Functions

Numba provides a set of decorators to create NumPy universal functions-like routines that are JIT compiled. Although, a close analog to NumPy universal functions Numba’s @vectorize are not fully compatible with a regular NumPy ufunc.. Refer Creating NumPy universal functions for details.

numba-dppy only supports numba.vectorize decorator and not yet the numba.guvectorize decorator. Another present limitation is that numba-dppy ufunc kernels cannot invoke numba_dppy.kernel functions. Ongoing work is in progress to address these limitations.

Example 1: Basic Example

Full example can be found at numba_dppy/examples/vectorize.py.

@vectorize(nopython=True)
def ufunc_kernel(x, y):
    return x + y
def test_ufunc():
    N = 10
    dtype = np.float64

    A = np.arange(N, dtype=dtype)
    B = np.arange(N, dtype=dtype) * 10

    context = get_context()
    with dpctl.device_context(context):
        C = ufunc_kernel(A, B)

    print(C)

Example 2: Calling numba.vectorize inside a numba_dppy.kernel

Full example can be found at numba_dppy/examples/blacksholes_njit.py.

@numba.vectorize(nopython=True)
def cndf2(inp):
    out = 0.5 + 0.5 * math.erf((math.sqrt(2.0) / 2.0) * inp)
    return out

Note

numba.cuda requires target='cuda' parameter for numba.vectorize and numba.guvectorize functions. numba-dppy eschews the target parameter for @vectorize and infers the target from the dpctl.device_context in which the numba.vectorize function is called.

Full Examples

  • numba_dppy/examples/vectorize.py

  • numba_dppy/examples/blacksholes_njit.py