diff src/parallel_execution/main.c @ 138:337fdbffa693 default tip

Merge
author Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
date Sat, 01 Oct 2016 00:23:35 +0900
parents a574ba0da60f 77e60b6cdace
children
line wrap: on
line diff
--- a/src/parallel_execution/main.c	Thu Sep 29 22:22:36 2016 +0900
+++ b/src/parallel_execution/main.c	Sat Oct 01 00:23:35 2016 +0900
@@ -6,6 +6,7 @@
 
 extern __code initContext(struct Context* context);
 extern void allocator(struct Context* context);
+extern void metaAllocator(struct Context* context);
 
 int cpu_num = 1;
 int length = 102400;
@@ -14,7 +15,7 @@
 
 void print_queue(struct Element* element) {
     while (element) {
-        printf("%d\n", ((struct Task *)(element->data))->key);
+        //printf("%d\n", ((struct Task *)(element->data))->key);
         element = element->next;
     }
 }
@@ -51,15 +52,15 @@
 __code code2(struct Context* context, struct Array* array, struct LoopCounter* loopCounter) {
     int i = loopCounter->i;
 
-    if (i < length) {
-           //printf("%d\n", array->array[i]);
-        if (array->array[i] == (i*2)) {
-            loopCounter->i++;
-            goto meta(context, Code2);
-        } else
-            puts("wrong result");
+    //if (i < length) {
+    //       //printf("%d\n", array->array[i]);
+    //    if (array->array[i] == (i*2)) {
+    //        loopCounter->i++;
+    //        goto meta(context, Code2);
+    //    } else
+    //        puts("wrong result");
 
-    }
+    //}
 
     goto meta(context, Exit);
 }
@@ -68,13 +69,13 @@
     goto code2(context, &context->data[Node]->node.value->array, &context->data[LoopCounter]->loopCounter);
 }
 
+/*
 __code createData1(struct Context* context, struct Allocate* allocate, struct LoopCounter* loopCounter) {
     int i = loopCounter->i;
 
     if ((length/split*i) < length) {
         allocate->size = sizeof(struct Array);
         allocator(context);
-
         goto meta(context, CreateData2);
     }
 
@@ -106,6 +107,44 @@
             &context->data[LoopCounter]->loopCounter,
             &context->data[context->dataNum]->array,
             &context->data[Node]->node);
+} */
+
+__code createData1(struct Context* context, struct Allocate* allocate, struct LoopCounter* loopCounter) {
+    int i = loopCounter->i;
+
+    if (i < 4) {
+        allocate->size = sizeof(struct Queue);
+        metaAllocator(context);
+        allocate->size = sizeof(struct Integer);
+        allocator(context);
+        goto meta(context, CreateData2);
+    }
+
+    goto meta(context, CreateTask1);
+}
+
+__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 Integer* integer, struct Node* node) {
+    int i = loopCounter->i;
+    integer->value = i;
+    integer->mDataOffset = -sizeof(struct Queue);
+    node->key = i;
+    node->value = (union Data*)integer;
+
+    context->next = CreateData1;
+    loopCounter->i++;
+
+    goto meta(context, PutTree);
+}
+
+__code createData2_stub(struct Context* context) {
+    goto createData2(context,
+            &context->data[LoopCounter]->loopCounter,
+            &context->data[context->dataNum]->integer,
+            &context->data[Node]->node);
 }
 
 __code createTask1(struct Context* context, struct Allocate* allocate) {
@@ -118,18 +157,55 @@
     goto createTask1(context, &context->data[Allocate]->allocate);
 }
 
