view src/parallel_execution/CPUWorker.cbc @ 314:1839586f5b41

pthread CUDA test
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Wed, 15 Feb 2017 12:34:19 +0900
parents ec0a5b4fba05
children 7d664be4efa5
line wrap: on
line source

#include <libkern/OSAtomic.h>

#include "../context.h"

static void start_worker(Worker* worker);

Worker* createCPUWorker(struct Context* context, int id, Queue* queue) {
    struct Worker* worker = new Worker();
    struct CPUWorker* cpuWorker = new CPUWorker();
    worker->worker = (union Data*)cpuWorker;
    worker->tasks = queue;
    cpuWorker->id = id;
    worker->taskReceive = C_taskReceiveWorker;
    worker->shutdown = C_shutdownWorker;
    pthread_create(&worker->worker->CPUWorker.thread, NULL, (void*)&start_worker, worker);
    return worker;
}

static void start_worker(Worker* worker) {
    CPUWorker* cpuWorker = (CPUWorker*)worker->worker;
    cpuWorker->context = NEW(struct Context);
    initContext(cpuWorker->context);
    Gearef(cpuWorker->context, Worker)->worker = (union Data*)worker;
    goto meta(cpuWorker->context, worker->taskReceive);
}

__code taskReceiveWorker(struct Worker* worker,struct Queue* queue) {
    queue->queue = (union Data*)worker->tasks;
    queue->next = C_getTask;
    goto meta(context, worker->tasks->take);
}

__code taskReceiveWorker_stub(struct Context* context) {
    goto taskReceiveWorker(context, &Gearef(context, Worker)->worker->Worker, Gearef(context, Queue));
}

__code getTask(struct Worker* worker, struct Context* task) {
    if (!task)
        return; // end thread
    task->worker = worker;
    enum Code taskCg = task->next;
    task->next = C_odgCommit; // set CG after task exec
    goto meta(task, taskCg);
}

__code getTask_stub(struct Context* context) {
    Worker* worker = &Gearef(context,Worker)->worker->Worker;
    struct Context* task = &Gearef(context, Queue)->data->Context;
    goto getTask(context, worker, task);
}

__code odgCommit(struct LoopCounter* loopCounter, struct Queue* queue, struct Context* task) {
    int i = loopCounter->i ;
    if(task->odg + i < task->maxOdg) {
        queue->queue = (union Data*)GET_WAIT_LIST(task->data[task->odg+i]);
        queue->next = C_odgCommit1;
        goto meta(context, queue->queue->Queue.take);
    }
    loopCounter->i = 0;
    goto meta(context, C_taskReceiveWorker);
}

__code odgCommit_stub(struct Context* context) {
    struct Context* workerContext = context->worker->worker->CPUWorker.context;
    goto odgCommit(workerContext,
                   Gearef(workerContext, LoopCounter),
                   Gearef(workerContext, Queue),
                   context);
}

__code odgCommit1(struct TaskManager* taskManager, struct Context* task) {
    if(__sync_fetch_and_sub(&task->idgCount, 1)) {
        if(task->idgCount == 0) {
            taskManager->taskManager = (union Data*)task->taskManager;
            taskManager->context = task;
            taskManager->next = C_odgCommit;
            goto meta(context, task->taskManager->spawn);
        }
    } else {
        goto meta(context, C_odgCommit1);
    }
}

__code odgCommit1_stub(struct Context* context) {
    struct Context* task = &Gearef(context, Queue)->data->Context;
    goto odgCommit1(context,
                    Gearef(context, TaskManager),
                    task);
                 
}

__code shutdownWorker(struct CPUWorker* worker) {
}