Mercurial > hg > Game > Cerium
annotate TaskManager/Fifo/FifoTaskManagerImpl.cc @ 1222:d9f2e56bba86 draft
fix multicore for FifoTaskManager
author | Daichi TOMA <e085740@ie.u-ryukyu.ac.jp> |
---|---|
date | Sat, 23 Jul 2011 09:37:03 +0900 |
parents | 26dea600d4cd |
children | 17508bef43be |
rev | line source |
---|---|
3 | 1 #include <stdio.h> |
2 #include <stdlib.h> | |
3 #include <string.h> | |
4 #include "FifoTaskManagerImpl.h" | |
1179 | 5 #include "CellTaskManagerImpl.h" |
955 | 6 #include "QueueInfo.h" |
7 #include "TaskList.h" | |
373 | 8 #include "Scheduler.h" |
619 | 9 #include "SchedTask.h" |
47 | 10 #include "types.h" |
109 | 11 #include "error.h" |
690 | 12 #include "SchedNop.h" |
703 | 13 #include "SysFunc.h" |
3 | 14 |
955 | 15 extern QueueInfo<TaskList> *taskListPool; |
16 | |
1078 | 17 static void send_alloc_reply(FifoTaskManagerImpl *tm, int id, MainScheduler *s); |
721 | 18 |
475
4e0308d2ba73
BufferManager removed.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
373
diff
changeset
|
19 FifoTaskManagerImpl::~FifoTaskManagerImpl() |
109 | 20 { |
640
ecf056ddd21a
SimpeTask on Cell worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
638
diff
changeset
|
21 delete mainScheduler; |
475
4e0308d2ba73
BufferManager removed.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
373
diff
changeset
|
22 |
4e0308d2ba73
BufferManager removed.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
373
diff
changeset
|
23 delete taskQueueImpl ; |
4e0308d2ba73
BufferManager removed.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
373
diff
changeset
|
24 delete htaskImpl ; |
4e0308d2ba73
BufferManager removed.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
373
diff
changeset
|
25 |
109 | 26 } |
27 | |
28 /** | |
853 | 29 * FifoTaskManager 単独で走るときの初期化 |
321 | 30 * MailManager は PPE スケジューラとのメール交換、 |
31 * FifoScheduler は PPE 側のスケジューラ | |
109 | 32 */ |
3 | 33 void |
1057
8cd123d2f3ca
debug_check_spu_idle add. commandline option [-spuidle].
tkaito@henri
parents:
972
diff
changeset
|
34 FifoTaskManagerImpl::init(int spuIdle_) |
3 | 35 { |
719
cc1b7333de92
clean up scheduler main loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
713
diff
changeset
|
36 // TaskManager から呼ばれるので、かなりの部分は初期化されている。 |
cc1b7333de92
clean up scheduler main loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
713
diff
changeset
|
37 |
640
ecf056ddd21a
SimpeTask on Cell worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
638
diff
changeset
|
38 mainScheduler = new MainScheduler(); |
ecf056ddd21a
SimpeTask on Cell worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
638
diff
changeset
|
39 mainScheduler->init(this); |
ecf056ddd21a
SimpeTask on Cell worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
638
diff
changeset
|
40 mainScheduler->id = 0; |
ecf056ddd21a
SimpeTask on Cell worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
638
diff
changeset
|
41 set_scheduler(mainScheduler); |
42 | 42 |
955 | 43 taskListInfo = new QueueInfo<TaskList>(taskListPool); |
44 ppeTaskList = new QueueInfo<TaskList>(taskListPool); | |
475
4e0308d2ba73
BufferManager removed.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
373
diff
changeset
|
45 |
619 | 46 schedTaskManager = new SchedTask(); |
835 | 47 others = 0; |
972 | 48 schedTaskManager->init(0,0,0,mainScheduler,0); |
619 | 49 |
109 | 50 } |
51 | |
52 /** | |
321 | 53 * これは CellTaskManagerImpl から呼ばれる。 |
475
4e0308d2ba73
BufferManager removed.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
373
diff
changeset
|
54 * TaskList等 は共用で使うので引数に。 |
321 | 55 * CellTaskManagerImpl と FifoTaskManagerImpl が同時に |
373 | 56 * 上のデータにアクセスする事は(今は)ないのでこれでおk |
109 | 57 */ |
58 void | |
475
4e0308d2ba73
BufferManager removed.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
373
diff
changeset
|
59 FifoTaskManagerImpl::init(MainScheduler *_sched, TaskManagerImpl *tm) |
109 | 60 { |
61 | |
640
ecf056ddd21a
SimpeTask on Cell worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
638
diff
changeset
|
62 mainScheduler = _sched; |
ecf056ddd21a
SimpeTask on Cell worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
638
diff
changeset
|
63 mainScheduler->init(this); |
ecf056ddd21a
SimpeTask on Cell worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
638
diff
changeset
|
64 set_scheduler(mainScheduler); |
109 | 65 |
475
4e0308d2ba73
BufferManager removed.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
373
diff
changeset
|
66 taskQueueImpl = tm-> taskQueueImpl ; |
4e0308d2ba73
BufferManager removed.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
373
diff
changeset
|
67 htaskImpl = tm-> htaskImpl ; |
719
cc1b7333de92
clean up scheduler main loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
713
diff
changeset
|
68 waitTaskQueue = tm->waitTaskQueue; |
1077 | 69 activeTaskQueue = tm->activeTaskQueue; |
109 | 70 |
955 | 71 taskListInfo = new QueueInfo<TaskList>(taskListPool); |
72 ppeTaskList = new QueueInfo<TaskList>(taskListPool); | |
475
4e0308d2ba73
BufferManager removed.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
373
diff
changeset
|
73 |
936
178fbcc81fda
dead lock on spu/ppu mail
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
853
diff
changeset
|
74 // schedTaskManager = new SchedTask(); |
835 | 75 others = tm; |
936
178fbcc81fda
dead lock on spu/ppu mail
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
853
diff
changeset
|
76 // schedTaskManager->init(0,0,0,mainScheduler); |
479
bf2d2625485e
Double Linked List base TaskQueue
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
475
diff
changeset
|
77 |
109 | 78 } |
79 | |
721 | 80 // void FifoTaskManagerImpl::show_profile() {} |
81 // void FifoTaskManagerImpl::start_profile() {} | |
672 | 82 |
109 | 83 /** |
321 | 84 * スケジューラに渡す TaskList を取得する。 |
109 | 85 * |
321 | 86 * @return 実行タスクリスト |
109 | 87 * |
321 | 88 * ActiveTaskQueue (依存条件は満たし済み) のタスクを |
89 * 実行タスクリストに入れる | |
1079 | 90 * CPU_PPE |
109 | 91 */ |
808 | 92 void |
1077 | 93 FifoTaskManagerImpl::set_runTaskList(QueueInfo<HTask> *activeTaskQueue) |
109 | 94 { |
499 | 95 // printf("active task queue length = %d\n",activeTaskQueue->length()); |
1077 | 96 HTaskPtr htask = activeTaskQueue->getFirst(); |
97 while (htask != NULL) { | |
98 if (htask->cpu_type == CPU_PPE) { | |
806 | 99 set_taskList(htask, taskListInfo ); |
1077 | 100 HTaskPtr next = activeTaskQueue->getNext(htask); |
101 activeTaskQueue->remove(htask); | |
102 htask = next; | |
103 } else { | |
104 htask = activeTaskQueue->getNext(htask); | |
105 } | |
109 | 106 } |
107 } | |
108 | |
1079 | 109 /** |
110 * exec all cpu type as simulation | |
111 */ | |
112 void | |
113 FifoTaskManagerImpl::set_runTaskList1(QueueInfo<HTask> *activeTaskQueue) | |
114 { | |
115 // printf("active task queue length = %d\n",activeTaskQueue->length()); | |
116 HTaskPtr htask = activeTaskQueue->getFirst(); | |
117 while (htask != NULL) { | |
118 set_taskList(htask, taskListInfo ); | |
119 HTaskPtr next = activeTaskQueue->getNext(htask); | |
120 activeTaskQueue->remove(htask); | |
121 htask = next; | |
122 } | |
123 } | |
124 | |
1081 | 125 /** |
126 * called from CellTaskManagerImpl | |
127 */ | |
808 | 128 void |
129 FifoTaskManagerImpl::poll() | |
130 { | |
1081 | 131 set_runTaskList(activeTaskQueue); |
808 | 132 // list を実行する |
133 sendTaskList(); | |
134 // ppe scheduler からの mail を調べる | |
135 mail_check(); | |
136 } | |
109 | 137 |
1081 | 138 /** |
139 * single CPU run | |
140 */ | |
109 | 141 void |
1082 | 142 FifoTaskManagerImpl::poll1() |
143 { | |
144 set_runTaskList1(activeTaskQueue); | |
145 // list を実行する | |
146 sendTaskList(); | |
147 // ppe scheduler からの mail を調べる | |
148 mail_check(); | |
149 } | |
150 | |
151 /** | |
152 * single CPU run | |
153 */ | |
154 void | |
475
4e0308d2ba73
BufferManager removed.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
373
diff
changeset
|
155 FifoTaskManagerImpl::run() |
109 | 156 { |
808 | 157 do { |
1082 | 158 poll1(); |
809 | 159 } while(!activeTaskQueue->empty()) ; |
109 | 160 |
721 | 161 if (!waitTaskQueue->empty()) { |
736 | 162 get_scheduler()->printf("Dead lock detected\n"); |
721 | 163 } |
109 | 164 } |
165 | |
166 /** | |
321 | 167 * @param [list] 実行タスクリスト |
168 * @return FifoScheduler からのメール | |
109 | 169 * |
321 | 170 * [Tasklist] -> [番兵] -> scheduler->run を抜ける |
109 | 171 */ |
647
7ba4ad4538b1
MailManager rewrite. not yet worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
644
diff
changeset
|
172 void |
808 | 173 FifoTaskManagerImpl::sendTaskList() |
109 | 174 { |
808 | 175 if (taskListInfo->empty()) return; |
853 | 176 // ppeTaskList は走り終わった ppe の Task の List. |
177 // taskListInfo はこれから走る Task の List. | |
178 // 交換して実行する | |
955 | 179 QueueInfo<TaskList>* tmp = ppeTaskList; |
808 | 180 ppeTaskList = taskListInfo; |
181 taskListInfo = tmp; | |
853 | 182 // ppeTaskList は本来は循環リストなのだけど、実行中は線形リストである。 |
183 // なので、最後に 0 を代入する. 後でなおす。 | |
809 | 184 ppeTaskList->getLast()->next = 0; |
721 | 185 // TaskList のアドレスを送る |
808 | 186 mainScheduler->mail_write_from_host((memaddr)ppeTaskList->getFirst()); |
109 | 187 |
721 | 188 // EXIT_COMMAND (番兵的な意味で) |
189 // これを読むと、mainScheduler->run() から抜けて来る。 | |
190 mainScheduler->mail_write_from_host((memaddr)MY_SPE_COMMAND_EXIT); | |
373 | 191 |
721 | 192 // scheduler は受け取ったメールを元に実行する |
193 mainScheduler->run(new SchedNop()); | |
194 // すべてのlistを実行するまで戻らない | |
853 | 195 |
196 ppeTaskList->getLast()->next = ppeTaskList; | |
197 | |
109 | 198 } |
199 | |
200 /** | |
321 | 201 * PPE Scheduler からのメールをチェックする |
109 | 202 * |
203 * @param [mail_list] | |
321 | 204 * PPE 側で動く Scheduler からのメールリスト |
205 * 終了した Task や、その他(今はまだ実装してないけど)の情報が入ってる | |
109 | 206 * |
373 | 207 * @return Scheduler が次に実行する Task List |
321 | 208 * NULL なら全てのタスクが実行終了したということ |
109 | 209 */ |
210 void | |
719
cc1b7333de92
clean up scheduler main loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
713
diff
changeset
|
211 FifoTaskManagerImpl::mail_check() |
485 | 212 { |
647
7ba4ad4538b1
MailManager rewrite. not yet worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
644
diff
changeset
|
213 while (mainScheduler->has_mail_from_host()) { |
7ba4ad4538b1
MailManager rewrite. not yet worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
644
diff
changeset
|
214 memaddr data = mainScheduler->mail_read_from_host(); |
109 | 215 |
625
94d82f2c842f
64bit mode worked on Mac OS X.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
619
diff
changeset
|
216 if (data == (memaddr)MY_SPE_STATUS_READY) { |
373 | 217 __debug_ppe("mail_check(): Task List finish\n"); |
808 | 218 ppeTaskList->freeAll(); |
640
ecf056ddd21a
SimpeTask on Cell worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
638
diff
changeset
|
219 } else if (data == (memaddr)MY_SPE_COMMAND_EXIT) { |
ecf056ddd21a
SimpeTask on Cell worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
638
diff
changeset
|
220 __debug_ppe("mail_check(): Task List finish COMMAND\n"); |
721 | 221 } else if (data == (memaddr)MY_SPE_COMMAND_MALLOC) { |
1078 | 222 send_alloc_reply(this, 0, mainScheduler); |
625
94d82f2c842f
64bit mode worked on Mac OS X.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
619
diff
changeset
|
223 } else if (data != (memaddr)MY_SPE_NOP) { |
373 | 224 __debug_ppe("mail_check(): recv from 0x%x\n", data); |
619 | 225 // post_func を先に実行しないと、systask_finish が active_queue |
226 // 移されてから、wait_for されるという事態が起きることがある。 | |
830 | 227 #ifdef TASK_LIST_MAIL |
228 TaskListPtr list = (TaskListPtr)data; | |
229 check_task_list_finish(schedTaskManager, list, waitTaskQueue); | |
230 #else | |
619 | 231 HTaskPtr task = (HTaskPtr)data; |
1151 | 232 #if 0 |
1150 | 233 if (task->cpu_type != CPU_PPE) { |
234 const char *name = get_task_name(task); | |
235 if (name != NULL) { | |
236 printf("[PPE] "); | |
237 printf("Task id : %d, ", task->command); | |
238 printf("Task name : %s\n", name); | |
239 } | |
240 } | |
241 #endif | |
1147 | 242 #ifndef NOT_CHECK |
243 | |
244 if (task != NULL) { | |
245 //PPE で処理された Task が返ってくるはず。 | |
246 if (task->cpu_type != CPU_PPE) { | |
247 printf("attention : SPE task run on PPE\n"); | |
248 printf("Task id : %d\n", task->command); | |
249 const char *name = get_task_name(task); | |
250 if (name != NULL) { | |
251 printf("Task name : %s\n", name); | |
252 } | |
253 } | |
254 } | |
255 | |
256 #endif | |
257 | |
619 | 258 task->post_func(schedTaskManager, task->post_arg1, task->post_arg2); |
719
cc1b7333de92
clean up scheduler main loop
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
713
diff
changeset
|
259 check_task_finish(task, waitTaskQueue); |
830 | 260 #endif |
373 | 261 } |
109 | 262 } |
3 | 263 } |
264 | |
833
577bde5d0cec
poling (may recurse..)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
830
diff
changeset
|
265 void |
577bde5d0cec
poling (may recurse..)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
830
diff
changeset
|
266 FifoTaskManagerImpl::polling() |
577bde5d0cec
poling (may recurse..)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
830
diff
changeset
|
267 { |
577bde5d0cec
poling (may recurse..)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
830
diff
changeset
|
268 if (others!=0) |
577bde5d0cec
poling (may recurse..)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
830
diff
changeset
|
269 others->polling(); |
577bde5d0cec
poling (may recurse..)
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
830
diff
changeset
|
270 } |
373 | 271 |
1078 | 272 static void |
273 send_alloc_reply(FifoTaskManagerImpl *tm, int id, MainScheduler *s) | |
274 { | |
275 | |
276 /** | |
277 * info[0] = alloc_id; (CellScheduler::mainMem_alloc 参照) | |
278 * info[1] = alloc_addr; | |
279 */ | |
280 memaddr alloc_info[2]; | |
281 long alloc_size; | |
282 long command; | |
283 | |
284 alloc_info[0] = s->mail_read_from_host(); | |
285 alloc_info[1] = s->mail_read_from_host(); | |
286 command = (long)alloc_info[0]; | |
287 alloc_size = (long)alloc_info[1]; | |
288 | |
289 | |
290 alloc_info[1] = (memaddr)tm->allocate(alloc_size); | |
291 //__debug_ppe("[PPE] MALLOCED 0x%lx from [SPE %d]\n", alloc_info[1],id); | |
292 // 今のところ何もしてない。どうも、この allocate を free | |
293 // するのは、SPE task が返した値を見て行うらしい。それは、 | |
294 // 忘れやすいのではないか? | |
295 // s->add_output_tasklist(command, alloc_info[1], alloc_size); | |
296 | |
297 s->mail_write_from_host(alloc_info[0]); | |
298 s->mail_write_from_host(alloc_info[1]); | |
299 } | |
300 | |
301 | |
1067 | 302 void |
303 FifoTaskManagerImpl::print_arch() | |
304 { | |
305 printf("FifoTaskManagerImpl\n"); | |
306 } | |
307 | |
109 | 308 /** |
475
4e0308d2ba73
BufferManager removed.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
373
diff
changeset
|
309 * # # # # # # # # |
109 | 310 * Abstract Factory Pattern |
475
4e0308d2ba73
BufferManager removed.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
373
diff
changeset
|
311 * # # # # # # # |
109 | 312 */ |
313 #ifdef __CERIUM_FIFO__ | |
373 | 314 TaskManagerImpl* |
3 | 315 create_impl(int num) |
316 { | |
1222
d9f2e56bba86
fix multicore for FifoTaskManager
Daichi TOMA <e085740@ie.u-ryukyu.ac.jp>
parents:
1185
diff
changeset
|
317 #ifdef __CERIUM_PARALLEL__ |
d9f2e56bba86
fix multicore for FifoTaskManager
Daichi TOMA <e085740@ie.u-ryukyu.ac.jp>
parents:
1185
diff
changeset
|
318 //マルチコアverでコンパイルしたのにかかわらず、 |
d9f2e56bba86
fix multicore for FifoTaskManager
Daichi TOMA <e085740@ie.u-ryukyu.ac.jp>
parents:
1185
diff
changeset
|
319 //CPU数が0だと、FifoTaskManagerが呼ばれてしまうので |
d9f2e56bba86
fix multicore for FifoTaskManager
Daichi TOMA <e085740@ie.u-ryukyu.ac.jp>
parents:
1185
diff
changeset
|
320 //0の場合は1を入れて、CellTaskManagerが呼ばれるようにする。 |
d9f2e56bba86
fix multicore for FifoTaskManager
Daichi TOMA <e085740@ie.u-ryukyu.ac.jp>
parents:
1185
diff
changeset
|
321 if (num == 0) num = 1; |
d9f2e56bba86
fix multicore for FifoTaskManager
Daichi TOMA <e085740@ie.u-ryukyu.ac.jp>
parents:
1185
diff
changeset
|
322 #endif |
d9f2e56bba86
fix multicore for FifoTaskManager
Daichi TOMA <e085740@ie.u-ryukyu.ac.jp>
parents:
1185
diff
changeset
|
323 |
1179 | 324 if (num == 0) { |
1147 | 325 return new FifoTaskManagerImpl(num); |
1179 | 326 } else { |
1185
26dea600d4cd
fix CellTaskManagerImpl
Daichi TOMA <amothic@cr.ie.u-ryukyu.ac.jp>
parents:
1179
diff
changeset
|
327 Threads *cpus = new CpuThreads(num); |
1179 | 328 cpus->init(); |
329 return new CellTaskManagerImpl(num,cpus); | |
330 } | |
3 | 331 } |
109 | 332 #endif // __CERIUM_FIFO__ |
475
4e0308d2ba73
BufferManager removed.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
373
diff
changeset
|
333 |