Mercurial > hg > Members > kono > Cerium
annotate TaskManager/Cell/SpeThreads.cc @ 647:7c9ded1ea750
MailManager rewrite. not yet worked.
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Sat, 21 Nov 2009 16:18:47 +0900 |
parents | 8843edf37c0e |
children | 31d0a5baafdf |
rev | line source |
---|---|
109 | 1 #include <stdlib.h> |
76 | 2 #include "types.h" |
65 | 3 #include "SpeThreads.h" |
4 | |
5 SpeThreads::SpeThreads(int num) : spe_num(num) {} | |
6 | |
76 | 7 SpeThreads::~SpeThreads(void) |
8 { | |
629
8843edf37c0e
Cell 64 bit tried, but not yet worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
621
diff
changeset
|
9 memaddr mail = (memaddr)MY_SPE_COMMAND_EXIT; |
76 | 10 int ret; |
11 | |
12 for (int i = 0; i < spe_num; i++) { | |
244 | 13 send_mail(i, 1, &mail); |
76 | 14 } |
15 | |
16 for (int i = 0; i < spe_num; i++) { | |
17 pthread_join(threads[i], NULL); | |
18 ret = spe_context_destroy(spe_ctx[i]); | |
19 if (ret) { | |
20 perror("[~SpeThreads] spe_context_destroy"); | |
21 } | |
22 } | |
23 | |
24 spe_image_close(spe_handle); | |
25 | |
26 delete [] spe_ctx; | |
27 delete [] threads; | |
28 delete [] args; | |
29 } | |
30 | |
65 | 31 void* |
32 SpeThreads::spe_thread_run(void *arg) | |
33 { | |
34 unsigned int entry = SPE_DEFAULT_ENTRY; | |
109 | 35 //spe_context_ptr_t ctx = (spe_context_ptr_t)arg; |
36 thread_arg_t *arg_t = (thread_arg_t *)arg; | |
37 | |
38 spe_stop_info_t stop_info; | |
39 unsigned long long status; | |
40 | |
41 spe_context_run(arg_t->ctx, &entry, 0, (void*)arg_t->speid, NULL, &stop_info); | |
65 | 42 |
109 | 43 status = ((stop_info.result.spe_exit_code & 0xff) << 8) |
44 | (stop_info.result.spe_signal_code & 0xff); | |
45 | |
46 switch(stop_info.stop_reason) { | |
47 case SPE_EXIT: | |
194 | 48 //printf("SPE_EXIT stop_info.result.stop_exit_code=0x%x\n", stop_info.result.spe_exit_code); |
109 | 49 break; |
50 case SPE_STOP_AND_SIGNAL: | |
194 | 51 printf("[SPE %d] SPE_STOP_AND_SIGNAL stop_info.result.stop_signal_code=%d\n", arg_t->speid, stop_info.result.spe_signal_code); |
109 | 52 break; |
53 case SPE_RUNTIME_ERROR: | |
194 | 54 printf("[SPE %d] SPE_RUNTIME_ERROR stop_info.result.spe_runtime_error=%d\n", arg_t->speid, stop_info.result.spe_runtime_error); |
109 | 55 break; |
56 case SPE_RUNTIME_EXCEPTION: | |
194 | 57 printf("[SPE %d] SPE_RUNTIME_EXCEPTION stop_info.result.spe_runtime_exception=%d\n", arg_t->speid, stop_info.result.spe_runtime_exception); |
109 | 58 break; |
59 } | |
65 | 60 |
61 pthread_exit(NULL); | |
62 } | |
63 | |
64 void* | |
65 SpeThreads::frontend_thread_run(void *arg) | |
66 { | |
67 pthread_t thread; | |
68 thread_arg_t *arg_t = (thread_arg_t *)arg; | |
69 | |
70 pthread_create(&thread, NULL, &spe_thread_run, (void*)arg_t->ctx); | |
71 | |
321 | 72 // mail read の blocking ができれば |
73 // ここで呼んだ方が早い。 | |
76 | 74 |
65 | 75 pthread_exit(NULL); |
76 } | |
77 | |
78 void | |
79 SpeThreads::init(void) | |
80 { | |
109 | 81 spe_handle = spe_image_open(SPE_ELF); |
65 | 82 |
109 | 83 if (spe_handle == NULL) { |
84 perror("spe_image_open"); | |
85 exit(EXIT_FAILURE); | |
86 } | |
65 | 87 |
88 spe_ctx = new spe_context_ptr_t[spe_num]; | |
89 threads = new pthread_t[spe_num]; | |
90 args = new thread_arg_t[spe_num]; | |
91 | |
109 | 92 for (int i = 0; i < spe_num; i++) { |
65 | 93 args[i].speid = i; |
94 spe_ctx[i] = spe_context_create(0, NULL); | |
95 spe_program_load(spe_ctx[i], spe_handle); | |
96 args[i].ctx = spe_ctx[i]; | |
97 } | |
98 | |
109 | 99 for (int i = 0; i < spe_num; i++) { |
100 pthread_create(&threads[i], NULL, | |
101 &spe_thread_run, (void*)&args[i]); | |
65 | 102 } |
103 } | |
104 | |
244 | 105 |
109 | 106 /** |
321 | 107 * SPE からのメールを受信する。 |
109 | 108 * |
109 * @param [speid] SPE ID | |
110 * | |
111 * @return Received 32-bit mailbox messages | |
112 * if ([ret] < 0) no data read | |
113 */ | |
621 | 114 int |
629
8843edf37c0e
Cell 64 bit tried, but not yet worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
621
diff
changeset
|
115 SpeThreads::get_mail(int speid, int count, memaddr *ret) |
244 | 116 { |
629
8843edf37c0e
Cell 64 bit tried, but not yet worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
621
diff
changeset
|
117 // only used in CellTaskManagerImpl (should be removed?) |
8843edf37c0e
Cell 64 bit tried, but not yet worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
621
diff
changeset
|
118 return spe_out_mbox_read(spe_ctx[speid], (unsigned int*)ret, count*(sizeof(memaddr)/sizeof(int))); |
244 | 119 } |
120 | |
621 | 121 int |
647
7c9ded1ea750
MailManager rewrite. not yet worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
629
diff
changeset
|
122 SpeThreads::has_mail(int speid, int count, memaddr *ret) |
65 | 123 { |
321 | 124 /* |
125 * spe_out_mbox_status return only 1, waiting for multiple length | |
126 * does not work. | |
127 */ | |
604 | 128 if (spe_out_mbox_status(spe_ctx[speid]) >= 1) { |
629
8843edf37c0e
Cell 64 bit tried, but not yet worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
621
diff
changeset
|
129 return spe_out_mbox_read(spe_ctx[speid], (unsigned int*)ret, count*(sizeof(memaddr)/sizeof(int))); |
244 | 130 } else { |
131 return 0; | |
132 } | |
65 | 133 } |
66 | 134 |
109 | 135 /** |
136 * Inbound Mailbox | |
321 | 137 * メール送信 PPE -> SPE |
109 | 138 * |
321 | 139 * なるべく NONBLOCKING なんだけど、 |
140 * Inbound Mailbox キューに空きがないと送信できないので | |
141 * 送信する数だけ空いているか確認してから送る。空いて無い場合は待つ。 | |
161
1f4c3f3238e6
texture の座標がマイナスになったあと、それを 0 にし忘れてた
gongo@localhost.localdomain
parents:
109
diff
changeset
|
142 * |
321 | 143 * 結局待つんだよな。しかも ALL_BLOCKING って実は busy wait だったりするし |
109 | 144 * |
145 * @param [speid] SPE ID | |
146 * @param [data] Send 32-bit mailbox messages | |
147 * @param [num] The number of messages | |
148 */ | |
66 | 149 void |
629
8843edf37c0e
Cell 64 bit tried, but not yet worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
621
diff
changeset
|
150 SpeThreads::send_mail(int speid, int num, memaddr *data) |
617 | 151 |
244 | 152 { |
629
8843edf37c0e
Cell 64 bit tried, but not yet worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
621
diff
changeset
|
153 spe_in_mbox_write(spe_ctx[speid], (unsigned int *)data, num*(sizeof(memaddr)/sizeof(int)), SPE_MBOX_ALL_BLOCKING); |
244 | 154 } |
155 | |
156 void | |
629
8843edf37c0e
Cell 64 bit tried, but not yet worked.
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
621
diff
changeset
|
157 SpeThreads::add_output_tasklist(int command, memaddr buff, int alloc_size) |
66 | 158 { |
244 | 159 /* |
321 | 160 * output TaskList が無ければ新しく作る |
161 * あれば TaskList に allocate した Task を追加 | |
162 * command に対応した Task の初期化を実行する | |
163 * SPE に data が書き出し終わった後に PPE 側で初期化 | |
244 | 164 */ |
165 | |
66 | 166 } |
604 | 167 |
168 /* end */ |