Mercurial > hg > GearsTemplate
annotate src/parallel_execution/main.c @ 170:ee7134f3bef1
Fix
author | Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 22 Nov 2016 10:36:45 +0900 |
parents | ea7b11f3e717 |
children | 57a11c15ff4c |
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> |
132 | 3 #include <stdlib.h> |
86 | 4 |
5 #include "context.h" | |
6 #include "origin_cs.h" | |
7 | |
8 extern __code initContext(struct Context* context); | |
90 | 9 extern void allocator(struct Context* context); |
86 | 10 |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
11 int cpu_num = 1; |
116 | 12 int length = 102400; |
13 int split = 8; | |
90 | 14 int* array_ptr; |
15 | |
16 void print_queue(struct Element* element) { | |
17 while (element) { | |
113
d05b9937aa95
Change element data from Task to Data
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
112
diff
changeset
|
18 printf("%d\n", ((struct Task *)(element->data))->key); |
90 | 19 element = element->next; |
20 } | |
21 } | |
22 | |
23 void print_tree(struct Node* node) { | |
24 if (node != 0) { | |
25 printf("%d\n", node->value->array.index); | |
26 print_tree(node->left); | |
27 print_tree(node->right); | |
28 } | |
29 } | |
86 | 30 |
31 __code code1(struct Context* context) { | |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
32 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
|
33 printf("length:\t\t%d\n", length); |
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
34 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
|
35 /* puts("queue"); */ |
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
36 /* print_queue(context->data[ActiveQueue]->queue.first); */ |
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
37 /* puts("tree"); */ |
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
38 /* print_tree(context->data[Tree]->tree.root); */ |
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
39 /* puts("result"); */ |
90 | 40 |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
41 context->next = CreateWorker; |
102 | 42 |
149 | 43 struct Time *t = &context->data[D_Time]->Time; |
102 | 44 t->next = CreateWorker; |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
45 goto meta(context, StartTime); |
86 | 46 } |
47 | |
48 __code code1_stub(struct Context* context) { | |
49 goto code1(context); | |
50 } | |
51 | |
92 | 52 __code code2(struct Context* context, struct Array* array, struct LoopCounter* loopCounter) { |
53 int i = loopCounter->i; | |
111 | 54 |
92 | 55 if (i < length) { |
114 | 56 //printf("%d\n", array->array[i]); |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
57 if (array->array[i] == (i*2)) { |
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
58 loopCounter->i++; |
144 | 59 goto meta(context, C_code2); |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
60 } else |
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
61 puts("wrong result"); |
92 | 62 |
63 } | |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
64 |
92 | 65 goto meta(context, Exit); |
66 } | |
67 | |
68 __code code2_stub(struct Context* context) { | |
148
473b7d990a1f
fix data gears type enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
144
diff
changeset
|
69 goto code2(context, &context->data[D_Node]->node.value->array, &context->data[D_LoopCounter]->loopCounter); |
92 | 70 } |
71 | |
90 | 72 __code createData1(struct Context* context, struct Allocate* allocate, struct LoopCounter* loopCounter) { |
73 int i = loopCounter->i; | |
74 | |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
75 if ((length/split*i) < length) { |
90 | 76 allocate->size = sizeof(struct Array); |
77 allocator(context); | |
78 | |
79 goto meta(context, CreateData2); | |
80 } | |
81 | |
92 | 82 loopCounter->i = 0; |
144 | 83 goto meta(context, C_code1); |
90 | 84 } |
111 | 85 |
90 | 86 __code createData1_stub(struct Context* context) { |
148
473b7d990a1f
fix data gears type enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
144
diff
changeset
|
87 goto createData1(context, &context->data[D_Allocate]->allocate, &context->data[D_LoopCounter]->loopCounter); |
90 | 88 } |
89 | |
160 | 90 __code createData2(struct Context* context, struct LoopCounter* loopCounter, struct Array* array, struct Node* node, Tree* tree) { |
90 | 91 int i = loopCounter->i; |
92 | |
93 array->index = i; | |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
94 array->prefix = length/split; |
90 | 95 array->array = array_ptr; |
96 | |
97 node->key = i; | |
98 node->value = (union Data*)array; | |
111 | 99 |
160 | 100 tree->next = CreateTask1; |
101 tree->node = node; | |
90 | 102 |
160 | 103 goto meta(context, loopCounter->tree->put); |
90 | 104 } |
105 | |
106 __code createData2_stub(struct Context* context) { | |
107 goto createData2(context, | |
148
473b7d990a1f
fix data gears type enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
144
diff
changeset
|
108 &context->data[D_LoopCounter]->loopCounter, |
111 | 109 &context->data[context->dataNum]->array, |
160 | 110 &context->data[D_Node]->node, |
111 Gearef(context, Tree)); | |
112 | 112 } |
113 | |
169
ea7b11f3e717
Using Queue Interface
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
166
diff
changeset
|
114 __code createTask1(struct Context* context, struct LoopCounter* loopCounter, struct Task* task, struct Queue* waitMe, struct Queue* waitI, struct Element* element) { |
90 | 115 int i = loopCounter->i; |
109
059b26a250cc
Change put_queue process
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
102
diff
changeset
|
116 |
92 | 117 task->code = Twice; |
90 | 118 task->key = i; |
112 | 119 task->waitMe = waitMe; |
120 task->waitI = waitI; | |
121 task->idsCount = 0; | |
109
059b26a250cc
Change put_queue process
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
102
diff
changeset
|
122 |
113
d05b9937aa95
Change element data from Task to Data
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
112
diff
changeset
|
123 element->data = (union Data *)task; |
90 | 124 |
125 context->next = CreateData1; | |
126 loopCounter->i++; | |
109
059b26a250cc
Change put_queue process
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
102
diff
changeset
|
127 |
112 | 128 goto meta(context, SpawnTask); |
90 | 129 } |
130 | |
160 | 131 __code createTask1_stub(struct Context* context) { |
132 Task* task = &ALLOCATE(context, Task)->Task; | |
133 Queue* waitMe = &ALLOCATE(context, Queue)->Queue; | |
134 Queue* waitI = &ALLOCATE(context, Queue)->Queue; | |
135 goto createTask1(context, | |
148
473b7d990a1f
fix data gears type enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
144
diff
changeset
|
136 &context->data[D_LoopCounter]->loopCounter, |
160 | 137 task, |
138 waitMe, | |
139 waitI, | |
169
ea7b11f3e717
Using Queue Interface
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
166
diff
changeset
|
140 &context->data[D_Element]->element); |
90 | 141 } |
142 | |
112 | 143 //__code createTask4(struct Context* context, struct LoopCounter* loopCounter, struct Task* task, struct Queue* waitMe, struct OdsQueue* waitI, struct Element* element, struct Queue* activeQueue) { |
111 | 144 // int i = loopCounter->i; |
112 | 145 // |
111 | 146 // task->code = TaskC; |
147 // task->key = i; | |
112 | 148 // task->waitMe = waitMe; |
149 // task->waitI = waitI; | |
150 // task->idsCount = 1; | |
151 // | |
113
d05b9937aa95
Change element data from Task to Data
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
112
diff
changeset
|
152 // element->data = (union Data *)task; |
111 | 153 // |
154 // context->next = CreateData1; | |
155 // loopCounter->i++; | |
156 // | |
112 | 157 // goto meta_createTask2(context, activeQueue, PutQueue1); |
111 | 158 //} |
159 // | |
112 | 160 //__code createTask4_stub(struct Context* context) { |
111 | 161 // goto createTask2(context, |
112 | 162 // &context->data[LoopCounter]->loopCounter, |
163 // &context->data[context->dataNum]->task, | |
164 // &context->data[context->dataNum-1]->queue, | |
165 // &context->data[context->dataNum-2]->odsQueue, | |
166 // &context->data[Element]->element, | |
167 // &context->data[ActiveQueue]->queue); | |
111 | 168 //} |
169 // | |
112 | 170 //__code createTask5(struct Context* context, struct LoopCounter* loopCounter, struct Task* task, struct Task* slave, struct Element* element) { |
111 | 171 // int i = loopCounter->i; |
112 | 172 // |
111 | 173 // task->code = TaskB; |
174 // task->key = i; | |
113
d05b9937aa95
Change element data from Task to Data
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
112
diff
changeset
|
175 // element->data = (union Data *)task; |
111 | 176 // |
177 // context->next = CreateData1; | |
178 // loopCounter->i++; | |
112 | 179 // |
111 | 180 // goto meta(context, WaitFor1); |
181 //} | |
182 // | |
112 | 183 //__code createTask5_stub(struct Context* context) { |
111 | 184 // goto createTask2(context, |
112 | 185 // &context->data[context->dataNum]->task, |
186 // &context->data[Element]->element); | |
111 | 187 //} |
188 | |
86 | 189 __code createWorker(struct Context* context, struct LoopCounter* loopCounter, struct Worker* worker) { |
190 int i = loopCounter->i; | |
191 | |
192 if (i < worker->num) { | |
193 struct Context* worker_context = &worker->contexts[i]; | |
170 | 194 worker_context->next = C_getTask1; |
148
473b7d990a1f
fix data gears type enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
144
diff
changeset
|
195 worker_context->data[D_Tree] = context->data[D_Tree]; |
473b7d990a1f
fix data gears type enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
144
diff
changeset
|
196 worker_context->data[D_ActiveQueue] = context->data[D_ActiveQueue]; |
86 | 197 pthread_create(&worker_context->thread, NULL, (void*)&start_code, worker_context); |
92 | 198 worker_context->thread_num = i; |
86 | 199 loopCounter->i++; |
200 | |
201 goto meta(context, CreateWorker); | |
202 } | |
203 | |
204 loopCounter->i = 0; | |
205 goto meta(context, TaskManager); | |
206 } | |
109
059b26a250cc
Change put_queue process
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
102
diff
changeset
|
207 |
86 | 208 __code createWorker_stub(struct Context* context) { |
148
473b7d990a1f
fix data gears type enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
144
diff
changeset
|
209 goto createWorker(context, &context->data[D_LoopCounter]->loopCounter, &context->data[D_Worker]->worker); |
86 | 210 } |
211 | |
212 __code taskManager(struct Context* context, struct LoopCounter* loopCounter, struct Worker* worker) { | |
213 int i = loopCounter->i; | |
214 | |
215 if (i < worker->num) { | |
216 pthread_join(worker->contexts[i].thread, NULL); | |
217 loopCounter->i++; | |
218 | |
219 goto meta(context, TaskManager); | |
220 } | |
221 | |
222 loopCounter->i = 0; | |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
223 |
149 | 224 Time *t = &context->data[D_Time]->Time; |
144 | 225 t->next = C_code2; |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
226 goto meta(context, EndTime); |
86 | 227 } |
109
059b26a250cc
Change put_queue process
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
102
diff
changeset
|
228 |
86 | 229 __code taskManager_stub(struct Context* context) { |
148
473b7d990a1f
fix data gears type enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
144
diff
changeset
|
230 goto taskManager(context, &context->data[D_LoopCounter]->loopCounter, &context->data[D_Worker]->worker); |
86 | 231 } |
232 | |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
233 void init(int argc, char** argv) { |
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
234 for (int i = 1; argv[i]; ++i) { |
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
235 if (strcmp(argv[i], "-cpu") == 0) |
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
236 cpu_num = (int)atoi(argv[i+1]); |
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
237 else if (strcmp(argv[i], "-l") == 0) |
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
238 length = (int)atoi(argv[i+1]); |
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
239 else if (strcmp(argv[i], "-s") == 0) |
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
240 split = (int)atoi(argv[i+1]); |
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
241 } |
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
242 } |
109
059b26a250cc
Change put_queue process
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
102
diff
changeset
|
243 |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
244 |
86 | 245 int main(int argc, char** argv) { |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
246 init(argc, argv); |
90 | 247 |
101 | 248 array_ptr = NEWN(length, int); |
90 | 249 |
250 for(int i=0; i<length; i++) | |
251 array_ptr[i]=i; | |
86 | 252 |
101 | 253 struct Context* main_context = NEW(struct Context); |
86 | 254 initContext(main_context); |
90 | 255 main_context->next = CreateData1; |
86 | 256 |
101 | 257 struct Context* worker_contexts = NEWN(cpu_num, struct Context); |
109
059b26a250cc
Change put_queue process
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
102
diff
changeset
|
258 |
148
473b7d990a1f
fix data gears type enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
144
diff
changeset
|
259 struct Worker* worker = &main_context->data[D_Worker]->worker; |
86 | 260 worker->num = cpu_num; |
261 worker->contexts = worker_contexts; | |
109
059b26a250cc
Change put_queue process
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
102
diff
changeset
|
262 |
86 | 263 for (int i = 0;i<cpu_num;i++) |
264 initContext(&worker_contexts[i]); | |
109
059b26a250cc
Change put_queue process
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
102
diff
changeset
|
265 |
86 | 266 goto start_code(main_context); |
267 } |