changeset 1507:7abad41d12af draft

minor fix OpenCL example
author Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
date Mon, 10 Sep 2012 17:03:39 +0900
parents a7895ab4d0e3
children 0e1318e7caed
files example/OpenCL/twice.cc example/OpenCL/twice.cl
diffstat 2 files changed, 48 insertions(+), 52 deletions(-) [+]
line wrap: on
line diff
--- a/example/OpenCL/twice.cc	Mon Sep 10 15:04:39 2012 +0900
+++ b/example/OpenCL/twice.cc	Mon Sep 10 17:03:39 2012 +0900
@@ -20,7 +20,7 @@
 
     // 無効な引数ならデフォルトの値として432を設定
     int task_array_num = DEFAULT;
-    
+
     if (argc>1) {
         if (atoi(argv[1])) {
             task_array_num = atoi(argv[1]);
@@ -32,94 +32,90 @@
     cl_device_id device_id = NULL;
     cl_uint ret_num_devices = NULL;
     cl_int ret;
-    
+
     clGetPlatformIDs(1, &platform_id, &ret_num_platforms);
-    clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_DEFAULT, 1, &device_id, 
+    clGetDeviceIDs(platform_id, CL_DEVICE_TYPE_DEFAULT, 1, &device_id,
                    &ret_num_devices);
 
-    cl_context context = clCreateContext( NULL, 1, &device_id, NULL, NULL, &ret);    
+    cl_context context = clCreateContext( NULL, 1, &device_id, NULL, NULL, &ret);
     cl_command_queue command_queue = clCreateCommandQueue(context, device_id, 0, &ret);
 
     //ファイルオープン
-    int fp;
-    char *kernel_src_str;
-    size_t kernel_code_size;
+
     const char* filename = "twice.cl";
     const char* functionname = "twice";
-    
-    fp = open(filename, O_RDONLY);
+
+    int fp = open(filename, O_RDONLY);
 
     if (!fp) {
         fprintf(stderr, "Failed to load kernel.\n");
         exit(1);
     }
-    
+
     struct stat stats;
     fstat(fp,&stats);
     off_t size = stats.st_size;
-    
+
     if (!size) {
         fprintf(stderr, "Failed to load kernel.\n");
     }
-    
-    kernel_src_str = (char*)malloc(size);
 
-    kernel_code_size = read(fp, kernel_src_str, size);    
+    char *kernel_src_str = new char[size];
+    size_t kernel_code_size = read(fp, kernel_src_str, size);
     close(fp);
 
-
-    
-    cl_program program = NULL;
-    cl_kernel kernel = NULL;
-    program = clCreateProgramWithSource(context, 1, (const char **)&kernel_src_str,
-                                        (const size_t *)&kernel_code_size, &ret);
+    cl_program program = clCreateProgramWithSource(context, 1, (const char **)&kernel_src_str,
+                                                   (const size_t *)&kernel_code_size, &ret);
     clBuildProgram(program, 1, &device_id, NULL, NULL, NULL);
-    kernel = clCreateKernel(program,functionname, &ret);
-    
-    int *data,*output_data;
-    data = (int *)malloc(sizeof(int)*task_array_num);
-    output_data = (int *)malloc(sizeof(int)*task_array_num);
-    
+    cl_kernel kernel = clCreateKernel(program,functionname, &ret);
+
+    int *data = new int[task_array_num];
+    int *output_data = new int[task_array_num];
+
     int count = 0;
     for (int c = 0; c < task_array_num ; count++,c++){
         data[c] = c;
     }
-    
-    cl_mem memobj_in, memobj_out, data_count = NULL;
+
     //メモリバッファの作成
-    memobj_in = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(int)*count, NULL, &ret);
-    memobj_out = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(int)*count, NULL, &ret);
-    data_count = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(int)*count, NULL, &ret);
-    
+    cl_mem data_count = clCreateBuffer(context, CL_MEM_READ_ONLY, sizeof(int)*count, NULL, &ret);
+    cl_mem memobj_in = clCreateBuffer(context, CL_MEM_READ_ONLY, sizeof(int)*count, NULL, &ret);
+    cl_mem memobj_out = clCreateBuffer(context, CL_MEM_WRITE_ONLY, sizeof(int)*count, NULL, &ret);
+
+
     //メモリバッファに入力データを書き込み
+    ret = clEnqueueWriteBuffer(command_queue, data_count, CL_TRUE, 0,
+                               sizeof(count), &count, 0, NULL, NULL);
     ret = clEnqueueWriteBuffer(command_queue, memobj_in, CL_TRUE, 0,
                                sizeof(int)*count, data, 0, NULL, NULL);
-    ret = clEnqueueWriteBuffer(command_queue, data_count, CL_TRUE, 0,
-                               sizeof(count), &count, 0, NULL, NULL);
-    
+
     print_data(data, count, "before");
-    
-    clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *)&memobj_in);
-    clSetKernelArg(kernel, 1, sizeof(cl_mem), (void *)&memobj_out);
-    clSetKernelArg(kernel, 2, sizeof(cl_mem), (void *)&data_count);
-     
+
+    clSetKernelArg(kernel, 0, sizeof(cl_mem), (void *)&data_count);
+    clSetKernelArg(kernel, 1, sizeof(cl_mem), (void *)&memobj_in);
+    clSetKernelArg(kernel, 2, sizeof(cl_mem), (void *)&memobj_out);
+
+
     cl_event ev = NULL;
     ret = clEnqueueTask(command_queue, kernel, 0, NULL, &ev);
-    
+
     //メモリバッファから結果を取得
     ret = clEnqueueReadBuffer(command_queue, memobj_out, CL_TRUE, 0,
                               sizeof(int)*count, output_data, 1, &ev, NULL);
-        
+
     print_data(output_data, count, "after");
 
-    free(data);
-    free(output_data);
     clReleaseKernel(kernel);
     clReleaseProgram(program);
+    clReleaseMemObject(data_count);
+    clReleaseMemObject(memobj_in);
+    clReleaseMemObject(memobj_out);
     clReleaseCommandQueue(command_queue);
     clReleaseContext(context);
 
-    free(kernel_src_str);
+    delete [] kernel_src_str;
+    delete [] data;
+    delete [] output_data;
 
     return 0;
 }
--- a/example/OpenCL/twice.cl	Mon Sep 10 15:04:39 2012 +0900
+++ b/example/OpenCL/twice.cl	Mon Sep 10 17:03:39 2012 +0900
@@ -1,11 +1,11 @@
 __kernel void
-twice(__global int *input_data,
-      __global int *output_data,
-      __global int *data_count)
+twice(__global int *data_count,
+      __global int *input_data,
+      __global int *output_data)
 {
 
-    int count = *data_count;
-	for (int i = 0; i<count; i++) {
-	    output_data[i] = input_data[i] * 2;
-	}        
+    long count = (long)data_count[0];
+        for (int i = 0; i<count; i++) {
+            output_data[i] = input_data[i] * 2;
+        }
 }