comparison example/OpenCL/twice.cc~ @ 1455:2b886dcc0e7d draft

add OpenCL example
author Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
date Tue, 08 May 2012 18:40:10 +0900
parents
children
comparison
equal deleted inserted replaced
1454:5b79077e4e1f 1455:2b886dcc0e7d
1 #include <stdlib.h>
2 #include <OpenCL/opencl.h>
3 #include <stdio.h>
4 #include <fcntl.h>
5 #include <sys/stat.h>
6
7 #define DEFAULT 432
8
9 void
10 print_data(int *data, int size, const char *title)
11 {
12 printf("%s ---\n", title);
13 for ( int i = 0; i < size; i++) {
14 printf("%2d ", data[i]);
15 }
16 printf("\n");
17 }
18
19 int main(int argc, char *argv[]) {
20
21 // 無効な引数ならデフォルトの値として432を設定
22 int task_array_num = DEFAULT;
23
24 if (argc>1) {
25 if (atoi(argv[1])) {
26 task_array_num = atoi(argv[1]);
27 }
28 }
29
30 cl_platform_id platform_id = NULL;
31 cl_uint ret_num_platforms = NULL;
32 cl_device_id device_id = NULL;
33 cl_uint ret_num_devices = NULL;
34 cl_int ret;
35
36 clGetPlatformIDs(1, &platform_id, &ret_num_platforms);
37 clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_DEFAULT, 1, &device_id,
38 &ret_num_devices);
39
40 cl_context context = clCreateContext( NULL, 1, &device_id, NULL, NULL, &ret);
41 cl_command_queue command_queue = clCreateCommandQueue(context, device_id, 0, &ret);
42
43 //ファイルオープン
44 int fp;
45 char *kernel_src_str;
46 size_t kernel_code_size;
47 const char* filename = "twice.cl";
48 const char* functionname = "twice";
49
50 fp = open(filename, O_RDONLY);
51
52 if (!fp) {
53 fprintf(stderr, "Failed to load kernel.\n");
54 exit(1);
55 }
56
57 struct stat stats;
58 fstat(fp,&stats);
59 off_t size = stats.st_size;
60
61 if (!size) {
62 fprintf(stderr, "Failed to load kernel.\n");
63 }
64
65 kernel_src_str = (char*)malloc(size);
66
67 kernel_code_size = read(fp, kernel_src_str, size);
68 close(fp);
69
70
71
72 cl_program program = NULL;
73 cl_kernel kernel = NULL;
74 program = clCreateProgramWithSource(context, 1, (const char **)&kernel_src_str,
75 (const size_t *)&kernel_code_size, &ret);
76 clBuildProgram(program, 1, &device_id, NULL, NULL, NULL);
77 kernel = clCreateKernel(program,functionname, &ret);
78
79 int *data,*output_data;
80 data = (int *)malloc(sizeof(int)*task_array_num);
81 output_data = (int *)malloc(sizeof(int)*task_array_num);
82
83 int count = 0;
84 for (int c = 0; c < task_array_num ; count++,c++){
85 data[c] = c;
86 }
87
88 cl_mem memobj_in, memobj_out, data_count = NULL;
89 //メモリバッファの作成
90 memobj_in = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(int)*count, NULL, &ret);
91 memobj_out = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(int)*count, NULL, &ret);
92 data_count = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(int)*count, NULL, &ret);
93
94 //メモリバッファに入力データを書き込み
95 ret = clEnqueueWriteBuffer(command_queue, memobj_in, CL_TRUE, 0,
96 sizeof(int)*count, data, 0, NULL, NULL);
97 ret = clEnqueueWriteBuffer(command_queue, data_count, CL_TRUE, 0,
98 sizeof(count), &count, 0, NULL, NULL);
99
100 print_data(data, count, "before");
101
102 clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *)&memobj_in);
103 clSetKernelArg(kernel, 1, sizeof(cl_mem), (void *)&memobj_out);
104 clSetKernelArg(kernel, 2, sizeof(cl_mem), (void *)&data_count);
105
106 cl_event ev = NULL;
107 ret = clEnqueueTask(command_queue, kernel, 0, NULL, &ev);
108
109 //メモリバッファから結果を取得
110 ret = clEnqueueReadBuffer(command_queue, memobj_out, CL_TRUE, 0,
111 sizeof(int)*count, output_data, 1, &ev, NULL);
112
113 print_data(output_data, count, "after");
114
115 free(data);
116 free(output_data);
117 clReleaseKernel(kernel);
118 clReleaseProgram(program);
119 clReleaseCommandQueue(command_queue);
120 clReleaseContext(context);
121
122 free(kernel_src_str);
123
124 return 0;
125 }