86
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
1 #include <stdio.h>
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
2 #include <unistd.h>
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
3
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
4 #include "context.h"
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
5 #include "origin_cs.h"
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
6
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
7 extern __code initContext(struct Context* context);
|
90
|
8 extern void allocator(struct Context* context);
|
86
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
9
|
90
|
10 int length;
|
|
11 int* array_ptr;
|
|
12
|
|
13 void print_queue(struct Element* element) {
|
|
14 while (element) {
|
|
15 printf("%d\n", element->task->key);
|
|
16 element = element->next;
|
|
17 }
|
|
18 }
|
|
19
|
|
20 void print_tree(struct Node* node) {
|
|
21 if (node != 0) {
|
|
22 printf("%d\n", node->value->array.index);
|
|
23 print_tree(node->left);
|
|
24 print_tree(node->right);
|
|
25 }
|
|
26 }
|
86
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
27
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
28 __code code1(struct Context* context) {
|
91
|
29 puts("queue");
|
|
30 print_queue(context->data[ActiveQueue]->queue.first);
|
|
31 puts("tree");
|
|
32 print_tree(context->data[Tree]->tree.root);
|
90
|
33
|
|
34 goto meta(context, Exit);
|
86
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
35 }
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
36
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
37 __code code1_stub(struct Context* context) {
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
38 goto code1(context);
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
39 }
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
40
|
90
|
41 __code createData1(struct Context* context, struct Allocate* allocate, struct LoopCounter* loopCounter) {
|
|
42 int i = loopCounter->i;
|
|
43
|
|
44 if (i < length) {
|
|
45 allocate->size = sizeof(struct Array);
|
|
46 allocator(context);
|
|
47
|
|
48 goto meta(context, CreateData2);
|
|
49 }
|
|
50
|
|
51 goto meta(context, Code1);
|
|
52 }
|
|
53
|
|
54 __code createData1_stub(struct Context* context) {
|
|
55 goto createData1(context, &context->data[Allocate]->allocate, &context->data[LoopCounter]->loopCounter);
|
|
56 }
|
|
57
|
|
58 __code createData2(struct Context* context, struct LoopCounter* loopCounter, struct Array* array, struct Node* node) {
|
|
59 int i = loopCounter->i;
|
|
60
|
|
61 array->index = i;
|
|
62 array->array = array_ptr;
|
|
63
|
|
64 node->key = i;
|
|
65 node->value = (union Data*)array;
|
|
66
|
|
67 context->next = CreateTask1;
|
|
68
|
|
69 goto meta(context, PutTree);
|
|
70 }
|
|
71
|
|
72 __code createData2_stub(struct Context* context) {
|
|
73 goto createData2(context,
|
|
74 &context->data[LoopCounter]->loopCounter,
|
|
75 &context->data[context->dataNum]->array,
|
|
76 &context->data[Node]->node);
|
|
77 }
|
|
78
|
|
79 __code createTask1(struct Context* context, struct Allocate* allocate) {
|
|
80 allocate->size = sizeof(struct Task);
|
|
81 allocator(context);
|
|
82
|
|
83 goto meta(context, CreateTask2);
|
|
84 }
|
|
85
|
|
86 __code createTask1_stub(struct Context* context) {
|
|
87 goto createTask1(context, &context->data[Allocate]->allocate);
|
|
88 }
|
|
89
|
|
90 __code createTask2(struct Context* context, struct LoopCounter* loopCounter, struct Task* task, struct Element* element) {
|
|
91 int i = loopCounter->i;
|
|
92
|
|
93 task->code = Code1;
|
|
94 task->key = i;
|
|
95
|
|
96 element->task = task;
|
|
97
|
|
98 context->next = CreateData1;
|
|
99 loopCounter->i++;
|
|
100
|
|
101 goto meta(context, PutQueue1);
|
|
102 }
|
|
103
|
|
104 __code createTask2_stub(struct Context* context) {
|
|
105 goto createTask2(context,
|
|
106 &context->data[LoopCounter]->loopCounter,
|
|
107 &context->data[context->dataNum]->task,
|
|
108 &context->data[Element]->element);
|
|
109 }
|
|
110
|
|
111 __code putQueue1(struct Context* context, struct Allocate* allocate) {
|
|
112 allocate->size = sizeof(struct Element);
|
|
113 allocator(context);
|
|
114
|
|
115 goto meta(context, PutQueue2);
|
|
116 }
|
|
117
|
|
118 __code putQueue1_stub(struct Context* context) {
|
|
119 goto putQueue1(context, &context->data[Allocate]->allocate);
|
|
120 }
|
|
121
|
|
122 __code putQueue2(struct Context* context, struct Element* new_element, struct Element* element, struct Queue* queue) {
|
|
123 new_element->task = element->task;
|
|
124
|
|
125 if (queue->first)
|
|
126 goto meta(context, PutQueue3);
|
|
127 else
|
|
128 goto meta(context, PutQueue4);
|
|
129 }
|
|
130
|
|
131 __code putQueue2_stub(struct Context* context) {
|
|
132 goto putQueue2(context,
|
|
133 &context->data[context->dataNum]->element,
|
|
134 &context->data[Element]->element,
|
|
135 &context->data[ActiveQueue]->queue);
|
|
136 }
|
|
137
|
|
138 __code putQueue3(struct Context* context, struct Queue* queue, struct Element* new_element) {
|
|
139 struct Element* last = queue->last;
|
|
140
|
|
141 if (__sync_bool_compare_and_swap(&queue->last, last, new_element)) {
|
|
142 last->next = new_element;
|
|
143 queue->count++;
|
|
144
|
|
145 goto meta(context, context->next);
|
|
146 } else {
|
|
147 goto meta(context, PutQueue3);
|
|
148 }
|
|
149 }
|
|
150
|
|
151 __code putQueue3_stub(struct Context* context) {
|
|
152 goto putQueue3(context, &context->data[ActiveQueue]->queue, &context->data[context->dataNum]->element);
|
|
153 }
|
|
154
|
|
155 __code putQueue4(struct Context* context, struct Queue* queue, struct Element* new_element) {
|
|
156 if (__sync_bool_compare_and_swap(&queue->first, 0, new_element)) {
|
|
157 queue->last = new_element;
|
|
158 queue->count++;
|
|
159
|
|
160 goto meta(context, context->next);
|
|
161 } else {
|
|
162 goto meta(context, PutQueue3);
|
|
163 }
|
|
164 }
|
|
165
|
|
166 __code putQueue4_stub(struct Context* context) {
|
|
167 goto putQueue4(context, &context->data[ActiveQueue]->queue, &context->data[context->dataNum]->element);
|
|
168 }
|
|
169
|
91
|
170 __code getQueue(struct Context* context, struct Queue* queue) {
|
|
171 if (queue->count == 0)
|
|
172 return;
|
|
173
|
|
174 struct Element* first = queue->first;
|
|
175 if (__sync_bool_compare_and_swap(&queue->first, first, first->next)) {
|
|
176 queue->count--;
|
|
177
|
|
178 context->next = GetQueue;
|
|
179 stack_push(context->code_stack, &context->next);
|
|
180
|
|
181 context->next = first->task->code;
|
|
182 stack_push(context->code_stack, &context->next);
|
|
183
|
|
184 goto meta(context, Search);
|
|
185 } else {
|
|
186 goto meta(context, GetQueue);
|
|
187 }
|
|
188 }
|
|
189
|
|
190 __code getQueue_stub(struct Context* context) {
|
|
191 goto getQueue(context, &context->data[ActiveQueue]->queue);
|
|
192 }
|
|
193
|
86
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
194 __code createWorker(struct Context* context, struct LoopCounter* loopCounter, struct Worker* worker) {
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
195 int i = loopCounter->i;
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
196
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
197 if (i < worker->num) {
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
198 struct Context* worker_context = &worker->contexts[i];
|
91
|
199 worker_context->next = GetQueue;
|
|
200 worker_context->data[Tree] = context->data[Tree];
|
|
201 worker_context->data[ActiveQueue] = context->data[ActiveQueue];
|
86
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
202 pthread_create(&worker_context->thread, NULL, (void*)&start_code, worker_context);
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
203 loopCounter->i++;
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
204
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
205 goto meta(context, CreateWorker);
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
206 }
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
207
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
208 loopCounter->i = 0;
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
209 goto meta(context, TaskManager);
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
210 }
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
211
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
212 __code createWorker_stub(struct Context* context) {
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
213 goto createWorker(context, &context->data[LoopCounter]->loopCounter, &context->data[Worker]->worker);
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
214 }
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
215
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
216 __code taskManager(struct Context* context, struct LoopCounter* loopCounter, struct Worker* worker) {
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
217 int i = loopCounter->i;
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
218
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
219 if (i < worker->num) {
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
220 pthread_join(worker->contexts[i].thread, NULL);
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
221 loopCounter->i++;
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
222
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
223 goto meta(context, TaskManager);
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
224 }
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
225
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
226 loopCounter->i = 0;
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
227 goto meta(context, Exit);
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
228 }
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
229
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
230 __code taskManager_stub(struct Context* context) {
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
231 goto taskManager(context, &context->data[LoopCounter]->loopCounter, &context->data[Worker]->worker);
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
232 }
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
233
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
234 int main(int argc, char** argv) {
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
235 int cpu_num = (int)atoi(argv[1]);
|
90
|
236 length = (int)atoi(argv[2]);
|
|
237
|
|
238 array_ptr = (int*)malloc(sizeof(int)*length);
|
|
239
|
|
240 for(int i=0; i<length; i++)
|
|
241 array_ptr[i]=i;
|
86
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
242
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
243 struct Context* main_context = (struct Context*)malloc(sizeof(struct Context));
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
244 initContext(main_context);
|
90
|
245 //main_context->next = CreateWorker;
|
|
246 main_context->next = CreateData1;
|
86
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
247
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
248 struct Context* worker_contexts = (struct Context*)malloc(sizeof(struct Context)*cpu_num);
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
249
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
250 struct Worker* worker = &main_context->data[Worker]->worker;
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
251 worker->num = cpu_num;
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
252 worker->contexts = worker_contexts;
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
253
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
254 for (int i = 0;i<cpu_num;i++)
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
255 initContext(&worker_contexts[i]);
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
256
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
257 goto start_code(main_context);
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
258 }
|