CUDA-1.5ΒΆ
Treat CUdevice objects as handles not integers
Required inputs: IR
CUDA 1.5 [cudevice.handles] Treat CUdevice objects as handles not integers
Treat CUdevice objects as handles to internal CUDA driver device objects, not integers.
Scope: Host.
Audience: CUDA C++ and Libraries.
Category: Required.
Hardware Applicability: All Compute Capabilities.
Rationale
CUdevice is a pointer type, so they may be assigned integer values. In some historical cases, the CUDA
driver interface has accepted CUdevice objects created by assigning an integer device ordinal to a CUdevice
instead of calling cuDeviceGet with an integer device ordinal to obtain a CUdevice.
While this shortcut may work in certain circumstances, it is not guaranteed and should not be relied on.
Example 1 (Bad)
# include <cassert> # include <cuda.h> CUcontext init() { cuInit(0); // Create a context to device 0. CUcontext raw_context; CUresult const result0 = cuCtxCreate(&raw_context, 0, 0); // The third argument to `cuCtxCreate` should be a `CUdevice` handle to a // device obtained with `cuDeviceGet`, not an integer device ordinal. assert(result0 == CUDA_SUCCESS); return raw_context; }
Example 2 (Good)
# include <memory> # include <cassert> # include <cuda.h> CUcontext cudevice__good() { cuInit(0); // Get a handle to device 0. CUdevice device0; CUresult const result0 = cuDeviceGet(&device0, 0); assert(result0 == CUDA_SUCCESS); // Create a context to device 0. CUcontext raw_context; CUresult const result1 = cuCtxCreate(&raw_context, 0, device0); assert(result1 == CUDA_SUCCESS); return raw_context; }Excerpt from NVIDIA CUDA C++ Guidelines for robust and safety-critical programming, Version 3.0.1, Copyright (C) 2018-2023 NVIDIA Corporation.
Possible Messages
Key |
Text |
Severity |
Disabled |
|---|---|---|---|
cudevice_used_as_int |
Treat CUdevice objects as handles, not integers |
None |
False |
Options
This rule shares the following common options: exclude_in_macros, exclude_messages_in_system_headers, excludes, extend_exclude_to_macro_invocations, includes, justification_checker, languages, post_processing, provider, report_at, severity
The following places define options that affect this rule: Stylechecks, Analysis-GlobalOptions
This rule has no individual options.