Mercurial > hg > Game > Cerium
changeset 1449:a193b282952b draft
create check routine, and rename GpuThreadsTest to GpuRegistTaskTest
author | Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 24 Apr 2012 14:20:09 +0900 |
parents | 3ee29c1733d1 |
children | 27ed99a7a48c |
files | TaskManager/test/GpuRegistTaskTest/GpuRegistTaskTest.cc TaskManager/test/GpuRegistTaskTest/Makefile TaskManager/test/GpuRegistTaskTest/twice.cl TaskManager/test/GpuThreadTest/GpuScheduler.cc TaskManager/test/GpuThreadTest/GpuScheduler.h TaskManager/test/GpuThreadTest/GpuTaskManagerImpl.cc TaskManager/test/GpuThreadTest/GpuTaskManagerImpl.h TaskManager/test/GpuThreadTest/GpuThreads.cc TaskManager/test/GpuThreadTest/GpuThreads.h TaskManager/test/GpuThreadTest/GpuThreadsTest.cc TaskManager/test/GpuThreadTest/Makefile TaskManager/test/GpuThreadTest/twice.cl |
diffstat | 12 files changed, 120 insertions(+), 480 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TaskManager/test/GpuRegistTaskTest/GpuRegistTaskTest.cc Tue Apr 24 14:20:09 2012 +0900 @@ -0,0 +1,94 @@ +#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 *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",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"); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TaskManager/test/GpuRegistTaskTest/Makefile Tue Apr 24 14:20:09 2012 +0900 @@ -0,0 +1,15 @@ +include ../../Makefile.def + +CPPFLAGS += -g -Wall -I../../../include/TaskManager -m$(ABIBIT) + +TARGET= GpuRegistTaskTest + +$(TARGET) : + +LIBS += ../../libFifoManager.a + +CpuThreadsTest : GpuRegistTaskTest.o + $(CC) $(CFLAGS) -o $@ $? $(LIBS) + +clean: + rm -rf *.o $(TARGET)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/TaskManager/test/GpuRegistTaskTest/twice.cl Tue Apr 24 14:20:09 2012 +0900 @@ -0,0 +1,11 @@ +__kernel void +twice(__global int *input_data, + __global int *output_data, + __global int *data_count) +{ + int count = *data_count; + for (int i = 0; i<count; i++) { + output_data[i] = input_data[i] * 2; + } + +} \ No newline at end of file
--- a/TaskManager/test/GpuThreadTest/GpuScheduler.cc Tue Apr 24 13:52:41 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,88 +0,0 @@ -#include "GpuScheduler.h" -#include "GpuDmaManager.h" -#include "GpuThreads.h" -#include "stdio.h" -#include <fcntl.h> - -void -GpuScheduler::init_impl(int useRefDma) -{ - connector = new GpuDmamanager; -} - -int -GpuScheduler::run() -{ - memaddr params_addr = connector->task_list_mail_read(); - - if ((memaddr)params_addr == (memaddr)MY_SPE_COMMAND_EXIT) { - // 終了確認 - return 0; - } - - TaskListPtr *tasklist = (TaskListPtr)connector->dma_load(tasklist, params_addr, - sizeof(TaskList), DMA_READ_TASKLIST); - GpuThreads gputhreads = GpuThreads::getInstance(); - cl_command_queue& command_queue = gputhreads.cl_command_queue; - - for (int cur_index = 0; cur_index < tasklist->length; cur_index++) { - TaskPtr task = tasklist.tasks[cur_index]; - cl_kernel& kernel = task_list[task->command].kernel; - // メモリオブジェクトの生成? GpuThreadsでやるのがよい? - // カーネル引数の設定 - clEnqueueTask(command_queue, kernel, 0, NULL, NULL); - // メモリオブジェクトからの読み込み - } - // TaskArrayの処理 -} - - - -void -gpu_register_task(int cmd, char* filename, char* functionname) -{ - GpuThreads gputhreads = GpuThreads::getInstance(); - cl_context& context = &gputhreads.context; - cl_device_id& device_id = &gputhreads.device_id; - - int *fp; - char *souce_str; - size_t source_size; - - fp = open(filename, O_RDONLY); - - if (!fp) { - fprintf(stderr, "Failed to load kernel.\n"); - exit(1); - } - - struct stat stats; - fstat(fileno(fp),&stats); - off_t size = stas.st_size; - - if (!size) { - fprintf(stderr, "Failed to load kernel.\n"); - } - - source_str = (char*)malloc(size); - - source_size = read(source_str, size, fp); - close(fp); - - cl_program program = NULL; - cl_int ret; - program = clCreateProgramWithSource(context, 1, (const char **)&source_str, - (const size_t *)&source_size, &ret); - - clBuildProgram(program, 1, &device_id, NULL, NULL, NULL); - - cl_kernel *kernel = new cl_kernel; - *kernel = clCreateKernel(program, functionname, &ret); - - task_list[cmd].run = run; - task_list[cmd].load = null_loader; - task_list[cmd].wait = null_waiter; - task_list[cmd].name = str; - task_list[cmd].kernel = kernel; - -}
--- a/TaskManager/test/GpuThreadTest/GpuScheduler.h Tue Apr 24 13:52:41 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,24 +0,0 @@ -#ifndef INCLUDED_GPU_SCHEDULER -#define INCLUDED_GPU_SCHEDULER - -#include "Scheduler.h" - -#ifdef __APPLE__ -#include <OpenCL/opencl.h> -#else -#include <CL/cl.h> -#endif - -class GpuScheduler : public Scheduler { -public: - void init_impl(int useRefDma); - int run(); - -private: - -}; - -#endif - -#define GpuSchedRegister(str, filename, functionname) \ - gpu_register_task(str, filename, functionname)
--- a/TaskManager/test/GpuThreadTest/GpuTaskManagerImpl.cc Tue Apr 24 13:52:41 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,97 +0,0 @@ -#include "GpuTaskManagerImpl.h" -#include "MainScheduler.h" -#include "SchedTask.h" - - -GpuTaskManagerImpl::~GpuTaskManagerImpl() { - -} - -void GpuTaskManagerImpl::init() { - - gpuTaskList = new QueueInfo<TaskList> - TaskListInfo = new QueueInfo<TaskList> - - ppeManager = new FifoTaskManagerImpl(machineNum); - MainScheduler *mscheduler = new MainScheduler; - set_scheduler(mscheduler); - ppeManager->init(mscheduler, this, useRefDma); - - htaskImpl = activeTaskQueue; - mscheduler->set_manager(this); - - gpuThreads->init(); - - schedTaskManager = new SchedTask(); - schedTaskManager->init(0, 0, 0, ppeManager->get_scheduler(), 0); - ppeManager->schedTaskManager = schedTaskManager; -} - -void GpuTaskManagerImpl::run() { - do { - ppeManager->poll(); - do { - poll(); - } while (ppeManager->activeTaskQueue->empty()); - // ちゃんと最後のタスクまで実行される? - } while (!ppeManager->activeTaskQueue->empty() || !activeTaskQueue->empty()); -} - -void GpuTaskManagerImpl::poll() { - set_runTaskList(); - sendTaskList(); -} - -void GpuTaskManagerImpl::set_runTaskList() { - HtaskPtr htask = activeTaskQueue->getFirst(); - while (htask != NULL) { - if (htask->cpu_type == CPU_PPE) { - htask = activeTaskQueue->getNext(htask); - } else { - set_taskList(htask, taskListInfo); - - HtaskPtr next = activeTaskQueue->getNext(htask); - activeTaskQueue->remove(htask); - htask = next; - } - } -} - -void GpuTaskManagerImpl::sendTaskList() { - mail_check(); - if (!gpuTaskList->empty()) { - continue; - } - if (!taskListInfo->empty()) { - send_taskList(); - } -} - -void GpuTaskManagerImpl::send_taskList() { - // swap gpuTaskList for taskListInfo - QueueInfo<TaskList> *tmp = taskListInfo; - taskListInfo = gpuTaskList; - gpuTaskList = tmp; - - gpuTaskList->getLast()->next = 0; - TaskListPtr p = gpuTaskList->getFirst(); - - // send taskList - gpuThreads->send_mail((memaddr *) &p); -} - -void GpuTaskManagerImpl::mail_check() { - memaddr data; - while (speThreads->has_mail(&data)) { - //どのメールが必要かよく考える - } -} - -#ifdef __CERIUM_GPU__ -TaskManagerImpl* -create_impl(int num, int useRefDma) -{ - GpuThreads *gpus = GpuThreads::getInstance(); - return new GpuTaskManagerImpl(num, gpus); -} -#endif
--- a/TaskManager/test/GpuThreadTest/GpuTaskManagerImpl.h Tue Apr 24 13:52:41 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,34 +0,0 @@ -#ifndef INCLUDED_GPU_TASK_MANAGER_IMPL -#define INCLUDED_GPU_TASK_MANAGER_IMPL - -#include "TaskManagerImpl.h" -#include "FifoTaskManagerImpl.h" -#include "GpuThreads.h" -#include "QueueInfo.h" - -class GpuTaskManagerImpl : public TaskManagerImpl { -public: - GpuTaskManagerImpl(int num, Threads *gpus) : TaskManagerImpl(num) {gpuThreads = gpus;} - ~GpuTaskManagerImpl(); - - void init(int spuIdle,int useRefDma); - void run(); - void poll(); - void set_runTaskList(); - void sendTaskList(); - void send_taskList(); - void mail_check(); - -public: - QueueInfo<TaskList> *gpuTaskList; - QueueInfo<TaskList> *taskListInfo; - - FifoTaskManagerImpl *ppeManager; - - GpuThreads *gpuThreads; - FifoTaskManagerImpl *ppeManager; - -}; - -#endif -
--- a/TaskManager/test/GpuThreadTest/GpuThreads.cc Tue Apr 24 13:52:41 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,75 +0,0 @@ -#include "GpuScheduler.h" -#include "GpuThreads.h" -#include "TaskManagerImpl.h" - -GpuThreads::GpuThreads() -{ - threads = new pthread_t; - args = new gpu_arg; -} - -GpuThreads::~GpuThreads() -{ - delete threads; - delete args; -} - -void -GpuThreads::init() -{ - clGetPlatformIDs(1, &platfrom_id, &ret_num_platforms); - clGetDeviceIds(platform_id, CL_DEVICE_TYPE_GPU, 1, &device_id, &ret_num_devices); - - // unavailable GPU - if( ret_num_devices == 0) { - exit(EXIT_FAILURE); - } - context = clCreateContext(NULL, 1, &device_id, NULL, NULL, &ret); - command_queue = clCreateCommandQueue(context, device_id, 0, &ret); - - args.scheduler = new GpuScheduler(); - args.useRefDma = use_refdma; - - pthread_create(&threads, NULL, &cpu_thread_run, (void*)&args); - -} - -void * -GpuThreads::gpu_thread_run(void *args) -{ - gpu_arg *argt = (gpu_arg *) args; - Scheduler *g_scheduler = argt->scheduler; - - TaskManagerImpl *manager = new GpuTaskManagerImpl(); - g_scheduler->init(manager, argt->useRefDma); - - manager->set_scheduler(g_scheduler); - - g_scheduler->run(); - g_scheduler->finish(); - - return NULL -} - -int -GpuThreads::get_mail(memaddr *ret) -{ - *ret = args.scheduler->mail_read_from_host(); - return 1; -} - -int -GpuThreads::has_mail(memaddr *ret) -{ - if (args.scheduler->has_mail_from_host() != 0) { - return get_mail(ret); - } else { - return 0; - } -} - -void -CpuThreads::send_mail(int cpuid, int num, memaddr *data) -{ - args.scheduler->mail_write_from_host(*data); -}
--- a/TaskManager/test/GpuThreadTest/GpuThreads.h Tue Apr 24 13:52:41 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,51 +0,0 @@ -#ifndef INCLUDED_GPU_THREADS -#define INCLUDED_GPU_THREADS - -#include <pthread.h> -#include "Threads.h" -#include "GpuScheduler.h" -#ifdef __APPLE__ -#include <OpenCL/opencl.h> -#else -#include <CL/cl.h> -#endif - -// Singleton Pattern -struct gpu_arg { - GpuScheduler *scheduler; - int useRefDma; -}; - -class GpuThreads : public Threads { -public: - static GpuThreads* getInstance() { - static GpuThreads* singleton; - return singleton; - } - ~GpuThreads(); - - void init(/*cl_device_id device_id*/); - void *gpu_thread_run(void *args); - - int get_mail(memaddr *ret); - int has_mail(memaddr *ret); - void send_mail(int cpuid, int num, memaddr *data); - -public: - cl_platform_id platform_id; - cl_device_id device_id; - cl_uint ret_num_platforms; - cl_uint ret_num_devices; - cl_context context; - - cl_command_queue command_queue; - -private: - GpuThreads(); - - gpu_arg *args; - pthread_t *threads; - int use_refdma; -}; - -#endif
--- a/TaskManager/test/GpuThreadTest/GpuThreadsTest.cc Tue Apr 24 13:52:41 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,85 +0,0 @@ -#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"); -}
--- a/TaskManager/test/GpuThreadTest/Makefile Tue Apr 24 13:52:41 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,15 +0,0 @@ -include ../../Makefile.def - -CPPFLAGS += -g -Wall -I../../../include/TaskManager -m$(ABIBIT) - -TARGET= GpuThreadsTest - -$(TARGET) : - -LIBS += ../../libFifoManager.a - -CpuThreadsTest : GpuThreadsTest.o - $(CC) $(CFLAGS) -o $@ $? $(LIBS) - -clean: - rm -rf *.o $(TARGET)
--- a/TaskManager/test/GpuThreadTest/twice.cl Tue Apr 24 13:52:41 2012 +0900 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,11 +0,0 @@ -__kernel void -twice(__global int *input_data, - __global int *output_data, - __global int *data_count) -{ - int count = *data_count; - for (int i = 0; i<count; i++) { - output_data[i] = input_data[i] * 2; - } - -} \ No newline at end of file