-__code createTask2(struct Context* context, struct Allocate* allocate) {
-    allocate->size = sizeof(struct Queue);
-    allocator(context);
-    goto meta(context, CreateTask3);
+/*
+   __code createTask2(struct Context* context, struct LoopCounter* loopCounter, struct Task* task, struct Element* element) {
+   int i = loopCounter->i;
+
+   task->code = Twice;
+   task->key = i;
+
+   element->data = (union Data *)task;
+
+   context->next = CreateData1;
+   loopCounter->i++;
+
+   goto meta(context, SpawnTask);
+   }
+
+   __code createTask2_stub(struct Context* context) {
+   goto createTask2(context,
+   &context->data[LoopCounter]->loopCounter,
+   &context->data[context->dataNum]->task,
+   &context->data[Element]->element);
+   }
+*/
+
+
+__code createTask2(struct Context* context, struct LoopCounter* loopCounter, struct Task* task, struct Element* element) {
+    int i = loopCounter->i;
+
+    task->code = Mult;
+    task->iKeys[0] = i-1;
+    task->iKeys[1] = i-2;
+    task->idsCount = 1;
+    loopCounter->i -= 2;
+
+    element->data = (union Data*)task;
+
+    context->next = CreateTask3;
+
+    goto meta(context, SpawnTask);
 }
 
 __code createTask2_stub(struct Context* context) {
-    goto createTask2(context, &context->data[Allocate]->allocate);
+    goto createTask2(context,
+            &context->data[LoopCounter]->loopCounter,
+            &context->data[context->dataNum]->task,
+            &context->data[Element]->element);
 }
 
 __code createTask3(struct Context* context, struct Allocate* allocate) {
-    allocate->size = sizeof(struct Queue);
+    allocate->size = sizeof(struct Task);
     allocator(context);
     goto meta(context, CreateTask4);
 }
@@ -138,92 +214,55 @@
     goto createTask3(context, &context->data[Allocate]->allocate);
 }
 
-__code meta_createTask4(struct Context* context, struct Queue* activeQueue, enum Code next) {
-    context->data[Queue] = (union Data *)activeQueue;
-    goto (context->code[next])(context);
-}
 
-__code createTask4(struct Context* context, struct LoopCounter* loopCounter, struct Task* task, struct Queue* waitMe, struct Queue* waitI, struct Element* element, struct Queue* activeQueue) {
+__code createTask4(struct Context* context, struct LoopCounter* loopCounter, struct Task* task, struct Node* node) {
     int i = loopCounter->i;
 
-    waitMe->first = 0;
-    waitMe->last = 0;
-    waitMe->count = 0;
-
-    waitI->first = 0;
-    waitI->last = 0;
-    waitI->count = 0;
+    task->code = Add;
+    task->oKeys[0] = i;
+    task->iKeys[0] = i-1;
+    task->iKeys[1] = i-2;
+    task->idsCount = 0;
+    loopCounter->i -= 2;
+    context->next = CreateTask5;
 
-    task->code = Twice;
-    task->key = i;
-    task->waitMe = waitMe;
-    task->waitI = waitI;
-    task->idsCount = 0;
-
-    element->data = (union Data *)task;
-
-    context->next = CreateData1;
-    loopCounter->i++;
-
-    goto meta(context, SpawnTask);
+    node->key = task->oKeys[0];
+    goto meta(context, Get);
 }
 
 __code createTask4_stub(struct Context* context) {
     goto createTask4(context,
             &context->data[LoopCounter]->loopCounter,
-            &context->data[context->dataNum-2]->task,
-            &context->data[context->dataNum-1]->queue,
-            &context->data[context->dataNum]->queue,
-            &context->data[Element]->element,
-            &context->data[ActiveQueue]->queue);
+            &context->data[context->dataNum]->task,
+            &context->data[Node]->node
+            );
+}
+
+__code createTask5(struct Context* context, struct Task* task) {
+    context->next = CreateTask6;
+    goto meta(context, WaitFor);
 }
 
+__code createTask5_stub(struct Context* context) {
+    goto createTask5(context,
+            &context->data[context->dataNum]->task
+            );
+}
 
