Can't use basic NumPy functions with CUDA, like zeros or empty

Hello.

I’m a newbie with Python and Numba, and I’m quite stuck trying to use even the most basic functions in Numpy (things like empty, zeros or ones). This very simple code, for example:

from numba import cuda
import numba
import numpy as np

@cuda.jit
def my_kernel(G):
    G = np.zeros((128, 128), dtype=np.float64)

G_shape = (128, 128)
G = np.empty(G_shape)
d_G = cuda.device_array(G_shape, dtype=np.float64)

threadsperblock=32
blockspergrid = (G.size + (threadsperblock - 1))
my_kernel[blockspergrid, threadsperblock](d_G)
cuda.synchronize()

d_G.copy_to_host(G)

If I try to run it, Numba refuses to compile with this error:

Traceback (most recent call last):
  File "test.py", line 18, in <module>
    my_kernel[blockspergrid, threadsperblock](d_G)
  File "/home/marc/anaconda3/envs/mudpy/lib/python2.7/site-packages/numba/cuda/compiler.py", line 798, in __call__
    kernel = self.specialize(*args)
  File "/home/marc/anaconda3/envs/mudpy/lib/python2.7/site-packages/numba/cuda/compiler.py", line 809, in specialize
    kernel = self.compile(argtypes)
  File "/home/marc/anaconda3/envs/mudpy/lib/python2.7/site-packages/numba/cuda/compiler.py", line 825, in compile
    **self.targetoptions)
  File "/home/marc/anaconda3/envs/mudpy/lib/python2.7/site-packages/numba/compiler_lock.py", line 32, in _acquire_compile_lock
    return func(*args, **kwargs)
  File "/home/marc/anaconda3/envs/mudpy/lib/python2.7/site-packages/numba/cuda/compiler.py", line 61, in compile_kernel
    cres = compile_cuda(pyfunc, types.void, args, debug=debug, inline=inline)
  File "/home/marc/anaconda3/envs/mudpy/lib/python2.7/site-packages/numba/compiler_lock.py", line 32, in _acquire_compile_lock
    return func(*args, **kwargs)
  File "/home/marc/anaconda3/envs/mudpy/lib/python2.7/site-packages/numba/cuda/compiler.py", line 50, in compile_cuda
    locals={})
  File "/home/marc/anaconda3/envs/mudpy/lib/python2.7/site-packages/numba/compiler.py", line 551, in compile_extra
    return pipeline.compile_extra(func)
  File "/home/marc/anaconda3/envs/mudpy/lib/python2.7/site-packages/numba/compiler.py", line 331, in compile_extra
    return self._compile_bytecode()
  File "/home/marc/anaconda3/envs/mudpy/lib/python2.7/site-packages/numba/compiler.py", line 393, in _compile_bytecode
    return self._compile_core()
  File "/home/marc/anaconda3/envs/mudpy/lib/python2.7/site-packages/numba/compiler.py", line 373, in _compile_core
    raise e
numba.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Use of unsupported NumPy function 'numpy.zeros' or unsupported use of the function.

File "test.py", line 9:
def my_kernel(G):
    G = np.zeros(shape=(128, 128), dtype=np.float64)
    ^

[1] During: typing of get attribute at test.py (9)

File "test.py", line 9:
def my_kernel(G):
    G = np.zeros(shape=(128, 128), dtype=np.float64)
    ^

According to the documentation, these methods are supported and should work. I’ve tried with and without dtype, I’ve tried with different types (both from Numba and NumPy), different sizes… Nothing seems to work. What am I doing wrong? Can anybody help me, please?

By the way, I’m using Numba 0.47, becauseI’m working with a Python 2.7 code base.

Marc

The list of supported Python features, libraries, and functions differs for the CUDA target. The list supported by the CUDA target is: Supported Python features in CUDA Python — Numba 0.53.0-py3.7-linux-x86_64.egg documentation

For this particular example, you can’t do anything that allocates memory on the GPU, like np.zeros - in general most NumPy functions that operate on arrays are also not available.

You will instead need to allocate G outside the kernel and pass it in (or you can use local / shared memory for small arrays that need only be used within the kernel).

Oh, OK. I didn’t know the subset of Python features was different for CUDA. Thanks!