Mercurial > hg > Members > kono > Cerium
annotate TaskManager/Cell/SpeThreads.cc @ 611:2cfbb36e221a
cell fix memaddr
author | Shinji KONO <kono@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 10 Nov 2009 20:24:07 +0900 |
parents | 7a41d2cb0a84 |
children | 4cfcac2367d5 |
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 { | |
611 | 9 memaddr mail = 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 #if 0 | |
65 | 101 pthread_create(&threads[i], NULL, |
102 &frontend_thread_run, (void*)&args[i]); | |
109 | 103 #else |
104 pthread_create(&threads[i], NULL, | |
105 &spe_thread_run, (void*)&args[i]); | |
106 #endif | |
65 | 107 } |
108 } | |
109 | |
244 | 110 |
109 | 111 /** |
321 | 112 * SPE からのメールを受信する。 |
109 | 113 * |
114 * @param [speid] SPE ID | |
115 * | |
116 * @return Received 32-bit mailbox messages | |
117 * if ([ret] < 0) no data read | |
118 */ | |
604 | 119 memaddr |
244 | 120 SpeThreads::get_mail(int speid, int count, unsigned int* ret) |
121 { | |
604 | 122 memaddr mail = spe_out_mbox_read(spe_ctx[speid], ret, 1); |
123 if (sizeof(memaddr)>4) { | |
124 mail += spe_out_mbox_read(spe_ctx[speid], ret, 1)<<4; | |
125 } | |
126 return mail; | |
244 | 127 } |
128 | |
604 | 129 memaddr |
244 | 130 SpeThreads::check_mail(int speid, int count, unsigned int* ret) |
65 | 131 { |
321 | 132 /* |
133 * spe_out_mbox_status return only 1, waiting for multiple length | |
134 * does not work. | |
135 */ | |
604 | 136 if (spe_out_mbox_status(spe_ctx[speid]) >= 1) { |
137 memaddr mail = spe_out_mbox_read(spe_ctx[speid], ret, 1); | |
138 if (sizeof(memaddr)>4) { | |
139 mail += spe_out_mbox_read(spe_ctx[speid], ret, 1)<<4; | |
140 } | |
141 return mail; | |
244 | 142 } else { |
143 return 0; | |
144 } | |
65 | 145 } |
66 | 146 |
109 | 147 /** |
148 * Inbound Mailbox | |
321 | 149 * メール送信 PPE -> SPE |
109 | 150 * |
321 | 151 * なるべく NONBLOCKING なんだけど、 |
152 * Inbound Mailbox キューに空きがないと送信できないので | |
153 * 送信する数だけ空いているか確認してから送る。空いて無い場合は待つ。 | |
161
1f4c3f3238e6
texture の座標がマイナスになったあと、それを 0 にし忘れてた
gongo@localhost.localdomain
parents:
109
diff
changeset
|
154 * |
321 | 155 * 結局待つんだよな。しかも ALL_BLOCKING って実は busy wait だったりするし |
109 | 156 * |
157 * @param [speid] SPE ID | |
158 * @param [data] Send 32-bit mailbox messages | |
159 * @param [num] The number of messages | |
160 */ | |
66 | 161 void |
611 | 162 SpeThreads::send_mail(int speid, int num, memaddr *data1) |
244 | 163 { |
611 | 164 unsigned int *data = (unsigned int *) data1; |
244 | 165 spe_in_mbox_write(spe_ctx[speid], data, num, SPE_MBOX_ALL_BLOCKING); |
611 | 166 // if (sizeof(memaddr)>4) { |
167 // spe_in_mbox_write(spe_ctx[speid], (data>>4), num, SPE_MBOX_ALL_BLOCKING); | |
168 // } | |
244 | 169 } |
170 | |
171 void | |
253
1d8b8a4ac453
usr_help_str is nessesary for example
tkaito@localhost.localdomain
parents:
244
diff
changeset
|
172 SpeThreads::add_output_tasklist(int command, unsigned int buff, int alloc_size) |
66 | 173 { |
244 | 174 /* |
321 | 175 * output TaskList が無ければ新しく作る |
176 * あれば TaskList に allocate した Task を追加 | |
177 * command に対応した Task の初期化を実行する | |
178 * SPE に data が書き出し終わった後に PPE 側で初期化 | |
244 | 179 */ |
180 | |
66 | 181 } |
604 | 182 |
183 /* end */ |