OpenCl入门getting-started-with-opencl-and-gpu-computing

原文来自于getting-started-with-opencl-and-gpu-computing/

对整个程序的注释:http://www.kimicat.com/opencl-1/opencl-jiao-xue-yi

但是对CUDA比较熟悉的用户来说,应该不需要看注释就能理解全部的程序

main.cpp

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <CL/cl.h>
 4 #define MAX_SOURCE_SIZE (0x100000)
 5 int main(void)
 6 {
 7         // Create the two input vectors
 8         int i;
 9         const int LIST_SIZE = 1000;
10         int *A = (int*) malloc(sizeof(int) * LIST_SIZE);
11         int *B = (int*) malloc(sizeof(int) * LIST_SIZE);
12         for (i = 0; i < LIST_SIZE; i++)
13         {
14                 A[i] = i;
15                 B[i] = LIST_SIZE - i;
16         }
17 
18         // Load the kernel source code into the array source_str
19         FILE *fp;
20         char *source_str;
21         size_t source_size;
22         fp = fopen("vector_add_kernel.cl", "r");
23 
24         if (!fp)
25         {
26                 fprintf(stderr, "Failed to load kernel.
");
27                 exit(1);
28         }
29         source_str = (char*) malloc(MAX_SOURCE_SIZE);
30         source_size = fread(source_str, 1, MAX_SOURCE_SIZE, fp);
31         fclose(fp);
32 
33         // Get platform and device information
34         cl_platform_id platform_id = NULL;
35         cl_device_id device_id = NULL;
36         cl_uint ret_num_devices;
37         cl_uint ret_num_platforms;
38         cl_int ret = clGetPlatformIDs(1, &platform_id, &ret_num_platforms);
39         ret = clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_DEFAULT, 1, &device_id,&ret_num_devices);
40 
41         // Create an OpenCL context
42         cl_context context = clCreateContext(NULL, 1, &device_id, NULL, NULL, &ret);
43         // Create a command queue
44         cl_command_queue command_queue = clCreateCommandQueue(context, device_id, 0, &ret);
45 
46         // Create memory buffers on the device for each vector
47         cl_mem a_mem_obj = clCreateBuffer(context, CL_MEM_READ_ONLY,LIST_SIZE * sizeof(int), NULL, &ret);
48         cl_mem b_mem_obj = clCreateBuffer(context, CL_MEM_READ_ONLY,LIST_SIZE * sizeof(int), NULL, &ret);
49         cl_mem c_mem_obj = clCreateBuffer(context, CL_MEM_WRITE_ONLY,LIST_SIZE * sizeof(int), NULL, &ret);
50 
51         // Copy the lists A and B to their respective memory buffers
52         ret = clEnqueueWriteBuffer(command_queue, a_mem_obj, CL_TRUE, 0,LIST_SIZE * sizeof(int), A, 0, NULL, NULL);
53         ret = clEnqueueWriteBuffer(command_queue, b_mem_obj, CL_TRUE, 0,LIST_SIZE * sizeof(int), B, 0, NULL, NULL);
54 
55         // Create a program from the kernel source
56         cl_program program = clCreateProgramWithSource(context, 1,(const char **) &source_str, (const size_t *) &source_size, &ret);
57 
58         // Build the program
59         ret = clBuildProgram(program, 1, &device_id, NULL, NULL, NULL);
60         // Create the OpenCL kernel
61         cl_kernel kernel = clCreateKernel(program, "vector_add", &ret);
62         // Set the arguments of the kernel
63         ret = clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *) &a_mem_obj);
64         ret = clSetKernelArg(kernel, 1, sizeof(cl_mem), (void *) &b_mem_obj);
65         ret = clSetKernelArg(kernel, 2, sizeof(cl_mem), (void *) &c_mem_obj);
66 
67         // Execute the OpenCL kernel on the list
68         size_t global_item_size = LIST_SIZE; // Process the entire lists
69         size_t local_item_size = 1; // Process one item at a time
70         ret = clEnqueueNDRangeKernel(command_queue, kernel, 1, NULL,&global_item_size, &local_item_size, 0, NULL, NULL);
71 
72         // Read the memory buffer C on the device to the local variable C
73         int *C = (int*) malloc(sizeof(int) * LIST_SIZE);
74         ret = clEnqueueReadBuffer(command_queue, c_mem_obj, CL_TRUE, 0,LIST_SIZE * sizeof(int), C, 0, NULL, NULL);
75 
76         // Display the result to the screen
77         for (i = 0; i < LIST_SIZE; i++)
78                 printf("%d + %d = %d
", A[i], B[i], C[i]);
79 
80         // Clean up
81         ret = clFlush(command_queue);
82         ret = clFinish(command_queue);
83         ret = clReleaseKernel(kernel);
84         ret = clReleaseProgram(program);
85         ret = clReleaseMemObject(a_mem_obj);
86         ret = clReleaseMemObject(b_mem_obj);
87         ret = clReleaseMemObject(c_mem_obj);
88         ret = clReleaseCommandQueue(command_queue);
89         ret = clReleaseContext(context);
90 
91         free(A);
92         free(B);
93         free(C);
94 
95         return 0;
96 
97 }

vector_add_kernel.cl

__kernel void vector_add(__global const int *A, __global const int *B, __global int *C)
{
    // Get the index of the current element to be processed
    int i = get_global_id(0);
    // Do the operation
    C[i] = A[i] + B[i];
}

之前已经安装好了CUDA的运行环境,这里作者说使用g++ -I/usr/local/cuda/include -L/usr/local/cuda/lib64 -lOpenCL main.cpp -o openclApp命令来执行,结果提示

'clGetPlatformIDs' undefined reference,但是我的include和lib都是正常的,因此,调整编译命令为:

g++  main.cpp -o openclApp -I/usr/local/cuda/include -L/usr/local/cuda/lib64 -lOpenCL

编译通过并运行通过,因此gcc编译选项的顺序也对程序有一定影响(理论上不应该有这个问题)。但是,这个问题使用clang编译就没有任何影响。

原文地址:https://www.cnblogs.com/jourluohua/p/10783864.html