diff src/parallel_execution/main.c @ 90:4b5bf5b40970

put queue
author Shohei KOKUBO <e105744@ie.u-ryukyu.ac.jp>
date Tue, 26 Jan 2016 06:47:35 +0900
parents 9e139a340bd1
children 1e074c3878c7
line wrap: on
line diff
--- a/src/parallel_execution/main.c	Tue Jan 19 18:05:15 2016 +0900
+++ b/src/parallel_execution/main.c	Tue Jan 26 06:47:35 2016 +0900
@@ -5,23 +5,167 @@
 #include "origin_cs.h"
 
 extern __code initContext(struct Context* context);
+extern void allocator(struct Context* context);
 
-int a;
+int length;
+int* array_ptr;
+
+void print_queue(struct Element* element) {
+    while (element) {
+        printf("%d\n", element->task->key);
+        element = element->next;
+    }
+}
+
+void print_tree(struct Node* node) {
+    if (node != 0) {
+        printf("%d\n", node->value->array.index);
+        print_tree(node->left);
+        print_tree(node->right);
+    }
+}
 
 __code code1(struct Context* context) {
-    if(__sync_bool_compare_and_swap(&a, 10, 0)) {
-        puts("success");
-        a = 10;
-    } else {
-        puts("failure");
-        goto meta(context, Code1);
-    }
+    print_queue(context->activeQueue->first);
+    puts("");
+    print_tree(context->tree->root);
+
+    goto meta(context, Exit);
 }
 
 __code code1_stub(struct Context* context) {
     goto code1(context);
 }
 
+__code createData1(struct Context* context, struct Allocate* allocate, struct LoopCounter* loopCounter) {
+    int i = loopCounter->i;
+
+    if (i < length) {
+        allocate->size = sizeof(struct Array);
+        allocator(context);
+
+        goto meta(context, CreateData2);
+    }
+
+    goto meta(context, Code1);
+}
+        
+__code createData1_stub(struct Context* context) {
+    goto createData1(context, &context->data[Allocate]->allocate, &context->data[LoopCounter]->loopCounter);
+}
+
+__code createData2(struct Context* context, struct LoopCounter* loopCounter, struct Array* array, struct Node* node) {
+    int i = loopCounter->i;
+
+    array->index = i;
+    array->array = array_ptr;
+
+    node->key = i;
+    node->value = (union Data*)array;
+
+    context->next = CreateTask1;
+
+    goto meta(context, PutTree);
+}
+
+__code createData2_stub(struct Context* context) {
+    goto createData2(context,
+                     &context->data[LoopCounter]->loopCounter,
+                     &context->data[context->dataNum]->array,
+                     &context->data[Node]->node);
+}
+
+__code createTask1(struct Context* context, struct Allocate* allocate) {
+    allocate->size = sizeof(struct Task);
+    allocator(context);
+
+    goto meta(context, CreateTask2);
+}
+
+__code createTask1_stub(struct Context* context) {
+    goto createTask1(context, &context->data[Allocate]->allocate);
+}
+
+__code createTask2(struct Context* context, struct LoopCounter* loopCounter, struct Task* task, struct Element* element) {
+    int i = loopCounter->i;
+    
+    task->code = Code1;
+    task->key = i;
+    
+    element->task = task;
+
+    context->next = CreateData1;
+    loopCounter->i++;
+    
+    goto meta(context, PutQueue1);
+}
+
+__code createTask2_stub(struct Context* context) {
+    goto createTask2(context,
+                     &context->data[LoopCounter]->loopCounter,
+                     &context->data[context->dataNum]->task,
+                     &context->data[Element]->element);
+}
+
+__code putQueue1(struct Context* context, struct Allocate* allocate) {
+    allocate->size = sizeof(struct Element);
+    allocator(context);
+
+    goto meta(context, PutQueue2);
+}
+
+__code putQueue1_stub(struct Context* context) {
+    goto putQueue1(context, &context->data[Allocate]->allocate);
+}
+
+__code putQueue2(struct Context* context, struct Element* new_element, struct Element* element, struct Queue* queue) {
+    new_element->task = element->task;
+
+    if (queue->first)
+        goto meta(context, PutQueue3);
+    else
+        goto meta(context, PutQueue4);
+}
+
+__code putQueue2_stub(struct Context* context) {
+    goto putQueue2(context,
+                   &context->data[context->dataNum]->element,
+                   &context->data[Element]->element,
+                   &context->data[ActiveQueue]->queue);
+}
+
+__code putQueue3(struct Context* context, struct Queue* queue, struct Element* new_element) {
+    struct Element* last = queue->last;
+
+    if (__sync_bool_compare_and_swap(&queue->last, last, new_element)) {
+        last->next = new_element;
+        queue->count++;
+        
+        goto meta(context, context->next);
+    } else {
+        goto meta(context, PutQueue3);
+    }
+}
+
+__code putQueue3_stub(struct Context* context) {
+    goto putQueue3(context, &context->data[ActiveQueue]->queue, &context->data[context->dataNum]->element);
+}
+
+__code putQueue4(struct Context* context, struct Queue* queue, struct Element* new_element) {
+    if (__sync_bool_compare_and_swap(&queue->first, 0, new_element)) {
+        queue->last = new_element;
+        queue->count++;
+        
+        goto meta(context, context->next);
+    } else {
+        goto meta(context, PutQueue3);
+    }
+}
+
+__code putQueue4_stub(struct Context* context) {
+    goto putQueue4(context, &context->data[ActiveQueue]->queue, &context->data[context->dataNum]->element);
+}
+
 __code createWorker(struct Context* context, struct LoopCounter* loopCounter, struct Worker* worker) {
     int i = loopCounter->i;
 
@@ -61,12 +205,18 @@
 }
 
 int main(int argc, char** argv) {
-    a = 10;
     int cpu_num = (int)atoi(argv[1]);
+    length = (int)atoi(argv[2]);
+
+    array_ptr = (int*)malloc(sizeof(int)*length);
+
+    for(int i=0; i<length; i++)
+        array_ptr[i]=i;
 
     struct Context* main_context = (struct Context*)malloc(sizeof(struct Context));
     initContext(main_context);
-    main_context->next = CreateWorker;
+    //main_context->next = CreateWorker;
+    main_context->next = CreateData1;
 
     struct Context* worker_contexts = (struct Context*)malloc(sizeof(struct Context)*cpu_num);