Mercurial > hg > Members > Moririn
annotate src/parallel_execution/examples/bitonicSort.cbc @ 375:ad44fdb11433
Fix compile error but not work
author | Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Tue, 18 Jul 2017 16:13:11 +0900 |
parents | fb50cf8aa615 |
children | b46f38645587 |
rev | line source |
---|---|
371 | 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 = 100; | |
10 int gpu_num = 0; | |
11 int CPU_ANY = -1; | |
12 int CPU_CUDA = -1; | |
13 | |
14 void *start_taskManager(struct Context *context) { | |
15 goto initDataGears(context, Gearef(context, LoopCounter), Gearef(context, TaskManager)); | |
16 return 0; | |
17 } | |
18 | |
19 #ifdef USE_CUDAWorker | |
20 #ifdef USE_CUDA_MAIN_THREAD | |
21 extern volatile int cuda_initialized; | |
22 #endif | |
23 #endif | |
24 | |
25 __code initDataGears(struct LoopCounter* loopCounter, struct TaskManager* taskManager) { | |
26 // loopCounter->tree = createRedBlackTree(context); | |
27 loopCounter->i = 0; | |
28 taskManager->taskManager = (union Data*)createTaskManagerImpl(context, cpu_num, gpu_num, 0); | |
29 #ifdef USE_CUDAWorker | |
30 #ifdef USE_CUDA_MAIN_THREAD | |
31 while(! cuda_initialized) {}; | |
32 #endif | |
33 #endif | |
374
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
34 goto meta(context, C_code1); |
371 | 35 } |
36 | |
37 __code initDataGears_stub(struct Context* context) { | |
38 struct TaskManager* taskManager = Gearef(context, TaskManager); | |
39 taskManager->taskManager = 0; | |
40 #if (! defined(USE_CUDAWorker) || ! defined(USE_CUDA_MAIN_THREAD)) | |
41 struct LoopCounter* loopCounter = Gearef(context, LoopCounter); | |
42 goto initDataGears(context, loopCounter, taskManager); | |
43 #else | |
44 cuda_initialized = 0; | |
45 pthread_t thread; | |
46 pthread_create(&thread, NULL, (void*)&start_taskManager, context); | |
47 while (taskManager->taskManager == 0); | |
48 TaskManager *t = (TaskManager*)taskManager->taskManager; | |
49 TaskManagerImpl *im = (TaskManagerImpl*)t->taskManager; | |
50 struct Queue *q = (Queue *)im->workers[0]; | |
51 createCUDAWorker(context,0,q, im); | |
52 pthread_join(thread,0); | |
53 exit(0); | |
54 #endif | |
55 } | |
56 | |
57 __code code1(struct Time* time) { | |
58 printf("cpus:\t\t%d\n", cpu_num); | |
59 printf("gpus:\t\t%d\n", gpu_num); | |
60 printf("length:\t\t%d\n", length); | |
61 /* puts("queue"); */ | |
62 /* print_queue(context->data[ActiveQueue]->queue.first); */ | |
63 /* puts("tree"); */ | |
64 /* print_tree(context->data[Tree]->tree.root); */ | |
65 /* puts("result"); */ | |
66 | |
374
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
67 time->time = (union Data*)createTimeImpl(context); |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
68 time->next = C_createTask1; |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
69 goto meta(context, time->time->Time.start); |
371 | 70 } |
71 | |
374
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
72 //__code createTask1(struct LoopCounter* loopCounter, struct TaskManager* taskManager) { |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
73 // int waitFlag = 0; |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
74 // struct SortArray* inputSortArray = &ALLOCATE_DATA_GEAR(context, sortArray)->sortArray; |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
75 // inputSortArray->array = array; |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
76 // for (int i=2; i <= length; i=2*i) { |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
77 // int first = 1; |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
78 // lastOne = (length <= i*2) ? true : false; |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
79 // for (int j=i>>1; 0 < j; j=j>>1) { |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
80 // struct SortArray* outputSortArray = &ALLOCATE_DATA_GEAR(context, sortArray)->sortArray; |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
81 // outputSortArray->array = array; |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
82 // for(int k=0; 0 < length/2; k++) { |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
83 // struct Context* task = NEW(struct Context); |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
84 // struct Integer* integer1 = &ALLOCATE_DATA_GEAR(context, Integer)->Integer; |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
85 // struct Integer* integer2 = &ALLOCATE_DATA_GEAR(context, Integer)->Integer; |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
86 // struct Integer* integer3 = &ALLOCATE_DATA_GEAR(context, Integer)->Integer; |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
87 // integer1->value = j; |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
88 // integer2->value = first; |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
89 // integer3->value = k; |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
90 // |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
91 // task->next = C_swap; |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
92 // if (waitFlag) { |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
93 // task->idgCount = length/2; |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
94 // } else { |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
95 // task->idgCount = 0; |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
96 // } |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
97 // task->idg = task->dataNum; |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
98 // task->data[task->idg] = (union Data*)inputSortArray; |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
99 // task->data[task->idg+1] = (union Data*)integer1; |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
100 // task->data[task->idg+2] = (union Data*)integer2; |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
101 // task->data[task->idg+3] = (union Data*)integer3; |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
102 // task->maxIdg = task->idg + 4; |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
103 // task->odg = task->maxIdg; |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
104 // task->data[task->odg] = outputSortArray; |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
105 // task->maxOdg = task->odg + 1; |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
106 // first = 0; |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
107 // } |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
108 // inputSortArray = outputSortArray; |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
109 // } |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
110 // waitFlag = 1; |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
111 // } |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
112 // goto meta(context, taskManager->taskManager->TaskManager.shutdown); |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
113 //} |
371 | 114 |
374
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
115 __code createTask1(struct LoopCounter* loopCounter, struct TaskManager* taskManager, struct Time* time) { |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
116 int i = loopCounter->i; |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
117 |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
118 if (i < length) { |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
119 loopCounter->i++; |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
120 goto meta(context, C_createTask2); |
371 | 121 } |
374
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
122 |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
123 loopCounter->i = 0; |
375
ad44fdb11433
Fix compile error but not work
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
374
diff
changeset
|
124 taskManager->next = C_exit_code; |
371 | 125 goto meta(context, taskManager->taskManager->TaskManager.shutdown); |
126 } | |
127 | |
128 __code createTask2(struct LoopCounter* loopCounter, struct TaskManager* taskManager) { | |
374
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
129 struct Context** tasks = (struct Context**)ALLOC_ARRAY(context, Context, 1); |
371 | 130 struct Context* task = NEW(struct Context); |
374
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
131 task->iterator = createOneDimIterator(context, 10); |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
132 task->next = C_printIterator; |
371 | 133 task->idgCount = 0; |
134 task->idg = task->dataNum; | |
135 task->maxIdg = task->idg; | |
136 task->odg = task->maxIdg; | |
374
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
137 task->maxOdg = task->odg; |
fb50cf8aa615
Add Iterator Interface
Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
parents:
371
diff
changeset
|
138 tasks[0] = task; |
371 | 139 taskManager->contexts = tasks; |
140 taskManager->next1 = C_createTask1; | |
141 goto meta(context, taskManager->taskManager->TaskManager.spawnTasks); | |
142 } | |
143 | |
144 void init(int argc, char** argv) { | |
145 for (int i = 1; argv[i]; ++i) { | |
146 if (strcmp(argv[i], "-cpu") == 0) | |
147 cpu_num = (int)atoi(argv[i+1]); | |
148 else if (strcmp(argv[i], "-l") == 0) | |
149 length = (int)atoi(argv[i+1]); | |
150 else if (strcmp(argv[i], "-cuda") == 0) { | |
151 gpu_num = 1; | |
152 CPU_CUDA = 0; | |
153 } | |
154 } | |
155 } | |
156 | |
157 int main(int argc, char** argv) { | |
158 init(argc, argv); | |
159 struct Context* main_context = NEW(struct Context); | |
160 initContext(main_context); | |
161 main_context->next = C_initDataGears; | |
162 | |
163 goto start_code(main_context); | |
164 } |