Title: Cannot use CUDA Python to interact with external C/C++ code · Issue #22 · NVIDIA/cuda-python · GitHub
Open Graph Title: Cannot use CUDA Python to interact with external C/C++ code · Issue #22 · NVIDIA/cuda-python
X Title: Cannot use CUDA Python to interact with external C/C++ code · Issue #22 · NVIDIA/cuda-python
Description: The problem The Cython example below demonstrates an attempt to use CUDA Python to interact with some external C++ code. Note that the "external code" is included inline in the Cython. # distutils: language=c++ # distutils: extra_compile...
Open Graph Description: The problem The Cython example below demonstrates an attempt to use CUDA Python to interact with some external C++ code. Note that the "external code" is included inline in the Cython. # distutils:...
X Description: The problem The Cython example below demonstrates an attempt to use CUDA Python to interact with some external C++ code. Note that the "external code" is included inline in the Cython. # ...
Opengraph URL: https://github.com/NVIDIA/cuda-python/issues/22
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Cannot use CUDA Python to interact with external C/C++ code","articleBody":"# The problem\r\n\r\nThe Cython example below demonstrates an attempt to use CUDA Python to interact with some external C++ code. Note that the \"external code\" is included inline in the Cython.\r\n\r\n```cython\r\n# distutils: language=c++\r\n# distutils: extra_compile_args=-I/usr/local/cuda/include/\r\n\r\nfrom cuda.ccudart cimport cudaMemAllocationHandleType\r\n\r\ncdef extern from *:\r\n \"\"\"\r\n #include \u003ccuda_runtime_api.h>\r\n \r\n void foo(cudaMemAllocationHandleType x) {\r\n return;\r\n }\r\n \"\"\"\r\n void foo(cudaMemAllocationHandleType x)\r\n\r\nfoo(cudaMemAllocationHandleType.cudaMemHandleTypeNone)\r\n```\r\n\r\nThe external code is a function `foo` that accepts a `cudaMemAllocationHandleType`. We attempt to invoke that function from Cython by passing in a `cuda.ccudart.cudaMemAlloccationHandleType`, but this fails with an error like:\r\n\r\n```cython\r\nerror: cannot convert '__pyx_t_4cuda_7ccudart_cudaMemAllocationHandleType' to 'cudaMemAllocationHandleType'\r\n 4857 | foo(__pyx_e_4cuda_7ccudart_cudaMemHandleTypeNone);\r\n```\r\n\r\nTo reproduce the problem, save the example above to a flle `foo.pyx`, then run `cythonize -i foo.pyx`.\r\n\r\n# Why this happens\r\n\r\nThis is because the function `foo` expects a `cudaMemAllocationHandleType` that is defined in the CUDA runtime library. But CUDA Python \"rewrites\" the runtime library at the Cython layer, and has its _own_ `cudaMemAllocationHandleType` (which ends up with a mangled name when transpiled from Cython to C++). The two are not interchangeable.\r\n\r\n# A potential solution\r\n\r\nA potential solution, proposed by @leofang in an offline discussion, is to use extern declarations for types in `ccudart.pxd`, rather than to redefine them. For example:\r\n\r\n```diff\r\ndiff --git a/cuda/ccudart.pxd b/cuda/ccudart.pxd\r\nindex 57e1e96..6c0b5d4 100644\r\n--- a/cuda/ccudart.pxd\r\n+++ b/cuda/ccudart.pxd\r\n@@ -678,11 +678,12 @@ cdef enum cudaMemAllocationType:\r\n cudaMemAllocationTypePinned = 1\r\n cudaMemAllocationTypeMax = 2147483647\r\n \r\n-cdef enum cudaMemAllocationHandleType:\r\n- cudaMemHandleTypeNone = 0\r\n- cudaMemHandleTypePosixFileDescriptor = 1\r\n- cudaMemHandleTypeWin32 = 2\r\n- cudaMemHandleTypeWin32Kmt = 4\r\n+cdef extern from 'driver_types.h':\r\n+ ctypedef enum cudaMemAllocationHandleType 'cudaMemAllocationHandleType':\r\n+ cudaMemHandleTypeNone = 0\r\n+ cudaMemHandleTypePosixFileDescriptor = 1\r\n+ cudaMemHandleTypeWin32 = 2\r\n+ cudaMemHandleTypeWin32Kmt = 4\r\n \r\n cdef struct cudaMemPoolProps:\r\n cudaMemAllocationType allocType\r\ndiff --git a/setup.py b/setup.py\r\nindex 394166e..16fad9f 100644\r\n--- a/setup.py\r\n+++ b/setup.py\r\n@@ -30,6 +30,7 @@ except Exception:\r\n \r\n include_dirs = [\r\n os.path.dirname(sysconfig.get_path(\"include\")),\r\n+ '/usr/local/cuda-11.4/include',\r\n ]\r\n \r\n library_dirs = [get_python_lib(), os.path.join(os.sys.prefix, \"lib\")]\r\n```\r\n\r\n## Gotcha\r\n\r\nCurrently, we ship a single version of CUDA Python that is built with the latest CUDA toolkit, and we expect it to work for older minor versions of the CUDA toolkit by leveraging CUDA enhanced compatibility.\r\n\r\nHistorically, there have been cases when the runtime API has changed across minor versions of the CUDA toolkit. In particular, the names/ordering of enum members have changed between minor versions. For example, in CUDA 10.1, there was a typo in the enum member `cudaErrorDeviceUninitilialized` that was fixed in 10.2.\r\n\r\nIt's not clear how we would handle the situation if something like that were to happen again. In the example above, we would have to have separate extern declarations for 10.1 and 10.2 somehow.","author":{"url":"https://github.com/shwina","@type":"Person","name":"shwina"},"datePublished":"2022-05-11T19:10:53Z","url":"https://github.com/NVIDIA/cuda-python/issues/22"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react-reconciler,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@d5750093facf |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:f463560f-35f4-9ad2-1c2e-99d2dd857380 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | BE9A:1E497:428A1F:5C25B0:6AF840B2 |
| html-safe-nonce | 8d822bd3352a95710f80e8d889fc8cc034e405a18dc41189fd6c783880c156bf |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCRTlBOjFFNDk3OjQyOEExRjo1QzI1QjA6NkFGODQwQjIiLCJ2aXNpdG9yX2lkIjoiNjk4NTc0MTA0NjE2Nzc5OTU1IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | 6ac27a8ddcb79c798f13191f05add01964e078ac4442db6f95da97d9c0ee7da3 |
| hovercard-subject-tag | issue:1233073815 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/NVIDIA/cuda-python/22/issue_layout |
| twitter:image | https://opengraph.githubassets.com/5b30cb459de6ab9dc6306ecc1cc93858aa86e234cc35b2bf8fdea06e4102846d/NVIDIA/cuda-python/issues/22 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/5b30cb459de6ab9dc6306ecc1cc93858aa86e234cc35b2bf8fdea06e4102846d/NVIDIA/cuda-python/issues/22 |
| og:image:alt | The problem The Cython example below demonstrates an attempt to use CUDA Python to interact with some external C++ code. Note that the "external code" is included inline in the Cython. # distutils:... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | shwina |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5713ecd83001b2f55c3509fb7a292bf7d66d0f167c7ae87a9f7eaec5a52bf8db |
| turbo-cache-control | no-preview |
| go-import | github.com/NVIDIA/cuda-python git https://github.com/NVIDIA/cuda-python.git |
| octolytics-dimension-user_id | 1728152 |
| octolytics-dimension-user_login | NVIDIA |
| octolytics-dimension-repository_id | 381173759 |
| octolytics-dimension-repository_nwo | NVIDIA/cuda-python |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 381173759 |
| octolytics-dimension-repository_network_root_nwo | NVIDIA/cuda-python |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 653be32ff5c755082a4db662491bfbbcfc9112c1 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width