# HG changeset patch # User mir3636 # Date 1480410568 -32400 # Node ID 5077cf9bf54e2f65abbacaae7463dbc2ff2aad1a # Parent bb52f7f77bf5b2de2d4fec038c7f6dd11875eb13 add TaskManager.c diff -r bb52f7f77bf5 -r 5077cf9bf54e src/parallel_execution/TaskManager.c --- /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 + +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); +} + diff -r bb52f7f77bf5 -r 5077cf9bf54e src/parallel_execution/context.c --- 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; */ diff -r bb52f7f77bf5 -r 5077cf9bf54e src/parallel_execution/context.h --- 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; }