Skip to content

Commit 3957037

Browse files
committed
GPU: Do not try use OpenCL platforms where device query fails or which have 0 devices
1 parent 7560127 commit 3957037

File tree

2 files changed

+51
-51
lines changed

2 files changed

+51
-51
lines changed

GPU/GPUTracking/Base/opencl/GPUReconstructionOCL.cxx

Lines changed: 51 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -110,29 +110,60 @@ int32_t GPUReconstructionOCLBackend::InitDevice_Runtime()
110110
}
111111

112112
bool found = false;
113+
char platform_profile[256] = {}, platform_version[256] = {}, platform_name[256] = {}, platform_vendor[256] = {};
114+
auto queryPlatforms = [&platform_profile, &platform_version, &platform_name, &platform_vendor](auto platform) {
115+
clGetPlatformInfo(platform, CL_PLATFORM_PROFILE, sizeof(platform_profile), platform_profile, nullptr);
116+
clGetPlatformInfo(platform, CL_PLATFORM_VERSION, sizeof(platform_version), platform_version, nullptr);
117+
clGetPlatformInfo(platform, CL_PLATFORM_NAME, sizeof(platform_name), platform_name, nullptr);
118+
clGetPlatformInfo(platform, CL_PLATFORM_VENDOR, sizeof(platform_vendor), platform_vendor, nullptr);
119+
};
120+
auto checkPlatform = [&](auto platform) {
121+
cl_uint tmp;
122+
if (clGetDeviceIDs(platform, CL_DEVICE_TYPE_ALL, 0, nullptr, &tmp) != CL_SUCCESS || tmp == 0) {
123+
return false;
124+
}
125+
126+
queryPlatforms(platform);
127+
float ver1 = 0;
128+
sscanf(platform_version, "OpenCL %f", &ver1);
129+
if (ver1 >= 2.2f) {
130+
if (mProcessingSettings.debugLevel >= 2) {
131+
GPUInfo("OpenCL 2.2 capable platform found");
132+
}
133+
return true;
134+
}
135+
136+
if (strcmp(platform_vendor, "Advanced Micro Devices, Inc.") == 0 && ver1 >= 2.0f) {
137+
float ver2 = 0;
138+
const char* pos = strchr(platform_version, '(');
139+
if (pos) {
140+
sscanf(pos, "(%f)", &ver2);
141+
}
142+
if ((ver1 >= 2.f && ver2 >= 2000.f) || ver1 >= 2.1f) {
143+
if (mProcessingSettings.debugLevel >= 2) {
144+
GPUInfo("AMD ROCm OpenCL Platform found");
145+
}
146+
return true;
147+
}
148+
}
149+
return false;
150+
};
151+
113152
if (mProcessingSettings.platformNum >= 0) {
114153
if (mProcessingSettings.platformNum >= (int32_t)num_platforms) {
115154
quit("Invalid platform specified");
116155
}
117156
mInternals->platform = mInternals->platforms[mProcessingSettings.platformNum];
118157
found = true;
119158
if (mProcessingSettings.debugLevel >= 2) {
120-
char platform_profile[256] = {}, platform_version[256] = {}, platform_name[256] = {}, platform_vendor[256] = {};
121-
clGetPlatformInfo(mInternals->platform, CL_PLATFORM_PROFILE, sizeof(platform_profile), platform_profile, nullptr);
122-
clGetPlatformInfo(mInternals->platform, CL_PLATFORM_VERSION, sizeof(platform_version), platform_version, nullptr);
123-
clGetPlatformInfo(mInternals->platform, CL_PLATFORM_NAME, sizeof(platform_name), platform_name, nullptr);
124-
clGetPlatformInfo(mInternals->platform, CL_PLATFORM_VENDOR, sizeof(platform_vendor), platform_vendor, nullptr);
159+
queryPlatforms(mInternals->platform);
125160
GPUInfo("Selected Platform %d: (%s %s) %s %s", mProcessingSettings.platformNum, platform_profile, platform_version, platform_vendor, platform_name);
126161
}
127162
} else {
128163
for (uint32_t i_platform = 0; i_platform < num_platforms; i_platform++) {
129-
char platform_profile[256] = {}, platform_version[256] = {}, platform_name[256] = {}, platform_vendor[256] = {};
130-
clGetPlatformInfo(mInternals->platforms[i_platform], CL_PLATFORM_PROFILE, sizeof(platform_profile), platform_profile, nullptr);
131-
clGetPlatformInfo(mInternals->platforms[i_platform], CL_PLATFORM_VERSION, sizeof(platform_version), platform_version, nullptr);
132-
clGetPlatformInfo(mInternals->platforms[i_platform], CL_PLATFORM_NAME, sizeof(platform_name), platform_name, nullptr);
133-
clGetPlatformInfo(mInternals->platforms[i_platform], CL_PLATFORM_VENDOR, sizeof(platform_vendor), platform_vendor, nullptr);
164+
queryPlatforms(mInternals->platforms[i_platform]);
134165
const char* platformUsageInfo = "";
135-
if (!found && CheckPlatform(i_platform)) {
166+
if (!found && checkPlatform(mInternals->platforms[i_platform])) {
136167
found = true;
137168
mInternals->platform = mInternals->platforms[i_platform];
138169
if (mProcessingSettings.debugLevel >= 2) {
@@ -149,14 +180,14 @@ int32_t GPUReconstructionOCLBackend::InitDevice_Runtime()
149180
quit("Did not find compatible OpenCL Platform");
150181
}
151182

152-
cl_uint count, bestDevice = (cl_uint)-1;
153-
if (GPUFailedMsgI(clGetDeviceIDs(mInternals->platform, CL_DEVICE_TYPE_ALL, 0, nullptr, &count))) {
183+
cl_uint deviceCount, bestDevice = (cl_uint)-1;
184+
if (GPUFailedMsgI(clGetDeviceIDs(mInternals->platform, CL_DEVICE_TYPE_ALL, 0, nullptr, &deviceCount))) {
154185
quit("Error getting OPENCL Device Count");
155186
}
156187

157188
// Query devices
158-
mInternals->devices.reset(new cl_device_id[count]);
159-
if (GPUFailedMsgI(clGetDeviceIDs(mInternals->platform, CL_DEVICE_TYPE_ALL, count, mInternals->devices.get(), nullptr))) {
189+
mInternals->devices.reset(new cl_device_id[deviceCount]);
190+
if (GPUFailedMsgI(clGetDeviceIDs(mInternals->platform, CL_DEVICE_TYPE_ALL, deviceCount, mInternals->devices.get(), nullptr))) {
160191
quit("Error getting OpenCL devices");
161192
}
162193

@@ -167,8 +198,8 @@ int32_t GPUReconstructionOCLBackend::InitDevice_Runtime()
167198
if (mProcessingSettings.debugLevel >= 2) {
168199
GPUInfo("Available OPENCL devices:");
169200
}
170-
std::vector<bool> devicesOK(count, false);
171-
for (uint32_t i = 0; i < count; i++) {
201+
std::vector<bool> devicesOK(deviceCount, false);
202+
for (uint32_t i = 0; i < deviceCount; i++) {
172203
if (mProcessingSettings.debugLevel >= 3) {
173204
GPUInfo("Examining device %d", i);
174205
}
@@ -215,11 +246,11 @@ int32_t GPUReconstructionOCLBackend::InitDevice_Runtime()
215246
}
216247
}
217248
if (bestDevice == (cl_uint)-1) {
218-
quit("No %sOPENCL Device available, aborting OPENCL Initialisation", count ? "appropriate " : "");
249+
quit("No %sOPENCL Device available, aborting OPENCL Initialisation", deviceCount ? "appropriate " : "");
219250
}
220251

221252
if (mProcessingSettings.deviceNum > -1) {
222-
if (mProcessingSettings.deviceNum >= (signed)count) {
253+
if (mProcessingSettings.deviceNum >= (signed)deviceCount) {
223254
quit("Requested device ID %d does not exist", mProcessingSettings.deviceNum);
224255
} else if (!devicesOK[mProcessingSettings.deviceNum]) {
225256
quit("Unsupported device requested (%d)", mProcessingSettings.deviceNum);
@@ -269,7 +300,7 @@ int32_t GPUReconstructionOCLBackend::InitDevice_Runtime()
269300
mWarpSize = 32;
270301
mMaxBackendThreads = std::max<int32_t>(mMaxBackendThreads, maxWorkGroup * mBlockCount);
271302

272-
mInternals->context = clCreateContext(nullptr, ContextForAllPlatforms() ? count : 1, ContextForAllPlatforms() ? mInternals->devices.get() : &mInternals->device, nullptr, nullptr, &ocl_error);
303+
mInternals->context = clCreateContext(nullptr, ContextForAllPlatforms() ? deviceCount : 1, ContextForAllPlatforms() ? mInternals->devices.get() : &mInternals->device, nullptr, nullptr, &ocl_error);
273304
if (GPUFailedMsgI(ocl_error)) {
274305
quit("Could not create OPENCL Device Context!");
275306
}
@@ -608,33 +639,3 @@ int32_t GPUReconstructionOCLBackend::GetOCLPrograms()
608639

609640
return 0;
610641
}
611-
612-
bool GPUReconstructionOCLBackend::CheckPlatform(uint32_t i)
613-
{
614-
char platform_version[64] = {}, platform_vendor[64] = {};
615-
clGetPlatformInfo(mInternals->platforms[i], CL_PLATFORM_VERSION, sizeof(platform_version), platform_version, nullptr);
616-
clGetPlatformInfo(mInternals->platforms[i], CL_PLATFORM_VENDOR, sizeof(platform_vendor), platform_vendor, nullptr);
617-
float ver1 = 0;
618-
sscanf(platform_version, "OpenCL %f", &ver1);
619-
if (ver1 >= 2.2f) {
620-
if (mProcessingSettings.debugLevel >= 2) {
621-
GPUInfo("OpenCL 2.2 capable platform found");
622-
}
623-
return true;
624-
}
625-
626-
if (strcmp(platform_vendor, "Advanced Micro Devices, Inc.") == 0 && ver1 >= 2.0f) {
627-
float ver2 = 0;
628-
const char* pos = strchr(platform_version, '(');
629-
if (pos) {
630-
sscanf(pos, "(%f)", &ver2);
631-
}
632-
if ((ver1 >= 2.f && ver2 >= 2000.f) || ver1 >= 2.1f) {
633-
if (mProcessingSettings.debugLevel >= 2) {
634-
GPUInfo("AMD ROCm OpenCL Platform found");
635-
}
636-
return true;
637-
}
638-
}
639-
return false;
640-
}

GPU/GPUTracking/Base/opencl/GPUReconstructionOCL.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ class GPUReconstructionOCLBackend : public GPUReconstructionDeviceBase
7474
S& getKernelObject();
7575

7676
int32_t GetOCLPrograms();
77-
bool CheckPlatform(uint32_t i);
7877
};
7978

8079
using GPUReconstructionOCL = GPUReconstructionKernels<GPUReconstructionOCLBackend>;

0 commit comments

Comments
 (0)