Mercurial > hg > Game > Cerium
diff example/many_task/sort.cc @ 945:9ed1c4a877ca draft
sort example fix ( simple task accepts one param and more compatible with old task)
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 01 Aug 2010 19:29:27 +0900 |
parents | fc6cfaae6de7 |
children | 774eba654643 |
line wrap: on
line diff
--- a/example/many_task/sort.cc Sat Jul 31 21:40:30 2010 +0900 +++ b/example/many_task/sort.cc Sun Aug 01 19:29:27 2010 +0900 @@ -4,14 +4,9 @@ #include "Func.h" #include <string.h> -extern void check_data(); +extern int get_split_num(int len, int num); extern int all; // allocate task at once -static void sort_start(SchedTask *); -static int data_length; -static int cpuNum; -static int split_num; - /** * 一つの block にある data の数が MAX_BLOCK_SIZE 超えないような * len の分割数を返す @@ -24,7 +19,7 @@ * TODO: * len が num 以下とか考えてません */ -static int +int get_split_num(int len, int num) { if (len / num < MAX_BLOCK_SIZE) { @@ -36,235 +31,95 @@ } -static HTaskPtr *fsort; -static HTaskPtr *bsort; - -static DataPtr data; - /** * btask が全て終了したら、再び sort_start を実行する * @param d 生成された btask の数 */ -SchedDefineTask1(RESTART, sort_restart ); +SchedDefineTask1(SortSimple, sort_start ); static int -sort_restart(SchedTask *s, void *d, void *e) +sort_start(SchedTask *manager, void *d, void *e) { - // static int ccc = 0; - - // printf("restarted %d %% %d\n",ccc++,split_num); - sort_start(s); - return 0; -} -#ifdef USE_SIMPLE_TASK - -static void -sort_start(SchedTask *manager) -{ - int half_num = split_num-1; - static int sort_count = split_num; // sort 完了に必要な回数 + Sort *s = (Sort*)manager->get_param(0); + int half_num = s->split_num-1; + static int sort_count = s->split_num; // sort 完了に必要な回数 // 一つのタスクで sort する data 数 - int block_num = (data_length + split_num -1)/split_num; + int block_num = (s->data_length + s->split_num -1)/s->split_num; int half_block_num = block_num/2; - int last_block_num = data_length - (split_num-1)*block_num; + int last_block_num = s->data_length - (s->split_num-1)*block_num; int last_half_block_num = half_block_num+(last_block_num/2); if (--sort_count < 0) { - return; + return 0; } - for (int i = 0; i < split_num-1; i++) { - fsort[i] = manager->create_task(QUICK_SORT, - (memaddr)&data[i*block_num], sizeof(Data)*block_num, - (memaddr)&data[i*block_num], sizeof(Data)*block_num); - if (i>0 && bsort[i-1]) { - fsort[i]->wait_for(bsort[i-1]); + for (int i = 0; i < s->split_num-1; i++) { + s->fsort[i] = manager->create_task(QUICK_SORT, + (memaddr)&s->data[i*block_num], sizeof(Data)*block_num, + (memaddr)&s->data[i*block_num], sizeof(Data)*block_num); + if (i>0 && s->bsort[i-1]) { + s->fsort[i]->wait_for(s->bsort[i-1]); } - if (i<split_num-2 && bsort[i]) { - fsort[i]->wait_for(bsort[i]); + if (i<s->split_num-2 && s->bsort[i]) { + s->fsort[i]->wait_for(s->bsort[i]); } - fsort[i]->set_cpu(SPE_ANY); + s->fsort[i]->set_cpu(SPE_ANY); } // 最後の block は端数なので last_block_num を使う { - int i = split_num-1; + int i = s->split_num-1; - fsort[i] = manager->create_task(QUICK_SORT, - (memaddr)&data[i*block_num], sizeof(Data)*last_block_num, - (memaddr)&data[i*block_num], sizeof(Data)*last_block_num); - if (i>0 && bsort[i-1]) { - fsort[i]->wait_for(bsort[i-1]); + s->fsort[i] = manager->create_task(QUICK_SORT, + (memaddr)&s->data[i*block_num], sizeof(Data)*last_block_num, + (memaddr)&s->data[i*block_num], sizeof(Data)*last_block_num); + if (i>0 && s->bsort[i-1]) { + s->fsort[i]->wait_for(s->bsort[i-1]); } - fsort[i]->set_cpu(SPE_ANY); + s->fsort[i]->set_cpu(SPE_ANY); } - if (split_num > 1) { + if (s->split_num > 1) { for (int i = 0; i < half_num-1; i++) { - if (bsort[i]) manager->free_htask(bsort[i]); - bsort[i] = manager->create_task(QUICK_SORT, - (memaddr)&data[i*block_num+half_block_num], sizeof(Data)*block_num, - (memaddr)&data[i*block_num+half_block_num], sizeof(Data)*block_num); - bsort[i]->set_cpu(SPE_ANY); + if (s->bsort[i]) manager->free_htask(s->bsort[i]); + s->bsort[i] = manager->create_task(QUICK_SORT, + (memaddr)&s->data[i*block_num+half_block_num], sizeof(Data)*block_num, + (memaddr)&s->data[i*block_num+half_block_num], sizeof(Data)*block_num); + s->bsort[i]->set_cpu(SPE_ANY); } { int i = half_num-1; - if (bsort[i]) manager->free_htask(bsort[i]); - bsort[i] = manager->create_task(QUICK_SORT, - (memaddr)&data[i*block_num+half_block_num], sizeof(Data)*last_half_block_num, - (memaddr)&data[i*block_num+half_block_num], sizeof(Data)*last_half_block_num); - bsort[i]->set_cpu(SPE_ANY); + if (s->bsort[i]) manager->free_htask(s->bsort[i]); + s->bsort[i] = manager->create_task(QUICK_SORT, + (memaddr)&s->data[i*block_num+half_block_num], sizeof(Data)*last_half_block_num, + (memaddr)&s->data[i*block_num+half_block_num], sizeof(Data)*last_half_block_num); + s->bsort[i]->set_cpu(SPE_ANY); } for (int i = 0; i < half_num; i++) { - bsort[i]->wait_for(fsort[i]); - bsort[i]->wait_for(fsort[i+1]); - bsort[i]->no_auto_free(); - bsort[i]->spawn(); + s->bsort[i]->wait_for(s->fsort[i]); + s->bsort[i]->wait_for(s->fsort[i+1]); + s->bsort[i]->no_auto_free(); + s->bsort[i]->spawn(); } } - HTaskPtr restart = manager->create_task(RESTART,0,0,0,0); - for (int i = 0; i < split_num; i++) { - if (!all) restart->wait_for(fsort[i]); - fsort[i]->spawn(); + HTaskPtr restart = manager->create_task(SortSimple,0,0,0,0); + restart->set_param(0,(memaddr)s); + if (!all) restart->wait_for(s->fsort[0]); + for (int i = 0; i < s->split_num; i++) { + s->fsort[i]->spawn(); } restart->spawn(); -} - -#else - -static void -sort_start(SchedTask *manager) -{ - int half_num = split_num-1; - static int sort_count = split_num; // sort 完了に必要な回数 - - // 一つのタスクで sort する data 数 - int block_num = (data_length + split_num -1)/split_num; - int half_block_num = block_num/2; - - int last_block_num = data_length - (split_num-1)*block_num; - int last_half_block_num = half_block_num+(last_block_num/2); - - - if (--sort_count < 0) { - return; - } - - - for (int i = 0; i < split_num-1; i++) { - fsort[i] = manager->create_task(QUICK_SORT); - fsort[i]->add_inData(&data[i*block_num], sizeof(Data)*block_num); - fsort[i]->add_outData(&data[i*block_num], sizeof(Data)*block_num); - fsort[i]->set_param(0,(memaddr)block_num); - if (i>0 && bsort[i-1]) { - fsort[i]->wait_for(bsort[i-1]); - } - if (i<split_num-2 && bsort[i]) { - fsort[i]->wait_for(bsort[i]); - } - fsort[i]->set_cpu(SPE_ANY); - } - - // 最後の block は端数なので last_block_num を使う - { - int i = split_num-1; - - fsort[i] = manager->create_task(QUICK_SORT); - fsort[i]->add_inData(&data[i*block_num], sizeof(Data)*last_block_num); - fsort[i]->add_outData(&data[i*block_num], sizeof(Data)*last_block_num); - fsort[i]->set_param(0,(memaddr)last_block_num); - if (i>0 && bsort[i-1]) { - fsort[i]->wait_for(bsort[i-1]); - } - fsort[i]->set_cpu(SPE_ANY); - } - - if (split_num > 1) { - - for (int i = 0; i < half_num-1; i++) { - if (bsort[i]) manager->free_htask(bsort[i]); - bsort[i] = manager->create_task(QUICK_SORT); - bsort[i]->add_inData(&data[i*block_num+half_block_num], - sizeof(Data)*block_num); - bsort[i]->add_outData(&data[i*block_num+half_block_num], - sizeof(Data)*block_num); - bsort[i]->set_param(0,(memaddr)block_num); - bsort[i]->set_cpu(SPE_ANY); - } - - { - int i = half_num-1; - - if (bsort[i]) manager->free_htask(bsort[i]); - bsort[i] = manager->create_task(QUICK_SORT); - bsort[i]->add_inData(&data[i*block_num+half_block_num], - sizeof(Data)*last_half_block_num); - bsort[i]->add_outData(&data[i*block_num+half_block_num], - sizeof(Data)*last_half_block_num); - bsort[i]->set_param(0,(memaddr)last_half_block_num); - bsort[i]->set_cpu(SPE_ANY); - } - - for (int i = 0; i < half_num; i++) { - bsort[i]->wait_for(fsort[i]); - bsort[i]->wait_for(fsort[i+1]); - bsort[i]->no_auto_free(); - bsort[i]->spawn(); - } - } - - HTaskPtr restart = manager->create_task(RESTART,0,0,0,0); - for (int i = 0; i < split_num; i++) { - if (!all) restart->wait_for(fsort[i]); - fsort[i]->spawn(); - } - restart->spawn(); -} - -#endif - -void check_data() -{ - for(int i=0; i< data_length-1;i++) { - if (data[i].index>data[i+1].index) { - printf("Data are not sorted at %d. %d > %d \n",i, data[i].index,data[i+1].index); - return; - } - } - printf("Data are sorted\n"); + return 0; } -void -sort_init(SchedTask *manager, void *a, void *b) -{ - cpuNum = (int)a; - int length = (int)b; - - data = (DataPtr)manager->allocate(sizeof(Data)*length); - data_length = length; - - split_num = get_split_num(data_length, cpuNum); // data の分割数 - int half_num = split_num-1; - fsort = (HTaskPtr*)manager->allocate(sizeof(HTaskPtr)*split_num); - bsort = (HTaskPtr*)manager->allocate(sizeof(HTaskPtr)*half_num); - memset((void*)bsort,0, sizeof(HTaskPtr)*half_num); - - for (int i = 0; i < length; i++) { - data[i].index = manager->get_random()%10000; - data[i].ptr = i; - } - - sort_start(manager); -} - /* end */