Skip to content

Commit fb1344b

Browse files
Use namedtuple turn to pack offset/size for individual kernel argument
1 parent 6de62d0 commit fb1344b

2 files changed

Lines changed: 25 additions & 18 deletions

File tree

cuda_core/cuda/core/experimental/_module.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
#
33
# SPDX-License-Identifier: Apache-2.0
44

5-
from typing import Optional, Union
5+
from collections import namedtuple
6+
from typing import NamedTuple, Optional, Union
67
from warnings import warn
78

89
from cuda.core.experimental._utils.clear_error_support import (
@@ -216,18 +217,20 @@ def attributes(self) -> KernelAttributes:
216217
self._attributes = KernelAttributes._init(self._handle)
217218
return self._attributes
218219

219-
def _get_arguments_info(self, param_info=False) -> tuple[int, list[tuple[int, int]]]:
220+
def _get_arguments_info(self, param_info=False) -> tuple[int, list[NamedTuple]]:
220221
attr_impl = self.attributes
221222
if attr_impl._backend_version != "new":
222223
raise NotImplementedError("New backend is required")
223224
arg_pos = 0
225+
if param_info:
226+
ParamInfo = namedtuple("ParamInfo", ["offset", "size"])
224227
param_info_data = []
225228
while True:
226229
result = attr_impl._loader["paraminfo"](self._handle, arg_pos)
227230
if result[0] != driver.CUresult.CUDA_SUCCESS:
228231
break
229232
if param_info:
230-
param_info_data.append(result[1:3])
233+
param_info_data.append(ParamInfo._make(result[1:3]))
231234
arg_pos = arg_pos + 1
232235
return arg_pos, param_info_data
233236

@@ -238,8 +241,8 @@ def num_arguments(self) -> int:
238241
return num_args
239242

240243
@property
241-
def arguments_info(self) -> list[tuple[int, int]]:
242-
"""list[tuple[int, int]]: (offset, size) for each argument of this function"""
244+
def arguments_info(self) -> list[NamedTuple]:
245+
"""list[NamedTuple[int, int]]: (offset, size) for each argument of this function"""
243246
_, param_info = self._get_arguments_info(param_info=True)
244247
return param_info
245248

cuda_core/tests/test_module.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
# is strictly prohibited.
88

99

10-
import warnings
1110
import ctypes
11+
import warnings
1212

1313
import pytest
1414

@@ -173,15 +173,18 @@ def test_saxpy_arguments(get_saxpy_kernel):
173173
arg_info = krn.arguments_info
174174
n_args = len(arg_info)
175175
assert n_args == krn.num_arguments
176+
176177
class ExpectedStruct(ctypes.Structure):
177178
_fields_ = [
178-
('a', ctypes.c_float),
179-
('x', ctypes.POINTER(ctypes.c_float)),
180-
('y', ctypes.POINTER(ctypes.c_float)),
181-
('out', ctypes.POINTER(ctypes.c_float)),
182-
('N', ctypes.c_size_t)
179+
("a", ctypes.c_float),
180+
("x", ctypes.POINTER(ctypes.c_float)),
181+
("y", ctypes.POINTER(ctypes.c_float)),
182+
("out", ctypes.POINTER(ctypes.c_float)),
183+
("N", ctypes.c_size_t),
183184
]
184-
offsets, sizes = zip(*arg_info)
185+
186+
offsets = [p.offset for p in arg_info]
187+
sizes = [p.size for p in arg_info]
185188
members = [getattr(ExpectedStruct, name) for name, _ in ExpectedStruct._fields_]
186189
expected_offsets = tuple(m.offset for m in members)
187190
assert all(actual == expected for actual, expected in zip(offsets, expected_offsets))
@@ -197,15 +200,16 @@ def test_num_arguments(init_cuda, nargs, c_type_name, c_type):
197200
prog = Program(src, code_type="c++")
198201
mod = prog.compile(
199202
"cubin",
200-
name_expressions=(f"foo{nargs}", ),
203+
name_expressions=(f"foo{nargs}",),
201204
)
202205
krn = mod.get_kernel(f"foo{nargs}")
203206
assert krn.num_arguments == nargs
204207

205208
class ExpectedStruct(ctypes.Structure):
206-
_fields_ = [
207-
(f'arg_{i}', c_type) for i in range(nargs)
208-
]
209+
_fields_ = [(f"arg_{i}", c_type) for i in range(nargs)]
210+
209211
members = tuple(getattr(ExpectedStruct, f"arg_{i}") for i in range(nargs))
210-
expected = [(m.offset, m.size) for m in members]
211-
assert krn.arguments_info == expected
212+
213+
arg_info = krn.arguments_info
214+
assert all([actual.offset == expected.offset for actual, expected in zip(arg_info, members)])
215+
assert all([actual.size == expected.size for actual, expected in zip(arg_info, members)])

0 commit comments

Comments
 (0)