comparison src/parallel_execution/main.c @ 95:3e28ee215c0e

modify twice, use OSAtomiceCompareAndSwap
author Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
date Fri, 29 Jan 2016 05:36:52 +0900
parents 851da1107223
children 8987cf13d5bb
comparison
equal deleted inserted replaced
92:851da1107223 95:3e28ee215c0e
1 #include <stdio.h> 1 #include <stdio.h>
2 #include <unistd.h> 2 #include <string.h>
3 3
4 #include "context.h" 4 #include "context.h"
5 #include "origin_cs.h" 5 #include "origin_cs.h"
6 6
7 extern __code initContext(struct Context* context); 7 extern __code initContext(struct Context* context);
8 extern void allocator(struct Context* context); 8 extern void allocator(struct Context* context);
9 9
10 int length; 10 int cpu_num = 1;
11 int length = 1024;
12 int split;
11 int* array_ptr; 13 int* array_ptr;
12 14
13 void print_queue(struct Element* element) { 15 void print_queue(struct Element* element) {
14 while (element) { 16 while (element) {
15 printf("%d\n", element->task->key); 17 printf("%d\n", element->task->key);
24 print_tree(node->right); 26 print_tree(node->right);
25 } 27 }
26 } 28 }
27 29
28 __code code1(struct Context* context) { 30 __code code1(struct Context* context) {
29 puts("queue"); 31 printf("cpus:\t\t%d\n", cpu_num);
30 print_queue(context->data[ActiveQueue]->queue.first); 32 printf("length:\t\t%d\n", length);
31 puts("tree"); 33 printf("length/task:\t%d\n", length/split);
32 print_tree(context->data[Tree]->tree.root); 34 /* puts("queue"); */
33 puts("result"); 35 /* print_queue(context->data[ActiveQueue]->queue.first); */
34 36 /* puts("tree"); */
35 goto meta(context, CreateWorker); 37 /* print_tree(context->data[Tree]->tree.root); */
38 /* puts("result"); */
39
40 context->next = CreateWorker;
41 stack_push(context->code_stack, &context->next);
42
43 goto meta(context, StartTime);
36 } 44 }
37 45
38 __code code1_stub(struct Context* context) { 46 __code code1_stub(struct Context* context) {
39 goto code1(context); 47 goto code1(context);
40 } 48 }
41 49
42 __code code2(struct Context* context, struct Array* array, struct LoopCounter* loopCounter) { 50 __code code2(struct Context* context, struct Array* array, struct LoopCounter* loopCounter) {
43 int i = loopCounter->i; 51 int i = loopCounter->i;
44 52
45 if (i < length) { 53 if (i < length) {
46 printf("%d\n", array->array[i]); 54 // printf("%d\n", array->array[i]);
47 loopCounter->i++; 55 if (array->array[i] == (i*2)) {
48 56 loopCounter->i++;
49 goto meta(context, Code2); 57 goto meta(context, Code2);
50 } 58 } else
51 59 puts("wrong result");
60
61 }
62
52 goto meta(context, Exit); 63 goto meta(context, Exit);
53 } 64 }
54 65
55 __code code2_stub(struct Context* context) { 66 __code code2_stub(struct Context* context) {
56 goto code2(context, &context->data[Node]->node.value->array, &context->data[LoopCounter]->loopCounter); 67 goto code2(context, &context->data[Node]->node.value->array, &context->data[LoopCounter]->loopCounter);
57 } 68 }
58 69
59 __code createData1(struct Context* context, struct Allocate* allocate, struct LoopCounter* loopCounter) { 70 __code createData1(struct Context* context, struct Allocate* allocate, struct LoopCounter* loopCounter) {
60 int i = loopCounter->i; 71 int i = loopCounter->i;
61 72
62 if (i < length) { 73 if ((length/split*i) < length) {
63 allocate->size = sizeof(struct Array); 74 allocate->size = sizeof(struct Array);
64 allocator(context); 75 allocator(context);
65 76
66 goto meta(context, CreateData2); 77 goto meta(context, CreateData2);
67 } 78 }
76 87
77 __code createData2(struct Context* context, struct LoopCounter* loopCounter, struct Array* array, struct Node* node) { 88 __code createData2(struct Context* context, struct LoopCounter* loopCounter, struct Array* array, struct Node* node) {
78 int i = loopCounter->i; 89 int i = loopCounter->i;
79 90
80 array->index = i; 91 array->index = i;
92 array->prefix = length/split;
81 array->array = array_ptr; 93 array->array = array_ptr;
82 94
83 node->key = i; 95 node->key = i;
84 node->value = (union Data*)array; 96 node->value = (union Data*)array;
85 97
86 context->next = CreateTask1; 98 context->next = CreateTask1;
87 99
88 goto meta(context, PutTree); 100 goto meta(context, PutTree);
89 } 101 }
90 102
182 } 194 }
183 } 195 }
184 196
185 __code putQueue4_stub(struct Context* context) { 197 __code putQueue4_stub(struct Context* context) {
186 goto putQueue4(context, &context->data[ActiveQueue]->queue, &context->data[context->dataNum]->element); 198 goto putQueue4(context, &context->data[ActiveQueue]->queue, &context->data[context->dataNum]->element);
187 }
188
189 __code getQueue(struct Context* context, struct Queue* queue, struct Node* node) {
190 if (queue->count == 0)
191 return;
192
193 struct Element* first = queue->first;
194 if (__sync_bool_compare_and_swap(&queue->first, first, first->next)) {
195 queue->count--;
196
197 context->next = GetQueue;
198 stack_push(context->code_stack, &context->next);
199
200 context->next = first->task->code;
201 node->key = first->task->key;
202
203 goto meta(context, Get);
204 } else {
205 goto meta(context, GetQueue);
206 }
207 }
208
209 __code getQueue_stub(struct Context* context) {
210 goto getQueue(context, &context->data[ActiveQueue]->queue, &context->data[Node]->node);
211 } 199 }
212 200
213 __code createWorker(struct Context* context, struct LoopCounter* loopCounter, struct Worker* worker) { 201 __code createWorker(struct Context* context, struct LoopCounter* loopCounter, struct Worker* worker) {
214 int i = loopCounter->i; 202 int i = loopCounter->i;
215 203
242 230
243 goto meta(context, TaskManager); 231 goto meta(context, TaskManager);
244 } 232 }
245 233
246 loopCounter->i = 0; 234 loopCounter->i = 0;
247 goto meta(context, Code2); 235
236 context->next = Code2;
237 stack_push(context->code_stack, &context->next);
238
239 goto meta(context, EndTime);
248 } 240 }
249 241
250 __code taskManager_stub(struct Context* context) { 242 __code taskManager_stub(struct Context* context) {
251 goto taskManager(context, &context->data[LoopCounter]->loopCounter, &context->data[Worker]->worker); 243 goto taskManager(context, &context->data[LoopCounter]->loopCounter, &context->data[Worker]->worker);
252 } 244 }
253 245
246 void init(int argc, char** argv) {
247 for (int i = 1; argv[i]; ++i) {
248 if (strcmp(argv[i], "-cpu") == 0)
249 cpu_num = (int)atoi(argv[i+1]);
250 else if (strcmp(argv[i], "-l") == 0)
251 length = (int)atoi(argv[i+1]);
252 else if (strcmp(argv[i], "-s") == 0)
253 split = (int)atoi(argv[i+1]);
254 }
255 }
256
257
254 int main(int argc, char** argv) { 258 int main(int argc, char** argv) {
255 int cpu_num = (int)atoi(argv[1]); 259 init(argc, argv);
256 length = (int)atoi(argv[2]);
257 260
258 array_ptr = (int*)malloc(sizeof(int)*length); 261 array_ptr = (int*)malloc(sizeof(int)*length);
259 262
260 for(int i=0; i<length; i++) 263 for(int i=0; i<length; i++)
261 array_ptr[i]=i; 264 array_ptr[i]=i;
262 265
263 struct Context* main_context = (struct Context*)malloc(sizeof(struct Context)); 266 struct Context* main_context = (struct Context*)malloc(sizeof(struct Context));
264 initContext(main_context); 267 initContext(main_context);
265 //main_context->next = CreateWorker;
266 main_context->next = CreateData1; 268 main_context->next = CreateData1;
267 269
268 struct Context* worker_contexts = (struct Context*)malloc(sizeof(struct Context)*cpu_num); 270 struct Context* worker_contexts = (struct Context*)malloc(sizeof(struct Context)*cpu_num);
269 271
270 struct Worker* worker = &main_context->data[Worker]->worker; 272 struct Worker* worker = &main_context->data[Worker]->worker;