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