86
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
1 #include <stdio.h>
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
2 #include <unistd.h>
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
3
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
4 #include "context.h"
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
5 #include "origin_cs.h"
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
6
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
7 extern __code initContext(struct Context* context);
|
90
|
8 extern void allocator(struct Context* context);
|
86
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
9
|
90
|
10 int length;
|
|
11 int* array_ptr;
|
|
12
|
|
13 void print_queue(struct Element* element) {
|
|
14 while (element) {
|
|
15 printf("%d\n", element->task->key);
|
|
16 element = element->next;
|
|
17 }
|
|
18 }
|
|
19
|
|
20 void print_tree(struct Node* node) {
|
|
21 if (node != 0) {
|
|
22 printf("%d\n", node->value->array.index);
|
|
23 print_tree(node->left);
|
|
24 print_tree(node->right);
|
|
25 }
|
|
26 }
|
86
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
27
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
28 __code code1(struct Context* context) {
|
90
|
29 print_queue(context->activeQueue->first);
|
|
30 puts("");
|
|
31 print_tree(context->tree->root);
|
|
32
|
|
33 goto meta(context, Exit);
|
86
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
34 }
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
35
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
36 __code code1_stub(struct Context* context) {
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
37 goto code1(context);
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
38 }
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
39
|
90
|
40 __code createData1(struct Context* context, struct Allocate* allocate, struct LoopCounter* loopCounter) {
|
|
41 int i = loopCounter->i;
|
|
42
|
|
43 if (i < length) {
|
|
44 allocate->size = sizeof(struct Array);
|
|
45 allocator(context);
|
|
46
|
|
47 goto meta(context, CreateData2);
|
|
48 }
|
|
49
|
|
50 goto meta(context, Code1);
|
|
51 }
|
|
52
|
|
53 __code createData1_stub(struct Context* context) {
|
|
54 goto createData1(context, &context->data[Allocate]->allocate, &context->data[LoopCounter]->loopCounter);
|
|
55 }
|
|
56
|
|
57 __code createData2(struct Context* context, struct LoopCounter* loopCounter, struct Array* array, struct Node* node) {
|
|
58 int i = loopCounter->i;
|
|
59
|
|
60 array->index = i;
|
|
61 array->array = array_ptr;
|
|
62
|
|
63 node->key = i;
|
|
64 node->value = (union Data*)array;
|
|
65
|
|
66 context->next = CreateTask1;
|
|
67
|
|
68 goto meta(context, PutTree);
|
|
69 }
|
|
70
|
|
71 __code createData2_stub(struct Context* context) {
|
|
72 goto createData2(context,
|
|
73 &context->data[LoopCounter]->loopCounter,
|
|
74 &context->data[context->dataNum]->array,
|
|
75 &context->data[Node]->node);
|
|
76 }
|
|
77
|
|
78 __code createTask1(struct Context* context, struct Allocate* allocate) {
|
|
79 allocate->size = sizeof(struct Task);
|
|
80 allocator(context);
|
|
81
|
|
82 goto meta(context, CreateTask2);
|
|
83 }
|
|
84
|
|
85 __code createTask1_stub(struct Context* context) {
|
|
86 goto createTask1(context, &context->data[Allocate]->allocate);
|
|
87 }
|
|
88
|
|
89 __code createTask2(struct Context* context, struct LoopCounter* loopCounter, struct Task* task, struct Element* element) {
|
|
90 int i = loopCounter->i;
|
|
91
|
|
92 task->code = Code1;
|
|
93 task->key = i;
|
|
94
|
|
95 element->task = task;
|
|
96
|
|
97 context->next = CreateData1;
|
|
98 loopCounter->i++;
|
|
99
|
|
100 goto meta(context, PutQueue1);
|
|
101 }
|
|
102
|
|
103 __code createTask2_stub(struct Context* context) {
|
|
104 goto createTask2(context,
|
|
105 &context->data[LoopCounter]->loopCounter,
|
|
106 &context->data[context->dataNum]->task,
|
|
107 &context->data[Element]->element);
|
|
108 }
|
|
109
|
|
110 __code putQueue1(struct Context* context, struct Allocate* allocate) {
|
|
111 allocate->size = sizeof(struct Element);
|
|
112 allocator(context);
|
|
113
|
|
114 goto meta(context, PutQueue2);
|
|
115 }
|
|
116
|
|
117 __code putQueue1_stub(struct Context* context) {
|
|
118 goto putQueue1(context, &context->data[Allocate]->allocate);
|
|
119 }
|
|
120
|
|
121 __code putQueue2(struct Context* context, struct Element* new_element, struct Element* element, struct Queue* queue) {
|
|
122 new_element->task = element->task;
|
|
123
|
|
124 if (queue->first)
|
|
125 goto meta(context, PutQueue3);
|
|
126 else
|
|
127 goto meta(context, PutQueue4);
|
|
128 }
|
|
129
|
|
130 __code putQueue2_stub(struct Context* context) {
|
|
131 goto putQueue2(context,
|
|
132 &context->data[context->dataNum]->element,
|
|
133 &context->data[Element]->element,
|
|
134 &context->data[ActiveQueue]->queue);
|
|
135 }
|
|
136
|
|
137 __code putQueue3(struct Context* context, struct Queue* queue, struct Element* new_element) {
|
|
138 struct Element* last = queue->last;
|
|
139
|
|
140 if (__sync_bool_compare_and_swap(&queue->last, last, new_element)) {
|
|
141 last->next = new_element;
|
|
142 queue->count++;
|
|
143
|
|
144 goto meta(context, context->next);
|
|
145 } else {
|
|
146 goto meta(context, PutQueue3);
|
|
147 }
|
|
148 }
|
|
149
|
|
150 __code putQueue3_stub(struct Context* context) {
|
|
151 goto putQueue3(context, &context->data[ActiveQueue]->queue, &context->data[context->dataNum]->element);
|
|
152 }
|
|
153
|
|
154 __code putQueue4(struct Context* context, struct Queue* queue, struct Element* new_element) {
|
|
155 if (__sync_bool_compare_and_swap(&queue->first, 0, new_element)) {
|
|
156 queue->last = new_element;
|
|
157 queue->count++;
|
|
158
|
|
159 goto meta(context, context->next);
|
|
160 } else {
|
|
161 goto meta(context, PutQueue3);
|
|
162 }
|
|
163 }
|
|
164
|
|
165 __code putQueue4_stub(struct Context* context) {
|
|
166 goto putQueue4(context, &context->data[ActiveQueue]->queue, &context->data[context->dataNum]->element);
|
|
167 }
|
|
168
|
86
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
169 __code createWorker(struct Context* context, struct LoopCounter* loopCounter, struct Worker* worker) {
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
170 int i = loopCounter->i;
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
171
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
172 if (i < worker->num) {
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
173 struct Context* worker_context = &worker->contexts[i];
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
174 worker_context->next = Code1;
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
175 pthread_create(&worker_context->thread, NULL, (void*)&start_code, worker_context);
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
176 loopCounter->i++;
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
177
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
178 goto meta(context, CreateWorker);
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
179 }
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
180
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
181 loopCounter->i = 0;
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
182 goto meta(context, TaskManager);
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
183 }
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
184
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
185 __code createWorker_stub(struct Context* context) {
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
186 goto createWorker(context, &context->data[LoopCounter]->loopCounter, &context->data[Worker]->worker);
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
187 }
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
188
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
189 __code taskManager(struct Context* context, struct LoopCounter* loopCounter, struct Worker* worker) {
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
190 int i = loopCounter->i;
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
191
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
192 if (i < worker->num) {
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
193 pthread_join(worker->contexts[i].thread, NULL);
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
194 loopCounter->i++;
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
195
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
196 goto meta(context, TaskManager);
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
197 }
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
198
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
199 loopCounter->i = 0;
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
200 goto meta(context, Exit);
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
201 }
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
202
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
203 __code taskManager_stub(struct Context* context) {
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
204 goto taskManager(context, &context->data[LoopCounter]->loopCounter, &context->data[Worker]->worker);
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
205 }
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
206
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
207 int main(int argc, char** argv) {
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
208 int cpu_num = (int)atoi(argv[1]);
|
90
|
209 length = (int)atoi(argv[2]);
|
|
210
|
|
211 array_ptr = (int*)malloc(sizeof(int)*length);
|
|
212
|
|
213 for(int i=0; i<length; i++)
|
|
214 array_ptr[i]=i;
|
86
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
215
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
216 struct Context* main_context = (struct Context*)malloc(sizeof(struct Context));
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
217 initContext(main_context);
|
90
|
218 //main_context->next = CreateWorker;
|
|
219 main_context->next = CreateData1;
|
86
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
220
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
221 struct Context* worker_contexts = (struct Context*)malloc(sizeof(struct Context)*cpu_num);
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
222
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
223 struct Worker* worker = &main_context->data[Worker]->worker;
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
224 worker->num = cpu_num;
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
225 worker->contexts = worker_contexts;
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
226
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
227 for (int i = 0;i<cpu_num;i++)
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
228 initContext(&worker_contexts[i]);
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
229
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
230 goto start_code(main_context);
|
Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
parents:
diff
changeset
|
231 }
|