ubuntu opencl example

152 篇文章 1 订阅
123 篇文章 1 订阅

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, &param_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)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hjwang1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值