Mercurial > hg > Members > innparusu > Gears
annotate src/parallel_execution/main.c @ 102:64c98838a291
remove stack StartTime&EndTime
author | ikkun |
---|---|
date | Wed, 02 Mar 2016 20:51:33 +0900 |
parents | 8987cf13d5bb |
children | 059b26a250cc |
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; |
102 | 41 |
42 struct Time *t = &context->data[Time]->time; | |
43 t->next = CreateWorker; | |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
44 goto meta(context, StartTime); |
86 | 45 } |
46 | |
47 __code code1_stub(struct Context* context) { | |
48 goto code1(context); | |
49 } | |
50 | |
92 | 51 __code code2(struct Context* context, struct Array* array, struct LoopCounter* loopCounter) { |
52 int i = loopCounter->i; | |
53 | |
54 if (i < length) { | |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
55 // printf("%d\n", array->array[i]); |
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
56 if (array->array[i] == (i*2)) { |
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
57 loopCounter->i++; |
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
58 goto meta(context, Code2); |
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
59 } else |
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
60 puts("wrong result"); |
92 | 61 |
62 } | |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
63 |
92 | 64 goto meta(context, Exit); |
65 } | |
66 | |
67 __code code2_stub(struct Context* context) { | |
68 goto code2(context, &context->data[Node]->node.value->array, &context->data[LoopCounter]->loopCounter); | |
69 } | |
70 | |
90 | 71 __code createData1(struct Context* context, struct Allocate* allocate, struct LoopCounter* loopCounter) { |
72 int i = loopCounter->i; | |
73 | |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
74 if ((length/split*i) < length) { |
90 | 75 allocate->size = sizeof(struct Array); |
76 allocator(context); | |
77 | |
78 goto meta(context, CreateData2); | |
79 } | |
80 | |
92 | 81 loopCounter->i = 0; |
90 | 82 goto meta(context, Code1); |
83 } | |
84 | |
85 __code createData1_stub(struct Context* context) { | |
86 goto createData1(context, &context->data[Allocate]->allocate, &context->data[LoopCounter]->loopCounter); | |
87 } | |
88 | |
89 __code createData2(struct Context* context, struct LoopCounter* loopCounter, struct Array* array, struct Node* node) { | |
90 int i = loopCounter->i; | |
91 | |
92 array->index = i; | |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
93 array->prefix = length/split; |
90 | 94 array->array = array_ptr; |
95 | |
96 node->key = i; | |
97 node->value = (union Data*)array; | |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
98 |
90 | 99 context->next = CreateTask1; |
100 | |
101 goto meta(context, PutTree); | |
102 } | |
103 | |
104 __code createData2_stub(struct Context* context) { | |
105 goto createData2(context, | |
106 &context->data[LoopCounter]->loopCounter, | |
107 &context->data[context->dataNum]->array, | |
108 &context->data[Node]->node); | |
109 } | |
110 | |
111 __code createTask1(struct Context* context, struct Allocate* allocate) { | |
112 allocate->size = sizeof(struct Task); | |
113 allocator(context); | |
114 | |
115 goto meta(context, CreateTask2); | |
116 } | |
117 | |
118 __code createTask1_stub(struct Context* context) { | |
119 goto createTask1(context, &context->data[Allocate]->allocate); | |
120 } | |
121 | |
122 __code createTask2(struct Context* context, struct LoopCounter* loopCounter, struct Task* task, struct Element* element) { | |
123 int i = loopCounter->i; | |
124 | |
92 | 125 task->code = Twice; |
90 | 126 task->key = i; |
127 | |
128 element->task = task; | |
129 | |
130 context->next = CreateData1; | |
131 loopCounter->i++; | |
132 | |
133 goto meta(context, PutQueue1); | |
134 } | |
135 | |
136 __code createTask2_stub(struct Context* context) { | |
137 goto createTask2(context, | |
138 &context->data[LoopCounter]->loopCounter, | |
139 &context->data[context->dataNum]->task, | |
140 &context->data[Element]->element); | |
141 } | |
142 | |
143 __code putQueue1(struct Context* context, struct Allocate* allocate) { | |
144 allocate->size = sizeof(struct Element); | |
145 allocator(context); | |
146 | |
147 goto meta(context, PutQueue2); | |
148 } | |
149 | |
150 __code putQueue1_stub(struct Context* context) { | |
151 goto putQueue1(context, &context->data[Allocate]->allocate); | |
152 } | |
153 | |
154 __code putQueue2(struct Context* context, struct Element* new_element, struct Element* element, struct Queue* queue) { | |
155 new_element->task = element->task; | |
156 | |
157 if (queue->first) | |
158 goto meta(context, PutQueue3); | |
159 else | |
160 goto meta(context, PutQueue4); | |
161 } | |
162 | |
163 __code putQueue2_stub(struct Context* context) { | |
164 goto putQueue2(context, | |
165 &context->data[context->dataNum]->element, | |
166 &context->data[Element]->element, | |
167 &context->data[ActiveQueue]->queue); | |
168 } | |
169 | |
170 __code putQueue3(struct Context* context, struct Queue* queue, struct Element* new_element) { | |
171 struct Element* last = queue->last; | |
172 | |
173 if (__sync_bool_compare_and_swap(&queue->last, last, new_element)) { | |
174 last->next = new_element; | |
175 queue->count++; | |
176 | |
177 goto meta(context, context->next); | |
178 } else { | |
179 goto meta(context, PutQueue3); | |
180 } | |
181 } | |
182 | |
183 __code putQueue3_stub(struct Context* context) { | |
184 goto putQueue3(context, &context->data[ActiveQueue]->queue, &context->data[context->dataNum]->element); | |
185 } | |
186 | |
187 __code putQueue4(struct Context* context, struct Queue* queue, struct Element* new_element) { | |
188 if (__sync_bool_compare_and_swap(&queue->first, 0, new_element)) { | |
189 queue->last = new_element; | |
190 queue->count++; | |
191 | |
192 goto meta(context, context->next); | |
193 } else { | |
194 goto meta(context, PutQueue3); | |
195 } | |
196 } | |
197 | |
198 __code putQueue4_stub(struct Context* context) { | |
199 goto putQueue4(context, &context->data[ActiveQueue]->queue, &context->data[context->dataNum]->element); | |
200 } | |
201 | |
86 | 202 __code createWorker(struct Context* context, struct LoopCounter* loopCounter, struct Worker* worker) { |
203 int i = loopCounter->i; | |
204 | |
205 if (i < worker->num) { | |
206 struct Context* worker_context = &worker->contexts[i]; | |
91 | 207 worker_context->next = GetQueue; |
208 worker_context->data[Tree] = context->data[Tree]; | |
209 worker_context->data[ActiveQueue] = context->data[ActiveQueue]; | |
86 | 210 pthread_create(&worker_context->thread, NULL, (void*)&start_code, worker_context); |
92 | 211 worker_context->thread_num = i; |
86 | 212 loopCounter->i++; |
213 | |
214 goto meta(context, CreateWorker); | |
215 } | |
216 | |
217 loopCounter->i = 0; | |
218 goto meta(context, TaskManager); | |
219 } | |
220 | |
221 __code createWorker_stub(struct Context* context) { | |
222 goto createWorker(context, &context->data[LoopCounter]->loopCounter, &context->data[Worker]->worker); | |
223 } | |
224 | |
225 __code taskManager(struct Context* context, struct LoopCounter* loopCounter, struct Worker* worker) { | |
226 int i = loopCounter->i; | |
227 | |
228 if (i < worker->num) { | |
229 pthread_join(worker->contexts[i].thread, NULL); | |
230 loopCounter->i++; | |
231 | |
232 goto meta(context, TaskManager); | |
233 } | |
234 | |
235 loopCounter->i = 0; | |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
236 |
102 | 237 struct Time *t = &context->data[Time]->time; |
238 t->next = Code2; | |
95
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 |
101 | 261 array_ptr = NEWN(length, int); |
90 | 262 |
263 for(int i=0; i<length; i++) | |
264 array_ptr[i]=i; | |
86 | 265 |
101 | 266 struct Context* main_context = NEW(struct Context); |
86 | 267 initContext(main_context); |
90 | 268 main_context->next = CreateData1; |
86 | 269 |
101 | 270 struct Context* worker_contexts = NEWN(cpu_num, struct Context); |
86 | 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 } |