Mercurial > hg > Game > Cerium
diff example/word_count_test/main.cc @ 663:ad4b6b556483 draft
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 | c253468da197 |
children | beb0f17c19f9 |
line wrap: on
line diff
--- 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 */