view src/parallel_execution/worker.c @ 232:123b0d277b84

worker interface
author mir3636
date Sun, 22 Jan 2017 19:02:12 +0900
parents a1fb3f2d1a36
children 06133afb3b5b
line wrap: on
line source

#include <libkern/OSAtomic.h>

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

union Data* createCPUWorker(struct Context* context, int id, Queue* queue, enum Code next) {
    struct Worker* worker = &ALLOCATE(context, Worker)->Worker;
    struct CPUWorker* cpuWorker = &ALLOCATE(context, CPUWorker)->CPUWorker;
    worker->worker = (union Data*)cpuWorker;
    cpuWorker->tasks = queue;
    cpuWorker->id = id;
    cpuWorker->runFlag = 1;
    cpuWorker->next = next;
    worker->taskReceive = C_taskReceiveWorker;
    worker->shutdown = C_shutdownWorker;
    return (union Data*)(worker);
}

__code taskReceiveWorker(struct Context* context, CPUWorker* worker) {
    if (! worker->runFlag)
        goto meta(context, worker->next);
    Queue* queue = worker->tasks;
    queue->next = C_getTask1;
    goto meta(context, queue->take);
}

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

__code getTask1(struct Context* context, Worker* worker, struct Context* task) {
    task->worker = worker;
    goto meta(task, task->next);
}

__code getTask1_stub(struct Context* context) {
    Worker* worker = Gearef(context,Worker);
    CPUWorker* cpuWorker = (CPUWorker *)GearImpl(context, Worker, worker);
    Context* task = &cpuWorker->tasks->data->context; 
    goto getTask1(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) {
    worker->runFlag = 0;
}

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