From 83090292ffe72379e9aa0a70e2b6dc82cf2182fb Mon Sep 17 00:00:00 2001 From: stubbiali Date: Fri, 23 Jun 2023 00:09:03 +0200 Subject: [PATCH 1/3] Introduce __hip_array_interface__. --- include/gridtools/storage/adapter/python_sid_adapter.hpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/include/gridtools/storage/adapter/python_sid_adapter.hpp b/include/gridtools/storage/adapter/python_sid_adapter.hpp index 255d63485..2290c6199 100644 --- a/include/gridtools/storage/adapter/python_sid_adapter.hpp +++ b/include/gridtools/storage/adapter/python_sid_adapter.hpp @@ -268,7 +268,11 @@ namespace gridtools { static_assert(std::is_trivially_copy_constructible_v, "as_cuda_sid should be instantiated with the trivially copyable type"); +#if defined(__HIP__) + auto iface = src.attr("__hip_array_interface__").cast(); +#else auto iface = src.attr("__cuda_array_interface__").cast(); +#endif // shape array shape; From b0340e45b399b90d15fffdfbc0983b8a5ff4a40c Mon Sep 17 00:00:00 2001 From: Hannes Vogt Date: Tue, 18 Jul 2023 12:34:49 +0200 Subject: [PATCH 2/3] fix testing of hip interface --- .../storage/adapter/python_sid_adapter.hpp | 2 ++ tests/regression/py_bindings/CMakeLists.txt | 2 +- tests/regression/py_bindings/driver.py | 14 +++++++++++--- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/include/gridtools/storage/adapter/python_sid_adapter.hpp b/include/gridtools/storage/adapter/python_sid_adapter.hpp index 2290c6199..265c1b4be 100644 --- a/include/gridtools/storage/adapter/python_sid_adapter.hpp +++ b/include/gridtools/storage/adapter/python_sid_adapter.hpp @@ -269,6 +269,8 @@ namespace gridtools { "as_cuda_sid should be instantiated with the trivially copyable type"); #if defined(__HIP__) + // This is a custom property that has to be added manually (not provided by cupy). + // Should be replaced by `dlpack` for uniform array interface support. auto iface = src.attr("__hip_array_interface__").cast(); #else auto iface = src.attr("__cuda_array_interface__").cast(); diff --git a/tests/regression/py_bindings/CMakeLists.txt b/tests/regression/py_bindings/CMakeLists.txt index 2991dc29d..bb8b3d50b 100644 --- a/tests/regression/py_bindings/CMakeLists.txt +++ b/tests/regression/py_bindings/CMakeLists.txt @@ -29,4 +29,4 @@ pybind11_add_module(py_implementation implementation.cpp) target_link_libraries(py_implementation PRIVATE gridtools) -add_test(NAME py_bindings COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/driver.py) +add_test(NAME py_bindings COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/driver.py ${GT_CUDA_TYPE}) diff --git a/tests/regression/py_bindings/driver.py b/tests/regression/py_bindings/driver.py index bcb34845e..e53c85134 100644 --- a/tests/regression/py_bindings/driver.py +++ b/tests/regression/py_bindings/driver.py @@ -6,6 +6,8 @@ import numpy as np import py_implementation as testee +HIP = True if len(sys.argv) > 1 and sys.argv[1] == "HIPCC-AMDGPU" else False + def test_3d(): src = np.fromfunction(lambda i, j, k : i + j + k, (3, 4, 5), dtype=np.double) dst = np.zeros_like(src) @@ -35,9 +37,15 @@ def test_cuda_sid(): class Mock: def __init__(self, **kwargs): self.kwargs = kwargs - @property - def __cuda_array_interface__(self): - return self.kwargs + + if not HIP: + @property + def __cuda_array_interface__(self): + return self.kwargs + else: + @property + def __hip_array_interface__(self): + return self.kwargs mock = Mock( shape=(3, 4, 5), From fe68c1889d095c32eee3409fed6061625d2d2ea6 Mon Sep 17 00:00:00 2001 From: Hannes Vogt Date: Tue, 18 Jul 2023 17:01:55 +0200 Subject: [PATCH 3/3] Update AUTHORS --- AUTHORS | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS b/AUTHORS index 45790d056..ec3f45d3d 100644 --- a/AUTHORS +++ b/AUTHORS @@ -16,3 +16,4 @@ Auriane Reverdell (aurianer), ETH Zurich (CSCS) Mikael Simberg (msimberg), ETH Zurich (CSCS) Till Ehrengruber (tehrengruber), ETH Zurich (CSCS) Péter Kardos (petiaccja), ETH Zurich (EXCLAIM) +Stefano Ubbiali (stubbiali), ETH Zurich