Mercurial > hg > Game > Cerium
annotate example/many_task/sort.cc @ 674:07351a5a51c9 draft
fix many task example (sort).
Dummy task is now system supported.
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Sun, 06 Dec 2009 00:54:10 +0900 |
parents | e78f70dc7ef8 |
children | 53ad3a61b40b |
rev | line source |
---|---|
227 | 1 #include "TaskManager.h" |
573 | 2 #include "SchedTask.h" |
227 | 3 #include "sort.h" |
4 #include "Func.h" | |
5 | |
6 DataPtr data; // sort array | |
7 int data_length; | |
8 static int sort_count; // sort 完了に必要な回数 | |
9 static int split_num; // data の分割数 | |
10 static int half_num; | |
11 static int block_num; // 一つのタスクで sort する data 数 | |
12 static int last_block_num; | |
13 static int half_block_num; | |
14 static int last_half_block_num; | |
15 | |
573 | 16 static void sort_restart(SchedTask *, void *, void *); |
674
07351a5a51c9
fix many task example (sort).
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
651
diff
changeset
|
17 static void sort_start(SchedTask *); |
227 | 18 |
19 /** | |
20 * 一つの block にある data の数が MAX_BLOCK_SIZE 超えないような | |
21 * len の分割数を返す | |
22 * | |
23 * @param len sort する data の総数 | |
24 * @param num 使用する SPE の数 | |
25 * | |
26 * @return data の分割数 | |
27 * | |
28 * TODO: | |
29 * len が num 以下とか考えてません | |
30 */ | |
31 static int | |
32 get_split_num(int len, int num) | |
33 { | |
34 if (len / num < MAX_BLOCK_SIZE) { | |
35 return num; | |
36 } else { | |
37 // 切り上げ | |
38 return (len + MAX_BLOCK_SIZE - 1) / MAX_BLOCK_SIZE; | |
39 } | |
40 } | |
41 | |
42 /** | |
43 * btask が全て終了したら、再び sort_start を実行する | |
44 * @param d 生成された btask の数 | |
45 */ | |
46 static void | |
573 | 47 sort_restart(SchedTask *s, void *d, void *e) |
227 | 48 { |
49 static int cnt = 0; | |
674
07351a5a51c9
fix many task example (sort).
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
651
diff
changeset
|
50 // static int ccc = 0; |
625
94d82f2c842f
64bit mode worked on Mac OS X.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
573
diff
changeset
|
51 long max = (long)d; |
227 | 52 |
53 if (++cnt == max) { | |
54 cnt = 0; | |
674
07351a5a51c9
fix many task example (sort).
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
651
diff
changeset
|
55 // printf("restarted %d\n",ccc++); |
07351a5a51c9
fix many task example (sort).
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
651
diff
changeset
|
56 sort_start(s); |
227 | 57 } |
58 } | |
59 | |
60 static void | |
674
07351a5a51c9
fix many task example (sort).
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
651
diff
changeset
|
61 sort_start(SchedTask *manager) |
227 | 62 { |
63 if (--sort_count < 0) { | |
64 return; | |
65 } | |
66 | |
67 HTaskPtr fsort[split_num]; | |
68 | |
69 for (int i = 0; i < split_num-1; i++) { | |
70 fsort[i] = manager->create_task(QUICK_SORT); | |
71 fsort[i]->add_inData(&data[i*block_num], sizeof(Data)*block_num); | |
72 fsort[i]->add_outData(&data[i*block_num], sizeof(Data)*block_num); | |
625
94d82f2c842f
64bit mode worked on Mac OS X.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
573
diff
changeset
|
73 fsort[i]->set_param(0,(memaddr)block_num); |
227 | 74 fsort[i]->set_cpu(SPE_ANY); |
75 } | |
76 | |
77 // 最後の block は端数なので last_block_num を使う | |
78 { | |
79 int i = split_num-1; | |
80 | |
81 fsort[i] = manager->create_task(QUICK_SORT); | |
82 fsort[i]->add_inData(&data[i*block_num], sizeof(Data)*last_block_num); | |
83 fsort[i]->add_outData(&data[i*block_num], sizeof(Data)*last_block_num); | |
625
94d82f2c842f
64bit mode worked on Mac OS X.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
573
diff
changeset
|
84 fsort[i]->set_param(0,(memaddr)last_block_num); |
227 | 85 fsort[i]->set_cpu(SPE_ANY); |
86 } | |
87 | |
88 if (split_num > 1) { | |
89 HTaskPtr bsort[half_num]; | |
90 | |
91 for (int i = 0; i < half_num-1; i++) { | |
92 bsort[i] = manager->create_task(QUICK_SORT); | |
93 bsort[i]->add_inData(&data[i*block_num+half_block_num], | |
94 sizeof(Data)*block_num); | |
95 bsort[i]->add_outData(&data[i*block_num+half_block_num], | |
96 sizeof(Data)*block_num); | |
625
94d82f2c842f
64bit mode worked on Mac OS X.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
573
diff
changeset
|
97 bsort[i]->set_param(0,(memaddr)block_num); |
227 | 98 bsort[i]->set_cpu(SPE_ANY); |
99 } | |
100 | |
101 { | |
102 int i = half_num-1; | |
103 | |
104 bsort[i] = manager->create_task(QUICK_SORT); | |
105 bsort[i]->add_inData(&data[i*block_num+half_block_num], | |
106 sizeof(Data)*last_half_block_num); | |
107 bsort[i]->add_outData(&data[i*block_num+half_block_num], | |
108 sizeof(Data)*last_half_block_num); | |
625
94d82f2c842f
64bit mode worked on Mac OS X.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
573
diff
changeset
|
109 bsort[i]->set_param(0,(memaddr)last_half_block_num); |
227 | 110 bsort[i]->set_cpu(SPE_ANY); |
111 } | |
112 | |
113 for (int i = 0; i < half_num; i++) { | |
114 bsort[i]->wait_for(fsort[i]); | |
115 bsort[i]->wait_for(fsort[i+1]); | |
674
07351a5a51c9
fix many task example (sort).
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
651
diff
changeset
|
116 bsort[i]->set_post(sort_restart, (void*)(half_num),(void*)manager); |
227 | 117 bsort[i]->spawn(); |
118 } | |
119 } | |
120 | |
121 for (int i = 0; i < split_num; i++) { | |
122 fsort[i]->spawn(); | |
123 } | |
124 } | |
125 | |
126 void | |
674
07351a5a51c9
fix many task example (sort).
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
651
diff
changeset
|
127 sort_init(SchedTask *manager, void *a, void *b) |
227 | 128 { |
674
07351a5a51c9
fix many task example (sort).
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
651
diff
changeset
|
129 int cpuNum = (int)a; |
07351a5a51c9
fix many task example (sort).
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
651
diff
changeset
|
130 int length = (int)b; |
400 | 131 |
227 | 132 data = (DataPtr)manager->allocate(sizeof(Data)*length); |
133 data_length = length; | |
134 | |
135 for (int i = 0; i < length; i++) { | |
230 | 136 data[i].index = manager->get_random()%10000; |
227 | 137 data[i].ptr = 0; |
138 } | |
139 | |
140 split_num = get_split_num(length, cpuNum); | |
141 half_num = split_num-1; | |
142 sort_count = split_num; | |
143 | |
144 block_num = (length + split_num -1)/split_num; | |
145 half_block_num = block_num/2; | |
146 | |
147 last_block_num = length - (split_num-1)*block_num; | |
148 last_half_block_num = half_block_num+(last_block_num/2); | |
149 | |
674
07351a5a51c9
fix many task example (sort).
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
651
diff
changeset
|
150 sort_start(manager); |
227 | 151 } |
400 | 152 |
651 | 153 /* end */ |