Mercurial > hg > Members > kono > Cerium
annotate TaskManager/Cell/CellTaskManagerImpl.cc @ 637:5530fa36d42e
not yet worked...
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 19 Nov 2009 11:13:42 +0900 |
parents | 8cc609285bbe |
children | 671fca057ad3 |
rev | line source |
---|---|
321 | 1 #define DEBUG |
2 #include "error.h" | |
57 | 3 #include <stdio.h> |
4 #include <stdlib.h> | |
5 #include <string.h> | |
6 #include "CellTaskManagerImpl.h" | |
476
926d6aff886c
CellBufferManager removed
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
380
diff
changeset
|
7 #include "CellTaskListInfo.h" |
501
4a2c9ff53605
Cell inData/outData DMA removal
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
496
diff
changeset
|
8 #include "HTaskInfo.h" |
621 | 9 #include "SchedTask.h" |
635
8cc609285bbe
SimpleTask worked on Mac OS X
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
629
diff
changeset
|
10 #include "MainScheduler.h" |
57 | 11 #include "types.h" |
12 | |
501
4a2c9ff53605
Cell inData/outData DMA removal
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
496
diff
changeset
|
13 CellTaskManagerImpl::~CellTaskManagerImpl() |
67 | 14 { |
109 | 15 delete speThreads; |
16 delete [] speTaskList; | |
17 delete [] speTaskList_bg; | |
18 /** | |
321 | 19 * bufferManager は |
20 * ppeManager のなかで delete してもらう | |
109 | 21 */ |
22 // delete bufferManager; | |
23 delete [] flag_sendTaskList; | |
24 | |
25 delete ppeManager; | |
67 | 26 } |
27 | |
57 | 28 void |
501
4a2c9ff53605
Cell inData/outData DMA removal
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
496
diff
changeset
|
29 CellTaskManagerImpl::init() |
57 | 30 { |
637 | 31 spe_running = 0; |
476
926d6aff886c
CellBufferManager removed
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
380
diff
changeset
|
32 taskListImpl = new CellTaskListInfo; |
926d6aff886c
CellBufferManager removed
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
380
diff
changeset
|
33 taskListImpl->init(machineNum*2); |
926d6aff886c
CellBufferManager removed
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
380
diff
changeset
|
34 |
501
4a2c9ff53605
Cell inData/outData DMA removal
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
496
diff
changeset
|
35 activeTaskQueue = new HTaskInfo(); |
481
4896dffad67c
Double linked list modification done (tested on Mac OS X)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
479
diff
changeset
|
36 |
501
4a2c9ff53605
Cell inData/outData DMA removal
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
496
diff
changeset
|
37 htaskImpl = activeTaskQueue ; // any HTaskInfo |
65 | 38 |
39 speThreads = new SpeThreads(machineNum); | |
40 speThreads->init(); | |
109 | 41 |
42 speTaskList = new TaskListPtr[machineNum]; | |
43 speTaskList_bg = new TaskListPtr[machineNum]; | |
476
926d6aff886c
CellBufferManager removed
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
380
diff
changeset
|
44 |
109 | 45 for (int i = 0; i < machineNum; i++) { |
476
926d6aff886c
CellBufferManager removed
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
380
diff
changeset
|
46 speTaskList[i] = taskListImpl->create(); |
926d6aff886c
CellBufferManager removed
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
380
diff
changeset
|
47 speTaskList_bg[i] = taskListImpl->create(); |
109 | 48 } |
49 | |
50 flag_sendTaskList = new int[machineNum]; | |
51 for (int i = 0; i < machineNum; i++) { | |
52 flag_sendTaskList[i] = 1; | |
476
926d6aff886c
CellBufferManager removed
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
380
diff
changeset
|
53 } |
321 | 54 // PPE 側の管理をする Manager |
109 | 55 ppeManager = new FifoTaskManagerImpl(machineNum); |
479
5bda98b0b56d
Double Linked List base TaskQueue
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
476
diff
changeset
|
56 // 大半のTaskQueueInfoは、共有される |
637 | 57 MainScheduler *mscheduler = new MainScheduler; |
58 set_scheduler((Scheduler*)mscheduler); | |
59 ppeManager->init(mscheduler, this); | |
619 | 60 |
61 ppeManager->get_scheduler()->set_manager(this); | |
637 | 62 |
619 | 63 schedTaskManager = new SchedTask(); |
64 schedTaskManager->init(0,0,0,ppeManager->get_scheduler()); | |
57 | 65 } |
66 | |
109 | 67 void |
501
4a2c9ff53605
Cell inData/outData DMA removal
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
496
diff
changeset
|
68 CellTaskManagerImpl::append_activeTask(HTaskPtr task) |
109 | 69 { |
70 if (task->cpu_type == CPU_PPE) { | |
501
4a2c9ff53605
Cell inData/outData DMA removal
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
496
diff
changeset
|
71 ppeManager->append_activeTask(task); |
109 | 72 } else { |
501
4a2c9ff53605
Cell inData/outData DMA removal
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
496
diff
changeset
|
73 activeTaskQueue->addLast(task); |
109 | 74 } |
75 } | |
76 | |
321 | 77 // SPE_ANY が指定されていた時に |
78 // これをインクリメントしつつ呼ぶことにする。 | |
79 // 乱数使ってもいいけどさ。 | |
109 | 80 int cur_anySpeid = 0; |
81 | |
82 /** | |
321 | 83 * ActiveTaskQueue から Task を |
84 * 各 SPE に渡す TaskList に入れる | |
109 | 85 * |
321 | 86 * ここの activeTaskQueue は FifoTaskManagerImpl のと意味が違い、 |
87 * spe に渡される Task だけ入っている | |
109 | 88 */ |
89 void | |
501
4a2c9ff53605
Cell inData/outData DMA removal
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
496
diff
changeset
|
90 CellTaskManagerImpl::set_runTaskList() |
70 | 91 { |
321 | 92 // ここ...直すかな |
70 | 93 TaskListPtr list; |
481
4896dffad67c
Double linked list modification done (tested on Mac OS X)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
479
diff
changeset
|
94 |
70 | 95 TaskPtr task; |
109 | 96 int speid; |
70 | 97 |
501
4a2c9ff53605
Cell inData/outData DMA removal
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
496
diff
changeset
|
98 while (HTaskPtr htask = activeTaskQueue->poll()) { |
70 | 99 |
109 | 100 if (htask->cpu_type == SPE_ANY) { |
101 speid = cur_anySpeid++; | |
102 cur_anySpeid = (cur_anySpeid < machineNum) | |
103 ? cur_anySpeid : 0; | |
70 | 104 } else { |
321 | 105 // -1 してるのは |
106 // htask->cpu_type - CPU_SPE で | |
107 // SPE0 = 1, SPE1 = 2, ... SPE5 = 6 ってなってるので | |
108 // 配列的 (SPE0 = arr[0], SPE1 = arr[1]) にするため | |
109 | 109 speid = htask->cpu_type - CPU_SPE - 1; |
110 | |
321 | 111 // SPU の数以上が指定されていれば |
112 // とりあえず MAX_USE_SPE_NUM (実際に動く SPE の最大数) で | |
113 // あまり求めてそれを使うことにする。 | |
114 // ここで判定するもんでもないか? | |
109 | 115 if (speid >= machineNum) { |
116 speid %= machineNum; | |
117 } | |
70 | 118 } |
109 | 119 |
120 list = speTaskList_bg[speid]; | |
121 | |
122 if (list->length >= TASK_MAX_SIZE) { | |
476
926d6aff886c
CellBufferManager removed
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
380
diff
changeset
|
123 TaskListPtr newList = taskListImpl->create(); |
109 | 124 newList = TaskListInfo::append(newList, speTaskList_bg[speid]); |
125 speTaskList_bg[speid] = newList; | |
126 list = newList; | |
127 } | |
128 | |
129 task = &list->tasks[list->length++]; | |
629
8843edf37c0e
Cell 64 bit tried, but not yet worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
621
diff
changeset
|
130 TaskPtr stask = (TaskPtr) task; |
8843edf37c0e
Cell 64 bit tried, but not yet worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
621
diff
changeset
|
131 *stask = *(TaskPtr) htask; |
70 | 132 } |
109 | 133 } |
134 | |
135 void | |
501
4a2c9ff53605
Cell inData/outData DMA removal
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
496
diff
changeset
|
136 CellTaskManagerImpl::run() |
109 | 137 { |
138 TaskListPtr ppeTaskList = NULL; | |
139 MailQueuePtr ppeMail = NULL; | |
70 | 140 |
321 | 141 // PPE 側で動く TaskList です |
142 // FifoTaskManagerImpl::run と上手く合うように | |
143 // こんなことやってますよ | |
637 | 144 // |
145 // 本来は、別 thread で動かすべきだろう... | |
109 | 146 ppeTaskList = ppeManager->get_runTaskList(); |
147 if (!ppeTaskList) { | |
148 goto cont; | |
149 } | |
70 | 150 |
321 | 151 // SPE からの Mailbox Check は |
152 // PPE 側の schedule から抜けて来たときに行う | |
153 // (speThreads で Blocking Mailbox read と | |
154 // セマフォとか使ってやってもいいが、今はこの方式で) | |
637 | 155 // |
156 // すべてのspe task が finish task を待つ場合は、ppeTaskList の | |
157 // 判定だけで十分だが、そうでない場合は、spe の task が残っているか | |
158 // どうかを調べる必要がある。 | |
159 // | |
109 | 160 do { |
161 ppeMail = ppeManager->schedule(ppeTaskList); | |
162 cont: | |
163 ppeTaskList = mail_check(ppeMail); | |
637 | 164 } while (ppeTaskList || spe_running >0); |
70 | 165 } |
166 | |
65 | 167 /** |
321 | 168 * SPE からのメールをチェックする |
109 | 169 * |
170 * @param [mail_list] | |
321 | 171 * PPE 側で動く Scheduler からのメールリスト |
172 * この中で PPE 側の mail check も行う | |
109 | 173 * |
321 | 174 * @return PPE Scheduler に対してのメール。 |
175 * 次に実行する TaskList のアドレスや、終了コマンドを送る | |
109 | 176 */TaskListPtr |
65 | 177 CellTaskManagerImpl::mail_check(MailQueuePtr mail_list) |
57 | 178 { |
321 | 179 // PPE Scheduler からの mail check |
485 | 180 ppeManager->mail_check(mail_list, waitTaskQueue); |
65 | 181 |
70 | 182 do { |
629
8843edf37c0e
Cell 64 bit tried, but not yet worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
621
diff
changeset
|
183 memaddr data; |
109 | 184 |
321 | 185 // SPE Scheduler からの mail check |
244 | 186 for (int id = 0; id < machineNum; id++) { |
187 while (speThreads->check_mail(id, 1, &data)) { | |
109 | 188 /** |
321 | 189 * MY_SPE_STATUS_READY: SPE が持ってた Task 全て終了 |
190 * MY_SPE_NOP: 特に意味のないコマンド | |
191 * それ以外:終了したタスク(PPEにあるのでアドレス | |
109 | 192 * |
321 | 193 * MY_SPE_NOP が 0 なので、 |
194 * 下のように data > MY_SPE_NOP とかしています。 | |
195 * 一目でよくわからない書き方なんで、直したいところですが。。。 | |
109 | 196 */ |
321 | 197 // 名前あとでちゃんと決めよう => MY_SPE_... とかじゃなくて |
629
8843edf37c0e
Cell 64 bit tried, but not yet worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
621
diff
changeset
|
198 if (data == (memaddr)MY_SPE_STATUS_READY) { |
334
20f2459041cb
[in test_render] push L key , call show_dma_wait, but incomplete.
e065746@localhost.localdomain
parents:
321
diff
changeset
|
199 //__debug_ppe("[SPE %d] finish\n", id); |
109 | 200 flag_sendTaskList[id] = 1; |
637 | 201 spe_running--; |
629
8843edf37c0e
Cell 64 bit tried, but not yet worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
621
diff
changeset
|
202 } else if (data == (memaddr)MY_SPE_COMMAND_MALLOC) { |
334
20f2459041cb
[in test_render] push L key , call show_dma_wait, but incomplete.
e065746@localhost.localdomain
parents:
321
diff
changeset
|
203 //__debug_ppe("[PPE] MALLOC COMMAND from [SPE %d]\n", id); |
109 | 204 |
205 /** | |
321 | 206 * info[0] = alloc_id; (CellScheduler::mainMem_alloc 参照) |
109 | 207 * info[1] = alloc_addr; |
208 */ | |
629
8843edf37c0e
Cell 64 bit tried, but not yet worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
621
diff
changeset
|
209 memaddr alloc_info[2]; |
8843edf37c0e
Cell 64 bit tried, but not yet worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
621
diff
changeset
|
210 long alloc_size; |
8843edf37c0e
Cell 64 bit tried, but not yet worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
621
diff
changeset
|
211 long command; |
109 | 212 |
244 | 213 speThreads->get_mail(id, 2, alloc_info); |
629
8843edf37c0e
Cell 64 bit tried, but not yet worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
621
diff
changeset
|
214 command = (long)alloc_info[0]; |
8843edf37c0e
Cell 64 bit tried, but not yet worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
621
diff
changeset
|
215 alloc_size = (long)alloc_info[1]; |
244 | 216 |
109 | 217 |
629
8843edf37c0e
Cell 64 bit tried, but not yet worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
621
diff
changeset
|
218 alloc_info[1] = (memaddr)allocate(alloc_size); |
244 | 219 /* |
321 | 220 * allocate された領域は今の SPE buffer にリンクとして接続する |
221 * ここでは TaskList を allocate(new) して登録してやろうか | |
244 | 222 */ |
109 | 223 |
629
8843edf37c0e
Cell 64 bit tried, but not yet worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
621
diff
changeset
|
224 //__debug_ppe("[PPE] MALLOCED 0x%lx from [SPE %d]\n", alloc_info[1],id); |
8843edf37c0e
Cell 64 bit tried, but not yet worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
621
diff
changeset
|
225 // 今のところ何もしてない。どうも、この allocate を free |
8843edf37c0e
Cell 64 bit tried, but not yet worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
621
diff
changeset
|
226 // するのは、SPE task が返した値を見て行うらしい。それは、 |
8843edf37c0e
Cell 64 bit tried, but not yet worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
621
diff
changeset
|
227 // 忘れやすいのではないか? |
300
0691f38195f6
remove TaskManager/Fifo/MainScheduler.o TaskManeger/Test/test_render/test_nogl
tkaito@localhost.localdomain
parents:
298
diff
changeset
|
228 speThreads->add_output_tasklist(command, alloc_info[1], alloc_size); |
273 | 229 |
621 | 230 speThreads->send_mail(id, 2, alloc_info); |
629
8843edf37c0e
Cell 64 bit tried, but not yet worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
621
diff
changeset
|
231 } else if (data > (memaddr)MY_SPE_NOP) { |
334
20f2459041cb
[in test_render] push L key , call show_dma_wait, but incomplete.
e065746@localhost.localdomain
parents:
321
diff
changeset
|
232 //__debug_ppe("[PPE] recv from [SPE %d] : 0x%x\n", id, data); |
619 | 233 HTaskPtr task = (HTaskPtr)data; |
234 task->post_func(schedTaskManager, task->post_arg1, task->post_arg2); | |
235 check_task_finish(task); | |
70 | 236 } |
65 | 237 } |
238 } | |
109 | 239 |
321 | 240 // 依存関係を満たしたタスクをアクティブに |
109 | 241 wakeup_waitTask(); |
242 | |
321 | 243 // SPE に送る TaskList の準備 |
109 | 244 set_runTaskList(); |
245 | |
321 | 246 // TaskList 待ちの SPE に TaskList を送る |
109 | 247 for (int i = 0; i < machineNum; i++) { |
248 if (flag_sendTaskList[i] == 1 && speTaskList_bg[i]->length >= 1 ) { | |
249 send_taskList(i); | |
637 | 250 spe_running++; |
109 | 251 } |
252 } | |
70 | 253 |
321 | 254 // 現在の FifoTaskManager の仕様では |
255 // ・PPE で実行するタスクが無くなれば終了する | |
256 // であり、この場合もし SPE にタスクが残っていても | |
257 // メインループから抜けてプログラム終了となってしまうので | |
258 // ここでストップかけてます。 | |
481
4896dffad67c
Double linked list modification done (tested on Mac OS X)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
479
diff
changeset
|
259 } while (ppeManager->activeTaskQueue->empty() && !waitTaskQueue->empty()); |
109 | 260 |
261 return ppeManager->get_runTaskList(); | |
262 } | |
66 | 263 |
109 | 264 /** |
321 | 265 * 条件を満たしたら SPE に TaskList を送信する |
266 * 条件1. SPE が持ってた TaskList を終了して、次の TaskList を待ってる | |
267 * 条件2. SPE に送る TaskList に Task がある | |
109 | 268 * |
321 | 269 * SPE で実行終了した TaskList [speTaskList] と |
270 * これから実行する TaskList [speTaskList_bg] のバッファを入れ替える | |
271 * ついでに実行終了したやつは clear しておく。 | |
109 | 272 */ |
273 void | |
274 CellTaskManagerImpl::send_taskList(int id) | |
275 { | |
276 TaskListPtr tmp; | |
277 | |
278 tmp = speTaskList[id]; | |
279 speTaskList[id] = speTaskList_bg[id]; | |
280 speTaskList_bg[id] = tmp; | |
281 | |
476
926d6aff886c
CellBufferManager removed
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
380
diff
changeset
|
282 taskListImpl->clear_taskList(speTaskList_bg[id]); |
109 | 283 |
629
8843edf37c0e
Cell 64 bit tried, but not yet worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
621
diff
changeset
|
284 speThreads->send_mail(id, 1, (memaddr *)&speTaskList[id]); |
109 | 285 flag_sendTaskList[id] = 0; |
57 | 286 } |
287 | |
637 | 288 #if 0 |
109 | 289 void* |
506 | 290 CellTaskManagerImpl::allocate(int size, int alignment) |
291 { | |
635
8cc609285bbe
SimpleTask worked on Mac OS X
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
629
diff
changeset
|
292 if (size==0) return 0; |
506 | 293 void *buff; |
294 posix_memalign(&buff, alignment, size); | |
295 return buff; | |
296 } | |
297 | |
298 void* | |
109 | 299 CellTaskManagerImpl::allocate(int size) |
300 { | |
635
8cc609285bbe
SimpleTask worked on Mac OS X
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
629
diff
changeset
|
301 if (size==0) return 0; |
109 | 302 void *buff; |
303 | |
304 posix_memalign(&buff, DEFAULT_ALIGNMENT, size); | |
244 | 305 |
321 | 306 // bzero はコストが高いのでやりたくない |
506 | 307 // bzero(buff, size); |
109 | 308 |
309 return buff; | |
310 } | |
311 | |
380 | 312 Scheduler* |
313 CellTaskManagerImpl::get_scheduler() | |
314 { | |
315 return ppeManager->get_scheduler(); | |
316 } | |
637 | 317 #endif |
380 | 318 |
109 | 319 #ifdef __CERIUM_CELL__ |
57 | 320 TaskManagerImpl* |
321 create_impl(int num) | |
322 { | |
67 | 323 return new CellTaskManagerImpl(num); |
57 | 324 } |
109 | 325 #endif // __CERIUM_CELL |