-//__code createTask4(struct Context* context, struct LoopCounter* loopCounter, struct Task* task, struct Queue* waitMe, struct OdsQueue* waitI, struct Element* element, struct Queue* activeQueue) {
-//    int i = loopCounter->i;
-//
-//    task->code = TaskC;
-//    task->key = i;
-//    task->waitMe = waitMe;
-//    task->waitI = waitI;
-//    task->idsCount = 1;
-//
-//    element->data = (union Data *)task;
-//
-//    context->next = CreateData1;
-//    loopCounter->i++;
-//
-//    goto meta_createTask2(context, activeQueue, PutQueue1);
-//}
-//
-//__code createTask4_stub(struct Context* context) {
-//    goto createTask2(context,
-//            &context->data[LoopCounter]->loopCounter,
-//            &context->data[context->dataNum]->task,
-//            &context->data[context->dataNum-1]->queue,
-//            &context->data[context->dataNum-2]->odsQueue,
-//            &context->data[Element]->element,
-//            &context->data[ActiveQueue]->queue);
-//}
-//
-//__code createTask5(struct Context* context, struct LoopCounter* loopCounter, struct Task* task, struct Task* slave, struct Element* element) {
-//    int i = loopCounter->i;
-//
-//    task->code = TaskB;
-//    task->key = i;
-//    element->data = (union Data *)task;
-//
-//    context->next = CreateData1;
-//    loopCounter->i++;
-//
-//    goto meta(context, WaitFor1);
-//}
-//
-//__code createTask5_stub(struct Context* context) {
-//    goto createTask2(context,
-//            &context->data[context->dataNum]->task,
-//            &context->data[Element]->element);
-//}
+__code createTask6(struct Context* context, struct LoopCounter* loopCounter, struct Task* task, struct Element* element) {
+    element->data = (union Data *)task;
+    loopCounter->i = 0;
+    context->next = Code1;
+    goto meta(context, SpawnTask);
+}
+
+__code createTask6_stub(struct Context* context) {
+    goto createTask6(context,
+            &context->data[LoopCounter]->loopCounter,
+            &context->data[context->dataNum-1]->task,
+            &context->data[Element]->element
+            );
+}
 
 __code putQueue1(struct Context* context, struct Allocate* allocate) {
     allocate->size = sizeof(struct Element);
@@ -289,9 +328,11 @@
 
     if (i < worker->num) {
         struct Context* worker_context = &worker->contexts[i];
-        worker_context->next = GetQueue;
+        worker_context->next = GetTask;
         worker_context->data[Tree] = context->data[Tree];
         worker_context->data[ActiveQueue] = context->data[ActiveQueue];
+        worker_context->data[WaitQueue] = context->data[WaitQueue];
+        worker_context->data[Queue] = context->data[ActiveQueue];
         pthread_create(&worker_context->thread, NULL, (void*)&start_code, worker_context);
         worker_context->thread_num = i;
         loopCounter->i++;
@@ -307,25 +348,54 @@
     goto createWorker(context, &context->data[LoopCounter]->loopCounter, &context->data[Worker]->worker);
 }
 
-__code taskManager(struct Context* context, struct LoopCounter* loopCounter, struct Worker* worker) {
+__code taskManager(struct Context* context, struct LoopCounter* loopCounter, struct Worker* worker, struct Queue* waitQueue, struct Queue* ActiveQueue) {
+    if (waitQueue->first) {
+       goto meta(context, Poll1);
+    }
     int i = loopCounter->i;
-
     if (i < worker->num) {
         pthread_join(worker->contexts[i].thread, NULL);
         loopCounter->i++;
 
         goto meta(context, TaskManager);
     }
-
     loopCounter->i = 0;
-
     struct Time *t = &context->data[Time]->time;
     t->next = Code2;
     goto meta(context, EndTime);
 }
 
 __code taskManager_stub(struct Context* context) {
-    goto taskManager(context, &context->data[LoopCounter]->loopCounter, &context->data[Worker]->worker);
+    goto taskManager(context, 
+                     &context->data[LoopCounter]->loopCounter,
+                     &context->data[Worker]->worker,
+                     &context->data[WaitQueue]->queue,
+                     &context->data[ActiveQueue]->queue);
+}
+
+__code meta_poll1(struct Context* context, struct Queue* queue, enum Code next) {
+    context->data[Queue] = (union Data *)queue;
+    goto (context->code[next])(context);
+}
+
+__code poll1(struct Context* context, struct Queue* waitQueue) {
+    usleep(100);
+    context->next = Poll2;
+    goto meta_poll1(context, waitQueue, GetQueue1);
+}
+
+__code poll1_stub(struct Context* context) {
+    goto poll1(context,
+            &context->data[WaitQueue]->queue);
+}
+
+__code poll2(struct Context* context) {
+    context->next = TaskManager;
+    goto meta(context, SpawnTask);
+}
+
+__code poll2_stub(struct Context* context) {
+    goto poll2(context);
 }
 
 void init(int argc, char** argv) {