首页 > 编程语言 >opencl 程序变为字符串

opencl 程序变为字符串

时间:2023-02-10 11:34:18浏览次数:44  
标签:cl int opencl 变为 platform device 字符串 NULL id

。。。。

  1 #include <CL/cl.h>
  2 
  3 #include <string.h>
  4 #include <stdio.h>
  5 #include <stdlib.h>
  6 #include <stdio.h>
  7 
  8 // using namespace std;
  9 
 10 char* cl_kernel_source = "__kernel void helloworld(__global double* in, __global double* out)\n"
 11  "{\n"
 12   "   int num = get_global_id(0);\n"
 13   "  out[num] = in[num] / 2.4 *(in[num]/6) ;\n"
 14  "}\n";
 15 
 16 
 17  int getPlatform(cl_platform_id *platform)
 18 {
 19     platform = NULL;//the chosen platform
 20 
 21     cl_uint numPlatforms;//the NO. of platforms
 22     cl_int    status = clGetPlatformIDs(0, NULL, &numPlatforms);
 23     if (status != CL_SUCCESS)
 24     {
 25         return -1;
 26     }
 27 
 28     /**For clarity, choose the first available platform. */
 29     if(numPlatforms > 0)
 30     {
 31         cl_platform_id* platforms =
 32             (cl_platform_id* )malloc(numPlatforms* sizeof(cl_platform_id));
 33         status = clGetPlatformIDs(numPlatforms, platforms, NULL);
 34         platform = platforms[0];
 35         free(platforms);
 36     }
 37     else
 38         return -1;
 39 }
 40 
 41 /**Step 2:Query the platform and choose the first GPU device if has one.*/
 42 cl_device_id *getCl_device_id(cl_platform_id *platform)
 43 {
 44     cl_uint numDevices = 0;
 45     cl_device_id *devices=NULL;
 46     cl_int    status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 0, NULL, &numDevices);
 47     if (numDevices > 0) //GPU available.
 48     {
 49         devices = (cl_device_id*)malloc(numDevices * sizeof(cl_device_id));
 50         status = clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, numDevices, devices, NULL);
 51     }
 52     return devices;
 53 }
 54 
 55 int main(int argc, char* argv[])
 56 {
 57     printf("0000000000000000");
 58     cl_int    status;
 59     /**Step 1: Getting platforms and choose an available one(first).*/
 60     cl_platform_id platform;
 61     getPlatform(platform);
 62 
 63     printf("11111111111111111111");
 64     /**Step 2:Query the platform and choose the first GPU device if has one.*/
 65     cl_device_id *devices=getCl_device_id(platform);
 66 
 67     /**Step 3: Create context.*/
 68     cl_context context = clCreateContext(NULL,1, devices,NULL,NULL,NULL);
 69 
 70     /**Step 4: Creating command queue associate with the context.*/
 71     cl_command_queue commandQueue = clCreateCommandQueue(context, devices[0], 0, NULL);
 72 
 73     size_t sourceSize[] = {strlen(cl_kernel_source)};
 74     cl_program program = clCreateProgramWithSource(context, 1, &cl_kernel_source, sourceSize, NULL);
 75 
 76     /**Step 6: Build program. */
 77     status=clBuildProgram(program, 1,devices,NULL,NULL,NULL);
 78 
 79     /**Step 7: Initial input,output for the host and create memory objects for the kernel*/
 80     const int NUM=512000;
 81     double input[NUM];
 82     double output[NUM];
 83     
 84       printf("22222222222222");
 85     for(int i=0;i<NUM;i++)
 86         input[i]=i;
 87 
 88     cl_mem inputBuffer = clCreateBuffer(context, CL_MEM_READ_ONLY|CL_MEM_COPY_HOST_PTR, (NUM) * sizeof(double),(void *) input, NULL);
 89     cl_mem outputBuffer = clCreateBuffer(context, CL_MEM_WRITE_ONLY , NUM * sizeof(double), NULL, NULL);
 90 
 91     /**Step 8: Create kernel object */
 92     cl_kernel kernel = clCreateKernel(program,"helloworld", NULL);
 93 
 94     /**Step 9: Sets Kernel arguments.*/
 95     status = clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *)&inputBuffer);
 96     status = clSetKernelArg(kernel, 1, sizeof(cl_mem), (void *)&outputBuffer);
 97 
 98     /**Step 10: Running the kernel.*/
 99     size_t global_work_size[1] = {NUM};
100     cl_event enentPoint;
101     status = clEnqueueNDRangeKernel(commandQueue, kernel, 1, NULL, global_work_size, NULL, 0, NULL, &enentPoint);
102     clWaitForEvents(1,&enentPoint); ///wait
103     clReleaseEvent(enentPoint);
104 
105     /**Step 11: Read the cout put back to host memory.*/
106     status = clEnqueueReadBuffer(commandQueue, outputBuffer, CL_TRUE, 0, NUM * sizeof(double), output, 0, NULL, NULL);
107 
108     /**Step 12: Clean the resources.*/
109     status = clReleaseKernel(kernel);//*Release kernel.
110     status = clReleaseProgram(program);    //Release the program object.
111     status = clReleaseMemObject(inputBuffer);//Release mem object.
112     status = clReleaseMemObject(outputBuffer);
113     status = clReleaseCommandQueue(commandQueue);//Release  Command queue.
114     status = clReleaseContext(context);//Release context.
115   printf("333333333333");
116     // if (output != NULL)
117     // {
118     //     free(output);
119     //     output = NULL;
120     // }
121 
122     if (devices != NULL)
123     {
124         free(devices);
125         devices = NULL;
126     }
127       printf("44444444444");
128     return 0;
129 }

 

。。。。

标签:cl,int,opencl,变为,platform,device,字符串,NULL,id
From: https://www.cnblogs.com/peifx/p/17108347.html

相关文章