changeset 178:5077cf9bf54e

add TaskManager.c
author mir3636
date Tue, 29 Nov 2016 18:09:28 +0900
parents bb52f7f77bf5
children b3be97ba0782
files src/parallel_execution/TaskManager.c src/parallel_execution/context.c src/parallel_execution/context.h
diffstat 3 files changed, 196 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/parallel_execution/TaskManager.c	Tue Nov 29 18:09:28 2016 +0900
@@ -0,0 +1,191 @@
+#include "context.h"
+#include "stack.h"
+#include "origin_cs.h"
+#include <stdio.h>
+
+union Data* createTaskManager(struct Context* context) {
+    struct TaskManager* taskManager = &ALLOCATE(context, TaskManager)->taskManager;
+    struct TaskManager* taskManagerImpl = &ALLOCATE(context, TaskManagerImpl)->taskManagerImpl;
+
+    taskManager->taskManager = (union Data*)taskManager;
+    taskManagerImpl->top = NULL;
+    taskManager->spawn = C_spawnTaskManager;
+    taskManager->shutdown  = C_shutdownTaskManager;
+    return (union Data*)(taskManager);
+}
+
+void printStack1(union Data* data) {
+    struct Node* node = &data->element.data->node;
+    if (node == NULL) {
+        printf("NULL");
+    } else {
+        printf("key = %d ,", node->key);
+        printStack1((union Data*)data->element.next);
+    }
+}
+
+void printStack(union Data* data) {
+    printStack1(data);
+    printf("\n");
+}
+
+__code spawnTaskManager(struct Context* context, struct Task* task, struct Element* element, struct Queue* queue, struct Queue* activeQueue, struct Queue* waitQueue) {
+    if (task->idsCount == 0) {
+        // enqueue activeQueue
+        queue->queue = (union Data*)activeQueue;
+    } else {
+        // enqueue waitQueue
+        queue->queue = (union Data*)waitQueue;
+    }
+    queue->data = element->data;
+    queue->next = context->next;
+    goto meta(context, queue->queue->put);
+}
+
+__code spawnTaskManager_stub(struct Context* context) {
+    goto spawnTask(context,
+            context->data[D_Element]->element.data,
+            &context->data[D_Element]->element,
+            &context->data[D_ActiveQueue]->Queue,
+            &context->data[D_WaitQueue]->Queue);
+}
+
+__code taskManager(struct Context* context, struct LoopCounter* loopCounter, struct Worker* worker) {
+    int i = loopCounter->i;
+
+    if (i < worker->num) {
+        pthread_join(worker->contexts[i].thread, NULL);
+        loopCounter->i++;
+
+        goto meta(context, TaskManager);
+    }
+
+    loopCounter->i = 0;
+
+    Time *t = &context->data[D_Time]->Time;
+    t->next = C_code2;
+    goto meta(context, EndTime);
+}
+
+__code taskManager_stub(struct Context* context) {
+    goto taskManager(context, &context->data[D_LoopCounter]->loopCounter, &context->data[D_Worker]->worker);
+}
+
+__code clearTaskManager(struct Context* context, struct SingleLinkedStack* stack,enum Code next) {
+    stack->top = NULL;
+    goto meta(context, next);
+}
+
+__code clearTaskManager_stub(struct Context* context) {
+    goto clearTaskManager(context, (struct SingleLinkedStack *)context->data[D_Stack]->stack.stack->stack.stack, context->data[D_Stack]->stack.next);
+}
+
+__code pushTaskManager(struct Context* context, struct SingleLinkedStack* stack, struct Element* element, union Data* data, enum Code next) {
+    element->next = stack->top;
+    element->data = data;
+    stack->top = element;
+    goto meta(context, next);
+}
+
+__code pushTaskManager_stub(struct Context* context) {
+    struct Element* element = &ALLOCATE(context, Element)->element;
+    goto pushTaskManager(context,
+                               (struct TaskManager *)context->data[D_Stack]->stack.stack->stack.stack,
+                               element,
+                               context->data[D_Stack]->stack.data,
+                               context->data[D_Stack]->stack.next);
+}
+
+__code popTaskManager(struct Context* context, struct SingleLinkedStack* stack, union Data** data, enum Code next) {
+    if (stack->top) {
+        *data = stack->top->data;
+        stack->top = stack->top->next;
+    } else {
+        *data = NULL;
+    }
+    goto meta(context, next);
+}
+
+__code popTaskManager_stub(struct Context* context) {
+    goto popTaskManager(context,
+                               (struct TaskManager *)context->data[D_Stack]->stack.stack->stack.stack,
+                               &context->data[D_Stack]->stack.data,
+                               context->data[D_Stack]->stack.next);
+}
+
+__code pop2TaskManager(struct Context* context, struct SingleLinkedStack* stack, union Data** data, union Data** data1, enum Code next) {
+    if (stack->top) {
+        *data = stack->top->data;
+        stack->top = stack->top->next;
+    } else {
+        *data = NULL;
+    }
+    if (stack->top) {
+        *data1 = stack->top->data;
+        stack->top = stack->top->next;
+    } else {
+        *data1 = NULL;
+    }
+    goto meta(context, next);
+}
+
+__code pop2TaskManager_stub(struct Context* context) {
+    goto pop2TaskManager(context,
+                               (struct TaskManager *)context->data[D_Stack]->stack.stack->stack.stack,
+                               &context->data[D_Stack]->stack.data,
+                               &context->data[D_Stack]->stack.data1,
+                               context->data[D_Stack]->stack.next);
+}
+
+__code getTaskManager(struct Context* context, struct SingleLinkedStack* stack, union Data** data, enum Code next) {
+    if (stack->top)
+        *data = stack->top->data;
+    else
+        *data = NULL;
+    goto meta(context, next);
+}
+
+__code getTaskManager_stub(struct Context* context) {
+    goto getTaskManager(context,
+                               (struct TaskManager *)context->data[D_Stack]->stack.stack->stack.stack,
+                               &context->data[D_Stack]->stack.data,
+                               context->data[D_Stack]->stack.next);
+}
+
+__code get2TaskManager(struct Context* context, struct SingleLinkedStack* stack, union Data** data, union Data** data1, enum Code next) {
+    if (stack->top) {
+        *data = stack->top->data;
+        if (stack->top->next) {
+            *data1 = stack->top->next->data;
+        } else {
+            *data1 = NULL;
+        }
+    } else {
+        *data = NULL;
+        *data1 = NULL;
+    }
+    goto meta(context, next);
+}
+
+__code get2TaskManager_stub(struct Context* context) {
+    goto get2TaskManager(context,
+                               (struct TaskManager *)context->data[D_Stack]->stack.stack->stack.stack,
+                               &context->data[D_Stack]->stack.data,
+                               &context->data[D_Stack]->stack.data1,
+                               context->data[D_Stack]->stack.next);
+}
+
+__code isEmptyTaskManager(struct Context* context, struct SingleLinkedStack* stack, enum Code next,enum Code whenEmpty) {
+    if (stack->top)
+        goto meta(context, next);
+    else
+        goto meta(context, whenEmpty);
+}
+
+__code isEmptyTaskManager_stub(struct Context* context) {
+    goto isEmptyTaskManager(context,
+                               (struct TaskManager *)context->data[D_Stack]->stack.stack->stack.stack,
+                               context->data[D_Stack]->stack.next,
+                               context->data[D_Stack]->stack.whenEmpty);
+}
+
--- a/src/parallel_execution/context.c	Mon Nov 28 22:51:12 2016 +0900
+++ b/src/parallel_execution/context.c	Tue Nov 29 18:09:28 2016 +0900
@@ -120,6 +120,10 @@
     context->code[C_takeSynchronizedQueue]    = takeSynchronizedQueue_stub;
     context->code[C_isEmptySynchronizedQueue] = isEmptySynchronizedQueue_stub;
 
+    context->code[C_spawnTaskManager] = spawnTaskManager_stub;
+    context->code[C_taskSendTaskManager] = taskSendTaskManager_stub;
+    context->code[C_shutdownTaskManager] = shutdownTaskManager_stub;
+
     /* context->code[Delete]        = delete_stub; */
     /* context->code[Delete1]       = delete1_stub; */
     /* context->code[Delete2]       = delete2_stub; */
--- a/src/parallel_execution/context.h	Mon Nov 28 22:51:12 2016 +0900
+++ b/src/parallel_execution/context.h	Tue Nov 29 18:09:28 2016 +0900
@@ -174,6 +174,7 @@
     struct TaskManager {
         int numWorker;
         enum Code spawn;
+        enum Code taskSend;
         enum Code shutdown;
         struct Worker* workers;
     }