-
Notifications
You must be signed in to change notification settings - Fork 307
[libcu++] Dynamically load CUDA library instead of using the runtime #6899
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+34
−13
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,11 @@ | |
| # include <cuda/std/__internal/namespaces.h> | ||
| # include <cuda/std/__type_traits/always_false.h> | ||
| # include <cuda/std/__type_traits/is_same.h> | ||
| # if _CCCL_OS(WINDOWS) | ||
| # include <windows.h> | ||
| # else | ||
| # include <dlfcn.h> | ||
| # endif | ||
|
|
||
| # include <cuda.h> | ||
|
|
||
|
|
@@ -46,21 +51,36 @@ _CCCL_BEGIN_NAMESPACE_CUDA_DRIVER | |
| _CCCL_SUPPRESS_DEPRECATED_PUSH | ||
|
|
||
| //! @brief Gets the cuGetProcAddress function pointer. | ||
| [[nodiscard]] _CCCL_HOST_API inline auto __getProcAddressFn() -> decltype(cuGetProcAddress)* | ||
| [[nodiscard]] _CCCL_PUBLIC_HOST_API inline auto __getProcAddressFn() -> decltype(cuGetProcAddress)* | ||
| { | ||
| // TODO switch to dlopen of libcuda.so instead of the below | ||
| void* __fn; | ||
| ::cudaDriverEntryPointQueryResult __result; | ||
| # if _CCCL_CTK_AT_LEAST(13, 0) | ||
| ::cudaError_t __status = | ||
| ::cudaGetDriverEntryPointByVersion("cuGetProcAddress", &__fn, 13000, ::cudaEnableDefault, &__result); | ||
| # else | ||
| ::cudaError_t __status = ::cudaGetDriverEntryPoint("cuGetProcAddress", &__fn, ::cudaEnableDefault, &__result); | ||
| # endif | ||
| if (__status != ::cudaSuccess || __result != ::cudaDriverEntryPointSuccess) | ||
| # if _CCCL_OS(WINDOWS) | ||
| static auto __driver_library = ::LoadLibraryExA("nvcuda.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32); | ||
| if (__driver_library == nullptr) | ||
| { | ||
| ::cuda::__throw_cuda_error(::cudaErrorUnknown, "Failed to load nvcuda.dll"); | ||
| } | ||
| static void* __fn = ::GetProcAddress(__driver_library, "cuGetProcAddress_v2"); | ||
| if (__fn == nullptr) | ||
| { | ||
| ::cuda::__throw_cuda_error(::cudaErrorUnknown, "Failed to get cuGetProcAddress_v2 from nvcuda.dll"); | ||
| } | ||
| # else // ^^^ _CCCL_OS(WINDOWS) ^^^ / vvv !_CCCL_OS(WINDOWS) vvv | ||
| # if _CCCL_OS(ANDROID) | ||
| const char* __driver_library_name = "libcuda.so"; | ||
| # else // ^^^ _CCCL_OS(ANDROID) ^^^ / vvv !_CCCL_OS(ANDROID) vvv | ||
| const char* __driver_library_name = "libcuda.so.1"; | ||
| # endif // ^^^ !_CCCL_OS(ANDROID) ^^^ | ||
| static void* __driver_library = ::dlopen(__driver_library_name, RTLD_NOW); | ||
| if (__driver_library == nullptr) | ||
| { | ||
| ::cuda::__throw_cuda_error(::cudaErrorUnknown, "Failed to load libcuda.so.1"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't we throw |
||
| } | ||
| static void* __fn = ::dlsym(__driver_library, "cuGetProcAddress_v2"); | ||
| if (__fn == nullptr) | ||
| { | ||
| ::cuda::__throw_cuda_error(::cudaErrorUnknown, "Failed to get cuGetProcAddress"); | ||
| ::cuda::__throw_cuda_error(::cudaErrorUnknown, "Failed to get cuGetProcAddress_v2 from libcuda.so.1"); | ||
| } | ||
| # endif // ^^^ !_CCCL_OS(WINDOWS) ^^^ | ||
| return reinterpret_cast<decltype(cuGetProcAddress)*>(__fn); | ||
| } | ||
|
|
||
|
|
@@ -151,7 +171,7 @@ _CCCL_HOST_API inline void __call_driver_fn(Fn __fn, const char* __err_msg, Args | |
| //! @return The address of the symbol. | ||
| //! | ||
| //! @throws @c cuda::cuda_error if the symbol cannot be obtained or the CUDA driver failed to initialize. | ||
| [[nodiscard]] _CCCL_HOST_API inline void* | ||
| [[nodiscard]] _CCCL_PUBLIC_HOST_API inline void* | ||
| __get_driver_entry_point(const char* __name, [[maybe_unused]] int __major = 12, [[maybe_unused]] int __minor = 0) | ||
| { | ||
| // Get cuGetProcAddress function and call cuInit(0) only on the first call | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we have a separate variable for the symbol name? It's used twice and I can see us changing it only in one place in the future..