changeset 1440:d66dcb067c89 draft

create test program
author YuuhiTOMARI
date Mon, 23 Apr 2012 05:48:02 +0900
parents 20e935f170c1
children 50d2c1e85535
files 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 9 files changed, 418 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TaskManager/test/GpuThreadTest/GpuScheduler.cc	Mon Apr 23 05:48:02 2012 +0900
@@ -0,0 +1,77 @@
+#include "GpuScheduler.h"
+#include "GpuDmaManager.h"
+#include "GpuThreads.h"
+#include "stdio.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;
+
+    FILE *fp;
+    char *souce_str;
+    size_t source_size;
+
+    fp = fopen(filename, "r");
+    if (!fp) {
+        fprintf(stderr, "Failed to load kernel.\n");
+        exit(1);
+    }
+
+    source_str = (char*)malloc(MAX_SOURCE_SIZE);
+    source_size = fread(source_str, 1, MAX_SOURCE_SIZE, fp);
+    fclose(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;
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TaskManager/test/GpuThreadTest/GpuScheduler.h	Mon Apr 23 05:48:02 2012 +0900
@@ -0,0 +1,24 @@
+#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)*/
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TaskManager/test/GpuThreadTest/GpuTaskManagerImpl.cc	Mon Apr 23 05:48:02 2012 +0900
@@ -0,0 +1,97 @@
+#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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TaskManager/test/GpuThreadTest/GpuTaskManagerImpl.h	Mon Apr 23 05:48:02 2012 +0900
@@ -0,0 +1,34 @@
+#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
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TaskManager/test/GpuThreadTest/GpuThreads.cc	Mon Apr 23 05:48:02 2012 +0900
@@ -0,0 +1,75 @@
+#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);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TaskManager/test/GpuThreadTest/GpuThreads.h	Mon Apr 23 05:48:02 2012 +0900
@@ -0,0 +1,51 @@
+#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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TaskManager/test/GpuThreadTest/GpuThreadsTest.cc	Mon Apr 23 05:48:02 2012 +0900
@@ -0,0 +1,28 @@
+#include <stdio.h>
+#include "GpuThreads.h"
+//#include "GpuScheduler.h"
+#include "CellTaskManagerImpl.h"
+
+#define MAX_SOURCE_SIZE (0x100000)
+
+void
+main(int argc, char* argv[])
+{
+	GpuThreads* c = GpuThreads::getInstance();
+    c->init();
+
+    FILE *fp;
+    char *kernel_src_str;
+    size_t kernel_code_size;
+    fp = fopen("twice.cl", "r");
+    kernel_src_str = (char *)malloc(MAX_SOURCE_SIZE);
+    kernel_code_size = fread(kernel_src_str, 1, MAX_SOURCE_SIZE, fp);
+    fclose(fp);
+
+    gpu_register_task(1, kernel_src_str,"Twice");
+    
+    delete c;
+    
+}
+
+/* end */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TaskManager/test/GpuThreadTest/Makefile	Mon Apr 23 05:48:02 2012 +0900
@@ -0,0 +1,21 @@
+include ../../Makefile.def
+
+CPPFLAGS += -g -Wall -I../../../include/TaskManager -m$(ABIBIT)
+
+TARGET= GpuScheduler GpuThreads GpuTaskManagerImpl GpuThreadsTest 
+
+$(TARGET) :
+
+LIBS += ../../libFifoManager.a
+
+CpuThreadsTest : GpuThreadsTest.o
+	$(CC) $(CFLAGS) -o $@ $? $(LIBS)
+
+CpuThreads : GpuThreads.o
+	$(CC) $(CFLAGS) -o $@ $? $(LIBS)
+
+CpuScheduler : GpuScheduler.o
+	$(CC) $(CFLAGS) -o $@ $? $(LIBS)
+
+clean:
+	rm -rf *.o $(TARGET)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TaskManager/test/GpuThreadTest/twice.cl	Mon Apr 23 05:48:02 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