Mercurial > hg > Gears > GearsAgda
comparison src/parallel_execution/examples/twice/main.cbc @ 409:4d1e3697a6b8
Add twice cbc file
author | Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Wed, 06 Sep 2017 22:01:27 +0900 |
parents | |
children | 85b0ddbf458e |
comparison
equal
deleted
inserted
replaced
408:8ee89eefbc6d | 409:4d1e3697a6b8 |
---|---|
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 *start_taskManager(struct Context *context) { | |
17 goto initDataGears(context, Gearef(context, LoopCounter), Gearef(context, TaskManager)); | |
18 return 0; | |
19 } | |
20 | |
21 #ifdef USE_CUDAWorker | |
22 #ifdef USE_CUDA_MAIN_THREAD | |
23 extern volatile int cuda_initialized; | |
24 #endif | |
25 #endif | |
26 | |
27 __code initDataGears(struct LoopCounter* loopCounter, struct TaskManager* taskManager) { | |
28 // loopCounter->tree = createRedBlackTree(context); | |
29 loopCounter->i = 0; | |
30 taskManager->taskManager = (union Data*)createTaskManagerImpl(context, cpu_num, gpu_num, 0); | |
31 #ifdef USE_CUDAWorker | |
32 #ifdef USE_CUDA_MAIN_THREAD | |
33 while(! cuda_initialized) {}; | |
34 #endif | |
35 #endif | |
36 goto meta(context, C_code1); | |
37 } | |
38 | |
39 __code initDataGears_stub(struct Context* context) { | |
40 struct TaskManager* taskManager = Gearef(context, TaskManager); | |
41 taskManager->taskManager = 0; | |
42 #if (! defined(USE_CUDAWorker) || ! defined(USE_CUDA_MAIN_THREAD)) | |
43 struct LoopCounter* loopCounter = Gearef(context, LoopCounter); | |
44 goto initDataGears(context, loopCounter, taskManager); | |
45 #else | |
46 cuda_initialized = 0; | |
47 pthread_t thread; | |
48 pthread_create(&thread, NULL, (void*)&start_taskManager, context); | |
49 while (taskManager->taskManager == 0); | |
50 TaskManager *t = (TaskManager*)taskManager->taskManager; | |
51 TaskManagerImpl *im = (TaskManagerImpl*)t->taskManager; | |
52 struct Queue *q = (Queue *)im->workers[0]; | |
53 createCUDAWorker(context,0,q, im); | |
54 pthread_join(thread,0); | |
55 exit(0); | |
56 #endif | |
57 } | |
58 | |
59 __code code1(struct Time* time) { | |
60 printf("cpus:\t\t%d\n", cpu_num); | |
61 printf("gpus:\t\t%d\n", gpu_num); | |
62 printf("length:\t\t%d\n", length); | |
63 printf("length/task:\t%d\n", length/split); | |
64 /* puts("queue"); */ | |
65 /* print_queue(context->data[ActiveQueue]->queue.first); */ | |
66 /* puts("tree"); */ | |
67 /* print_tree(context->data[Tree]->tree.root); */ | |
68 /* puts("result"); */ | |
69 time->time = (union Data*)createTimeImpl(context); | |
70 time->next = C_createTask1; | |
71 goto meta(context, time->time->Time.start); | |
72 } | |
73 | |
74 __code code2(struct Time* time, struct TaskManager* taskManager) { | |
75 time->next = C_code3; | |
76 taskManager->next = time->time->Time.end; | |
77 goto meta(context, taskManager->taskManager->TaskManager.shutdown); | |
78 } | |
79 | |
80 __code code3(struct LoopCounter* loopCounter) { | |
81 int i = loopCounter->i; | |
82 | |
83 if (i < length) { | |
84 //printf("%d\n", array_ptr[i]); | |
85 if (array_ptr[i] == (i*2)) { | |
86 loopCounter->i++; | |
87 goto meta(context, C_code3); | |
88 } else | |
89 puts("wrong result"); | |
90 | |
91 } | |
92 | |
93 goto meta(context, C_exit_code); | |
94 } | |
95 | |
96 __code createTask1(struct LoopCounter* loopCounter, struct TaskManager* taskManager) { | |
97 Array* array = &ALLOCATE_DATA_GEAR(context, Array)->Array; | |
98 | |
99 par goto createArray(array, __exit); | |
100 | |
101 par goto twice(array, iterate(split), __exit); | |
102 goto code2(); | |
103 } | |
104 | |
105 void init(int argc, char** argv) { | |
106 for (int i = 1; argv[i]; ++i) { | |
107 if (strcmp(argv[i], "-cpu") == 0) | |
108 cpu_num = (int)atoi(argv[i+1]); | |
109 else if (strcmp(argv[i], "-l") == 0) | |
110 length = (int)atoi(argv[i+1]); | |
111 else if (strcmp(argv[i], "-s") == 0) | |
112 split = (int)atoi(argv[i+1]); | |
113 else if (strcmp(argv[i], "-cuda") == 0) { | |
114 gpu_num = 1; | |
115 CPU_CUDA = 0; | |
116 } | |
117 } | |
118 } | |
119 | |
120 | |
121 int main(int argc, char** argv) { | |
122 init(argc, argv); | |
123 | |
124 array_ptr = NEWN(length, int); | |
125 | |
126 for(int i=0; i<length; i++) | |
127 array_ptr[i]=i; | |
128 | |
129 struct Context* main_context = NEW(struct Context); | |
130 initContext(main_context); | |
131 main_context->next = C_initDataGears; | |
132 | |
133 goto start_code(main_context); | |
134 } |