• opencl simple examples


    1:context

     1 {//根据设备创建上下文
     2     cl_platform pform;
     3     size_t num;
     4     cl_device_id* devices;
     5     cl_context context;
     6     size_t size;
     7 
     8     clGetDeviceIDs(    platform,CL_DEVICE_TYPE_GPU,0,NULL,&num);
     9     if (num > 0)
    10     {
    11         devices = (cl_device_id*)alloca(num);
    12         clGetDeviceIDs(platform,CL_DEVICE_TYPE_GPU,num.&devices[0],NULL);
    13     }
    14 
    15     cl_context_properties properties[] =
    16     {
    17         CL_CONTEXT_PLATFORM,(cl_context_properties)platform,0
    18     };
    19 
    20     context = clCreateContext(
    21         properties,size/sizeof(cl_device_id),devices,NULL,NULL,NULL
    22         );
    23 }
    24 
    25 {
    26     //查询上下文
    27     cl_uint numPlatforms;
    28     cl_platform_id *platformIDs;
    29     cl_context context = NULL;
    30     size_t size;
    31 
    32     clGetPlatformIDs(0,NULL,&numPlatforms);
    33     platFormIDs = (cl_platform_id*)alloca(sizeof(cl_platform_id)*numPlatforms);
    34 
    35     clGetPlatformIDs(numPlatforms,platformIDs,NULL);
    36 
    37     cl_context_properties properties[] =
    38     {
    39         CL_CONTEXT_PLATFORM,(cl_context_properties)platformIDs[0],0
    40     };
    41 
    42     context = clCreateContextFromType(
    43         properties,CL_DEVICE_TYPE_ALL,NULL,NULL,NULL);
    44 
    45     clGetContextInfo(context,CL_CONTEXT_DEVICES,0,NULL,&size);
    46     cl_device_id* devices = (cl_device_id*)alloca(sizeof(cl_device_id)*size);
    47     clGetContextInfo(context,CL_CONTEXT_DEVICES,size,devices,NULL);
    48 
    49     for (size_t i = 0;i < size/sizeof(cl_device_id);i++)
    50     {
    51         cl_device_type type;
    52 
    53         clGetDeviceInfo(device[i],CL_DEVICE_TYPE,sizeof(cl_device_type),&type,NULL);
    54         switch (type)
    55         {
    56         
    57         }
    58     }
    59 
    60 }

    2:converlution

     1 #include <iostream>
     2 #include <fstream>
     3 #include <sstream>
     4 #include <string>
     5 
     6 #ifdef __APPLE__
     7 #include <OpenCL/cl.h>
     8 #else
     9 #include <CL/cl.h>
    10 #endif
    11 
    12 
    13 int main(int argc,char **argv)
    14 {
    15     cl_int errNum;
    16     cl_uint numPlatforms;
    17     cl_uint numDevices;
    18     cl_platform_id* platformIDs;
    19     cl_context context = NULL;
    20     cl_command_queue queue;
    21     cl_program program;
    22     cl_kernel kernel;
    23     cl_mem inputSignalBuffer;
    24     cl_mem outSignalBuffer;
    25     cl_mem maskBuffer;
    26 
    27     errNum = clGetPlatformIDs(0,NULL,&numPlatforms);
    28     if (errNum != CL_SUCCESS)
    29     {
    30         std::cerr << "failed
    ";
    31         return ;
    32     }
    33 
    34     platformIDs = (cl_platform_id*)alloca(sizeof(cl_platform_id)*numPlatforms);
    35     errNum = clGetPlatformIDs(numPlatforms,platformIDs,NULL);
    36     if (errNum != CL_SUCCESS)
    37     {
    38         std::cerr << "failed
    ";
    39         return ;
    40     }
    41 
    42     deviceIDs = NULL;
    43     cl_uint i = 0;
    44     for (;i < numPlatforms;i++)
    45     {
    46         errNum = clGetDeviceIDs(platformIDs[i],CL_DEVICE_TYPE_CPU,0,NULL,&numDevice);
    47         if (errNum != CL_SUCCESS)
    48         {
    49             std::cerr << "failed
    ";
    50             return ;
    51         }
    52 
    53         deviceIDs = (cl_device_id*)alloca(sizeof(cl_device_id)*numDevices);
    54         errNum = clGetDeviceIDs(platformIDs[i],CL_DEVICE_TYPE_CPU,numDevices,&deviceIDs[0],NULL);
    55         if (errNum != CL_SUCCESS)
    56         {
    57             std::cerr << "failed
    ";
    58             return ;
    59         }
    60 
    61     }
    62     if (deviceIDs == NULL)
    63     {
    64         std::cerr << "device faile
    ";
    65         return;
    66     }
    67 
    68     cl_context_properties contextProperties[] = 
    69     {
    70         CL_CONTEXT_PLATFORM,(cl_context_properties)platformIDs[0],0
    71     };
    72 
    73     context = clCreateContext(contextProperties,numDevices,deviceIDs,&contextCallback,NULL,&errNum);
    74 
    75     std::ifstream srcFile("SRCFILE");
    76 
    77     std::string srcProg(std::istreambuf_iterator<char>(srcFile),(std::istreambuf_iterator<char>()));
    78 
    79     const char* src = srcProg.c_str();
    80     size_t length = srcProg.length();
    81 
    82     program = clCreateProgramWithSource(context,1,&src,&length,&errNum);
    83     errNum = clBuildProgram(program,numDevices,deviceIDs,NULL,NULL,NULL);
    84 
    85     kernel = clCreateKernel(program,"convolve",&errNum);
    86 
    87     inputSignalBuffer = clCreateBuffer();
    88 
    89     queue = clCreateCommandQueue();
    90     clSetKernelArg();
    91     clEnqueueNDRangeKernel();
    92     clEnqueueReadBuffer();
    93 
    94 
    95 }

    3:device

      1 {//查询选择一个设备
      2     cl_int errNum;
      3     cl_uint numDevices;
      4     cl_device_id deviceIds[1];
      5 
      6     errNum = clGetDeviceIDs(platform,CL_DEVICE_GPU,0,NULL,&numDevices);
      7     if (numDevice < 1 || errNum != CL_SUCCESS)
      8     {
      9         std::cout << "No GPU device for platform
    ";
     10         exit(1);
     11     }
     12 
     13     errNum = clGetDeviceIDs(platform,CL_DEVICE_TYPE_GPU,1,&deviceIds[0],NULL);
     14 }
     15 
     16 {
     17     //查询设备信息
     18     cl_int err;
     19     size_t size;
     20 
     21     err = clGetDeviceInfo(deviceID,CL_DEVICE_MAX_COUNTE_UINTS,sizeof(cl_uint),&maxComputeUnits,&size);
     22     std::cout << 
     23 }
     24 
     25 {
     26 //查询显示平台特定信息
     27 
     28 template<typename T>
     29 void appendBitfield(T info,T value,std::string name,std::string& str)
     30 {
     31 
     32     if (info & value)
     33     {
     34         if (str.length() > 0)
     35         {
     36             str.append("|");
     37         }
     38         str.append(name);
     39     }
     40 }
     41 
     42 template <typename T>
     43 class InfoDevice
     44 {
     45 public:
     46     static void display(
     47         cl_device_id id,cl_device_info name,std::string str)
     48     {
     49         cl_int errNum;
     50         std::size_t paramValueSize;
     51 
     52         errNum = clGetDeviceInfo(id,name,0,NULL,&paramValueSize);
     53         if (errNum != CL_SUCCESS)
     54         {
     55             std::cerr << "failed
    ";
     56             return ;
     57         }
     58 
     59         T* info = (T*)alloca(sizeof(T)*paramValueSize);
     60         errNum = clGetDeviceInfo(id,name,paramValueSize,info,NULL);
     61         if (errNum != CL_SUCCESS)
     62         {
     63             std::cerr << "failed
    ";
     64             return ;
     65         }
     66 
     67         switch(name)
     68         {
     69         case CL_DEVICE_TYPE:
     70             {
     71                 std::string deviceType;
     72                 appendBitfield<cl_device_type>(
     73                     *(reinterpret_cast<cl_device_type*>(info)),
     74                     CL_DEVICE_TYPE_CPU,"CL_DEVICE_TYPE_CPU",deviceType);
     75 
     76                 appendBitfield<cl_device_type>(
     77                     *(reinterpret_cast<cl_device_type*>(info)),
     78                     CL_DEVICE_TYPE_GPU,"CL_DEVICE_TYPE_CPU",deviceType);
     79 
     80                 appendBitfield<cl_device_type>(
     81                     *(reinterpret_cast<cl_device_type*>(info)),
     82                     CL_DEVICE_TYPE_ACCELETOR,"CL_DEVICE_TYPE_ACCELERATOR",deviceType);
     83 
     84                 appendBitfield<cl_device_type>(
     85                     *(reinterpret_cast<cl_device_type*>(info)),
     86                     CL_DEVICE_TYPE_DEFAULT,"CL_DEVICE_TYPE_DEFAULT",deviceType);
     87             
     88                 std::cout << "		" << str << ";	" << deviceType << std::endl;
     89             }
     90             break;
     91 
     92         case CL_DEVICE_SINGLE_FP_CONFIG:
     93             {
     94                 std::string fpType;
     95 
     96                 appendBitfield<cl_device_fp_config>(
     97                     *(reinterpret_cast<cl_device_fp_config*>(info)),
     98                     CL_FP_DENORM,"CL_FP_DENORM",fpType);
     99 
    100                 appendBitfield<cl_device_fp_config>(
    101                     *(reinterpret_cast<cl_device_fp_config*>(info)),
    102                     CL_FP_INF_NAN,"CL_FP_INF_NAN",fpType);
    103 
    104 
    105             }
    106             break;
    107 
    108         }
    109 
    110     }
    111 }
    112 }

    4:platform

    void DisplayPlatformInfo(cl_platform_id id,cl_platform_info name,std::string str);
    
    void displayInfo()
    {
        cl_int errNum;
        cl_uint numPlatforms;
        cl_platform_id * platformIds;
        cl_context context;
    
        errNum = clGetPlatformIDs(0,NULL,&numPlatforms);
        if (errNum != CL_SUCCESS || numPlatform <= 0)
        {
            std::cerr << "failed to find any platform
    ";
            return;
        }
        
        platformIds = (cl_platform_id *)alloca(sizeof(cl_platform_id)*numPlatforms);
        
        errNum = clGetPlatformIDs(numPlatforms,platformIds,NULL); 
        if (errNum != CL_SUCCESS)
        {
            std::cerr << "failed to find any platform
    ";
            return;
        }
    
        std::cout << "number of platform:	"
            << numPlatforms << endl;
    
        for(cl_uint i = 0;i < numPlatforms;i++)
        {
            DisplayPlatformInfo(platformIds[i],CL_PLATFORM_PROFILE,"CL_PLATFORM_PROFILE");
            DisplayPlatformInfo(platformIds[i],CL_PLATFORM_VERSION,"CL_PLATFORM_VERSION");
            DisplayPlatformInfo(platformIds[i],CL_PLATFORM_VENDER,"CL_PLATFORM_VENDOR");
            DisplayPlatformInfo(platformIds[i],CL_PLATFORM_EXTENSION,"CL_PLATFORM_EXTENSION");
    
        }
    }
    
    void DisplayPlatformInfo(cl_platform_id id,cl_platform_info name,std::string str)
    {
        /*
            1:获取待查信息大小
            2:为信息分配内存,存储信息
        */
        cl_int errNum;
        std::size_t paramValueSize;
    
        errNum = clGetPlatformInfo(
            id,name,0,NULL,&paramValueSize);
        if (errNum != CL_SUCCESS)
        {
            std::cerr << "failed to find any platform
    ";
            return;
        }
    
        char* info = (char*)alloca(sizeof(char)*paramValueSize);
        errNum = clGetPlatformInfo(
            id,name,&paramValueSize,info,NULL);
        if (errNum != CL_SUCCESS)
        {
            std::cerr << "failed to find any platform
    ";
            return;
        }
    
        std::cout << "	" << str << "	" << info << std::endl;
    }
  • 相关阅读:
    go语言从例子开始之Example22.协程之通道
    go语言从例子开始之Example21.协程
    go语言从例子开始之Example20.错误处理
    go语言从例子开始之Example19.接口
    级联复制改成主从复制
    一主二从改成级联复制架构步骤
    mysql8.0 备分常用命令
    mysql8基于gtid导出导入搭建主从
    MySQL 8.0 配置mysql_native_password身份验证插件的密码
    mysql_config_editor 安全登录方式
  • 原文地址:https://www.cnblogs.com/pengtangtang/p/13099809.html
Copyright © 2020-2023  润新知