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