Mercurial > hg > Gears > GearsAgda
annotate src/parallel_execution/main.cbc @ 279:b5e8fce400a5
Fix
author | Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp> |
---|---|
date | Fri, 03 Feb 2017 03:25:57 +0900 |
parents | 23767f714f4a |
children | 2c2e4e597eb0 |
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 |
278 | 5 #include "../context.h" |
86 | 6 |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
7 int cpu_num = 1; |
113 | 8 int length = 102400; |
9 int split = 8; | |
90 | 10 int* array_ptr; |
11 | |
12 void print_queue(struct Element* element) { | |
13 while (element) { | |
185 | 14 printf("%p\n", ((struct Task *)(element->data))); |
90 | 15 element = element->next; |
16 } | |
17 } | |
18 | |
19 void print_tree(struct Node* node) { | |
20 if (node != 0) { | |
195 | 21 printf("%d\n", node->value->Array.index); |
90 | 22 print_tree(node->left); |
23 print_tree(node->right); | |
24 } | |
25 } | |
86 | 26 |
278 | 27 __code initDataGears(struct LoopCounter* loopCounter, struct TaskManager* taskManager) { |
279 | 28 loopCounter->tree = createRedBlackTree(context); |
220 | 29 loopCounter->i = 0; |
279 | 30 taskManager->taskManager = (union Data*)createTaskManager(context, cpu_num, 0, 0); |
248
1ede5390cda2
Fix segmentation fault but not multi thread running
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
247
diff
changeset
|
31 goto meta(context, C_createTask1); |
220 | 32 } |
33 | |
34 __code initDataGears_stub(struct Context* context) { | |
35 goto initDataGears(context, Gearef(context, LoopCounter), Gearef(context, TaskManager)); | |
36 } | |
37 | |
278 | 38 __code code1(struct Time* time) { |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
39 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
|
40 printf("length:\t\t%d\n", length); |
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
41 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
|
42 /* puts("queue"); */ |
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
43 /* print_queue(context->data[ActiveQueue]->queue.first); */ |
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
44 /* puts("tree"); */ |
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
45 /* print_tree(context->data[Tree]->tree.root); */ |
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
46 /* puts("result"); */ |
90 | 47 |
232 | 48 time->next = C_code2; |
248
1ede5390cda2
Fix segmentation fault but not multi thread running
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
247
diff
changeset
|
49 goto meta(context, C_exit_code); |
1ede5390cda2
Fix segmentation fault but not multi thread running
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
247
diff
changeset
|
50 //goto meta(context, C_start_time); |
86 | 51 } |
52 | |
53 __code code1_stub(struct Context* context) { | |
220 | 54 goto code1(context, Gearef(context, Time)); |
86 | 55 } |
56 | |
278 | 57 __code code2(struct Array* array, struct LoopCounter* loopCounter) { |
92 | 58 int i = loopCounter->i; |
111 | 59 |
92 | 60 if (i < length) { |
115 | 61 //printf("%d\n", array->array[i]); |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
62 if (array->array[i] == (i*2)) { |
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
63 loopCounter->i++; |
144 | 64 goto meta(context, C_code2); |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
65 } else |
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
66 puts("wrong result"); |
92 | 67 |
68 } | |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
69 |
186 | 70 goto meta(context, C_code2); |
92 | 71 } |
72 | |
73 __code code2_stub(struct Context* context) { | |
217
c34e6aa10967
Fix DataGear access name
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
211
diff
changeset
|
74 goto code2(context, &context->data[D_Node]->Node.value->Array, &context->data[D_LoopCounter]->LoopCounter); |
92 | 75 } |
76 | |
278 | 77 __code createData1(struct Allocate* allocate, struct LoopCounter* loopCounter) { |
90 | 78 int i = loopCounter->i; |
79 | |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
80 if ((length/split*i) < length) { |
186 | 81 goto meta(context, C_createData2); |
90 | 82 } |
83 | |
92 | 84 loopCounter->i = 0; |
144 | 85 goto meta(context, C_code1); |
90 | 86 } |
111 | 87 |
90 | 88 __code createData1_stub(struct Context* context) { |
218
d8a59b727f65
Fix spawnTaskManager
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
217
diff
changeset
|
89 goto createData1(context, Gearef(context, Allocate), Gearef(context, LoopCounter)); |
90 | 90 } |
91 | |
278 | 92 __code createData2(struct LoopCounter* loopCounter, struct Array* array, struct Node* node, Tree* tree) { |
90 | 93 int i = loopCounter->i; |
94 | |
95 array->index = i; | |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
96 array->prefix = length/split; |
90 | 97 array->array = array_ptr; |
98 | |
99 node->key = i; | |
100 node->value = (union Data*)array; | |
220 | 101 |
102 tree->tree = (union Data*)loopCounter->tree; | |
111 | 103 |
186 | 104 tree->next = C_createTask1; |
160 | 105 tree->node = node; |
90 | 106 |
160 | 107 goto meta(context, loopCounter->tree->put); |
90 | 108 } |
109 | |
110 __code createData2_stub(struct Context* context) { | |
218
d8a59b727f65
Fix spawnTaskManager
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
217
diff
changeset
|
111 Array* array = &ALLOCATE(context, Array)->Array; |
90 | 112 goto createData2(context, |
218
d8a59b727f65
Fix spawnTaskManager
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
217
diff
changeset
|
113 Gearef(context, LoopCounter), |
d8a59b727f65
Fix spawnTaskManager
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
217
diff
changeset
|
114 array, |
d8a59b727f65
Fix spawnTaskManager
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
217
diff
changeset
|
115 Gearef(context, Node), |
160 | 116 Gearef(context, Tree)); |
112 | 117 } |
118 | |
278 | 119 __code createTask1(struct LoopCounter* loopCounter, struct TaskManager* taskManager) { |
247
ce262b2c1daf
Fix createTask for main
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
246
diff
changeset
|
120 int i = loopCounter->i; |
109
059b26a250cc
Change put_queue process
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
102
diff
changeset
|
121 |
247
ce262b2c1daf
Fix createTask for main
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
246
diff
changeset
|
122 if ((length/split*i) < length) { |
ce262b2c1daf
Fix createTask for main
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
246
diff
changeset
|
123 taskManager->next = C_createTask2; |
ce262b2c1daf
Fix createTask for main
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
246
diff
changeset
|
124 goto meta(context, taskManager->taskManager->TaskManager.createTask); |
ce262b2c1daf
Fix createTask for main
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
246
diff
changeset
|
125 } |
109
059b26a250cc
Change put_queue process
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
102
diff
changeset
|
126 |
247
ce262b2c1daf
Fix createTask for main
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
246
diff
changeset
|
127 loopCounter->i = 0; |
ce262b2c1daf
Fix createTask for main
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
246
diff
changeset
|
128 goto meta(context, C_code1); |
90 | 129 } |
130 | |
160 | 131 __code createTask1_stub(struct Context* context) { |
132 goto createTask1(context, | |
218
d8a59b727f65
Fix spawnTaskManager
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
217
diff
changeset
|
133 Gearef(context, LoopCounter), |
247
ce262b2c1daf
Fix createTask for main
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
246
diff
changeset
|
134 Gearef(context, TaskManager)); |
ce262b2c1daf
Fix createTask for main
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
246
diff
changeset
|
135 } |
ce262b2c1daf
Fix createTask for main
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
246
diff
changeset
|
136 |
278 | 137 __code createTask2(LoopCounter* loopCounter, TaskManager* taskManager,struct Context* task, Array* array) { |
247
ce262b2c1daf
Fix createTask for main
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
246
diff
changeset
|
138 int i = loopCounter->i; |
ce262b2c1daf
Fix createTask for main
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
246
diff
changeset
|
139 |
ce262b2c1daf
Fix createTask for main
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
246
diff
changeset
|
140 if ((length/split*i) < length) { |
ce262b2c1daf
Fix createTask for main
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
246
diff
changeset
|
141 array->index = i; |
ce262b2c1daf
Fix createTask for main
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
246
diff
changeset
|
142 array->prefix = length/split; |
ce262b2c1daf
Fix createTask for main
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
246
diff
changeset
|
143 array->array = array_ptr; |
ce262b2c1daf
Fix createTask for main
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
246
diff
changeset
|
144 task->idgCount = 0; |
ce262b2c1daf
Fix createTask for main
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
246
diff
changeset
|
145 task->next = C_twice; |
ce262b2c1daf
Fix createTask for main
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
246
diff
changeset
|
146 // task->data[task->idg] = (union Data*)array; |
ce262b2c1daf
Fix createTask for main
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
246
diff
changeset
|
147 taskManager->next = C_createTask1; |
ce262b2c1daf
Fix createTask for main
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
246
diff
changeset
|
148 loopCounter->i++; |
ce262b2c1daf
Fix createTask for main
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
246
diff
changeset
|
149 |
ce262b2c1daf
Fix createTask for main
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
246
diff
changeset
|
150 goto meta(context, taskManager->taskManager->TaskManager.spawn); |
ce262b2c1daf
Fix createTask for main
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
246
diff
changeset
|
151 } |
ce262b2c1daf
Fix createTask for main
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
246
diff
changeset
|
152 loopCounter->i = 0; |
248
1ede5390cda2
Fix segmentation fault but not multi thread running
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
247
diff
changeset
|
153 taskManager->next = C_code1; |
1ede5390cda2
Fix segmentation fault but not multi thread running
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
247
diff
changeset
|
154 goto meta(context, taskManager->taskManager->TaskManager.shutdown); |
247
ce262b2c1daf
Fix createTask for main
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
246
diff
changeset
|
155 } |
ce262b2c1daf
Fix createTask for main
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
246
diff
changeset
|
156 |
ce262b2c1daf
Fix createTask for main
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
246
diff
changeset
|
157 __code createTask2_stub(struct Context* context) { |
ce262b2c1daf
Fix createTask for main
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
246
diff
changeset
|
158 Array* array = &ALLOCATE(context, Array)->Array; |
ce262b2c1daf
Fix createTask for main
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
246
diff
changeset
|
159 goto createTask2(context, |
ce262b2c1daf
Fix createTask for main
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
246
diff
changeset
|
160 Gearef(context, LoopCounter), |
218
d8a59b727f65
Fix spawnTaskManager
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
217
diff
changeset
|
161 Gearef(context, TaskManager), |
247
ce262b2c1daf
Fix createTask for main
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
246
diff
changeset
|
162 Gearef(context, TaskManager)->context, |
ce262b2c1daf
Fix createTask for main
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
246
diff
changeset
|
163 array); |
90 | 164 } |
165 | |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
166 void init(int argc, char** argv) { |
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
167 for (int i = 1; argv[i]; ++i) { |
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
168 if (strcmp(argv[i], "-cpu") == 0) |
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
169 cpu_num = (int)atoi(argv[i+1]); |
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
170 else if (strcmp(argv[i], "-l") == 0) |
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
171 length = (int)atoi(argv[i+1]); |
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
172 else if (strcmp(argv[i], "-s") == 0) |
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
173 split = (int)atoi(argv[i+1]); |
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
174 } |
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
175 } |
109
059b26a250cc
Change put_queue process
Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
parents:
102
diff
changeset
|
176 |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
177 |
86 | 178 int main(int argc, char** argv) { |
95
3e28ee215c0e
modify twice, use OSAtomiceCompareAndSwap
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
92
diff
changeset
|
179 init(argc, argv); |
90 | 180 |
101 | 181 array_ptr = NEWN(length, int); |
90 | 182 |
183 for(int i=0; i<length; i++) | |
184 array_ptr[i]=i; | |
86 | 185 |
101 | 186 struct Context* main_context = NEW(struct Context); |
86 | 187 initContext(main_context); |
220 | 188 main_context->next = C_initDataGears; |
86 | 189 |
190 goto start_code(main_context); | |
191 } | |
186 | 192 |