Repository navigation
rsutils: robust detect runtime CUDA probe - #15045
Conversation
Nir-Az
left a comment
There was a problem hiding this comment.
[rs-agentic bot] Automated PR review. Summary of findings:
- 🐛 Bug: macOS not handled in branch of — is Linux-specific.
⚠️ Misleading logs: "cuInit failed" / "cuDeviceGetCount failed" messages conflate a missing symbol (nullptr) with a call returning an error code.⚠️ Enhancement: error code is silently discarded — logging the raw integer would improve debuggability.- 📋 Tests missing: No unit/integration tests cover the new runtime-probe logic (null library, cuInit failure, zero devices, happy path).
See inline comments for details.
| using cu_device_count_t = int ( * )( int * ); | ||
| void * handle = dlopen( "libcuda.so.1", RTLD_LAZY ); | ||
| if( ! handle ) | ||
| { |
There was a problem hiding this comment.
[rs-agentic bot – potential BUG] The #else branch is reached on both Linux and macOS, but libcuda.so.1 is a Linux soname. On macOS, CUDA ships as libcuda.dylib (or inside the CUDA framework). dlopen("libcuda.so.1", ...) will always fail on macOS, so the function will silently return false on any macOS machine with a real NVIDIA GPU.
Suggested fix — add an explicit macOS arm before the catch-all Linux path:
#elif defined(__APPLE__)
void * handle = dlopen( "libcuda.dylib", RTLD_LAZY );
#else
void * handle = dlopen( "libcuda.so.1", RTLD_LAZY );
#endif(macOS CUDA support is rare today, but the code should be correct rather than silently wrong.)
There was a problem hiding this comment.
Deferring this one intentionally. macOS hasn't supported CUDA since the 10.2 release (2019) — NVIDIA dropped Mac drivers. librealsense's own CUDA paths (cuda-pointcloud, cuda-align, etc.) are also Linux/Jetson-only, so a libcuda.dylib branch would be dead code on every reachable macOS configuration. Current behaviour (probe returns false on Mac) is the right answer in practice.
Happy to add a 3-line #elif defined(__APPLE__) if anyone wants belt-and-braces correctness, but as written this is "silently correct" rather than "silently wrong" — the silent false is the truth on every Mac you can run librealsense on today.
| LOG_INFO( "CUDA driver detected with " << count << " visible device(s) - GPU acceleration available." ); | ||
| else if( ! init_ok ) | ||
| LOG_INFO( "CUDA driver library loaded but cuInit failed - GPU acceleration unavailable." ); | ||
| else if( ! count_ok ) |
There was a problem hiding this comment.
[rs-agentic bot – misleading log / minor BUG] The message "cuInit failed" is used for two distinct failure modes that the init_ok expression rolls together:
cu_initisnullptr—GetProcAddress/dlsymdid not find thecuInitsymbol in the loaded library (corrupted/wrong driver?).cu_init(0) != 0— the symbol was found but the call returned a non-zeroCUresult.
Both set init_ok = false and fall into the same log branch. A developer seeing "cuInit failed" in production cannot tell whether the driver export is missing or whether the CUDA init call itself is returning an error.
Consider splitting the check:
bool init_ok = false;
if( ! cu_init )
LOG_INFO( "CUDA driver loaded but cuInit symbol not found." );
else
{
int rc = cu_init( 0 );
if( rc != 0 )
LOG_INFO( "cuInit returned error code " << rc << " - GPU acceleration unavailable." );
else
init_ok = true;
}There was a problem hiding this comment.
Fixed in 9fb4fd2. The two failure modes now have distinct log lines and the CUresult integer is included:
"CUDA driver loaded but cuInit symbol not found"— symbol resolution failed"cuInit returned CUresult N"— symbol resolved, call returned non-zero
(Same restructuring applied to the cuDeviceGetCount branch — see reply on that thread.)
| LOG_INFO( "CUDA driver library loaded but cuInit failed - GPU acceleration unavailable." ); | ||
| else if( ! count_ok ) | ||
| LOG_INFO( "CUDA driver initialised but cuDeviceGetCount failed - GPU acceleration unavailable." ); | ||
| else |
There was a problem hiding this comment.
[rs-agentic bot – misleading log / minor BUG] Same issue as above: "cuDeviceGetCount failed" is logged whether cu_count is nullptr (symbol not found) or the call returned a non-zero error code. These are different root causes and deserve distinct messages.
There was a problem hiding this comment.
Fixed in 9fb4fd2 — symmetric treatment with the cuInit branch:
"CUDA driver initialised but cuDeviceGetCount symbol not found"— symbol missing"cuDeviceGetCount returned CUresult N"— call returned non-zero
| int count = 0; | ||
| bool init_ok = cu_init && cu_init( 0 ) == 0; | ||
| bool count_ok = init_ok && cu_count && cu_count( &count ) == 0; | ||
| bool have_device = count_ok && count > 0; |
There was a problem hiding this comment.
[rs-agentic bot – enhancement] The CUresult returned by cuInit(0) is compared to 0 (CUDA_SUCCESS) but the actual error code is discarded when the call fails. Since CUresult is a plain int, it can be captured and logged cheaply:
int rc = cu_init ? cu_init( 0 ) : -1;
bool init_ok = ( rc == 0 );Then log rc in the failure branch. This makes field-debugging significantly easier (e.g. distinguishing CUDA_ERROR_NO_DEVICE from CUDA_ERROR_INVALID_DEVICE or driver/version mismatches).
There was a problem hiding this comment.
Done in 9fb4fd2. The CUresult is captured in init_rc/count_rc and included in the failure log lines as a raw integer, so a field debugger can map it to CUDA_ERROR_NO_DEVICE (100), CUDA_ERROR_INVALID_DEVICE (101), CUDA_ERROR_NOT_INITIALIZED (3), version-mismatch codes, etc., via the CUDA Driver API reference.
| // C++11 magic statics: thread-safe one-time init, probe runs once per process. | ||
| static bool const cached = probe_cuda_driver(); | ||
| return cached; | ||
| } |
There was a problem hiding this comment.
[rs-agentic bot – note: missing tests] probe_cuda_driver() has at least four distinct code paths (library not found → return false; cuInit fails; cuDeviceGetCount fails / returns 0; happy path with count > 0). None of these appear to be covered by the existing test suite.
Because probe_cuda_driver() is static and the result is cached in a magic static, the function cannot be easily unit-tested directly. Consider one of:
- Extracting
probe_cuda_driver()into a testable free function that accepts dependency-injected function pointers forcu_init/cu_count. - Adding an integration / acceptance test that at minimum verifies the function does not crash on the current CI machines (even when returning
false). - Documenting explicitly in the header that the function is intentionally un-mockable and that CI covers it implicitly via smoke tests on Jetson runners.
There was a problem hiding this comment.
Addressed via option (3) in 9fb4fd2 — added a paragraph to the implementation header explaining why the function is intentionally non-mockable (static singleton wrapping a live OS probe; DI would inflate the public surface for one-shot use).
Coverage is provided by manual smoke testing on four platforms before pushing:
| Platform | Result |
|---|---|
| Windows x64 (Intel Arc, no NVIDIA) | FALSE, no crash, only kernel32.dll in deps |
| Linux x86_64 perclnx466 (Intel Iris, no NVIDIA, no CUDA) | FALSE, no crash, "libcuda.so.1 not found" log |
| Jetson Xavier (L4T R35 / JP5 / CUDA 11) | TRUE, 1 device, both CUDA=OFF and CUDA=ON builds |
| Jetson Orin (L4T R36 / JP6 / CUDA 12) | TRUE, 1 device, both CUDA=OFF and CUDA=ON builds |
CI agents already exercise the negative path (no NVIDIA on Windows / x86 Linux jobs) and the positive path (Jetson runners) via any binary that calls rs2_is_cuda_available() — e.g. pointcloud::create() at startup. So the existing test plumbing implicitly smokes the function on every build, even without an explicit unit test.
Makes sure CUDA is available is a robust way for all platforms