comparison src/parallel_execution/examples/calc/calc.cbc @ 381:b81492c74d2b

Create examples directory
author Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
date Mon, 24 Jul 2017 16:52:09 +0900
parents src/parallel_execution/examples/calc.cbc@9049c19036fd
children bba401f93dcd
comparison
equal deleted inserted replaced
380:783017f6dfbe 381:b81492c74d2b
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <assert.h>
6
7 #include "../../../context.h"
8
9 int cpu_num = 1;
10 int length = 100;
11 int gpu_num = 0;
12 int CPU_ANY = -1;
13 int CPU_CUDA = -1;
14
15 void print_queue(struct Element* element) {
16 while (element) {
17 printf("%p\n", ((struct Task *)(element->data)));
18 element = element->next;
19 }
20 }
21
22 void print_tree(struct Node* node) {
23 if (node != 0) {
24 printf("%d\n", node->value->Array.index);
25 print_tree(node->left);
26 print_tree(node->right);
27 }
28 }
29
30 void *start_taskManager(struct Context *context) {
31 goto initDataGears(context, Gearef(context, LoopCounter), Gearef(context, TaskManager));
32 return 0;
33 }
34
35 #ifdef USE_CUDAWorker
36 #ifdef USE_CUDA_MAIN_THREAD
37 extern volatile int cuda_initialized;
38 #endif
39 #endif
40
41 __code initDataGears(struct LoopCounter* loopCounter, struct TaskManager* taskManager) {
42 // loopCounter->tree = createRedBlackTree(context);
43 loopCounter->i = 0;
44 taskManager->taskManager = (union Data*)createTaskManagerImpl(context, cpu_num, gpu_num, 0);
45 #ifdef USE_CUDAWorker
46 #ifdef USE_CUDA_MAIN_THREAD
47 while(! cuda_initialized) {};
48 #endif
49 #endif
50 goto meta(context, C_createTask1);
51 }
52
53 __code initDataGears_stub(struct Context* context) {
54 struct TaskManager* taskManager = Gearef(context, TaskManager);
55 taskManager->taskManager = 0;
56 #if (! defined(USE_CUDAWorker) || ! defined(USE_CUDA_MAIN_THREAD))
57 struct LoopCounter* loopCounter = Gearef(context, LoopCounter);
58 goto initDataGears(context, loopCounter, taskManager);
59 #else
60 cuda_initialized = 0;
61 pthread_t thread;
62 pthread_create(&thread, NULL, (void*)&start_taskManager, context);
63 while (taskManager->taskManager == 0);
64 TaskManager *t = (TaskManager*)taskManager->taskManager;
65 TaskManagerImpl *im = (TaskManagerImpl*)t->taskManager;
66 struct Queue *q = (Queue *)im->workers[0];
67 createCUDAWorker(context,0,q, im);
68 pthread_join(thread,0);
69 exit(0);
70 #endif
71 }
72
73 __code code1(struct Time* time) {
74 printf("cpus:\t\t%d\n", cpu_num);
75 printf("gpus:\t\t%d\n", gpu_num);
76 printf("length:\t\t%d\n", length);
77 /* puts("queue"); */
78 /* print_queue(context->data[ActiveQueue]->queue.first); */
79 /* puts("tree"); */
80 /* print_tree(context->data[Tree]->tree.root); */
81 /* puts("result"); */
82
83 //time->next = C_code2;
84 goto meta(context, C_exit_code);
85 //goto meta(context, C_start_time);
86 }
87
88 __code code1_stub(struct Context* context) {
89 goto code1(context, Gearef(context, Time));
90 }
91
92
93 __code createTask1(struct LoopCounter* loopCounter, struct TaskManager* taskManager) {
94 int i = loopCounter->i;
95
96 if (i < length) {
97 loopCounter->i++;
98 goto meta(context, C_createTask2);
99 }
100
101 loopCounter->i = 0;
102 taskManager->next = C_code1;
103 sleep(5);
104 goto meta(context, taskManager->taskManager->TaskManager.shutdown);
105 }
106
107 __code createTask2(struct LoopCounter* loopCounter, struct TaskManager* taskManager) {
108 struct Context** tasks = (struct Context**)ALLOC_ARRAY(context, Context, 3);
109
110 Integer* integer1 = &ALLOCATE_DATA_GEAR(context, Integer)->Integer;
111 Integer* integer2 = &ALLOCATE_DATA_GEAR(context, Integer)->Integer;
112 Integer* integer3 = &ALLOCATE_DATA_GEAR(context, Integer)->Integer;
113 // par goto mult(integer1, integer2, integer3, __exit);
114 struct Context* task = NEW(struct Context);
115 initContext(task);
116 task->next = C_mult;
117 task->idgCount = 2;
118 task->idg = task->dataNum;
119 task->data[task->idg] = (union Data*)integer1;
120 task->data[task->idg+1] = (union Data*)integer2;
121 task->maxIdg = task->idg + 2;
122 task->odg = task->maxIdg;
123 task->data[task->odg] = (union Data*)integer3;
124 task->maxOdg = task->odg + 1;
125 tasks[0] = task;
126
127 Integer* integer4 = &ALLOCATE_DATA_GEAR(context, Integer)->Integer;
128 Integer* integer5 = &ALLOCATE_DATA_GEAR(context, Integer)->Integer;
129 // par goto add(integer4, integer5, integer1, __exit);
130 task = NEW(struct Context);
131 initContext(task);
132 task->next = C_add;
133 task->idgCount = 2;
134 task->idg = task->dataNum;
135 task->data[task->idg] = (union Data*)integer4;
136 task->data[task->idg+1] = (union Data*)integer5;
137 task->maxIdg = task->idg + 2;
138 task->odg = task->maxIdg;
139 task->data[task->odg] = (union Data*)integer1;
140 task->maxOdg = task->odg + 1;
141 tasks[1] = task;
142
143 // par goto initIntegerDataGears(integer2, integer4, integer5, __exit);
144 task = NEW(struct Context);
145 initContext(task);
146 task->next = C_initIntegerDataGears;
147 task->idgCount = 0;
148 task->idg = task->dataNum;
149 task->maxIdg = task->idg;
150 task->odg = task->maxIdg;
151 task->data[task->odg] = (union Data*)integer2;
152 task->data[task->odg+1] = (union Data*)integer4;
153 task->data[task->odg+2] = (union Data*)integer5;
154 task->maxOdg = task->odg + 3;
155 tasks[2] = task;
156
157 taskManager->contexts = tasks;
158 // goto crateTask1();
159 taskManager->next1 = C_createTask1;
160 goto meta(context, taskManager->taskManager->TaskManager.spawnTasks);
161 }
162
163 void init(int argc, char** argv) {
164 for (int i = 1; argv[i]; ++i) {
165 if (strcmp(argv[i], "-cpu") == 0)
166 cpu_num = (int)atoi(argv[i+1]);
167 else if (strcmp(argv[i], "-l") == 0)
168 length = (int)atoi(argv[i+1]);
169 else if (strcmp(argv[i], "-cuda") == 0) {
170 gpu_num = 1;
171 CPU_CUDA = 0;
172 }
173 }
174 }
175
176 int main(int argc, char** argv) {
177 init(argc, argv);
178 struct Context* main_context = NEW(struct Context);
179 initContext(main_context);
180 main_context->next = C_initDataGears;
181
182 goto start_code(main_context);
183 }