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 {
|
|
9 unsigned int mail = MY_SPE_COMMAND_EXIT;
|
|
10 int ret;
|
|
11
|
|
12 for (int i = 0; i < spe_num; i++) {
|
109
|
13 send_mail(i, &mail, 1);
|
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 printf("[SPE %d] ", arg_t->speid);
|
|
47 switch(stop_info.stop_reason) {
|
|
48 case SPE_EXIT:
|
|
49 printf("SPE_EXIT stop_info.result.stop_exit_code=0x%x\n", stop_info.result.spe_exit_code);
|
|
50 break;
|
|
51 case SPE_STOP_AND_SIGNAL:
|
|
52 printf("SPE_STOP_AND_SIGNAL stop_info.result.stop_signal_code=%d\n", stop_info.result.spe_signal_code);
|
|
53 break;
|
|
54 case SPE_RUNTIME_ERROR:
|
|
55 printf("SPE_RUNTIME_ERROR stop_info.result.spe_runtime_error=%d\n", stop_info.result.spe_runtime_error);
|
|
56 break;
|
|
57 case SPE_RUNTIME_EXCEPTION:
|
|
58 printf("SPE_RUNTIME_EXCEPTION stop_info.result.spe_runtime_exception=%d\n", stop_info.result.spe_runtime_exception);
|
|
59 break;
|
|
60 }
|
65
|
61
|
|
62 pthread_exit(NULL);
|
|
63 }
|
|
64
|
|
65 void*
|
|
66 SpeThreads::frontend_thread_run(void *arg)
|
|
67 {
|
|
68 pthread_t thread;
|
|
69 thread_arg_t *arg_t = (thread_arg_t *)arg;
|
|
70
|
|
71 pthread_create(&thread, NULL, &spe_thread_run, (void*)arg_t->ctx);
|
|
72
|
76
|
73 // mail read の blocking ができれば
|
|
74 // ここで呼んだ方が早い。
|
|
75
|
65
|
76 pthread_exit(NULL);
|
|
77 }
|
|
78
|
|
79 void
|
|
80 SpeThreads::init(void)
|
|
81 {
|
109
|
82 spe_handle = spe_image_open(SPE_ELF);
|
65
|
83
|
109
|
84 if (spe_handle == NULL) {
|
|
85 perror("spe_image_open");
|
|
86 exit(EXIT_FAILURE);
|
|
87 }
|
65
|
88
|
|
89 spe_ctx = new spe_context_ptr_t[spe_num];
|
|
90 threads = new pthread_t[spe_num];
|
|
91 args = new thread_arg_t[spe_num];
|
|
92
|
109
|
93 for (int i = 0; i < spe_num; i++) {
|
65
|
94 args[i].speid = i;
|
|
95 spe_ctx[i] = spe_context_create(0, NULL);
|
|
96 spe_program_load(spe_ctx[i], spe_handle);
|
|
97 args[i].ctx = spe_ctx[i];
|
|
98 }
|
|
99
|
109
|
100 for (int i = 0; i < spe_num; i++) {
|
|
101 #if 0
|
65
|
102 pthread_create(&threads[i], NULL,
|
|
103 &frontend_thread_run, (void*)&args[i]);
|
109
|
104 #else
|
|
105 pthread_create(&threads[i], NULL,
|
|
106 //&spe_thread_run, (void*)spe_ctx[i]);
|
|
107 &spe_thread_run, (void*)&args[i]);
|
|
108 #endif
|
65
|
109 }
|
|
110 }
|
|
111
|
109
|
112 /**
|
|
113 * SPE からのメールを受信する。
|
|
114 *
|
|
115 * @param [speid] SPE ID
|
|
116 *
|
|
117 * @return Received 32-bit mailbox messages
|
|
118 * if ([ret] < 0) no data read
|
|
119 */
|
65
|
120 int
|
|
121 SpeThreads::get_mail(int speid)
|
|
122 {
|
109
|
123 unsigned int ret = (unsigned int)(-1);
|
65
|
124
|
109
|
125 spe_out_mbox_read(spe_ctx[speid], &ret, 1);
|
|
126 return ret;
|
65
|
127 }
|
66
|
128
|
109
|
129 /**
|
|
130 * Inbound Mailbox
|
|
131 * メール送信 PPE -> SPE
|
|
132 *
|
|
133 * なるべく NONBLOCKING なんだけど、
|
|
134 * Inbound Mailbox キューに空きがないと送信できないので
|
|
135 * 送信する数だけ空いているか確認してから送る。相手無い場合は待つ。
|
|
136 * 結局 BLOCKING よりたちの悪い busy_wait かよとか思ったり。
|
|
137 *
|
|
138 * @param [speid] SPE ID
|
|
139 * @param [data] Send 32-bit mailbox messages
|
|
140 * @param [num] The number of messages
|
|
141 */
|
66
|
142 void
|
109
|
143 SpeThreads::send_mail(int speid, unsigned int *data, int num)
|
66
|
144 {
|
109
|
145 while (spe_in_mbox_status(spe_ctx[speid]) < num);
|
|
146 spe_in_mbox_write(spe_ctx[speid], data, num, SPE_MBOX_ANY_NONBLOCKING);
|
66
|
147 }
|