view src/parallel_execution/GPUWorker.cbc @ 288:f1b0cc555b6e

Add odgCommit
author Tatsuki IHA <e125716@ie.u-ryukyu.ac.jp>
date Mon, 06 Feb 2017 04:04:25 +0900
parents 06dab015a54d
children
line wrap: on
line source

#include <libkern/OSAtomic.h>

#include "context.h"
#include "origin_cs.h"

static void start_worker(Worker* worker);

union Data* createGPUWorker(struct Context* context, int id, Queue* queue) {
    struct Worker* worker = ALLOC(context, Worker);
    struct GPUWorker* gpuWorker = ALLOC(context, GPUWorker);
    worker->worker = (union Data*)gpuWorker;
    worker->tasks = queue;
    cpuWorker->id = id;
    worker->taskReceive = C_taskReceiveGPUWorker;
    worker->shutdown = C_shutdownGPUWorker;
    pthread_create(&worker->worker->GPUWorker.thread, NULL, (void*)&start_GPUworker, worker);
    return (union Data*)(worker);
}

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

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

__code taskReceiveGPUWorker_stub(struct Context* context) {
    GPUWorker* gpuWorker = (GPUWorker *)GearImpl(context, GPUWorker, gPUworker);
    pthread_cond_wait(&gpuWorker->cond, &gpuWorker->mutex);
    goto taskReceiveGPUWorker(context, &Gearef(context, Worker)->worker->Worker, Gearef(context, Queue));
}

__code getGPUTask(struct Context* context, Worker* worker, struct Context* task) {
    if (!task)
        return; // end thread
    task->worker = worker;
    context->next = C_taskReceiveGPUWorker; // set CG after task exec
    goto meta(task, task->next);
}

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

#ifdef USE_CUDA
__code twiceGpu(struct Context* context) {
    cuMemcpyHtoDAsync(context,context,context,context->stream);
    cuLaunchkanel();
    cuMemcpyDtoHAsync();
}
#endif

__code shutdownWorker(struct Context* context, CPUWorker* worker) {
}

__code shutdownWorker_stub(struct Context* context) {
    CPUWorker* worker = (CPUWorker *)GearImpl(context, Worker, worker);
    goto shutdownWorker(context,worker);
}