Mercurial > hg > GearsTemplate
annotate src/parallel_execution/main.c @ 186:dbc064c26b98
generate context script
author | mir3636 |
---|---|
date | Thu, 15 Dec 2016 19:38:18 +0900 |
parents | 247a2c4a8908 |
children | bacc37265386 |
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) { | |
185 | 18 printf("%p\n", ((struct Task *)(element->data))); |
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 |
186 | 41 context->next = C_createWorker; |
102 | 42 |
149 | 43 struct Time *t = &context->data[D_Time]->Time; |
186 | 44 t->next = C_createWorker; |
45 goto meta(context, C_start_time); | |
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 |
186 | 65 goto meta(context, C_code2); |
92 | 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 | |
186 | 79 goto meta(context, C_createData2); |
90 | 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 |
186 | 100 tree->next = C_createTask1; |
160 | 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 | |
182 | 114 __code createTask1(struct Context* context, struct LoopCounter* loopCounter, struct Task* task, struct Element* element) { |
186 | 115 task->code = C_twice; |
112 | 116 task->idsCount = 0; |
109
059b26a250cc
Change put_queue process
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
102
diff
changeset
|
117 |
113
d05b9937aa95
Change element data from Task to Data
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
112
diff
changeset
|
118 element->data = (union Data *)task; |
90 | 119 |
186 | 120 context->next = C_createData1; |
90 | 121 loopCounter->i++; |
109
059b26a250cc
Change put_queue process
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
102
diff
changeset
|
122 |
186 | 123 goto meta(context, C_createTask1); |
90 | 124 } |
125 | |
160 | 126 __code createTask1_stub(struct Context* context) { |
127 Task* task = &ALLOCATE(context, Task)->Task; | |
128 goto createTask1(context, | |
148
473b7d990a1f
fix data gears type enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
144
diff
changeset
|
129 &context->data[D_LoopCounter]->loopCounter, |
160 | 130 task, |
169
ea7b11f3e717
Using Queue Interface
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
166
diff
changeset
|
131 &context->data[D_Element]->element); |
90 | 132 } |
133 | |
112 | 134 //__code createTask4(struct Context* context, struct LoopCounter* loopCounter, struct Task* task, struct Queue* waitMe, struct OdsQueue* waitI, struct Element* element, struct Queue* activeQueue) { |
111 | 135 // int i = loopCounter->i; |
112 | 136 // |
111 | 137 // task->code = TaskC; |
138 // task->key = i; | |
112 | 139 // task->waitMe = waitMe; |
140 // task->waitI = waitI; | |
141 // task->idsCount = 1; | |
142 // | |
113
d05b9937aa95
Change element data from Task to Data
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
112
diff
changeset
|
143 // element->data = (union Data *)task; |
111 | 144 // |
145 // context->next = CreateData1; | |
146 // loopCounter->i++; | |
147 // | |
112 | 148 // goto meta_createTask2(context, activeQueue, PutQueue1); |
111 | 149 //} |
150 // | |
112 | 151 //__code createTask4_stub(struct Context* context) { |
111 | 152 // goto createTask2(context, |
112 | 153 // &context->data[LoopCounter]->loopCounter, |
154 // &context->data[context->dataNum]->task, | |
155 // &context->data[context->dataNum-1]->queue, | |
156 // &context->data[context->dataNum-2]->odsQueue, | |
157 // &context->data[Element]->element, | |
158 // &context->data[ActiveQueue]->queue); | |
111 | 159 //} |
160 // | |
112 | 161 //__code createTask5(struct Context* context, struct LoopCounter* loopCounter, struct Task* task, struct Task* slave, struct Element* element) { |
111 | 162 // int i = loopCounter->i; |
112 | 163 // |
111 | 164 // task->code = TaskB; |
165 // task->key = i; | |
113
d05b9937aa95
Change element data from Task to Data
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
112
diff
changeset
|
166 // element->data = (union Data *)task; |
111 | 167 // |
168 // context->next = CreateData1; | |
169 // loopCounter->i++; | |
112 | 170 // |
111 | 171 // goto meta(context, WaitFor1); |
172 //} | |
173 // | |
112 | 174 //__code createTask5_stub(struct Context* context) { |
111 | 175 // goto createTask2(context, |
112 | 176 // &context->data[context->dataNum]->task, |
177 // &context->data[Element]->element); | |
111 | 178 //} |
179 | |
86 | 180 __code createWorker(struct Context* context, struct LoopCounter* loopCounter, struct Worker* worker) { |
181 int i = loopCounter->i; | |
182 | |
185 | 183 if (i < worker->id) { |
86 | 184 struct Context* worker_context = &worker->contexts[i]; |
170 | 185 worker_context->next = C_getTask1; |
148
473b7d990a1f
fix data gears type enum
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents:
144
diff
changeset
|
186 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
|
187 worker_context->data[D_ActiveQueue] = context->data[D_ActiveQueue]; |
86 | 188 pthread_create(&worker_context->thread, NULL, (void*)&start_code, worker_context); |
92 | 189 worker_context->thread_num = i; |
86 | 190 loopCounter->i++; |
191 | |
186 | 192 goto meta(context, C_createWorker); |
86 | 193 } |
194 | |
195 loopCounter->i = 0; | |
196 goto meta(context, TaskManager); | |
197 } | |
109
059b26a250cc
Change put_queue process
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
102
diff
changeset
|
198 |
86 | 199 __code createWorker_stub(struct Context* context) { |
185 | 200 goto createWorker(context, &context->data[D_LoopCounter]->loopCounter, &context->data[D_Worker]->Worker); |
86 | 201 } |
202 | |
203 __code taskManager(struct Context* context, struct LoopCounter* loopCounter, struct Worker* worker) { | |
204 int i = loopCounter->i; | |
205 | |
185 | 206 if (i < worker->id) { |
86 | 207 pthread_join(worker->contexts[i].thread, NULL); |
208 loopCounter->i++; | |
209 | |
210 goto meta(context, TaskManager); | |
211 } | |
212 | |
213 loopCounter->i = 0; | |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
214 |
149 | 215 Time *t = &context->data[D_Time]->Time; |
144 | 216 t->next = C_code2; |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
217 goto meta(context, EndTime); |
86 | 218 } |
109
059b26a250cc
Change put_queue process
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
102
diff
changeset
|
219 |
86 | 220 __code taskManager_stub(struct Context* context) { |
185 | 221 goto taskManager(context, &context->data[D_LoopCounter]->loopCounter, &context->data[D_Worker]->Worker); |
86 | 222 } |
223 | |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
224 void init(int argc, char** argv) { |
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
225 for (int i = 1; argv[i]; ++i) { |
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
226 if (strcmp(argv[i], "-cpu") == 0) |
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
227 cpu_num = (int)atoi(argv[i+1]); |
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
228 else if (strcmp(argv[i], "-l") == 0) |
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
229 length = (int)atoi(argv[i+1]); |
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
230 else if (strcmp(argv[i], "-s") == 0) |
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
231 split = (int)atoi(argv[i+1]); |
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
232 } |
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
233 } |
109
059b26a250cc
Change put_queue process
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
102
diff
changeset
|
234 |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
235 |
86 | 236 int main(int argc, char** argv) { |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
237 init(argc, argv); |
90 | 238 |
101 | 239 array_ptr = NEWN(length, int); |
90 | 240 |
241 for(int i=0; i<length; i++) | |
242 array_ptr[i]=i; | |
86 | 243 |
101 | 244 struct Context* main_context = NEW(struct Context); |
86 | 245 initContext(main_context); |
90 | 246 main_context->next = CreateData1; |
86 | 247 |
101 | 248 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
|
249 |
185 | 250 struct Worker* worker = &main_context->data[D_Worker]->Worker; |
251 worker->id = cpu_num; | |
86 | 252 worker->contexts = worker_contexts; |
109
059b26a250cc
Change put_queue process
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
102
diff
changeset
|
253 |
86 | 254 for (int i = 0;i<cpu_num;i++) |
255 initContext(&worker_contexts[i]); | |
109
059b26a250cc
Change put_queue process
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
102
diff
changeset
|
256 |
86 | 257 goto start_code(main_context); |
258 } | |
186 | 259 |