Mercurial > hg > Game > Cerium
view TaskManager/test/GpuRegistTaskTest/GpuRegistTaskTest.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 | 5b79077e4e1f |
children | 5ca4e9469c65 |
line wrap: on
line source
#include <stdio.h> #include <OpenCL/opencl.h> #include <fcntl.h> #include <sys/stat.h> #include "GpuScheduler.h" #include "CellTaskManagerImpl.h" #include "GpuThreads.h" #define DEFAULT 5 extern void gpu_register_task(int, const char*, const char*); 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"); } void tester(int *indata,int *outdata, int num){ //チェック int check = 0; for (int c=0; c<num; c++) { if(outdata[c] == indata[c]*2) { check++; } } printf("Computed '%d/%d' correct values\n",check,num); } void test(int task_array_num){ GpuThreads* gputhreads = GpuThreads::getInstance(); gputhreads->init(); int cmd = 1; GpuSchedRegister(cmd, "twice.cl", "twice"); int *indata = new int[task_array_num]; int *outdata = new int[task_array_num]; int count; for (count=0; count < task_array_num ;count++) { indata[count] = count; } print_data(indata, count, "before"); cl_int ret = gputhreads->ret; cl_context context = gputhreads->context; cl_mem memobj_in = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(int)*count, NULL, &ret); cl_mem memobj_out = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(int)*count, NULL, &ret); cl_mem data_count = clCreateBuffer(context, CL_MEM_READ_WRITE, sizeof(int)*count, NULL, &ret); cl_command_queue command_queue = gputhreads->command_queue; ret = clEnqueueWriteBuffer(command_queue, memobj_in, CL_TRUE, 0, sizeof(int)*count, indata, 0, NULL, NULL); ret = clEnqueueWriteBuffer(command_queue, data_count, CL_TRUE, 0, sizeof(count), &count, 0, NULL, NULL); cl_kernel kernel = *(task_list[cmd].kernel); 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; clEnqueueTask(command_queue, kernel, 0, NULL, &ev); clEnqueueReadBuffer(command_queue, memobj_out, CL_TRUE, 0, sizeof(int)*count, outdata, 1, &ev, NULL); print_data(outdata, count, "after"); tester(indata,outdata,count); delete [] indata; delete [] outdata; clReleaseCommandQueue(command_queue); clReleaseContext(context); clReleaseKernel(kernel); } int main(int argc, char* argv[]) { int task_array_num = DEFAULT; if (argc > 1) { //引数が渡されていて、 if(atoi(argv[1])) {//数字なら task_array_num = atoi(argv[1]); } } test(task_array_num); return 0; }