Mercurial > hg > GearsTemplate
changeset 271:f2dd6356eff2
add CPUWorker.cbc
author | mir3636 |
---|---|
date | Mon, 30 Jan 2017 15:02:08 +0900 |
parents | b6ed4b2a5d9d |
children | 68cf983475e7 |
files | src/parallel_execution/CPUWorke.c src/parallel_execution/Worker.cbc |
diffstat | 2 files changed, 80 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/parallel_execution/CPUWorke.c Mon Jan 30 15:02:08 2017 +0900 @@ -0,0 +1,73 @@ +#include <libkern/OSAtomic.h> + +#include "context.h" +#include "origin_cs.h" + +static void start_worker(Worker* worker); + +Worker* createCPUWorker(struct Context* context, int id, Queue* queue) { + struct Worker* worker = &ALLOCATE(context, Worker)->Worker; + struct CPUWorker* cpuWorker = &ALLOCATE(context, CPUWorker)->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(context, meta); +} + +__code taskReceiveWorker(struct Context *context,struct Worker* worker,struct Queue* queue) { + queue->queue = (union Data*)worker->tasks; + queue->next = C_getTask; + goto meta(context, meta); +} + +__code taskReceiveWorker_stub(struct Context* context) { + CPUWorker* cpuWorker = (CPUWorker *)GearImpl(context, Worker, worker); + pthread_cond_wait(&cpuWorker->cond, &cpuWorker->mutex); + goto meta(context, taskReceiveWorker); +} + +__code getTask(struct Worker* worker, struct Context* task) { + if (!task) + return; // end thread + task->worker = worker; + context->next = C_taskReceiveWorker; // set CG after task exec + goto meta(context, meta); +} + +__code getTask_stub(struct Context* context) { + Worker* worker = &Gearef(context,Worker)->worker->Worker; + struct Context* task = &Gearef(context, Queue)->data->Context; + goto meta(context, getTask); +} + +#ifdef USE_CUDA +__code twiceGpu(struct Context *context,struct Context* context) { + cuMemcpyHtoDAsync(context,context,context,context->stream); + cuLaunchkanel(); + cuMemcpyDtoHAsync(); +} +#endif + +__code twiceGpu_stub (struct Context* context) { + Context* context = Gearef(context, Context); + goto twiceGpu(context, context); +} + +__code shutdownWorker(struct Context *context,struct CPUWorker* worker) { +} +__code shutdownWorker_stub (struct Context* context) { + CPUWorker* worker = (CPUWorker*)GearImpl(context, Worker, worker); + goto shutdownWorker(context, worker); +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/parallel_execution/Worker.cbc Mon Jan 30 15:02:08 2017 +0900 @@ -0,0 +1,7 @@ +typedef struct Worker<:Impl>{ + union Data* worker; + __code taskReseive(struct Worker* worker,struct Queue* queue); + __code shutdown(Impl* worker); + __codenext(...); + struct Queue* queue; +} Worker;