Skip to content

Make CUDA runtime init soft-fail when no device is present - #107

Open
badnikhil wants to merge 1 commit into
libmir:masterfrom
badnikhil:fix/cuda-runtime-no-device
Open

Make CUDA runtime init soft-fail when no device is present#107
badnikhil wants to merge 1 commit into
libmir:masterfrom
badnikhil:fix/cuda-runtime-no-device

Conversation

@badnikhil

Copy link
Copy Markdown
Contributor

Follows up on #100 - ensure the runtime module does not cause issues trying to load itself when no CUDA is present.

The guard from #100 doesn't achieve this: it wraps runtime.d's module constructors in version(LDC_DCompute_CUDA), but no compiler defines that identifier — LDC predefines only LDC_DCompute The constructors are therefore compiled out in every build, and on machines that do have a GPU the default context is never created:

Changes

  • New Platform.tryInitialise(): a soft-failing probe that never throws and never mutates the thread-local error status - checks loadCUDA() (noLibrary/badLibrary), the cuInit result, and cuDeviceGetCount > 0.
  • _initPlatform() marks CUDA unavailable and returns quietly when the probe fails; device/context creation is wrapped in a defensive try/catch so nothing can escape shared static this() and abort the process before main().
  • Per-thread static this() no-ops when the platform isn't ready.
  • ensureInit() now throws a catchable DComputeDriverException("No CUDA driver or device available on this system.") when CUDA is absent, so launch!'s lazy-init path reports cleanly instead of failing later with a confusing invalidContext.

Verification (RTX 2050; device masking via CUDA_VISIBLE_DEVICES)

Scenario Before After
CUDA build, GPU present suite fails (invalidContext, exit 1) unmodified suite passes (exit 0)
CUDA build, device masked ("" / -1) constructors dead, invalidContext on first Buffer survives constructors; clear catchable error on first CUDA use
OpenCL build (also compiles cuda/runtime.d), CUDA masked constructors run and soft-fail quietly; program proceeds into OpenCL host code

An opt-in automated test in tests/runtime_init.d (env-guarded) covers the no-device path: DCOMPUTE_TEST_NO_CUDA=1 CUDA_VISIBLE_DEVICES="" ./dcompute verifies the constructors survive and ensureInit() throws as expected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant