changeset 1508:0e1318e7caed draft

create sort test
author Yuhi TOMARI <yuhi@cr.ie.u-ryukyu.ac.jp>
date Tue, 18 Sep 2012 18:59:54 +0900
parents 7abad41d12af
children acd86a0ff366
files TaskManager/Gpu/GpuScheduler.cc TaskManager/Gpu/GpuScheduler.h example/OpenCL/twice.cl example/many_task/Makefile.macosx example/many_task/main.cc example/many_task/ppe/Makefile example/many_task/ppe/QuickSort.cc example/many_task/ppe/QuickSort.cc.loop example/many_task/ppe/QuickSort.h example/many_task/ppe/mymethod.cc example/many_task/ppe/sort_test.cc example/many_task/ppe/task_init.cc
diffstat 12 files changed, 295 insertions(+), 69 deletions(-) [+]
line wrap: on
line diff
--- a/TaskManager/Gpu/GpuScheduler.cc	Mon Sep 10 17:03:39 2012 +0900
+++ b/TaskManager/Gpu/GpuScheduler.cc	Tue Sep 18 18:59:54 2012 +0900
@@ -36,35 +36,42 @@
         exit(EXIT_FAILURE);
     }
     context = clCreateContext(NULL, 1, &device_id, NULL, NULL, &ret);
