changeset 1456:5533106cc437 draft

add OpenCL example
author Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
date Tue, 08 May 2012 18:43:50 +0900
parents 2b886dcc0e7d
children 55f92ed3f244
files example/OpenCL/twice example/OpenCL/twice.cc~ example/OpenCL/twice.o
diffstat 3 files changed, 0 insertions(+), 125 deletions(-) [+]
line wrap: on
line diff
Binary file example/OpenCL/twice has changed
--- a/example/OpenCL/twice.cc~	Tue May 08 18:40:10 2012 +0900
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,125 +0,0 @@
-#include <stdlib.h>
-#include <OpenCL/opencl.h>
-#include <stdio.h>
-#include <fcntl.h>
-#include <sys/stat.h>
-
-#define DEFAULT 432
-
-void
-print_data(int *data, int size, const char *title)
-{
-    printf("%s ---\n", title);
-    for ( int i = 0; i < size; i++) {
-        printf("%2d ", data[i]);
-    }
-    printf("\n");
-}
-
-int main(int argc, char *argv[]) {
-
-    // 無効な引数ならデフォルトの値として432を設定
-    int task_array_num = DEFAULT;
-    
-    if (argc>1) {
-        if (atoi(argv[1])) {
-            task_array_num = atoi(argv[1]);
-        }
-    }
-
-    cl_platform_id platform_id = NULL;
-    cl_uint ret_num_platforms = NULL;
-    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, 
-                   &ret_num_devices);
-
-    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);
-
-    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);    
-    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);
-    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);
-    
-    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);
-    
-    //メモリバッファに入力データを書き込み
-    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);
-     
-    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);
-    clReleaseCommandQueue(command_queue);
-    clReleaseContext(context);
-
-    free(kernel_src_str);
-
-    return 0;
-}
Binary file example/OpenCL/twice.o has changed