Mercurial > hg > GearsTemplate
comparison src/parallel_execution/examples/calc.cbc @ 326:f23f6d0aa4e9 examples_directory
Add examples/calc.cbc and build but not work
author | Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 18 Apr 2017 01:47:42 +0900 |
parents | |
children | 534601ed8c50 |
comparison
equal
deleted
inserted
replaced
325:7d664be4efa5 | 326:f23f6d0aa4e9 |
---|---|
1 #include <stdio.h> | |
2 #include <string.h> | |
3 #include <stdlib.h> | |
4 #include <unistd.h> | |
5 | |
6 #include "../../context.h" | |
7 | |
8 int cpu_num = 1; | |
9 int length = 102400; | |
10 int split = 8; | |
11 int* array_ptr; | |
12 int gpu_num = 0; | |
13 int CPU_ANY = -1; | |
14 int CPU_CUDA = -1; | |
15 | |
16 void print_queue(struct Element* element) { | |
17 while (element) { | |
18 printf("%p\n", ((struct Task *)(element->data))); | |
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 } | |
30 | |
31 void *start_taskManager(struct Context *context) { | |
32 goto initDataGears(context, Gearef(context, LoopCounter), Gearef(context, TaskManager)); | |
33 return 0; | |
34 } | |
35 | |
36 #ifdef USE_CUDAWorker | |
37 #ifdef USE_CUDA_MAIN_THREAD | |
38 extern volatile int cuda_initialized; | |
39 #endif | |
40 #endif | |
41 | |
42 __code initDataGears(struct LoopCounter* loopCounter, struct TaskManager* taskManager) { | |
43 // loopCounter->tree = createRedBlackTree(context); | |
44 loopCounter->i = 0; | |
45 taskManager->taskManager = (union Data*)createTaskManagerImpl(context, cpu_num, gpu_num, 0); | |
46 #ifdef USE_CUDAWorker | |
47 #ifdef USE_CUDA_MAIN_THREAD | |
48 while(! cuda_initialized) {}; | |
49 #endif | |
50 #endif | |
51 goto meta(context, C_createTask1); | |
52 } | |
53 | |
54 __code initDataGears_stub(struct Context* context) { | |
55 struct TaskManager* taskManager = Gearef(context, TaskManager); | |
56 taskManager->taskManager = 0; | |
57 #if (! defined(USE_CUDAWorker) || ! defined(USE_CUDA_MAIN_THREAD)) | |
58 struct LoopCounter* loopCounter = Gearef(context, LoopCounter); | |
59 goto initDataGears(context, loopCounter, taskManager); | |
60 #else | |
61 cuda_initialized = 0; | |
62 pthread_t thread; | |
63 pthread_create(&thread, NULL, (void*)&start_taskManager, context); | |
64 while (taskManager->taskManager == 0); | |
65 TaskManager *t = (TaskManager*)taskManager->taskManager; | |
66 TaskManagerImpl *im = (TaskManagerImpl*)t->taskManager; | |
67 struct Queue *q = (Queue *)im->workers[0]; | |
68 createCUDAWorker(context,0,q, im); | |
69 pthread_join(thread,0); | |
70 exit(0); | |
71 #endif | |
72 } | |
73 | |
74 __code code1(struct Time* time) { | |
75 printf("cpus:\t\t%d\n", cpu_num); | |
76 printf("gpus:\t\t%d\n", gpu_num); | |
77 printf("length:\t\t%d\n", length); | |
78 printf("length/task:\t%d\n", length/split); | |
79 /* puts("queue"); */ | |
80 /* print_queue(context->data[ActiveQueue]->queue.first); */ | |
81 /* puts("tree"); */ | |
82 /* print_tree(context->data[Tree]->tree.root); */ | |
83 /* puts("result"); */ | |
84 | |
85 time->next = C_code2; | |
86 goto meta(context, C_code2); | |
87 //goto meta(context, C_exit_code); | |
88 //goto meta(context, C_start_time); | |
89 } | |
90 | |
91 __code code1_stub(struct Context* context) { | |
92 goto code1(context, Gearef(context, Time)); | |
93 } | |
94 | |
95 __code code2(struct LoopCounter* loopCounter) { | |
96 int i = loopCounter->i; | |
97 | |
98 if (i < length) { | |
99 //printf("%d\n", array_ptr[i]); | |
100 if (array_ptr[i] == (i*2)) { | |
101 loopCounter->i++; | |
102 goto meta(context, C_code2); | |
103 } else | |
104 puts("wrong result"); | |
105 | |
106 } | |
107 | |
108 goto meta(context, C_exit_code); | |
109 } | |
110 | |
111 __code createTask1(struct LoopCounter* loopCounter, struct TaskManager* taskManager) { | |
112 int i = loopCounter->i; | |
113 | |
114 if ((length/split*i) < length) { | |
115 taskManager->next = C_createTask2; | |
116 goto meta(context, taskManager->taskManager->TaskManager.createTask); | |
117 } | |
118 | |
119 loopCounter->i = 0; | |
120 taskManager->next = C_code1; | |
121 goto meta(context, taskManager->taskManager->TaskManager.shutdown); | |
122 } | |
123 | |
124 __code createTask2(LoopCounter* loopCounter, TaskManager* taskManager, struct Context *task, Integer *integer1, Integer *integer2, Integer *integer3) { | |
125 int i = loopCounter->i; | |
126 loopCounter->i++; | |
127 task->idgCount = 0; | |
128 task->next = C_mult; | |
129 integer2->value = i+1; | |
130 task->data[task->idg] = (union Data*)integer1; | |
131 task->data[task->idg+1] = (union Data*)integer2; | |
132 task->maxIdg = task->idg + 2; | |
133 task->odg = task->maxIdg; | |
134 task->data[task->odg] = (union Data*)integer3; | |
135 task->maxOdg = task->odg + 1; | |
136 taskManager->next = C_createTask3; | |
137 taskManager->data = (union Data*)integer1; | |
138 goto meta(context, taskManager->taskManager->TaskManager.setWaitTask); | |
139 } | |
140 | |
141 __code createTask2_stub(struct Context* context) { | |
142 Integer* integer1 = &ALLOCATE(context, Integer)->Integer; | |
143 Integer* integer2 = &ALLOCATE(context, Integer)->Integer; | |
144 Integer* integer3 = &ALLOCATE(context, Integer)->Integer; | |
145 goto createTask2(context, | |
146 Gearef(context, LoopCounter), | |
147 Gearef(context, TaskManager), | |
148 Gearef(context, TaskManager)->context, | |
149 integer1, | |
150 integer2, | |
151 integer3); | |
152 } | |
153 | |
154 __code createTask3(TaskManager* taskManager) { | |
155 taskManager->next = C_createTask4; | |
156 goto meta(context, taskManager->taskManager->TaskManager.spawn); | |
157 } | |
158 | |
159 __code createTask3_stub(struct Context* context) { | |
160 goto createTask3(context, | |
161 Gearef(context, TaskManager)); | |
162 } | |
163 | |
164 __code createTask4(struct TaskManager* taskManager) { | |
165 taskManager->next = C_createTask5; | |
166 goto meta(context, taskManager->taskManager->TaskManager.createTask); | |
167 } | |
168 | |
169 __code createTask5(LoopCounter* loopCounter, TaskManager* taskManager, struct Context* task, Integer *integer1, Integer *integer2, Integer *integer3) { | |
170 int i = loopCounter->i; | |
171 task->next = C_add; | |
172 task->idgCount = 0; | |
173 integer1->value = i; | |
174 integer1->value = i+1; | |
175 task->data[task->idg] = (union Data*)integer1; | |
176 task->data[task->idg+1] = (union Data*)integer2; | |
177 task->maxIdg = task->idg + 2; | |
178 task->odg = task->maxIdg; | |
179 task->data[task->odg] = (union Data*)integer3; | |
180 task->maxOdg = task->odg + 1; | |
181 taskManager->next = C_createTask3; | |
182 taskManager->data = (union Data*)integer1; | |
183 goto meta(context, taskManager->taskManager->TaskManager.spawn); | |
184 } | |
185 | |
186 __code createTask5_stub(struct Context* context) { | |
187 Integer* integer1 = &ALLOCATE(context, Integer)->Integer; | |
188 Integer* integer2 = &ALLOCATE(context, Integer)->Integer; | |
189 goto createTask5(context, | |
190 Gearef(context, LoopCounter), | |
191 Gearef(context, TaskManager), | |
192 Gearef(context, TaskManager)->context, | |
193 integer1, | |
194 integer2, | |
195 &Gearef(context, TaskManager)->data->Integer); | |
196 } | |
197 | |
198 | |
199 void init(int argc, char** argv) { | |
200 for (int i = 1; argv[i]; ++i) { | |
201 if (strcmp(argv[i], "-cpu") == 0) | |
202 cpu_num = (int)atoi(argv[i+1]); | |
203 else if (strcmp(argv[i], "-l") == 0) | |
204 length = (int)atoi(argv[i+1]); | |
205 else if (strcmp(argv[i], "-s") == 0) | |
206 split = (int)atoi(argv[i+1]); | |
207 else if (strcmp(argv[i], "-cuda") == 0) { | |
208 gpu_num = 1; | |
209 CPU_CUDA = 0; | |
210 } | |
211 } | |
212 } | |
213 | |
214 | |
215 int main(int argc, char** argv) { | |
216 init(argc, argv); | |
217 | |
218 array_ptr = NEWN(length, int); | |
219 | |
220 for(int i=0; i<length; i++) | |
221 array_ptr[i]=i; | |
222 | |
223 struct Context* main_context = NEW(struct Context); | |
224 initContext(main_context); | |
225 main_context->next = C_initDataGears; | |
226 | |
227 goto start_code(main_context); | |
228 } | |
229 | |
230 __code add(struct Integer* input1, struct Integer* input2, struct Integer* output) { | |
231 output->value = input1->value + input2->value; | |
232 printf("%d + %d = %d\n", input1->value, input2->value, output->value); | |
233 goto meta(context, context->next); | |
234 } | |
235 | |
236 __code add_stub(struct Context* context) { | |
237 goto add(context, | |
238 &context->data[context->idg]->Integer, | |
239 &context->data[context->idg + 1]->Integer, | |
240 &context->data[context->odg]->Integer); | |
241 | |
242 } | |
243 | |
244 __code mult(struct Integer* input1, struct Integer* input2, struct Integer* output) { | |
245 output->value = input1->value * input2->value; | |
246 printf("%d * %d = %d\n", input1->value, input2->value, output->value); | |
247 goto meta(context, context->next); | |
248 } | |
249 | |
250 __code mult_stub(struct Context* context) { | |
251 goto add(context, | |
252 &context->data[context->idg]->Integer, | |
253 &context->data[context->idg + 1]->Integer, | |
254 &context->data[context->odg]->Integer); | |
255 | |
256 } |