install opencl and clinfo may find platform & device
#include <iostream>
#include <string>
#include <CL/opencl.h>
std::string getPlatformName(const cl_platform_id pid){
size_t param_value_size;
clGetPlatformInfo(pid, CL_PLATFORM_NAME, 0, NULL, ¶m_value_size);
char *param_value = new char[param_value_size];
clGetPlatformInfo(pid, CL_PLATFORM_NAME, param_value_size, param_value, NULL);
return param_value;
}
int main(int argc, char **argv) {
std::cout << "Hello, world!" << std::endl;
cl_uint num_platforms;
clGetPlatformIDs(0, NULL, &num_platforms);
cl_platform_id *platforms = new cl_platform_id[num_platforms];
clGetPlatformIDs(num_platforms, platforms, NULL);
for (cl_uint i = 0; i < num_platforms; i++){
std::string platname = getPlatformName(platforms[i]);
std::cout << "<" << i << "> " << "Platform name is :" << platname << std::endl;
}
cl_platform_id platform_id = NULL;
cl_device_id device_id = NULL;
cl_uint num_platform = 0;
cl_uint num_device = 0;
clGetPlatformIDs(1, &platform_id, &num_platform);
std::cout << "stab num_platform=" << num_platform <<std::endl;
int index = 0;
while(true) {
platform_id = platforms[index];
if(clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_GPU, 1, &device_id, &num_device) != CL_SUCCESS) {
index++;
if(index >= num_platforms) break;
} else {
break;
}
}
if(index >= num_platforms) {
std::cout << "not found GPU" << std::endl;
} else {
std::cout << "found GPU index=" << index << std::endl;
}
clGetDeviceIDs(platforms[1], CL_DEVICE_TYPE_GPU, 1, &device_id, &num_device);
std::cout << "stab num_device=" << num_device <<std::endl;
std::string platnamenow = getPlatformName(platforms[1]);
std::cout << "Platform device name is :" << platnamenow << std::endl;
size_t valueSize;
clGetDeviceInfo(device_id, CL_DEVICE_NAME, 0, NULL, &valueSize);
char* value = (char*) malloc(valueSize);
clGetDeviceInfo(device_id, CL_DEVICE_NAME, valueSize, value, NULL);
std::cout << "device name is :" << std::string(value) << std::endl;
free(value);
cl_device_type cur_type;
clGetDeviceInfo(device_id, CL_DEVICE_TYPE, sizeof(cur_type), &cur_type, NULL);
std::cout << "device type is :" << cur_type << std::endl;
std::cout << "CL_DEVICE_TYPE_DEFAULT is :" << CL_DEVICE_TYPE_DEFAULT << std::endl;
std::cout << "CL_DEVICE_TYPE_CPU is :" << CL_DEVICE_TYPE_CPU << std::endl;
std::cout << "CL_DEVICE_TYPE_GPU is :" << CL_DEVICE_TYPE_GPU << std::endl;
std::cout << "CL_DEVICE_TYPE_ACCELERATOR is :" << CL_DEVICE_TYPE_ACCELERATOR << std::endl;
std::cout << "CL_DEVICE_TYPE_CUSTOM is :" << CL_DEVICE_TYPE_CUSTOM << std::endl;
std::cout << "CL_DEVICE_TYPE_ALL is :" << CL_DEVICE_TYPE_ALL << std::endl;
delete[] platforms;
return 0;
}
cmake
add_executable(opencl_iter opencl_iter.cpp)
target_link_libraries(opencl_iter /opt/intel/system_studio_2020/opencl-sdk/lib64/libOpenCL.so)