Skip to content

Commit

Permalink
Add dtype for numpy.uintp which is compatible with C uintptr_t (#1544)
Browse files Browse the repository at this point in the history
Need this to pass C pointers to DaCe sdfg and reinterpret cast them
inside a tasklet

---------

Co-authored-by: Tal Ben-Nun <[email protected]>
  • Loading branch information
kotsaloscv and tbennun authored Mar 25, 2024
1 parent a57877f commit da0cde2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions dace/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1216,6 +1216,7 @@ def isconstant(var):
int16 = typeclass(numpy.int16)
int32 = typeclass(numpy.int32)
int64 = typeclass(numpy.int64)
uintp = typeclass(numpy.uintp)
uint8 = typeclass(numpy.uint8)
uint16 = typeclass(numpy.uint16)
uint32 = typeclass(numpy.uint32)
Expand Down
37 changes: 37 additions & 0 deletions tests/uintptr_t_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Copyright 2019-2024 ETH Zurich and the DaCe authors. All rights reserved.
import dace
import ctypes
import numpy as np


def test_uintp_size():
# c_void_p: C type -> void*
size = ctypes.sizeof(ctypes.c_void_p)
# numpy.uintp: Unsigned integer large enough to fit pointer, compatible with C uintptr_t
size_of_np_uintp = np.uintp().itemsize
# Dace uintptr_t representation
size_of_dace_uintp = dace.uintp.bytes

assert size == size_of_np_uintp == size_of_dace_uintp


def test_uintp_use():

@dace.program
def tester(arr: dace.float64[20], pointer: dace.uintp[1]):
with dace.tasklet(dace.Language.CPP):
a << arr(-1)
"""
out = decltype(out)(a);
"""
out >> pointer[0]

ptr = np.empty([1], dtype=np.uintp)
arr = np.random.rand(20)
tester(arr, ptr)
assert arr.__array_interface__['data'][0] == ptr[0]


if __name__ == '__main__':
test_uintp_size()
test_uintp_use()

0 comments on commit da0cde2

Please sign in to comment.