Skip to content

Commit e060099

Browse files
committed
GPU: Fix CUDA GetMemInfo must use correct device and simplify context creation / cleanup
1 parent b6f15f8 commit e060099

File tree

1 file changed

+12
-23
lines changed

1 file changed

+12
-23
lines changed

GPU/GPUTracking/Base/cuda/GPUReconstructionCUDA.cu

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -125,34 +125,25 @@ int32_t GPUReconstructionCUDA::InitDevice_Runtime()
125125
}
126126
std::vector<bool> devicesOK(count, false);
127127
std::vector<size_t> devMemory(count, 0);
128-
bool contextCreated = false;
128+
std::vector<bool> contextCreated(count, false);
129129
for (int32_t i = 0; i < count; i++) {
130130
if (GetProcessingSettings().debugLevel >= 4) {
131131
GPUInfo("Examining device %d", i);
132132
}
133133
size_t free, total;
134-
#ifndef __HIPCC__ // CUDA
135-
if (GPUChkErrI(cudaInitDevice(i, 0, 0))) {
136-
#else // HIP
137-
if (GPUChkErrI(hipSetDevice(i))) {
138-
#endif
134+
if (GPUChkErrI(cudaSetDevice(i))) {
139135
if (GetProcessingSettings().debugLevel >= 4) {
140136
GPUWarning("Couldn't create context for device %d. Skipping it.", i);
141137
}
142138
continue;
143139
}
144-
contextCreated = true;
140+
contextCreated[i] = true;
145141
if (GPUChkErrI(cudaMemGetInfo(&free, &total))) {
146142
if (GetProcessingSettings().debugLevel >= 4) {
147143
GPUWarning("Error obtaining CUDA memory info about device %d! Skipping it.", i);
148144
}
149-
GPUChkErr(cudaDeviceReset());
150145
continue;
151146
}
152-
if (count > 1) {
153-
GPUChkErr(cudaDeviceReset());
154-
contextCreated = false;
155-
}
156147
if (GetProcessingSettings().debugLevel >= 4) {
157148
GPUInfo("Obtained current memory usage for device %d", i);
158149
}
@@ -212,13 +203,20 @@ int32_t GPUReconstructionCUDA::InitDevice_Runtime()
212203
bestDevice = GetProcessingSettings().deviceNum;
213204
}
214205
}
215-
if (noDevice) {
216-
if (contextCreated) {
206+
for (int32_t i = 0; i < count; i++) {
207+
if (contextCreated[i] && (noDevice || i != bestDevice)) {
208+
GPUChkErrI(cudaSetDevice(i));
217209
GPUChkErrI(cudaDeviceReset());
218210
}
211+
}
212+
if (noDevice) {
219213
return (1);
220214
}
221215
mDeviceId = bestDevice;
216+
if (GPUChkErrI(cudaSetDevice(mDeviceId))) {
217+
GPUError("Could not set CUDA Device!");
218+
return (1);
219+
}
222220

223221
GPUChkErrI(cudaGetDeviceProperties(&deviceProp, mDeviceId));
224222

@@ -262,15 +260,6 @@ int32_t GPUReconstructionCUDA::InitDevice_Runtime()
262260
}
263261
#endif
264262

265-
#ifndef __HIPCC__ // CUDA
266-
if (contextCreated == 0 && GPUChkErrI(cudaInitDevice(mDeviceId, 0, 0))) {
267-
#else // HIP
268-
if (contextCreated == 0 && GPUChkErrI(hipSetDevice(mDeviceId))) {
269-
#endif
270-
GPUError("Could not set CUDA Device!");
271-
return (1);
272-
}
273-
274263
#ifndef __HIPCC__ // CUDA
275264
if (GPUChkErrI(cudaDeviceSetLimit(cudaLimitStackSize, GPUCA_GPU_STACK_SIZE))) {
276265
GPUError("Error setting CUDA stack size");

0 commit comments

Comments
 (0)