-    command_queue = clCreateCommandQueue(context, device_id, 0, &ret);
+    command_queue = new cl_command_queue[2];
+    command_queue[0] = clCreateCommandQueue(context, device_id, 0, &ret);
+    command_queue[1] = clCreateCommandQueue(context, device_id, 0, &ret);
 }
 
 GpuScheduler::~GpuScheduler()
 {
-    clReleaseCommandQueue(command_queue);
+    clReleaseCommandQueue(command_queue[0]);
+    clReleaseCommandQueue(command_queue[1]);
     clReleaseContext(context);
 }
 
 void
 GpuScheduler::run()
 {
+    int cur = 0;
+    memaddr reply[2];
+    HTask::htask_flag flag;
+
     for (;;) {
         memaddr params_addr = connector->task_list_mail_read();
         // Get OpenCL infomation
 
         if ((memaddr)params_addr == (memaddr)MY_SPE_COMMAND_EXIT) {
-            clFinish(command_queue);
+            clFinish(command_queue[0]);
+            clFinish(command_queue[1]);
             return ;
         }
 
-        HTask::htask_flag flag;
-
         memset(&flag, 0, sizeof(HTask::htask_flag));
 
         while (params_addr) {
             // since we are on the same memory space, we don't hae to use dma_load here
             TaskListPtr tasklist = (TaskListPtr)connector->dma_load(this, params_addr,
                                                         sizeof(TaskList), DMA_READ_TASKLIST);
+
             if (tasklist->self) {
                 flag = tasklist->self->flag;
             }
@@ -82,7 +89,7 @@
 
                 cl_mem memparam = clCreateBuffer(context, CL_MEM_READ_ONLY,
                                                sizeof(memaddr)*nextTask->param_count, NULL, NULL);
-                err |= clEnqueueWriteBuffer(command_queue, memparam, CL_TRUE, 0, sizeof(memaddr)*nextTask->param_count,
+                err |= clEnqueueWriteBuffer(command_queue[cur], memparam, CL_TRUE, 0, sizeof(memaddr)*nextTask->param_count,
                                             nextTask->param(0), 0, NULL, NULL);
                 err |= clSetKernelArg(kernel, param, sizeof(memaddr),(void *)&memparam);
 
@@ -94,7 +101,7 @@
                     for(int i=0;i<nextTask->inData_count;i++) {
                         memin[i] = clCreateBuffer(context, mem_flag, nextTask->inData(i)->size, NULL, NULL);
 
-                        err |= clEnqueueWriteBuffer(command_queue, memin[i], CL_TRUE, 0,
+                        err |= clEnqueueWriteBuffer(command_queue[cur], memin[i], CL_TRUE, 0,
                                                     nextTask->inData(i)->size, nextTask->inData(i)->addr, 0, NULL, NULL);
                         err |= clSetKernelArg(kernel,  param, sizeof(memaddr), (void *)&memin[i]);
 
@@ -108,24 +115,29 @@
                 for(int i=0;i<nextTask->outData_count;i++) {
                     memout[i] = clCreateBuffer(context, out_mem_flag, nextTask->outData(i)->size, NULL, NULL);
                     if (flag.flip) { // use output buffer as input buffer
-                        err |= clEnqueueWriteBuffer(command_queue, memout[i], CL_TRUE, 0,
+                        err |= clEnqueueWriteBuffer(command_queue[cur], memout[i], CL_TRUE, 0,
                                                     nextTask->inData(i)->size, nextTask->inData(i)->addr, 0, NULL, NULL);                    }
                     err |= clSetKernelArg(kernel,  param, sizeof(memaddr), (void *)&memout[i]);
                     param++;
                 }
 
                 cl_event ev = NULL;
-                clEnqueueTask(command_queue, kernel, 0, NULL, &ev);
+                clEnqueueTask(command_queue[cur], kernel, 0, NULL, &ev);
 
+                // ndrange flagが0ならdim,global_work_size[0],local_work_size[0] = 1で固定に
+                // clEnqueueNDRange
+                // (command_queue[cur], kernel, dim, NULL,global_work_size[0],local_work_size[0],NULL&ev);
                 for(int i=0;i<nextTask->outData_count;i++) {
-                    err |= clEnqueueReadBuffer(command_queue, memout[i], CL_TRUE, 0,
+                    err |= clEnqueueReadBuffer(command_queue[cur], memout[i], CL_TRUE, 0,
                                                nextTask->outData(i)->size, nextTask->outData(i)->addr, 1, &ev, NULL);
                 }
             }
-            clFlush(command_queue); // waiting for queued task
+            reply[cur] = (memaddr)tasklist->waiter;
+            clFlush(command_queue[1-cur]); // waiting for queued task
 
-            connector->mail_write((memaddr)(tasklist->waiter));
+            connector->mail_write(reply[1-cur]);
             params_addr = (memaddr)tasklist->next;
+            cur = 1 - cur;
         }
         connector->mail_write((memaddr)MY_SPE_STATUS_READY);
     }
--- a/TaskManager/Gpu/GpuScheduler.h	Mon Sep 10 17:03:39 2012 +0900
+++ b/TaskManager/Gpu/GpuScheduler.h	Tue Sep 18 18:59:54 2012 +0900
@@ -19,17 +19,17 @@
     void init_impl(int useRefDma);
     void init_gpu();
     void run();
-    
+
     void mail_write_from_host(memaddr data) {
         fifoDmaManager->mail_write_from_host(data);
     }
 
     memaddr mail_read_from_host() {
-	return fifoDmaManager->mail_read_from_host();
+        return fifoDmaManager->mail_read_from_host();
     }
 
     int has_mail_from_host() {
-	return fifoDmaManager->has_mail_from_host();
+        return fifoDmaManager->has_mail_from_host();
     }
 
     cl_platform_id platform_id;
@@ -37,7 +37,7 @@
     cl_uint ret_num_platforms;
     cl_uint ret_num_devices;
     cl_context context;
-    cl_command_queue command_queue;
+    cl_command_queue *command_queue;
     cl_int ret;
 private:
     FifoDmaManager *fifoDmaManager;
--- a/example/OpenCL/twice.cl	Mon Sep 10 17:03:39 2012 +0900
+++ b/example/OpenCL/twice.cl	Tue Sep 18 18:59:54 2012 +0900
@@ -3,9 +3,18 @@
       __global int *input_data,
       __global int *output_data)
 {
-
+    int a = 1;
+    int b = rec(a);
     long count = (long)data_count[0];
-        for (int i = 0; i<count; i++) {
-            output_data[i] = input_data[i] * 2;
-        }
+    for (int i = 0; i<count; i++) {
+        output_data[i] = b;
+    }
 }
+
+int
+rec(int a)
+{
+    if (a<=1) return a;
+    return rec(a-1)+rec(a-2);
+
+}
--- a/example/many_task/Makefile.macosx	Mon Sep 10 17:03:39 2012 +0900
+++ b/example/many_task/Makefile.macosx	Tue Sep 18 18:59:54 2012 +0900
@@ -1,15 +1,14 @@
-
 include ./Makefile.def
 
 
 SRCS_TMP = $(wildcard *.cc)
-SRCS_EXCLUDE =   # 除外するファイルを書く
+SRCS_EXCLUDE =  # 除外するファイルを書く
 SRCS = $(filter-out $(SRCS_EXCLUDE),$(SRCS_TMP))
 OBJS = $(SRCS:.cc=.o)
 
 TASK_DIR  = ppe
 TASK_SRCS_TMP = $(wildcard $(TASK_DIR)/*.cc)
-TASK_SRCS_EXCLUDE = 
+TASK_SRCS_EXCLUDE = sort_test.cc
 TASK_SRCS = $(filter-out $(TASK_DIR)/$(TASK_SRCS_EXCLUDE),$(TASK_SRCS_TMP))
 TASK_OBJS = $(TASK_SRCS:.cc=.o)
 
--- a/example/many_task/main.cc	Mon Sep 10 17:03:39 2012 +0900
+++ b/example/many_task/main.cc	Tue Sep 18 18:59:54 2012 +0900
@@ -25,7 +25,6 @@
 static double ed_time;
 
 static int length = 1200;
-static int 
 
 // prototype
 void TMend(TaskManager *);
@@ -43,7 +42,7 @@
 {
     puts("-----------------------------------------------");
     for(int i = 0; i < data_length; i++) {
-	printf("data[%02d].index = %d\n", i, data[i].index);
+        printf("data[%02d].index = %d\n", i, data[i].index);
     }
     puts("-----------------------------------------------");
 }
@@ -66,7 +65,7 @@
             all = 1;
         }
         if (strcmp(argv[i], "-c") == 0 ) {
-            sort_task = SortCompat
+            sort_task = SortCompat;
         }
         if (strcmp(argv[i], "-s") == 0 ) {
             sort_task = SortSimple;
@@ -79,7 +78,7 @@
 
 Sort sorter;
 
-static void 
+static void
 check_data()
 {
     for(int i=0; i< sorter.data_length-1;i++) {
@@ -124,21 +123,21 @@
 TMmain(TaskManager *manager, int argc, char *argv[])
 {
     if (init(argc, argv) < 0) {
-	return -1;
+        return -1;
     }
 
     task_init();
 
     int cpu  = manager->get_cpuNum();
 
-    // in case of -cpu 0 
+    // in case of -cpu 0
     if (cpu==0) cpu = 1;
     if (1) {
-	HTask *dummy = manager->create_task(Dummy);
-	dummy->set_post(sort_init, (void*)cpu, (void*)length);
-	dummy->spawn();
+        HTask *dummy = manager->create_task(Dummy);
+        dummy->set_post(sort_init, (void*)cpu, (void*)length);
+        dummy->spawn();
     } else {
-	sort_init(manager->get_schedTask(),(void*)cpu, (void*)length);
+        sort_init(manager->get_schedTask(),(void*)cpu, (void*)length);
     }
 
     st_time = getTime();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/example/many_task/ppe/Makefile	Tue Sep 18 18:59:54 2012 +0900
@@ -0,0 +1,30 @@
+SRCS_TMP = $(wildcard *.cc)
+SRCS_EXCLUDE = task_init.cc mymethod.cc  # 除外するファイルを書く
+SRCS = $(filter-out $(SRCS_EXCLUDE),$(SRCS_TMP))
+OBJS = $(SRCS:.cc=.o)
+TARGET = sort_test
+CERIUM = ../../../../CERIUM
+CC      = g++
+CFLAGS  = -g -Wall
+
+INCLUDE =  -I${CERIUM}/include/TaskManager -I. -I..
+
+.SUFFIXES: .cc .o
+
+.cc.o:
+	$(CC) $(CFLAGS) $(INCLUDE) -c $< -o $@
+
+all: $(TARGET)
+
+$(TARGET): $(OBJS) $(TASK_OBJS)
+	$(CC) -o $@ $(OBJS) $(TASK_OBJS) $(LIBS)
+
+link:
+	$(CC) -o $(TARGET) $(OBJS) $(TASK_OBJS) $(LIBS)
+
+debug: $(TARGET)
+	sudo gdb ./$(TARGET) 
+
+clean:
+	rm -f $(TARGET) $(OBJS) $(TASK_OBJS)
+	rm -f *~ \#*
--- a/example/many_task/ppe/QuickSort.cc	Mon Sep 10 17:03:39 2012 +0900
+++ b/example/many_task/ppe/QuickSort.cc	Tue Sep 18 18:59:54 2012 +0900
@@ -9,7 +9,7 @@
 static void
 swap( Data *data, int left, int right )
 {
-    Data tmp	      = data[left];
+    Data tmp          = data[left];
     data[left]  = data[right];
     data[right] = tmp;
 }
@@ -19,7 +19,7 @@
 static int
 run(SchedTask *s, void* rbuff, void* wbuff) {
     // copy value
-    int begin	= 0;
+    int begin   = 0;
 #if USE_SIMPLE_TASK
     int end = s->read_size()/sizeof(Data);
     Data *r_data = (Data*)rbuff;
@@ -35,10 +35,10 @@
 #endif
 
     // printf("[PPE] Quick: length:%d addr->%x \n",end, (int)rbuff);
-    // printf("[PPE] Quick: data[0]: %ld addr->%lx\n",sizeof(r_data),(long)r_data); 
+    // printf("[PPE] Quick: data[0]: %ld addr->%lx\n",sizeof(r_data),(long)r_data);
 
-    //show_data(r_data, end);
     quick_sort(r_data, begin, end-1);
+
 #ifdef USE_MEMCPY
     memcpy(w_data, r_data, sizeof(Data)*end);
 #else
@@ -48,26 +48,32 @@
     return 0;
 }
 
-static void 
-quick_sort( Data *data, int begin, int end ) {
+void
+qsort_test(Data *data, int begin, int end ) {
+    quick_sort(data, begin, end);
+    printf("end is %d\n",end);
+}
+
+static void
+quick_sort(Data *data, int begin, int end ) {
 
     if (begin < end) {
-	int where = (begin + end) / 2;
-	int pivot = data[where].index;
-	data[where].index = data[begin].index;
-	int p = begin;
-	int i;
-	for (i=begin+1; i<=end; i++) {
-	    if (data[i].index < pivot) { 
-		p++; 
-		swap(data, p, i); 
-	    }
-	}
-	data[begin].index = data[p].index;
-	data[p].index = pivot;
-	
-	quick_sort(data, begin, p-1);
-	quick_sort(data, p+1, end); // tail call
+        int where = (begin + end) / 2;
+        int pivot = data[where].index;
+        data[where].index = data[begin].index;
+        int p = begin;
+        int i;
+        for (i=begin+1; i<end; i++) {
+            if (data[i].index < pivot) {
+                p++;
+                swap(data, p, i);
+            }
+        }
+        data[begin].index = data[p].index;
+        data[p].index = pivot;
+
+        quick_sort(data, begin, p-1);
+        quick_sort(data, p+1, end); // tail call
     }
 }
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/example/many_task/ppe/QuickSort.cc.loop	Tue Sep 18 18:59:54 2012 +0900
@@ -0,0 +1,97 @@
+#include "QuickSort.h"
+#include <stdio.h>
+#include <string.h>
+
+SchedDefineTask(QuickSort);
+
+static void quick_sort( Data *data, int begin, int end ) ;
+extern void show_data(DataPtr, int);
+
+static void
+swap( Data *data, int left, int right )
+{
+    Data tmp          = data[left];
+    data[left]  = data[right];
+    data[right] = tmp;
+}
+
+// #define USE_MEMCPY
+
+static int
+run(SchedTask *s, void* rbuff, void* wbuff)
+{
+    // copy value
+    int begin   = 0;
+#if USE_SIMPLE_TASK
+    int end = s->read_size()/sizeof(Data);
+    Data *r_data = (Data*)rbuff;
+#ifdef USE_MEMCPY
+    Data *w_data = (Data*)wbuff;
+#endif
+#else
+    int end = s->get_inputSize(0)/sizeof(Data);
+    DataPtr r_data = (DataPtr)s->get_input(0);
+#ifdef USE_MEMCPY
+    DataPtr w_data = (DataPtr)s->get_output(0);
+#endif
+#endif
+
+    printf("[PPE] Quick: data[0]: %ld addr->%lx\n",sizeof(r_data),(long)r_data);
+
+    //    show_data(r_data, end);
+    quick_sort(r_data, begin, end-1);
+    //    show_data(r_data, end);
+#ifdef USE_MEMCPY
+    memcpy(w_data, r_data, sizeof(Data)*end);
+#else
+    s->swap();
+#endif
+
+    return 0;
+}
+
+void
+qsort_test(Data *data, int begin, int end ) {
+    quick_sort(data, begin, end);
+    printf("end is %d\n",end);
+}
+
+static void
+quick_sort( Data *data, int begin, int end ) {
+    int stack[1024];
+    int sp = 0;
+    int p = begin;
+    while (begin < end) {
+        while (begin < end) {
+            int where = (begin + end) / 2;
+            int pivot = data[where].index;
+            data[where].index = data[begin].index;
+            int i;
+            for (i=begin+1; i<end; i++) {
+                if (data[i].index < pivot) {
+                    p++;
+                    swap(data, p, i);
+                }
+            }
+            data[begin].index = data[p].index;
+            data[p].index = pivot;
+
+            stack[sp++] = begin;
+            stack[sp++] = end;
+            end = p-1;
+
+            // quick_sort(data, begin, p-1);
+            // beginとp-1のみが変わっている(これだけを保持)
+            // beginとp-1(end)用のスタックを作ってやればよい
+        }
+        if (sp == 0) return;
+        end = stack[--sp];
+        begin = stack[--sp];
+        begin = p+1;
+        // quick_sort(data, p+1, end); // tail call
+        // そのままループに
+    }
+}
+
+
+/* end */
--- a/example/many_task/ppe/QuickSort.h	Mon Sep 10 17:03:39 2012 +0900
+++ b/example/many_task/ppe/QuickSort.h	Tue Sep 18 18:59:54 2012 +0900
@@ -7,5 +7,4 @@
 
 #include "sort.h"
 
-
 #endif
--- a/example/many_task/ppe/mymethod.cc	Mon Sep 10 17:03:39 2012 +0900
+++ b/example/many_task/ppe/mymethod.cc	Tue Sep 18 18:59:54 2012 +0900
@@ -10,34 +10,34 @@
     int i;
     srand(10);
     for (i=0; i<size; i++)
-    { 
-        data[i].index   = rand()%10000; 
-        data[i].ptr	= 0;
-    }
-	return;
+        { 
+            data[i].index   = rand()%10000; 
+            data[i].ptr	= 0;
+        }
+    return;
 }
 
 void
 show_data( Data *data, int size )
 {
-	puts("-----------------------------------------------");
-	for(int i=0; i<size; i++) printf("data[%02d].index = %d\n", i, data[i].index);
-	puts("-----------------------------------------------");
-	return;
+    puts("-----------------------------------------------");
+    for(int i=0; i<size; i++) printf("data[%02d].index = %d\n", i, data[i].index);
+    puts("-----------------------------------------------");
+    return;
 }
 
 void
 write_data( Data *data, int size )
 {
-	FILE* fd;
-	if ((fd = fopen("./sort.txt", "w")) < 0) {
+    FILE* fd;
+    if ((fd = fopen("./sort.txt", "w")) < 0) {
     	fprintf(stderr,"Can't open file");
-	}
+    }
 
     fprintf(fd,"-----------------------------------------------");
     for(int i=0; i<size; i++) fprintf(fd,"data[%02d].index = %d\n", i, data[i].index);
     fprintf(fd,"-----------------------------------------------");
 
-	fclose( fd );
+    fclose( fd );
     return;
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/example/many_task/ppe/sort_test.cc	Tue Sep 18 18:59:54 2012 +0900
@@ -0,0 +1,58 @@
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+//#include "sort.h"
+#include "QuickSort.h"
+// sort.cc
+extern int data_length;
+extern DataPtr data;
+extern void quick_sort(DataPtr, int, int);
+static int length = 1200;
+extern void qsort_test(Data*, int, int);
+
+void
+show( Data *data, int size )
+{
+    puts("-----------------------------------------------");
+    for(int i=0; i<=size; i++) printf("data[%02d].index = %d\n", i, data[i].index);
+    puts("-----------------------------------------------");
+    return;
+}
+
+int
+init(int argc, char **argv)
+{
+    for (int i = 1; argv[i]; ++i) {
+        if (strcmp(argv[i], "--length") == 0 || strcmp(argv[i], "-l") == 0) {
+            length = atoi(argv[++i]);
+        }
+    }
+
+    return 0;
+}
+
+Sort sorter;
+
+int
+main(int argc, char *argv[])
+{
+    if (init(argc, argv) < 0) {
+        return -1;
+    }
+
+    sorter.data = new Data[length];
+    sorter.data_length = length;
+
+    for (int i = 0; i < length; i++) {
+        sorter.data[i].index = rand()%10000;
+        sorter.data[i].ptr   = i;
+    }
+
+    int begin = 0;
+    int end = length;
+    show(sorter.data, end-1);
+    qsort_test(sorter.data, begin, end);
+    show(sorter.data, end-1);
+
+    return 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/example/many_task/ppe/task_init.cc	Tue Sep 18 18:59:54 2012 +0900
@@ -0,0 +1,17 @@
+#include "Func.h"
+#include "Scheduler.h"
+
+SchedExternTask(QuickSort);
+SchedExternTask(SortSimple);
+SchedExternTask(SortCompat);
+
+void
+task_init(void)
+{
+    // ex
+    // SchedRegisterNDRange(dim,global_size, local_size)
+
+    SchedRegisterTask(QUICK_SORT, QuickSort);
+    SchedRegister(SortSimple);
+    SchedRegister(SortCompat);
+}