changeset 663:8a807e2f64f8

incremental task creation on word count_test
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 02 Dec 2009 22:20:32 +0900
parents b72663cf056a
children 7405d92733a8
files TaskManager/Makefile.cell TaskManager/Makefile.def example/word_count/spe/Exec.cc example/word_count3/Makefile.def example/word_count3/Makefile.macosx example/word_count_test/Func.h example/word_count_test/Makefile.def example/word_count_test/main.cc example/word_count_test/spe/Exec.cc example/word_count_test/spe/Makefile
diffstat 10 files changed, 125 insertions(+), 76 deletions(-) [+]
line wrap: on
line diff
--- a/TaskManager/Makefile.cell	Wed Dec 02 20:44:01 2009 +0900
+++ b/TaskManager/Makefile.cell	Wed Dec 02 22:20:32 2009 +0900
@@ -35,7 +35,7 @@
 
 EXTRA_CFLAGS = -D__CERIUM_CELL__ -DHAS_POSIX_MEMALIGN -fno-strict-aliasing
 
-SPE_CFLAGS = -g $(SIMPLE_TASK) -D__CERIUM_CELL__ -fno-exceptions -fno-rtti -Wall # -O9
+SPE_CFLAGS = -g $(SIMPLE_TASK) -D__CERIUM_CELL__ -fno-exceptions -fno-rtti -Wall  -O9
 
 all: default
 
--- a/TaskManager/Makefile.def	Wed Dec 02 20:44:01 2009 +0900
+++ b/TaskManager/Makefile.def	Wed Dec 02 22:20:32 2009 +0900
@@ -33,7 +33,7 @@
 SIMPLE_TASK=
 
 CC     = g++   
-CFLAGS = $(SIMPLE_TASK) -Wall `sdl-config --cflags` -g -m$(ABIBIT)  # -O9 
+CFLAGS = $(SIMPLE_TASK) -Wall `sdl-config --cflags` -g -m$(ABIBIT)   -O9 
 LIBS   =  -m$(ABIBIT)
 
 INCLUDE = -I../include/TaskManager
--- a/example/word_count/spe/Exec.cc	Wed Dec 02 20:44:01 2009 +0900
+++ b/example/word_count/spe/Exec.cc	Wed Dec 02 22:20:32 2009 +0900
@@ -6,8 +6,8 @@
 /* これは必須 */
 SchedDefineTask(Exec);
 
-typedef char *cvector __attribute__ ((vector_size (16)));
-// typedef char *cvector;
+// typedef char *cvector __attribute__ ((vector_size (16)));
+typedef char *cvector;
 
 static int
 run(SchedTask *s, void *rbuf, void *wbuf)
--- a/example/word_count3/Makefile.def	Wed Dec 02 20:44:01 2009 +0900
+++ b/example/word_count3/Makefile.def	Wed Dec 02 22:20:32 2009 +0900
@@ -1,10 +1,10 @@
 TARGET = post
 
 # include/library path
-# ex: macosx
+# ex  macosx
 #CERIUM = /Users/gongo/Source/Cerium
 
-# ex: linux/ps3
+# ex  linux/ps3
 CERIUM = ../../../Cerium
 
 CC      = g++
--- a/example/word_count3/Makefile.macosx	Wed Dec 02 20:44:01 2009 +0900
+++ b/example/word_count3/Makefile.macosx	Wed Dec 02 22:20:32 2009 +0900
@@ -12,7 +12,7 @@
 TASK_OBJS = $(TASK_SRCS:.cc=.o)
 
 LIBS += -lFifoManager `sdl-config --libs`
-CC += -m64
+CC += -m32
 
 .SUFFIXES: .cc .o
 
--- a/example/word_count_test/Func.h	Wed Dec 02 20:44:01 2009 +0900
+++ b/example/word_count_test/Func.h	Wed Dec 02 22:20:32 2009 +0900
@@ -1,6 +1,7 @@
 enum {
 #include "SysTasks.h"
     TASK_EXEC,
+    RUN_TASK_BLOCKS,
     TASK_PRINT,
     RUN_FINISH,
 };
--- a/example/word_count_test/Makefile.def	Wed Dec 02 20:44:01 2009 +0900
+++ b/example/word_count_test/Makefile.def	Wed Dec 02 22:20:32 2009 +0900
@@ -1,10 +1,10 @@
 TARGET = word_count
 
 # include/library path
