Mercurial > hg > Game > Cerium
annotate example/many_task/sort.cc @ 954:774eba654643 draft
auto_free
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 03 Aug 2010 15:32:49 +0900 |
parents | 9ed1c4a877ca |
children | b3644b73d2cf |
rev | line source |
---|---|
227 | 1 #include "TaskManager.h" |
573 | 2 #include "SchedTask.h" |
227 | 3 #include "sort.h" |
4 #include "Func.h" | |
934
83b64b7a51bd
sort fix ( not working now )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
932
diff
changeset
|
5 #include <string.h> |
227 | 6 |
945
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
941
diff
changeset
|
7 extern int get_split_num(int len, int num); |
940
e01b551f25d6
unknown dead lock still...
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
939
diff
changeset
|
8 extern int all; // allocate task at once |
227 | 9 |
10 /** | |
11 * 一つの block にある data の数が MAX_BLOCK_SIZE 超えないような | |
12 * len の分割数を返す | |
13 * | |
14 * @param len sort する data の総数 | |
15 * @param num 使用する SPE の数 | |
16 * | |
17 * @return data の分割数 | |
18 * | |
19 * TODO: | |
20 * len が num 以下とか考えてません | |
21 */ | |
945
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
941
diff
changeset
|
22 int |
227 | 23 get_split_num(int len, int num) |
24 { | |
25 if (len / num < MAX_BLOCK_SIZE) { | |
26 return num; | |
27 } else { | |
28 // 切り上げ | |
29 return (len + MAX_BLOCK_SIZE - 1) / MAX_BLOCK_SIZE; | |
30 } | |
31 } | |
32 | |
934
83b64b7a51bd
sort fix ( not working now )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
932
diff
changeset
|
33 |
227 | 34 /** |
35 * btask が全て終了したら、再び sort_start を実行する | |
36 * @param d 生成された btask の数 | |
37 */ | |
935 | 38 |
945
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
941
diff
changeset
|
39 SchedDefineTask1(SortSimple, sort_start ); |
935 | 40 |
41 static int | |
945
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
941
diff
changeset
|
42 sort_start(SchedTask *manager, void *d, void *e) |
227 | 43 { |
945
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
941
diff
changeset
|
44 Sort *s = (Sort*)manager->get_param(0); |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
941
diff
changeset
|
45 int half_num = s->split_num-1; |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
941
diff
changeset
|
46 static int sort_count = s->split_num; // sort 完了に必要な回数 |
934
83b64b7a51bd
sort fix ( not working now )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
932
diff
changeset
|
47 |
83b64b7a51bd
sort fix ( not working now )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
932
diff
changeset
|
48 // 一つのタスクで sort する data 数 |
945
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
941
diff
changeset
|
49 int block_num = (s->data_length + s->split_num -1)/s->split_num; |
934
83b64b7a51bd
sort fix ( not working now )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
932
diff
changeset
|
50 int half_block_num = block_num/2; |
83b64b7a51bd
sort fix ( not working now )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
932
diff
changeset
|
51 |
945
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
941
diff
changeset
|
52 int last_block_num = s->data_length - (s->split_num-1)*block_num; |
934
83b64b7a51bd
sort fix ( not working now )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
932
diff
changeset
|
53 int last_half_block_num = half_block_num+(last_block_num/2); |
83b64b7a51bd
sort fix ( not working now )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
932
diff
changeset
|
54 |
932
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
674
diff
changeset
|
55 if (--sort_count < 0) { |
945
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
941
diff
changeset
|
56 return 0; |
932
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
674
diff
changeset
|
57 } |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
674
diff
changeset
|
58 |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
674
diff
changeset
|
59 |
945
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
941
diff
changeset
|
60 for (int i = 0; i < s->split_num-1; i++) { |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
941
diff
changeset
|
61 s->fsort[i] = manager->create_task(QUICK_SORT, |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
941
diff
changeset
|
62 (memaddr)&s->data[i*block_num], sizeof(Data)*block_num, |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
941
diff
changeset
|
63 (memaddr)&s->data[i*block_num], sizeof(Data)*block_num); |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
941
diff
changeset
|
64 if (i>0 && s->bsort[i-1]) { |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
941
diff
changeset
|
65 s->fsort[i]->wait_for(s->bsort[i-1]); |
934
83b64b7a51bd
sort fix ( not working now )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
932
diff
changeset
|
66 } |
945
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
941
diff
changeset
|
67 if (i<s->split_num-2 && s->bsort[i]) { |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
941
diff
changeset
|
68 s->fsort[i]->wait_for(s->bsort[i]); |
934
83b64b7a51bd
sort fix ( not working now )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
932
diff
changeset
|
69 } |
945
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
941
diff
changeset
|
70 s->fsort[i]->set_cpu(SPE_ANY); |
932
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
674
diff
changeset
|
71 } |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
674
diff
changeset
|
72 |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
674
diff
changeset
|
73 // 最後の block は端数なので last_block_num を使う |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
674
diff
changeset
|
74 { |
945
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
941
diff
changeset
|
75 int i = s->split_num-1; |
932
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
674
diff
changeset
|
76 |
945
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
941
diff
changeset
|
77 s->fsort[i] = manager->create_task(QUICK_SORT, |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
941
diff
changeset
|
78 (memaddr)&s->data[i*block_num], sizeof(Data)*last_block_num, |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
941
diff
changeset
|
79 (memaddr)&s->data[i*block_num], sizeof(Data)*last_block_num); |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
941
diff
changeset
|
80 if (i>0 && s->bsort[i-1]) { |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
941
diff
changeset
|
81 s->fsort[i]->wait_for(s->bsort[i-1]); |
934
83b64b7a51bd
sort fix ( not working now )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
932
diff
changeset
|
82 } |
945
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
941
diff
changeset
|
83 s->fsort[i]->set_cpu(SPE_ANY); |
932
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
674
diff
changeset
|
84 } |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
674
diff
changeset
|
85 |
945
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
941
diff
changeset
|
86 if (s->split_num > 1) { |
932
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
674
diff
changeset
|
87 |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
674
diff
changeset
|
88 for (int i = 0; i < half_num-1; i++) { |
945
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
941
diff
changeset
|
89 if (s->bsort[i]) manager->free_htask(s->bsort[i]); |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
941
diff
changeset
|
90 s->bsort[i] = manager->create_task(QUICK_SORT, |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
941
diff
changeset
|
91 (memaddr)&s->data[i*block_num+half_block_num], sizeof(Data)*block_num, |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
941
diff
changeset
|
92 (memaddr)&s->data[i*block_num+half_block_num], sizeof(Data)*block_num); |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
941
diff
changeset
|
93 s->bsort[i]->set_cpu(SPE_ANY); |
932
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
674
diff
changeset
|
94 } |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
674
diff
changeset
|
95 |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
674
diff
changeset
|
96 { |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
674
diff
changeset
|
97 int i = half_num-1; |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
674
diff
changeset
|
98 |
945
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
941
diff
changeset
|
99 if (s->bsort[i]) manager->free_htask(s->bsort[i]); |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
941
diff
changeset
|
100 s->bsort[i] = manager->create_task(QUICK_SORT, |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
941
diff
changeset
|
101 (memaddr)&s->data[i*block_num+half_block_num], sizeof(Data)*last_half_block_num, |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
941
diff
changeset
|
102 (memaddr)&s->data[i*block_num+half_block_num], sizeof(Data)*last_half_block_num); |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
941
diff
changeset
|
103 s->bsort[i]->set_cpu(SPE_ANY); |
932
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
674
diff
changeset
|
104 } |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
674
diff
changeset
|
105 |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
674
diff
changeset
|
106 for (int i = 0; i < half_num; i++) { |
945
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
941
diff
changeset
|
107 s->bsort[i]->wait_for(s->fsort[i]); |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
941
diff
changeset
|
108 s->bsort[i]->wait_for(s->fsort[i+1]); |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
941
diff
changeset
|
109 s->bsort[i]->no_auto_free(); |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
941
diff
changeset
|
110 s->bsort[i]->spawn(); |
932
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
674
diff
changeset
|
111 } |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
674
diff
changeset
|
112 } |
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
674
diff
changeset
|
113 |
945
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
941
diff
changeset
|
114 HTaskPtr restart = manager->create_task(SortSimple,0,0,0,0); |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
941
diff
changeset
|
115 restart->set_param(0,(memaddr)s); |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
941
diff
changeset
|
116 if (!all) restart->wait_for(s->fsort[0]); |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
941
diff
changeset
|
117 for (int i = 0; i < s->split_num; i++) { |
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
941
diff
changeset
|
118 s->fsort[i]->spawn(); |
932
53ad3a61b40b
sort test (add swap())
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
674
diff
changeset
|
119 } |
954 | 120 if (sort_count == 1) { |
121 // last loop wait for all task | |
122 // we should not need this? | |
123 for (int i = 0; i < half_num; i++) { | |
124 restart->wait_for(s->bsort[i]); | |
125 s->bsort[i]->auto_free(); | |
126 } | |
127 } | |
935 | 128 restart->spawn(); |
945
9ed1c4a877ca
sort example fix ( simple task accepts one param and more compatible with old task)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
941
diff
changeset
|
129 return 0; |
934
83b64b7a51bd
sort fix ( not working now )
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
932
diff
changeset
|
130 } |
227 | 131 |
935 | 132 |
651 | 133 /* end */ |