view src/parallel_execution/taskManager.c @ 241:9135e22799dd

Merge
author Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
date Tue, 24 Jan 2017 16:44:16 +0900
parents 864bb372f37f cc13a1608364
children 9f3f8ed6ed9f
line wrap: on
line source

#include "context.h"
#include "stack.h"
#include "queue.h"
#include "worker.h"
#include "origin_cs.h"
#include <stdio.h>

void createWorkers(Context* context, TaskManager * taskManeger);

union Data* createTaskManager(struct Context* context, int numCPU, int numGPU, int numIO) {
    struct TaskManager* taskManager = &ALLOCATE(context, TaskManager)->TaskManager;
    struct TaskManagerImpl* taskManagerImpl = &ALLOCATE(context, TaskManagerImpl)->TaskManagerImpl;
    taskManager->taskManager = (union Data*)taskManagerImpl;
    taskManagerImpl -> activeQueue = &createSingleLinkedQueue(context)->Queue;
    taskManagerImpl -> taskQueue = &createSingleLinkedQueue(context)->Queue;
    // 0...numIO-1 IOProcessor 
    // numIO...numIO+numGPU-1 GPUProcessor
    // numIO+numGPU...numIO+numGPU+numCPU-1 CPUProcessor
    taskManager->io = 0;
    taskManager->gpu = numIO;
    taskManager->cpu = numIO+numGPU;
    taskManager->maxCPU = numIO+numGPU+numCPU;
    taskManager->createTask = C_createTask;
    taskManager->spawn = C_spawnTaskManager;
    taskManager->shutdown  = C_shutdownTaskManager;
    createWorkers(context, taskManager);
    return (union Data*)(taskManager);
}

void createWorkers(Context* context, TaskManager* taskManager) {
    int i = 0;
    TaskManagerImpl* taskManagerImpl = (TaskManagerImpl*)GearImpl(context,TaskManager,taskManager);
    taskManagerImpl->workers = (Worker*)ALLOC_ARRAY(context,Worker,taskManager->maxCPU);
    for (;i<taskManager->gpu;i++) {
        Queue* queue = &createSynchronizedQueue(context)->Queue;
        taskManagerImpl->workers[i] = (Worker*)createCPUWorker(context,i,queue);
    }
    for (;i<taskManager->cpu;i++) {
#ifdef USE_CUDA
#else
        Queue* queue = &createSynchronizedQueue(context)->Queue;
        taskManagerImpl->workers[i] = (Worker*)createCPUWorker(context,i,queue);
#endif        
    }
    for (;i<taskManager->maxCPU;i++) {
        Queue* queue = &createSynchronizedQueue(context)->Queue;
        taskManagerImpl->workers[i] = (Worker*)createCPUWorker(context,i,queue);
    }
}

__code createTask(struct Context* context, TaskManager* taskManager, enum Code next) {
    taskManager->context = NEW(struct Context);
    initContext(taskManager->context);
    goto meta(context, next);
}

__code createTask_stub(struct Context* context) {
    goto createTask(context,Gearef(context,TaskManager),Gearef(context,TaskManager)->next);
}

__code spawnTaskManager(struct Context* context, struct TaskManagerImpl* taskManager, struct Queue* queue, struct Context* task, enum Code next) {
    if (task->idgCount == 0) {
        // enqueue activeQueue
        queue->queue = (union Data*)taskManager->activeQueue;
    } else {
        // enqueue waitQueue
        queue->queue = (union Data*)taskManager->taskQueue;
    }
    queue->data = (union Data*)task;
    queue->next = C_spawnTaskManager1;
    goto meta(context, queue->queue->Queue.put);
}

__code spawnTaskManager_stub(struct Context* context) {
    TaskManagerImpl* taskManager = (TaskManagerImpl*)GearImpl(context, TaskManager, taskManager);
    pthread_mutex_lock(&taskManager->mutex);
    goto spawnTaskManager(context,
                          taskManager,
                          Gearef(context, Queue),
                          Gearef(context, TaskManager)->context,
                          Gearef(context, TaskManager)->next);
}


__code spawnTaskManager1(struct Context* context, struct TaskManagerImpl* taskManager, enum Code next) {
    pthread_mutex_unlock(taskManagerImpl->mutex);
    goto meta(context, next);
}

__code spawnTaskManager1_stub(struct Context* context) {
    goto spawnTaskManager(context,
                          (struct TaskManager*)GearImpl(context, TaskManager, taskManager),
                          Gearef(context, TaskManager)->next);
}

__code taskSend(struct Context* context) {
    if(loopCounter->i < taskManager->numWorker) {
        queue->queue = taskManager->workers[i]->tasks;
        queue->next  =  C_taskSend;
        loopCounter->i++;
        goto meta(context, queue->queue->Queue.put);
    }
    goto meta(context, TaskManager->next);
}

__code taskSend_stub(struct Context* context) {
    goto taskSend(context, (TaskManagerImpl*)GearImpl(context, TaskManager, taskManager));
}

__code createWorker1(struct Context* context, struct LoopCounter* loopCounter, struct Worker* worker) {
    int i = loopCounter->i;

    if (i < worker->id) {
        struct Context* worker_context = &worker->contexts[i];
        worker_context->next = C_getTask1;
        worker_context->data[D_Tree] = context->data[D_Tree];
        // worker_context->data[D_ActiveQueue] = context->data[D_ActiveQueue];                                                            
        pthread_create(&worker_context->thread, NULL, (void*)&start_code, worker_context);
        worker_context->thread_num = i;
        loopCounter->i++;

        goto meta(context, C_createWorker1);
    }

    loopCounter->i = 0;
    goto meta(context, C_taskManager);
}

__code createWorker1_stub(struct Context* context) {
    goto createWorker1(context, &context->data[D_LoopCounter]->LoopCounter, &context->data[D_Worker]->Worker);
}

__code shutdownTaskManager(struct Context* context, struct LoopCounter* loopCounter, struct Worker* worker, struct TaskManager* taskManager) {
    int i = loopCounter->i;

    if (i < worker->id) {
        pthread_join(worker->contexts[i].thread, NULL);
        loopCounter->i++;

        goto meta(context, C_shutdownTaskManager);
    }

    loopCounter->i = 0;
    goto meta(context, taskManager->next);
}

__code shutdownTaskManager_stub(struct Context* context) {
    goto shutdownTaskManager(context, &context->data[D_LoopCounter]->LoopCounter, &context->data[D_Worker]->Worker, Gearef(context, TaskManager));
}