Created
March 12, 2021 05:15
-
-
Save gasgallo/6706e22662bf272f1ca983cb2e75024f to your computer and use it in GitHub Desktop.
Code to test openCL functionalities
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <dmlc/logging.h> | |
#include <vector> | |
#include <CL/opencl.h> | |
#define OPENCL_CHECK_ERROR(e) \ | |
{ CHECK(e == CL_SUCCESS) << "OpenCL Error, code=" << e << ": " << CLGetErrorString(e); } | |
#define OPENCL_CALL(func) \ | |
{ \ | |
cl_int e = (func); \ | |
OPENCL_CHECK_ERROR(e); \ | |
} | |
inline const char* CLGetErrorString(cl_int error) { | |
switch (error) { | |
case CL_SUCCESS: | |
return "CL_SUCCESS"; | |
case CL_DEVICE_NOT_FOUND: | |
return "CL_DEVICE_NOT_FOUND"; | |
case CL_DEVICE_NOT_AVAILABLE: | |
return "CL_DEVICE_NOT_AVAILABLE"; | |
case CL_COMPILER_NOT_AVAILABLE: | |
return "CL_COMPILER_NOT_AVAILABLE"; | |
case CL_MEM_OBJECT_ALLOCATION_FAILURE: | |
return "CL_MEM_OBJECT_ALLOCATION_FAILURE"; | |
case CL_OUT_OF_RESOURCES: | |
return "CL_OUT_OF_RESOURCES"; | |
case CL_OUT_OF_HOST_MEMORY: | |
return "CL_OUT_OF_HOST_MEMORY"; | |
case CL_PROFILING_INFO_NOT_AVAILABLE: | |
return "CL_PROFILING_INFO_NOT_AVAILABLE"; | |
case CL_MEM_COPY_OVERLAP: | |
return "CL_MEM_COPY_OVERLAP"; | |
case CL_IMAGE_FORMAT_MISMATCH: | |
return "CL_IMAGE_FORMAT_MISMATCH"; | |
case CL_IMAGE_FORMAT_NOT_SUPPORTED: | |
return "CL_IMAGE_FORMAT_NOT_SUPPORTED"; | |
case CL_BUILD_PROGRAM_FAILURE: | |
return "CL_BUILD_PROGRAM_FAILURE"; | |
case CL_MAP_FAILURE: | |
return "CL_MAP_FAILURE"; | |
case CL_INVALID_VALUE: | |
return "CL_INVALID_VALUE"; | |
case CL_INVALID_DEVICE_TYPE: | |
return "CL_INVALID_DEVICE_TYPE"; | |
case CL_INVALID_PLATFORM: | |
return "CL_INVALID_PLATFORM"; | |
case CL_INVALID_DEVICE: | |
return "CL_INVALID_DEVICE"; | |
case CL_INVALID_CONTEXT: | |
return "CL_INVALID_CONTEXT"; | |
case CL_INVALID_QUEUE_PROPERTIES: | |
return "CL_INVALID_QUEUE_PROPERTIES"; | |
case CL_INVALID_COMMAND_QUEUE: | |
return "CL_INVALID_COMMAND_QUEUE"; | |
case CL_INVALID_HOST_PTR: | |
return "CL_INVALID_HOST_PTR"; | |
case CL_INVALID_MEM_OBJECT: | |
return "CL_INVALID_MEM_OBJECT"; | |
case CL_INVALID_IMAGE_FORMAT_DESCRIPTOR: | |
return "CL_INVALID_IMAGE_FORMAT_DESCRIPTOR"; | |
case CL_INVALID_IMAGE_SIZE: | |
return "CL_INVALID_IMAGE_SIZE"; | |
case CL_INVALID_SAMPLER: | |
return "CL_INVALID_SAMPLER"; | |
case CL_INVALID_BINARY: | |
return "CL_INVALID_BINARY"; | |
case CL_INVALID_BUILD_OPTIONS: | |
return "CL_INVALID_BUILD_OPTIONS"; | |
case CL_INVALID_PROGRAM: | |
return "CL_INVALID_PROGRAM"; | |
case CL_INVALID_PROGRAM_EXECUTABLE: | |
return "CL_INVALID_PROGRAM_EXECUTABLE"; | |
case CL_INVALID_KERNEL_NAME: | |
return "CL_INVALID_KERNEL_NAME"; | |
case CL_INVALID_KERNEL_DEFINITION: | |
return "CL_INVALID_KERNEL_DEFINITION"; | |
case CL_INVALID_KERNEL: | |
return "CL_INVALID_KERNEL"; | |
case CL_INVALID_ARG_INDEX: | |
return "CL_INVALID_ARG_INDEX"; | |
case CL_INVALID_ARG_VALUE: | |
return "CL_INVALID_ARG_VALUE"; | |
case CL_INVALID_ARG_SIZE: | |
return "CL_INVALID_ARG_SIZE"; | |
case CL_INVALID_KERNEL_ARGS: | |
return "CL_INVALID_KERNEL_ARGS"; | |
case CL_INVALID_WORK_DIMENSION: | |
return "CL_INVALID_WORK_DIMENSION"; | |
case CL_INVALID_WORK_GROUP_SIZE: | |
return "CL_INVALID_WORK_GROUP_SIZE"; | |
case CL_INVALID_WORK_ITEM_SIZE: | |
return "CL_INVALID_WORK_ITEM_SIZE"; | |
case CL_INVALID_GLOBAL_OFFSET: | |
return "CL_INVALID_GLOBAL_OFFSET"; | |
case CL_INVALID_EVENT_WAIT_LIST: | |
return "CL_INVALID_EVENT_WAIT_LIST"; | |
case CL_INVALID_EVENT: | |
return "CL_INVALID_EVENT"; | |
case CL_INVALID_OPERATION: | |
return "CL_INVALID_OPERATION"; | |
case CL_INVALID_GL_OBJECT: | |
return "CL_INVALID_GL_OBJECT"; | |
case CL_INVALID_BUFFER_SIZE: | |
return "CL_INVALID_BUFFER_SIZE"; | |
case CL_INVALID_MIP_LEVEL: | |
return "CL_INVALID_MIP_LEVEL"; | |
default: | |
return "Unknown OpenCL error code"; | |
} | |
} | |
std::string GetPlatformInfo(cl_platform_id pid, cl_platform_info param_name) { | |
size_t ret_size; | |
OPENCL_CALL(clGetPlatformInfo(pid, param_name, 0, nullptr, &ret_size)); | |
std::string ret; | |
ret.resize(ret_size); | |
OPENCL_CALL(clGetPlatformInfo(pid, param_name, ret_size, &ret[0], nullptr)); | |
LOG(INFO) << "GetPlatformInfo: SUCCESS"; | |
return ret; | |
} | |
std::string GetDeviceInfo(cl_device_id pid, cl_device_info param_name) { | |
size_t ret_size; | |
OPENCL_CALL(clGetDeviceInfo(pid, param_name, 0, nullptr, &ret_size)); | |
std::string ret; | |
ret.resize(ret_size); | |
OPENCL_CALL(clGetDeviceInfo(pid, param_name, ret_size, &ret[0], nullptr)); | |
LOG(INFO) << "GetDeviceInfo: SUCCESS"; | |
return ret; | |
} | |
std::vector<cl_platform_id> GetPlatformIDs() { | |
cl_uint ret_size; | |
cl_int code = clGetPlatformIDs(0, nullptr, &ret_size); | |
std::vector<cl_platform_id> ret; | |
if (code != CL_SUCCESS) return ret; | |
ret.resize(ret_size); | |
OPENCL_CALL(clGetPlatformIDs(ret_size, &ret[0], nullptr)); | |
LOG(INFO) << "GetPlatformIDs: SUCCESS"; | |
return ret; | |
} | |
std::vector<cl_device_id> GetDeviceIDs(cl_platform_id pid, std::string device_type) { | |
cl_device_type dtype = CL_DEVICE_TYPE_ALL; | |
if (device_type == "cpu") dtype = CL_DEVICE_TYPE_CPU; | |
if (device_type == "gpu") dtype = CL_DEVICE_TYPE_GPU; | |
if (device_type == "accelerator") dtype = CL_DEVICE_TYPE_ACCELERATOR; | |
cl_uint ret_size; | |
cl_int code = clGetDeviceIDs(pid, dtype, 0, nullptr, &ret_size); | |
std::vector<cl_device_id> ret; | |
if (code != CL_SUCCESS) return ret; | |
ret.resize(ret_size); | |
OPENCL_CALL(clGetDeviceIDs(pid, dtype, ret_size, &ret[0], nullptr)); | |
LOG(INFO) << "GetDeviceIDs: SUCCESS"; | |
return ret; | |
} | |
bool MatchPlatformInfo(cl_platform_id pid, cl_platform_info param_name, std::string value) { | |
if (value.length() == 0) return true; | |
std::string param_value = GetPlatformInfo(pid, param_name); | |
return param_value.find(value) != std::string::npos; | |
} | |
int main(int argc, char* argv[]) { | |
std::string device_type = "gpu"; | |
std::string platform_name = ""; | |
cl_platform_id id; | |
std::string name; | |
std::string type; | |
std::vector<cl_device_id> devices; | |
// matched platforms | |
std::vector<cl_platform_id> platform_ids = GetPlatformIDs(); | |
if (platform_ids.size() == 0) { | |
LOG(WARNING) << "No OpenCL platform matched given existing options ..."; | |
return 1; | |
} | |
for (auto platform_id : platform_ids) { | |
LOG(INFO) << "Found platform: " << platform_id; | |
if (!MatchPlatformInfo(platform_id, CL_PLATFORM_NAME, platform_name)) { | |
LOG(INFO) << "Not matched platform: " << platform_id; | |
continue; | |
} | |
std::vector<cl_device_id> devices_matched = GetDeviceIDs(platform_id, device_type); | |
for (int i = 0; i < devices_matched.size(); i++) | |
LOG(INFO) << "Matched device" << i << ": " << devices_matched[i]; | |
if ((devices_matched.size() == 0) && (device_type == "gpu")) { | |
LOG(WARNING) << "Using CPU OpenCL device"; | |
devices_matched = GetDeviceIDs(platform_id, "cpu"); | |
} | |
if (devices_matched.size() > 0) { | |
id = platform_id; | |
name = GetPlatformInfo(platform_id, CL_PLATFORM_NAME); | |
type = device_type; | |
devices = devices_matched; | |
LOG(INFO) << "Matched platform: platform_id=" << id << ", platform_name=" << name << ", device_type=" << type; | |
break; | |
} | |
} | |
if (id == nullptr) { | |
LOG(WARNING) << "No OpenCL device"; | |
return 1; | |
} | |
LOG(INFO) << "SUCCESS"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment