Support multiple OpenCL platforms
Previously, the code only checked the first OpenCL platform and failed if thet platform didn't have any devices.
This commit is contained in:
parent
379fb4cadf
commit
c1c271b77e
1 changed files with 42 additions and 16 deletions
|
@ -694,34 +694,60 @@ OpenCLContext::OpenCLContext() : context_(nullptr), queue_(nullptr) {
|
||||||
//////////////////// set up cl_context
|
//////////////////// set up cl_context
|
||||||
cl_int err_num;
|
cl_int err_num;
|
||||||
cl_uint num_platforms;
|
cl_uint num_platforms;
|
||||||
cl_platform_id first_platform_id;
|
std::vector<cl_platform_id> platform_ids;
|
||||||
|
|
||||||
err_num = clGetPlatformIDs(1, &first_platform_id, &num_platforms);
|
err_num = clGetPlatformIDs(0, nullptr, &num_platforms);
|
||||||
if (err_num != CL_SUCCESS || num_platforms == 0) {
|
if (err_num != CL_SUCCESS) {
|
||||||
|
std::cout << "ERROR: OpenCLContext: Failed to query OpenCL platforms"
|
||||||
|
<< std::endl;
|
||||||
|
return;
|
||||||
|
} else if (num_platforms == 0) {
|
||||||
std::cout << "ERROR: OpenCLContext: Failed to find any OpenCL platforms"
|
std::cout << "ERROR: OpenCLContext: Failed to find any OpenCL platforms"
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
cl_context_properties context_properties[] = {
|
platform_ids.resize(num_platforms);
|
||||||
CL_CONTEXT_PLATFORM,
|
|
||||||
reinterpret_cast<cl_context_properties>(first_platform_id), 0};
|
|
||||||
|
|
||||||
context_ = clCreateContextFromType(context_properties, CL_DEVICE_TYPE_GPU,
|
|
||||||
nullptr, nullptr, &err_num);
|
|
||||||
|
|
||||||
|
err_num = clGetPlatformIDs(num_platforms, platform_ids.data(), nullptr);
|
||||||
if (err_num != CL_SUCCESS) {
|
if (err_num != CL_SUCCESS) {
|
||||||
std::cout << "ERROR: OpenCLContext: Failed to create GPU context, "
|
std::cout << "ERROR: OpenCLContext: Failed to get OpenCL platform id(s)"
|
||||||
<< "trying CPU..." << std::endl;
|
<< std::endl;
|
||||||
context_ = clCreateContextFromType(context_properties, CL_DEVICE_TYPE_CPU,
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool success = false;
|
||||||
|
for (auto i = 0; i < num_platforms; ++i) {
|
||||||
|
cl_context_properties context_properties[] = {
|
||||||
|
CL_CONTEXT_PLATFORM,
|
||||||
|
reinterpret_cast<cl_context_properties>(platform_ids[i]), 0};
|
||||||
|
|
||||||
|
context_ = clCreateContextFromType(context_properties, CL_DEVICE_TYPE_GPU,
|
||||||
nullptr, nullptr, &err_num);
|
nullptr, nullptr, &err_num);
|
||||||
|
|
||||||
if (err_num != CL_SUCCESS) {
|
if (err_num != CL_SUCCESS) {
|
||||||
std::cout << "ERROR: OpenCLContext: Failed to create CPU context"
|
std::cout << "ERROR: OpenCLContext: Failed to create GPU context, "
|
||||||
<< std::endl;
|
<< "trying CPU..." << std::endl;
|
||||||
context_ = nullptr;
|
context_ = clCreateContextFromType(context_properties, CL_DEVICE_TYPE_CPU,
|
||||||
return;
|
nullptr, nullptr, &err_num);
|
||||||
|
if (err_num != CL_SUCCESS) {
|
||||||
|
std::cout << "ERROR: OpenCLContext: Failed to create CPU context"
|
||||||
|
<< std::endl;
|
||||||
|
context_ = nullptr;
|
||||||
|
continue;
|
||||||
|
} else {
|
||||||
|
success = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
success = true;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (!success) {
|
||||||
|
std::cout << "ERROR: OpenCLContext: Failed to find a valid OpenCL context!"
|
||||||
|
<< std::endl;
|
||||||
|
}
|
||||||
//////////////////// end set up cl context
|
//////////////////// end set up cl context
|
||||||
|
|
||||||
//////////////////// set up command queue
|
//////////////////// set up command queue
|
||||||
|
|
Loading…
Reference in a new issue