From da0cde2912ffc32072cf85a1979ac09d8ab9225a Mon Sep 17 00:00:00 2001 From: Christos Kotsalos Date: Mon, 25 Mar 2024 14:24:54 +0100 Subject: [PATCH] Add dtype for numpy.uintp which is compatible with C uintptr_t (#1544) Need this to pass C pointers to DaCe sdfg and reinterpret cast them inside a tasklet --------- Co-authored-by: Tal Ben-Nun --- dace/dtypes.py | 1 + tests/uintptr_t_test.py | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 tests/uintptr_t_test.py diff --git a/dace/dtypes.py b/dace/dtypes.py index 76e6db8397..f04200e63b 100644 --- a/dace/dtypes.py +++ b/dace/dtypes.py @@ -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) diff --git a/tests/uintptr_t_test.py b/tests/uintptr_t_test.py new file mode 100644 index 0000000000..2b1941340d --- /dev/null +++ b/tests/uintptr_t_test.py @@ -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()