325
|
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 #if ( defined(USE_CUDAWorker) && defined(USE_CUDA_MAIN_THREAD))
|
|
122 sleep(5);
|
|
123 #endif
|
|
124 goto meta(context, taskManager->taskManager->TaskManager.shutdown);
|
|
125 }
|
|
126
|
|
127 __code createTask2(LoopCounter* loopCounter, TaskManager* taskManager,struct Context* task, LoopCounter* loopCounter2, Array* array) {
|
|
128 int i = loopCounter->i;
|
|
129 array->index = i;
|
|
130 array->prefix = length/split;
|
|
131 array->array = array_ptr;
|
|
132 array->size = length;
|
|
133 loopCounter2->i = 0;
|
|
134 task->idgCount = 0;
|
|
135 if (gpu_num) {
|
|
136 #ifdef USE_CUDAWorker
|
|
137 task->next = C_CUDAtwice;
|
|
138 task->workerId = CPU_CUDA;
|
|
139 #else
|
|
140 task->next = C_twice;
|
|
141 #endif
|
|
142 } else {
|
|
143 task->next = C_twice;
|
|
144 }
|
|
145 task->data[task->dataNum] = (union Data*)loopCounter2;
|
|
146 task->data[task->dataNum+1] = (union Data*)array;
|
|
147 task->odg = task->dataNum + 2;
|
|
148 task->maxOdg = task->odg;
|
|
149 taskManager->next = C_createTask1;
|
|
150 loopCounter->i++;
|
|
151 goto meta(context, taskManager->taskManager->TaskManager.spawn);
|
|
152 }
|
|
153
|
|
154 __code createTask2_stub(struct Context* context) {
|
|
155 LoopCounter* loopCounter = &ALLOCATE(context, LoopCounter)->LoopCounter;
|
|
156 Array* array = &ALLOCATE(context, Array)->Array;
|
|
157 goto createTask2(context,
|
|
158 Gearef(context, LoopCounter),
|
|
159 Gearef(context, TaskManager),
|
|
160 Gearef(context, TaskManager)->context,
|
|
161 loopCounter,
|
|
162 array);
|
|
163 }
|
|
164
|
|
165 void init(int argc, char** argv) {
|
|
166 for (int i = 1; argv[i]; ++i) {
|
|
167 if (strcmp(argv[i], "-cpu") == 0)
|
|
168 cpu_num = (int)atoi(argv[i+1]);
|
|
169 else if (strcmp(argv[i], "-l") == 0)
|
|
170 length = (int)atoi(argv[i+1]);
|
|
171 else if (strcmp(argv[i], "-s") == 0)
|
|
172 split = (int)atoi(argv[i+1]);
|
|
173 else if (strcmp(argv[i], "-cuda") == 0) {
|
|
174 gpu_num = 1;
|
|
175 CPU_CUDA = 0;
|
|
176 }
|
|
177 }
|
|
178 }
|
|
179
|
|
180
|
|
181 int main(int argc, char** argv) {
|
|
182 init(argc, argv);
|
|
183
|
|
184 array_ptr = NEWN(length, int);
|
|
185
|
|
186 for(int i=0; i<length; i++)
|
|
187 array_ptr[i]=i;
|
|
188
|
|
189 struct Context* main_context = NEW(struct Context);
|
|
190 initContext(main_context);
|
|
191 main_context->next = C_initDataGears;
|
|
192
|
|
193 goto start_code(main_context);
|
|
194 }
|