Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions numba_cuda/numba/cuda/np/arrayobj.py
Original file line number Diff line number Diff line change
Expand Up @@ -3643,6 +3643,8 @@ def constant_array(context, builder, ty, pyval):
"""
Create a constant array (mechanism is target-dependent).
"""
if isinstance(pyval, list):
pyval = np.asarray(pyval)
return context.make_constant_array(builder, ty, pyval)


Expand Down
3 changes: 1 addition & 2 deletions numba_cuda/numba/cuda/target.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the changes here are now superfluous?

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from numba.cuda.core.base import BaseContext
from numba.cuda.typing import cmathdecl
from numba.cuda import datamodel
from numba.cuda.cpython import listobj

from .cudadrv import nvvm
from numba.cuda import (
Expand Down Expand Up @@ -164,7 +165,6 @@ def load_additional_registries(self):
numbers,
slicing,
iterators,
listobj,
unicode,
charseq,
cmathimpl,
Expand Down Expand Up @@ -239,7 +239,6 @@ def build_list(self, builder, list_type, items):
"""
Build a list from the Numba *list_type* and its initial *items*.
"""
from numba.cuda.cpython import listobj

return listobj.build_list(self, builder, list_type, items)

Expand Down
38 changes: 38 additions & 0 deletions numba_cuda/numba/cuda/tests/cudapy/test_lists.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: BSD-2-Clause

"""
Test cases for a list object within CUDA kernels
"""

from numba import cuda
from numba.cuda import config
import numpy as np
from numba.cuda.testing import (
CUDATestCase,
)


class ListTest(CUDATestCase):
def setUp(self):
self.old_nrt_setting = config.CUDA_ENABLE_NRT
config.CUDA_ENABLE_NRT = True
super().setUp()

def tearDown(self):
config.CUDA_ENABLE_NRT = self.old_nrt_setting
super().tearDown()

def test_list_const_roundtrip(self):
lst = [1, 2, 3]

@cuda.jit
def kernel(out):
for i in range(len(lst)):
out[i] = lst[i]

out = cuda.to_device(np.zeros(len(lst)))

kernel[1, 1](out)
for g, e in zip(out.copy_to_host(), lst):
self.assertEqual(e, g)
2 changes: 1 addition & 1 deletion numba_cuda/numba/cuda/typing/typeof.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def _typeof_list(val, c):
ty = typeof_impl(val[0], c)
if ty is None:
raise ValueError(f"Cannot type list element type {type(val[0])}")
return types.List(ty, reflected=True)
return ty[::1]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a sketch of how things work for a 1D list only. A more robust implementation would:

  • Check that the purpose of the typeof operation is for a constant. We should not accept lists as arguments to kernels.
  • Convert the value to a NumPy array, then determine the Numba type of the NumPy array. This will allow handling of lists-of-lists, e.g. [[1, 2], [3, 4]]. It also prevents having to handle what happens if there are different element types (e.g. int and float), whereas the existing implementation assumes that all list elements have the same type, which need not be the case.
  • Not check for zero-length, because a zero-length NumPy array is possible and can be typed. The inability to handle a zero-length list is an issue of the reflected list implementation in Numba that doesn't present an issue for freezing global lists as arrays.



@typeof_impl.register(set)
Expand Down