comparison src/parallel_execution/main.cbc @ 277:9d671e63df74

generate extern
author mir3636
date Thu, 02 Feb 2017 18:29:50 +0900
parents src/parallel_execution/main.c@cd3486e4ba70
children 23767f714f4a
comparison
equal deleted inserted replaced
276:27bc962020de 277:9d671e63df74
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4
5 #include "context.h"
6 #include "origin_cs.h"
7
8 extern union Data* createRedBlackTree(struct Context* context);
9 extern union Data* createTaskManager(struct Context* context, int numCPU, int numGPU, int numIO);
10 extern void allocator(struct Context* context);
11
12 int cpu_num = 1;
13 int length = 102400;
14 int split = 8;
15 int* array_ptr;
16
17 void print_queue(struct Element* element) {
18 while (element) {
19 printf("%p\n", ((struct Task *)(element->data)));
20 element = element->next;
21 }
22 }
23
24 void print_tree(struct Node* node) {
25 if (node != 0) {
26 printf("%d\n", node->value->Array.index);
27 print_tree(node->left);
28 print_tree(node->right);
29 }
30 }
31
32 __code initDataGears(struct Context* context, struct LoopCounter* loopCounter, struct TaskManager* taskManager) {
33 loopCounter->tree = &createRedBlackTree(context)->Tree;
34 loopCounter->i = 0;
35 taskManager->taskManager = (union Data*)&createTaskManager(context, cpu_num, 0, 0)->TaskManager;
36 goto meta(context, C_createTask1);
37 }
38
39 __code initDataGears_stub(struct Context* context) {
40 goto initDataGears(context, Gearef(context, LoopCounter), Gearef(context, TaskManager));
41 }
42
43 __code code1(struct Context* context, struct Time* time) {
44 printf("cpus:\t\t%d\n", cpu_num);
45 printf("length:\t\t%d\n", length);
46 printf("length/task:\t%d\n", length/split);
47 /* puts("queue"); */
48 /* print_queue(context->data[ActiveQueue]->queue.first); */
49 /* puts("tree"); */
50 /* print_tree(context->data[Tree]->tree.root); */
51 /* puts("result"); */
52
53 time->next = C_code2;
54 goto meta(context, C_exit_code);
55 //goto meta(context, C_start_time);
56 }
57
58 __code code1_stub(struct Context* context) {
59 goto code1(context, Gearef(context, Time));
60 }
61
62 __code code2(struct Context* context, struct Array* array, struct LoopCounter* loopCounter) {
63 int i = loopCounter->i;
64
65 if (i < length) {
66 //printf("%d\n", array->array[i]);
67 if (array->array[i] == (i*2)) {
68 loopCounter->i++;
69 goto meta(context, C_code2);
70 } else
71 puts("wrong result");
72
73 }
74
75 goto meta(context, C_code2);
76 }
77
78 __code code2_stub(struct Context* context) {
79 goto code2(context, &context->data[D_Node]->Node.value->Array, &context->data[D_LoopCounter]->LoopCounter);
80 }
81
82 __code createData1(struct Context* context, struct Allocate* allocate, struct LoopCounter* loopCounter) {
83 int i = loopCounter->i;
84
85 if ((length/split*i) < length) {
86 goto meta(context, C_createData2);
87 }
88
89 loopCounter->i = 0;
90 goto meta(context, C_code1);
91 }
92
93 __code createData1_stub(struct Context* context) {
94 goto createData1(context, Gearef(context, Allocate), Gearef(context, LoopCounter));
95 }
96
97 __code createData2(struct Context* context, struct LoopCounter* loopCounter, struct Array* array, struct Node* node, Tree* tree) {
98 int i = loopCounter->i;
99
100 array->index = i;
101 array->prefix = length/split;
102 array->array = array_ptr;
103
104 node->key = i;
105 node->value = (union Data*)array;
106
107 tree->tree = (union Data*)loopCounter->tree;
108
109 tree->next = C_createTask1;
110 tree->node = node;
111
112 goto meta(context, loopCounter->tree->put);
113 }
114
115 __code createData2_stub(struct Context* context) {
116 Array* array = &ALLOCATE(context, Array)->Array;
117 goto createData2(context,
118 Gearef(context, LoopCounter),
119 array,
120 Gearef(context, Node),
121 Gearef(context, Tree));
122 }
123
124 __code createTask1(struct Context* context, struct LoopCounter* loopCounter, struct TaskManager* taskManager) {
125 int i = loopCounter->i;
126
127 if ((length/split*i) < length) {
128 taskManager->next = C_createTask2;
129 goto meta(context, taskManager->taskManager->TaskManager.createTask);
130 }
131
132 loopCounter->i = 0;
133 goto meta(context, C_code1);
134 }
135
136 __code createTask1_stub(struct Context* context) {
137 goto createTask1(context,
138 Gearef(context, LoopCounter),
139 Gearef(context, TaskManager));
140 }
141
142 __code createTask2(struct Context* context, LoopCounter* loopCounter, TaskManager* taskManager,struct Context* task, Array* array) {
143 int i = loopCounter->i;
144
145 if ((length/split*i) < length) {
146 array->index = i;
147 array->prefix = length/split;
148 array->array = array_ptr;
149 task->idgCount = 0;
150 task->next = C_twice;
151 // task->data[task->idg] = (union Data*)array;
152 taskManager->next = C_createTask1;
153 loopCounter->i++;
154
155 goto meta(context, taskManager->taskManager->TaskManager.spawn);
156 }
157 loopCounter->i = 0;
158 taskManager->next = C_code1;
159 goto meta(context, taskManager->taskManager->TaskManager.shutdown);
160 }
161
162 __code createTask2_stub(struct Context* context) {
163 Array* array = &ALLOCATE(context, Array)->Array;
164 goto createTask2(context,
165 Gearef(context, LoopCounter),
166 Gearef(context, TaskManager),
167 Gearef(context, TaskManager)->context,
168 array);
169 }
170
171 void init(int argc, char** argv) {
172 for (int i = 1; argv[i]; ++i) {
173 if (strcmp(argv[i], "-cpu") == 0)
174 cpu_num = (int)atoi(argv[i+1]);
175 else if (strcmp(argv[i], "-l") == 0)
176 length = (int)atoi(argv[i+1]);
177 else if (strcmp(argv[i], "-s") == 0)
178 split = (int)atoi(argv[i+1]);
179 }
180 }
181
182
183 int main(int argc, char** argv) {
184 init(argc, argv);
185
186 array_ptr = NEWN(length, int);
187
188 for(int i=0; i<length; i++)
189 array_ptr[i]=i;
190
191 struct Context* main_context = NEW(struct Context);
192 initContext(main_context);
193 main_context->next = C_initDataGears;
194
195 goto start_code(main_context);
196 }
197