view TaskManager/test/GpuThreadTest/GpuThreadsTest.cc @ 1448:3ee29c1733d1 draft

separate function for GpuTest
author Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
date Tue, 24 Apr 2012 13:52:41 +0900
parents 9de96bded518
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
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 *data,int 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,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");
}