-# ex: macosx
+# ex  macosx
 #CERIUM = /Users/gongo/Source/Cerium
 
-# ex: linux/ps3
+# ex  linux/ps3
 CERIUM = ../../../Cerium
 
 CC      = g++
--- a/example/word_count_test/main.cc	Wed Dec 02 20:44:01 2009 +0900
+++ b/example/word_count_test/main.cc	Wed Dec 02 22:20:32 2009 +0900
@@ -7,6 +7,7 @@
 #include <fcntl.h>
 #include <unistd.h>
 #include "TaskManager.h"
+#include "SchedTask.h"
 #include "Func.h"
 
 extern void task_init();
@@ -18,9 +19,23 @@
     off_t size;
 } st_mmap_t;
 
+typedef struct {
+    int size;             // remaining file size
+    int division_size;    // for each word count task
+    int division_out_size;    
+    int task_num;         // remaining task count
+    int task_blocks;      // spawn task one at a time
+    int status_num; 
+    unsigned long long *o_data;
+    unsigned long long *head_tail_flag;
+    int pad; 
+    caddr_t file_mmap;
+    HTaskPtr t_print;
+} WordCount;
+
 
 /*与えられたsizeをfix_byte_sizeの倍数にする(丸め込むっていうのかな?)*/
-int
+static int
 fix_byte(int size,int fix_byte_size)
 {
     size = (size/fix_byte_size)*fix_byte_size  + ((size%fix_byte_size)!= 0)*fix_byte_size;
@@ -29,7 +44,7 @@
 }
 
 
-st_mmap_t
+static st_mmap_t
 my_mmap(char *filename)
 {
 
@@ -65,98 +80,118 @@
 
 }
 
+static void
+run_tasks(SchedTask *manager, WordCount *w, int task_count, HTaskPtr t_next, int size) 
+{
+    for (int i = 0; i < task_count && w->size >= w->division_size; i++) {
+	HTaskPtr t_exec = manager->create_task(TASK_EXEC);
+	t_exec->add_inData(w->file_mmap + i*w->division_size, size);
+	t_exec->add_outData(w->o_data + i*w->status_num, w->division_out_size);
+	t_exec->add_outData(w->head_tail_flag + i*w->pad, w->division_out_size);
+	t_exec->add_param(size);
+	t_exec->set_cpu(SPE_ANY);
+	t_next->wait_for(t_exec);
+	t_exec->spawn();
+	w->size -= size;
+	w->task_num--;
+    }
+}
 
