Skip to content

Commit 7fbfedc

Browse files
authored
refactor: Rearrange initialize_cuda() for better err check, no warn (#4726)
Sonar was complaining that initialize_cuda() contained a loop that would only iterate once. Upon looking, this was true, but mainly because we just didn't adequately check errors. Fix up, which should both make the code more robust and also silence the Sonar complaint. Signed-off-by: Larry Gritz <lg@larrygritz.com>
1 parent 2015a79 commit 7fbfedc

1 file changed

Lines changed: 22 additions & 19 deletions

File tree

src/libOpenImageIO/oiio_gpu.cpp

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ initialize_cuda()
9999
{
100100
std::lock_guard lock(compute_mutex);
101101

102+
cuda_supported = false;
103+
102104
// Environment OPENIMAGEIO_CUDA=0 trumps everything else, turns off
103105
// Cuda functionality. We don't even initialize in this case.
104106
std::string env = Sysutil::getenv("OPENIMAGEIO_CUDA");
@@ -112,34 +114,35 @@ initialize_cuda()
112114
if (!checkCudaErrors(cudaGetDeviceCount(&deviceCount))) {
113115
return;
114116
}
117+
OIIO::debugfmt("Number of CUDA devices: {}\n", deviceCount);
115118

116119
// Initialize CUDA
117-
if (!CUDA_CHECK(cudaFree(0))) {
120+
bool ok = CUDA_CHECK(cudaFree(0)) && CUDA_CHECK(cudaSetDevice(0))
121+
&& CUDA_CHECK(cudaStreamCreate(&cuda_stream));
122+
if (!ok) {
118123
cuda_geterror(); // clear the error
119124
return;
120125
}
121126

122-
CUDA_CHECK(cudaSetDevice(0));
123-
CUDA_CHECK(cudaStreamCreate(&cuda_stream));
124-
125-
OIIO::debugfmt("Number of CUDA devices: {}\n", deviceCount);
126127
for (int dev = 0; dev < deviceCount; ++dev) {
127128
cudaDeviceProp deviceProp;
128-
CUDA_CHECK(cudaGetDeviceProperties(&deviceProp, dev));
129-
CUDA_CHECK(cudaDriverGetVersion(&cuda_driver_version));
130-
CUDA_CHECK(cudaRuntimeGetVersion(&cuda_runtime_version));
131-
cuda_device_name = ustring(deviceProp.name);
132-
cuda_compatibility = 100 * deviceProp.major + deviceProp.minor;
133-
cuda_total_memory = deviceProp.totalGlobalMem;
134-
OIIO::debugfmt(
135-
"CUDA device \"{}\": driver {}, runtime {}, Cuda compat {}\n",
136-
deviceProp.name, cuda_driver_version, cuda_runtime_version,
137-
cuda_compatibility);
138-
OIIO::debugfmt(" total mem {:g} MB\n",
139-
cuda_total_memory / (1024.0 * 1024.0));
140-
break; // only inventory the first Cuda device. FIXME?
129+
bool ok = CUDA_CHECK(cudaGetDeviceProperties(&deviceProp, dev))
130+
&& CUDA_CHECK(cudaDriverGetVersion(&cuda_driver_version))
131+
&& CUDA_CHECK(cudaRuntimeGetVersion(&cuda_runtime_version));
132+
if (ok) {
133+
cuda_device_name = ustring(deviceProp.name);
134+
cuda_compatibility = 100 * deviceProp.major + deviceProp.minor;
135+
cuda_total_memory = deviceProp.totalGlobalMem;
136+
OIIO::debugfmt(
137+
"CUDA device \"{}\": driver {}, runtime {}, Cuda compat {}\n",
138+
deviceProp.name, cuda_driver_version, cuda_runtime_version,
139+
cuda_compatibility);
140+
OIIO::debugfmt(" total mem {:g} MB\n",
141+
cuda_total_memory / (1024.0 * 1024.0));
142+
cuda_supported = true;
143+
break; // only inventory the first Cuda device. FIXME?
144+
}
141145
}
142-
cuda_supported = true;
143146
}
144147

145148
#endif /* defined(OIIO_USE_CUDA) */

0 commit comments

Comments
 (0)