Mercurial > hg > Members > Moririn
annotate src/parallel_execution/examples/twice.cbc @ 369:62a88864e1e2
little clean up bitonic_sort:)
author | Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 29 Jun 2017 22:21:19 +0900 |
parents | a0a3301bac4d |
children | fb50cf8aa615 |
rev | line source |
---|---|
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 | |
364
a0a3301bac4d
Add Time interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
363
diff
changeset
|
51 goto meta(context, C_code1); |
325 | 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"); */ | |
364
a0a3301bac4d
Add Time interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
363
diff
changeset
|
84 time->time = (union Data*)createTimeImpl(context); |
a0a3301bac4d
Add Time interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
363
diff
changeset
|
85 time->next = C_createTask1; |
a0a3301bac4d
Add Time interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
363
diff
changeset
|
86 goto meta(context, time->time->Time.start); |
a0a3301bac4d
Add Time interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
363
diff
changeset
|
87 //goto meta(context, C_createTask1); |
325 | 88 //goto meta(context, C_exit_code); |
89 //goto meta(context, C_start_time); | |
90 } | |
91 | |
92 __code code2(struct LoopCounter* loopCounter) { | |
93 int i = loopCounter->i; | |
94 | |
95 if (i < length) { | |
96 //printf("%d\n", array_ptr[i]); | |
97 if (array_ptr[i] == (i*2)) { | |
98 loopCounter->i++; | |
99 goto meta(context, C_code2); | |
100 } else | |
101 puts("wrong result"); | |
102 | |
103 } | |
104 | |
105 goto meta(context, C_exit_code); | |
106 } | |
107 | |
364
a0a3301bac4d
Add Time interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
363
diff
changeset
|
108 __code createTask1(struct LoopCounter* loopCounter, struct TaskManager* taskManager, struct Time* time) { |
325 | 109 int i = loopCounter->i; |
110 | |
111 if ((length/split*i) < length) { | |
363 | 112 goto meta(context, C_createTask2); |
325 | 113 } |
114 | |
115 loopCounter->i = 0; | |
364
a0a3301bac4d
Add Time interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
363
diff
changeset
|
116 taskManager->next = time->time->Time.end; |
a0a3301bac4d
Add Time interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
363
diff
changeset
|
117 time->next = C_code2; |
325 | 118 #if ( defined(USE_CUDAWorker) && defined(USE_CUDA_MAIN_THREAD)) |
119 sleep(5); | |
120 #endif | |
121 goto meta(context, taskManager->taskManager->TaskManager.shutdown); | |
122 } | |
123 | |
363 | 124 __code createTask2(struct LoopCounter* loopCounter, struct TaskManager* taskManager) { |
125 struct Context** tasks = (struct Context**)ALLOC_ARRAY(context, Context, 1); | |
126 | |
325 | 127 int i = loopCounter->i; |
363 | 128 LoopCounter* loopCounter2 = &ALLOCATE_DATA_GEAR(context, LoopCounter)->LoopCounter; |
129 Array* array = &ALLOCATE_DATA_GEAR(context, Array)->Array; | |
325 | 130 array->index = i; |
131 array->prefix = length/split; | |
132 array->array = array_ptr; | |
133 array->size = length; | |
134 loopCounter2->i = 0; | |
363 | 135 loopCounter->i++; |
136 | |
137 // par goto twice(loopCounter2, array, __exit); | |
138 struct Context* task = NEW(struct Context); | |
139 initContext(task); | |
140 task->taskManager = &taskManager->taskManager->TaskManager; | |
325 | 141 task->idgCount = 0; |
142 if (gpu_num) { | |
143 #ifdef USE_CUDAWorker | |
144 task->next = C_CUDAtwice; | |
145 task->workerId = CPU_CUDA; | |
146 #else | |
147 task->next = C_twice; | |
148 #endif | |
149 } else { | |
150 task->next = C_twice; | |
151 } | |
363 | 152 task->idg = task->dataNum; |
369
62a88864e1e2
little clean up bitonic_sort:)
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
364
diff
changeset
|
153 task->data[task->idg] = (union Data*)loopCounter2; |
62a88864e1e2
little clean up bitonic_sort:)
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
364
diff
changeset
|
154 task->data[task->idg+1] = (union Data*)array; |
363 | 155 task->maxIdg = task->idg + 2; |
156 task->odg = task->maxIdg; | |
325 | 157 task->maxOdg = task->odg; |
363 | 158 tasks[0] = task; |
325 | 159 |
363 | 160 taskManager->contexts = tasks; |
161 // goto crateTask1(); | |
162 taskManager->next1 = C_createTask1; | |
163 goto meta(context, taskManager->taskManager->TaskManager.spawnTasks); | |
325 | 164 } |
165 | |
166 void init(int argc, char** argv) { | |
167 for (int i = 1; argv[i]; ++i) { | |
168 if (strcmp(argv[i], "-cpu") == 0) | |
169 cpu_num = (int)atoi(argv[i+1]); | |
170 else if (strcmp(argv[i], "-l") == 0) | |
171 length = (int)atoi(argv[i+1]); | |
172 else if (strcmp(argv[i], "-s") == 0) | |
173 split = (int)atoi(argv[i+1]); | |
174 else if (strcmp(argv[i], "-cuda") == 0) { | |
175 gpu_num = 1; | |
176 CPU_CUDA = 0; | |
177 } | |
178 } | |
179 } | |
180 | |
181 | |
182 int main(int argc, char** argv) { | |
183 init(argc, argv); | |
184 | |
185 array_ptr = NEWN(length, int); | |
186 | |
187 for(int i=0; i<length; i++) | |
188 array_ptr[i]=i; | |
189 | |
190 struct Context* main_context = NEW(struct Context); | |
191 initContext(main_context); | |
192 main_context->next = C_initDataGears; | |
193 | |
194 goto start_code(main_context); | |
195 } |