-void
+SchedDefineTask1(RUN_TASK_16,run16);
+
+static int
+run16(SchedTask *manager, void *in, void *out)
+{
+    WordCount *w = (WordCount *)manager->get_param(1);
+   
+    if (w->task_num < w->task_blocks) {
+	if (w->size > w->division_size) 
+	    run_tasks(manager,w,w->task_blocks, w->t_print, w->division_size);
+	if (w->size>0 && w->size < w->division_size) 
+	    run_tasks(manager,w,1, w->t_print, w->size);
+    } else {
+
+	HTaskPtr t_next = manager->create_task(RUN_TASK_BLOCKS);
+	t_next->set_param(0,(void*)w);
+	w->t_print->wait_for(t_next);
+
+	run_tasks(manager,w, w->task_blocks, t_next, w->division_size);
+
+	t_next->spawn();
+    }
+    return 0;
+}
+
+
+static int blocks = 16;
+
+static void
 run_start(TaskManager *manager, char *filename)
 {
-    HTaskPtr t_exec;
     HTaskPtr t_print;
 
     st_mmap_t st_mmap;
     st_mmap = my_mmap(filename);
+    WordCount *w = (WordCount*)manager->allocate(sizeof(WordCount));
 
+    w->task_blocks = blocks;
 
     /*sizeはdivision_sizeの倍数にしている。*/
-    int size = st_mmap.size;
-    char *file_mmap = st_mmap.file_mmap;
+    w->size = st_mmap.size;
+    w->file_mmap = st_mmap.file_mmap;
 
     /* 1task分のデータサイズ(byte) */
-    int division_size;
-    if (size >= 4096*4) {
-	division_size = 4096 * 4;/*16kbyte*/
+    if (w->size >= 4096*4) {
+	w->division_size = 4096 * 4;/*16kbyte*/
     }
     else {
-	division_size = size;
+	w->division_size = w->size;
     }
 
-    printf("dvision_size %d\n",division_size);
+    printf("dvision_size %d\n",w->division_size);
 
     /* "word num" and "line num" */
-    int status_num = 2;
+    w-> status_num = 2;
     /* taskの数 */
-    int task_num = size / division_size;
-    int out_task_num = task_num + (division_size*task_num < size);
+    w-> task_num = w->size / w->division_size;
+    int out_task_num = w->task_num + (w->division_size*w->task_num < w->size);
 
-    printf("task_num %d\n",task_num);
+    printf("task_num %d\n",w->task_num);
 
     /* out用のdivision_size. statusが2つなので、あわせて16byteになるように、long long(8byte)を使用 */
-    int division_out_size = 16;
+    w-> division_out_size = 16;
     /* out用のデータのサイズ。*/
-    int out_size = division_out_size*out_task_num;
-    unsigned long long *o_data = (unsigned long long*)manager->allocate(out_size);
+    int out_size = w->division_out_size*out_task_num;
+    w->o_data = (unsigned long long*)manager->allocate(out_size);
 
-    int pad = 2;
-    unsigned long long *head_tail_flag = 
-        (unsigned long long*)manager->allocate(out_size);
+    w-> pad = 2;
+    w->head_tail_flag = (unsigned long long*)manager->allocate(out_size);
 
     printf("out size %d\n",out_size);
 
     /*各SPEの結果を合計して出力するタスク*/
 
     t_print = manager->create_task(TASK_PRINT);
-    t_print->add_inData(o_data, out_size);
-    t_print->add_inData(head_tail_flag, out_size);
-    t_print->add_param(out_task_num);
-    t_print->add_param(status_num);
+    t_print->add_inData(w->o_data, out_size);
+    t_print->add_inData(w->head_tail_flag, out_size);
     t_print->add_param(out_task_num);
-    t_print->add_param(pad);
-
-    /*各SPEに処理するデータを割り振る。*/
-
-    /*渡すデータの最後が文字かどうか。(スペース、改行以外)*/
-
-    int i;
-    for (i = 0; i < task_num; i++) {
+    t_print->add_param(w->status_num);
+    t_print->add_param(out_task_num);
+    t_print->add_param(w->pad);
 
-	t_exec = manager->create_task(TASK_EXEC);
-	t_exec->add_inData(file_mmap + i*division_size, division_size);
-	t_exec->add_outData(o_data + i*status_num, division_out_size);
-	t_exec->add_outData(head_tail_flag + i*pad, division_out_size);
-	t_exec->add_param(division_size);
-	t_exec->set_cpu(SPE_ANY);
-	t_print->wait_for(t_exec);
-	t_exec->spawn();
+    w->t_print = t_print;
 
-	size -= division_size;
-
-    }
+    /* Task を task_blocks ずつ起動する Task */
 
-    if (size) {
+    HTaskPtr t_exec = manager->create_task(RUN_TASK_BLOCKS);
+    t_exec->set_param(0,(void*)w);
+    t_exec->spawn();
 
-	t_exec = manager->create_task(TASK_EXEC);
-	t_exec->add_inData(file_mmap + i*division_size, size);
-	t_exec->add_outData(o_data + i*status_num, division_out_size);
-	t_exec->add_outData(head_tail_flag + i*pad, division_out_size);
-	t_exec->add_param(size);
-	t_exec->set_cpu(SPE_ANY);
-	t_print->wait_for(t_exec);
-	t_exec->spawn();
-    }
-
+    t_print->wait_for(t_exec);
     t_print->spawn();
 }
 
-char*
+static char*
 init(int argc, char **argv)
 {
     
@@ -165,10 +200,12 @@
     for (int i = 1; argv[i]; ++i) {	
 	if (strcmp(argv[i], "-file") == 0) {
 	    filename = argv[i+1];
+	} else if (strcmp(argv[i], "-block") == 0) {
+	    blocks = atoi(argv[i+1]);
 	}
     }
     if (filename==0) {
-        printf("usage: %s [-count 10] -file filename\n",argv[0]);
+        printf("usage: %s [-block 10] -file filename\n",argv[0]);
 	exit(1);
     }
     
@@ -191,3 +228,5 @@
 
     return 0;
 }
+
+/* end */
--- a/example/word_count_test/spe/Exec.cc	Wed Dec 02 20:44:01 2009 +0900
+++ b/example/word_count_test/spe/Exec.cc	Wed Dec 02 22:20:32 2009 +0900
@@ -6,32 +6,41 @@
 /* これは必須 */
 SchedDefineTask(Exec);
 
+typedef char *cvector __attribute__ ((vector_size (16)));
+// typedef char *cvector;
+
 static int
 run(SchedTask *s, void *rbuf, void *wbuf)
 {
-    char *i_data = (char*)s->get_input(rbuf, 0);
+    cvector i_data = (cvector)s->get_input(rbuf, 0);
     unsigned long long *o_data = (unsigned long long*)s->get_output(wbuf, 0);
     /*担当範囲の先頭、末尾が「改行、スペース」か、「それ以外の文字」かのフラグ*/
     unsigned long long  *head_tail_flag = (unsigned long long*)s->get_output(wbuf,1);
     int length = (long)s->get_param(0);
+
+    static const char spaces[] = {0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20} ;
+    cvector const space = (cvector)spaces;
+    static const char newlines[] = {0x0a,0x0a,0x0a,0x0a,0x0a,0x0a,0x0a,0x0a,0x0a,0x0a,0x0a,0x0a,0x0a,0x0a,0x0a,0x0a};
+    cvector const newline = (cvector)newlines;
+
     int word_flag = 0;
     int word_num = 0;
     int line_num = 0;
-    int i;
+    int i = 0;
     
     /*文字なら1,スペースか改行なら0*/
-    head_tail_flag[0] = (i_data[0] != 0x20) && (i_data[0] != 0x0A);
+    head_tail_flag[0] = (i_data[0] != space[i%4]) && (i_data[0] != newline[i%4]);
 
 
     /*先頭の文字まで、飛ぶルーチン*/
     for(i = 0; i < length; i++) {
-	if ((i_data[i] != 0x20) && (i_data[i] != 0x0A)) {
+	if ((i_data[i] != space[i%4]) && (i_data[i] != newline[i%4])) {
 	    //s->printf("[SPE%d] break[%d] %d\n",id,i,i_data[i]);
 	    break;
 	}
 	//s->printf("[SPE%d]%c",id,i_data[start_num]);
 
-	line_num += (i_data[i] == 0x0A);
+	line_num += (i_data[i] == newline[i%4]);
     }
 
     for (; i < length; i++) {
@@ -39,12 +48,12 @@
 	    //s->printf("[SPE%d]%c",id,i_data[i]);
 
 
-	if (i_data[i] == 0x20) {
+	if (i_data[i] == space[i%4]) {
 	    //s->printf("スペース\n");
 	    word_flag = 1;
 	}
 
-	else if (i_data[i] == 0x0A) {
+	else if (i_data[i] == newline[i%4]) {
 	    //s->printf("改行\n");
 	    line_num += 1;
 	    word_flag = 1;
@@ -60,7 +69,7 @@
     word_num += word_flag;
     /*文字なら1,スペースか改行なら0*/
     //printf("last word %c",i_data[i-1]);
-    head_tail_flag[1] = (i_data[i-1] != 0x20) && (i_data[i-1] != 0x0A);
+    head_tail_flag[1] = (i_data[i-1] != space[i%4]) && (i_data[i-1] != newline[i%4]);
 
     //s->printf("%d %d\n",word_num,line_num);
 
--- a/example/word_count_test/spe/Makefile	Wed Dec 02 20:44:01 2009 +0900
+++ b/example/word_count_test/spe/Makefile	Wed Dec 02 22:20:32 2009 +0900
@@ -7,7 +7,7 @@
 OBJS = $(SRCS:.cc=.o)
 
 CC      = spu-g++
-CFLAGS  = -g -Wall -fno-exceptions -fno-rtti #-DDEBUG
+CFLAGS  = -g -Wall -fno-exceptions -fno-rtti -O9 #-DDEBUG
 INCLUDE = -I../${CERIUM}/include/TaskManager -I. -I..
 LIBS = -L../${CERIUM}/TaskManager -lspemanager
 
@@ -23,4 +23,4 @@
 
 clean:
 	rm -f $(TARGET) $(OBJS)
-	rm -f *~ \#*
\ No newline at end of file
+	rm -f *~ \#*