changeset 232:123b0d277b84

worker interface
author mir3636
date Sun, 22 Jan 2017 19:02:12 +0900
parents 24da4f217447
children 06133afb3b5b
files src/parallel_execution/context.h src/parallel_execution/main.c src/parallel_execution/twice.c src/parallel_execution/worker.c
diffstat 4 files changed, 36 insertions(+), 61 deletions(-) [+]
line wrap: on
line diff
--- a/src/parallel_execution/context.h	Sun Jan 22 18:19:23 2017 +0900
+++ b/src/parallel_execution/context.h	Sun Jan 22 19:02:12 2017 +0900
@@ -37,6 +37,10 @@
 
 #define Gearef(context, t) (&(context)->data[D_##t]->t)
 
+// (struct SingleLinkedStack *)context->data[D_Stack]->Stack.stack->Stack.stack
+
+#define GearImpl(context, intf, name) (Gearef(context, intf)->name->intf.name) 
+
 #include "c/enumCode.h"
 
 enum Relational {
@@ -91,26 +95,34 @@
         struct Queue* taskQueue;
     } TaskManagerImpl;
     struct Worker {
-        int id;
-        struct Queue* tasks;
-        int runFlag;
-        struct Context* context;
+        union Data* worker;
         enum Code taskReceive;
         enum Code shutdown;
         enum Code next;
     } Worker;
+    struct CPUWorker {
+        struct Context* context;
+        int id;
+        struct Queue* tasks;
+        int runFlag;
+        enum Code next;
+    } CPUWorker;
 #ifdef USE_CUDA
-    struct CudaTask {
+    struct CudaWorker {
+        struct Context* context;
+        int id;
+        struct Queue* tasks;
+        int runFlag;
+        enum Code next;
         CUdevice device;
         CUcontext cuCtx;
         CUfunction code;
         CUdeviceptr* deviceptr;
         CUstream stream;
-    } Cudatask;
+    } CudaWorker;
 #else
-    struct CudaTask {
-        enum Code next;
-    } Cudatask;
+    struct CudaWorker {
+    } CudaWorker;
 #endif
     struct Main {
         enum Code code;
--- a/src/parallel_execution/main.c	Sun Jan 22 18:19:23 2017 +0900
+++ b/src/parallel_execution/main.c	Sun Jan 22 19:02:12 2017 +0900
@@ -51,7 +51,7 @@
     /* print_tree(context->data[Tree]->tree.root); */
     /* puts("result"); */
 
-    time->next = C_createWorker1;
+    time->next = C_code2;
     goto meta(context, C_start_time);
 }
 
@@ -140,39 +140,7 @@
             task);
 }
 
-__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 taskManager(struct Context* context, struct LoopCounter* loopCounter, struct Worker* worker) {
-    int i = loopCounter->i;
-
-    if (i < worker->id) {
-        pthread_join(worker->contexts[i].thread, NULL);
-        loopCounter->i++;
-
-        goto meta(context, C_taskManager);
-    }
-
     loopCounter->i = 0;
 
     Time *t = &context->data[D_Time]->Time;
@@ -210,10 +178,6 @@
 
     struct Context* worker_contexts = NEWN(cpu_num, struct Context);
 
-    struct Worker* worker = &main_context->data[D_Worker]->Worker;
-    worker->id = cpu_num;
-    worker->contexts = worker_contexts;
-
     for (int i = 0;i<cpu_num;i++)
         initContext(&worker_contexts[i]);
 
--- a/src/parallel_execution/twice.c	Sun Jan 22 18:19:23 2017 +0900
+++ b/src/parallel_execution/twice.c	Sun Jan 22 19:02:12 2017 +0900
@@ -20,10 +20,5 @@
 }
 
 __code twice_stub(struct Context* context) {
-    goto twice(context,
-               &context->data[task->data[0]]->LoopCounter,
-               context->data[task->data[1]]->Node.value->Array.index,
-               context->data[task->data[2]]->Node.value->Array.prefix,
-               context->data[task->data[3]]->Node.value->Array.array
-               task->next);
+    goto twice(context, NULL, 0, 0, NULL,0);
 }
--- a/src/parallel_execution/worker.c	Sun Jan 22 18:19:23 2017 +0900
+++ b/src/parallel_execution/worker.c	Sun Jan 22 19:02:12 2017 +0900
@@ -3,17 +3,20 @@
 #include "context.h"
 #include "origin_cs.h"
 
-union Data* createWorker(struct Context* context, int id, Queue* queue) {
+union Data* createCPUWorker(struct Context* context, int id, Queue* queue, enum Code next) {
     struct Worker* worker = &ALLOCATE(context, Worker)->Worker;
-    worker->tasks = queue;
-    worker->id = id;
-    worker->runFlag = 1;
+    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, Worker* worker) {
+__code taskReceiveWorker(struct Context* context, CPUWorker* worker) {
     if (! worker->runFlag)
         goto meta(context, worker->next);
     Queue* queue = worker->tasks;
@@ -22,7 +25,7 @@
 }
 
 __code taskReceiveWorker_stub(struct Context* context) {
-    Worker* worker = Gearef(context,Worker);
+    CPUWorker* worker = (CPUWorker *)GearImpl(context, Worker, worker);
     goto taskReceiveWorker(context,worker);
 }
 
@@ -33,7 +36,8 @@
 
 __code getTask1_stub(struct Context* context) {
     Worker* worker = Gearef(context,Worker);
-    Context* task = &worker->tasks->data->context; 
+    CPUWorker* cpuWorker = (CPUWorker *)GearImpl(context, Worker, worker);
+    Context* task = &cpuWorker->tasks->data->context; 
     goto getTask1(context,worker,task);
 }
 
@@ -45,11 +49,11 @@
 }
 #endif
 
-__code shutdownWorker(struct Context* context, Worker* worker) {
+__code shutdownWorker(struct Context* context, CPUWorker* worker) {
     worker->runFlag = 0;
 }
 
 __code shutdownWorker_stub(struct Context* context) {
-    Worker* worker = Gearef(context,Worker);
+    CPUWorker* worker = (CPUWorker *)GearImpl(context, Worker, worker);
     goto shutdownWorker(context,worker);
 }