view TaskManager/test/GpuRegistTaskTest/GpuRegistTaskTest.cc.orig @ 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
line wrap: on
line source

#include <stdio.h>
#include <OpenCL/opencl.h>
#include "GpuThreads.h"
#include "GpuScheduler.h"
#include "CellTaskManagerImpl.h"

#define DEFAULT 5
extern void gpu_register_task(int, char*, 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;
    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 *indata,*outdata;
    int count;
    indata = (int *)malloc(sizeof(int)*task_array_num);
    outdata = (int *)malloc(sizeof(int)*task_array_num);
    for (count=0; count < task_array_num ;count++) {
        indata[count] = count;
    }
    printf("%d",count);
    
    GpuSchedRegister(1,"./twice.cl","Twice");


    cl_int ret;
    cl_context context = gpuThreads->context;
    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_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);

    print_data(indata, count, "before");


    cl_kernel *kernel = task_list[1].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;
    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");

    free(indata);
    free(outdata);
    clReleaseCommandQueue(command_queue);
    clReleaseContext(context);

    tester(indata,outdata,count);

    delete gpuThreads;
}

int
main(int argc, char* argv[])
{   
    int task_array_num;
    if ( ((task_array_num = atoi(argv[1])) == 0)  || argc != 1 ){
        // 無効な引数ならデフォルトの値として5を設定
        task_array_num = DEFAULT;
    }
    test(task_array_num);
    printf("regist task succeed\n");
}