changeset 353:b07078bd1f2c

Add spawn Tasks to TaskManagerImpl
author Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
date Tue, 20 Jun 2017 03:02:29 +0900
parents 3e01e963eb2d
children 6dcf6f909e15
files src/parallel_execution/CMakeLists.txt src/parallel_execution/SemaphoreImpl.cbc src/parallel_execution/TaskManager.cbc src/parallel_execution/TaskManagerImpl.cbc src/parallel_execution/context.h src/parallel_execution/examples/Add.cbc src/parallel_execution/examples/Mult.cbc src/parallel_execution/examples/calc.cbc src/parallel_execution/generate_stub.pl
diffstat 9 files changed, 79 insertions(+), 125 deletions(-) [+]
line wrap: on
line diff
--- a/src/parallel_execution/CMakeLists.txt	Fri Jun 16 16:29:27 2017 +0900
+++ b/src/parallel_execution/CMakeLists.txt	Tue Jun 20 03:02:29 2017 +0900
@@ -67,7 +67,7 @@
   TARGET
       calc
   SOURCES 
-      examples/calc.cbc examples/Add.cbc examples/Mult.cbc examples/calc.cbc CPUWorker.cbc time.cbc TaskManagerImpl.cbc SingleLinkedQueue.cbc SynchronizedQueue.cbc SemaphoreImpl.cbc
+      examples/calc.cbc examples/Add.cbc examples/Mult.cbc examples/InitIntegerDataGears.cbc CPUWorker.cbc time.cbc TaskManagerImpl.cbc SingleLinkedQueue.cbc SynchronizedQueue.cbc SemaphoreImpl.cbc
 )
 
 if (${USE_CUDA})
--- a/src/parallel_execution/SemaphoreImpl.cbc	Fri Jun 16 16:29:27 2017 +0900
+++ b/src/parallel_execution/SemaphoreImpl.cbc	Tue Jun 20 03:02:29 2017 +0900
@@ -34,4 +34,3 @@
     pthread_mutex_unlock(&semaphore->mutex);
     goto next(...);
 }
-
--- a/src/parallel_execution/TaskManager.cbc	Fri Jun 16 16:29:27 2017 +0900
+++ b/src/parallel_execution/TaskManager.cbc	Tue Jun 20 03:02:29 2017 +0900
@@ -2,7 +2,7 @@
     union Data* taskManager;
     __code createTask(struct TaskManager* taskManager);
     __code spawn(Impl* taskManager, struct Queue* queue, struct Context* task, __code next(...));
-    __code setWaitTask(struct Queue* queue, struct Context* task, __code next(...));
+    __code spawnTasks(struct TaskManager* taskManager, Impl* taskManagerImpl, struct Context* contexts);
     __code shutdown(struct LoopCounter* loopCounter, struct TaskManager* taskManager, Impl* taskManagerImpl, struct Queue* queue, __code next(...));
     __code next(...);
     __code task(...);
--- a/src/parallel_execution/TaskManagerImpl.cbc	Fri Jun 16 16:29:27 2017 +0900
+++ b/src/parallel_execution/TaskManagerImpl.cbc	Tue Jun 20 03:02:29 2017 +0900
@@ -13,8 +13,8 @@
     taskManager->gpu = numIO;
     taskManager->cpu = numIO+numGPU;
     taskManager->maxCPU = numIO+numGPU+numCPU;
+    taskManager->spawnTasks = C_spawnTasksTaskManager;
     taskManager->spawn = C_spawnTaskManager;
-    taskManager->setWaitTask = C_setWaitTask;
     taskManager->shutdown  = C_shutdownTaskManager;
     struct TaskManagerImpl* taskManagerImpl = new TaskManagerImpl();
     taskManagerImpl -> activeQueue = createSingleLinkedQueue(context);
@@ -53,6 +53,47 @@
     }
 }
 
+__code spawnTasksTaskManager(struct TaskManager* taskManager, struct TaskManagerImpl* taskManagerImpl, struct Context** contexts) {
+    int i = taskManagerImpl->loopCounter->i;
+    struct Context* task = contexts[i];
+    if(i < GET_SIZE(contexts)) {
+        taskManager->context = task;
+        taskManager->next = C_spawnTasksTaskManager;
+        goto meta(context, C_setWorker);
+    }
+    taskManagerImpl->loopCounter->i = 0;
+    goto meta(context, C_spawnTasksTaskManager1);
+}
+
+__code spawnTasksTaskManager_stub(struct Context* context) {
+    TaskManagerImpl* taskManager = (TaskManagerImpl*)GearImpl(context, TaskManager, taskManager);
+    goto spawnTasksTaskManager(context,
+                               Gearef(context, TaskManager),
+                               taskManager,
+                               Gearef(context, TaskManager)->contexts);
+}
+
+__code spawnTasksTaskManager1(struct TaskManager* taskManager, struct TaskManagerImpl* taskManagerImpl, struct Context** contexts, __code next(...)) {
+    int i = taskManagerImpl->loopCounter->i;
+    struct Context* task = contexts[i];
+    if(i < GET_SIZE(contexts)) {
+        taskManager->context = task;
+        taskManager->next = C_spawnTasksTaskManager1;
+        goto meta(context, C_spawnTaskManager);
+    }
+    taskManagerImpl->loopCounter->i = 0;
+    goto meta(context, next);
+}
+
+__code spawnTasksTaskManager1_stub(struct Context* context) {
+    TaskManagerImpl* taskManager = (TaskManagerImpl*)GearImpl(context, TaskManager, taskManager);
+    goto spawnTasksTaskManager1(context,
+                               Gearef(context, TaskManager),
+                               taskManager,
+                               Gearef(context, TaskManager)->contexts,
+                               Gearef(context, TaskManager)->next1);
+}
+
 __code setWorker(struct TaskManagerImpl* taskManager, struct Context* task) {
     task->workerId = taskManager->sendWorkerIndex;
     if(++taskManager->sendWorkerIndex >= taskManager->numWorker) {
@@ -63,13 +104,7 @@
 
 __code setWorker_stub(struct Context* context) {
     TaskManagerImpl* taskManager = (TaskManagerImpl*)GearImpl(context, TaskManager, taskManager);
-    int i = taskManager->loopCounter->i;
-    struct Context** contexts = Gearef(context, TaskManager)->contexts;
-    struct Context* task = contexts[i];
-    if(i != GET_SIZE(contexts) - 1) {
-        taskManager->loopCounter->i++;
-    }
-    goto setWorker(context, taskManager, task);
+    goto setWorker(context, taskManager, Gearef(context, TaskManager)->context);
 }
 
 __code setWaitTask(struct Queue* queue, struct Context* task, struct LoopCounter* loopCounter, __code next(...)) {
@@ -86,16 +121,8 @@
 }
 
 __code setWaitTask_stub(struct Context* context) {
-    TaskManagerImpl* taskManager = (TaskManagerImpl*)GearImpl(context, TaskManager, taskManager);
-    int i = taskManager->loopCounter->i;
-    struct Context** contexts = Gearef(context, TaskManager)->contexts;
-    struct Context* task = contexts[i];
-    enum Code next = C_setWorker;
-    if(i == GET_SIZE(contexts) - 1) {
-        taskManager->loopCounter->i = 0;
-        next = C_spawnTaskManager;
-    }
-    goto setWaitTask(context, Gearef(context, Queue), task, Gearef(task, LoopCounter), next);
+    struct Context* task = Gearef(context, TaskManager)->context;
+    goto setWaitTask(context, Gearef(context, Queue), task, Gearef(task, LoopCounter), Gearef(context, TaskManager)->next);
 }
 
 __code spawnTaskManager(struct TaskManagerImpl* taskManager, struct Context* task, __code next(...)) {
@@ -109,20 +136,11 @@
 
 __code spawnTaskManager_stub(struct Context* context) {
     TaskManagerImpl* taskManager = (TaskManagerImpl*)GearImpl(context, TaskManager, taskManager);
-    int i = taskManager->loopCounter->i;
-    struct Context** contexts = Gearef(context, TaskManager)->contexts;
-    struct Context* task = contexts[i];
-    enum Code next = Gearef(context, TaskManager)->next;
-    if(i == GET_SIZE(contexts) - 1) {
-        taskManager->loopCounter->i = 0;
-    } else {
-        taskManager->loopCounter->i++;
-        next = C_spawnTaskManager;
-    }
+    pthread_mutex_lock(&taskManager->mutex);
     goto spawnTaskManager(context,
             taskManager,
-            task,
-            next);
+            Gearef(context, TaskManager)->context,
+            Gearef(context, TaskManager)->next);
 }
 
 __code taskSend(struct TaskManagerImpl* taskManager, struct Queue* queue, struct Context* task, __code next(...)) {
@@ -136,7 +154,10 @@
 
 __code taskSend_stub(struct Context* context) {
     TaskManagerImpl* taskManager = (TaskManagerImpl*)GearImpl(context, TaskManager, taskManager);
-    goto taskSend(context, taskManager, Gearef(context, Queue), Gearef(context, TaskManager)->context, Gearef(context, TaskManager)->next);
+    goto taskSend(context,
+            taskManager, Gearef(context, Queue),
+            Gearef(context, TaskManager)->context,
+            Gearef(context, TaskManager)->next);
 }
 
 __code shutdownTaskManager(struct LoopCounter* loopCounter, struct TaskManager* taskManager, struct TaskManagerImpl* taskManagerImpl, struct Queue* queue, __code next(...)) {
--- a/src/parallel_execution/context.h	Fri Jun 16 16:29:27 2017 +0900
+++ b/src/parallel_execution/context.h	Tue Jun 20 03:02:29 2017 +0900
@@ -116,9 +116,10 @@
 #endif
         union Data* taskManager;
         enum Code spawn;      // start NEW context on the worker
-        enum Code setWaitTask;
+        enum Code spawnTasks; // start NEW contexts on the worker
         enum Code shutdown;
         enum Code next;
+        enum Code next1;
         enum Code task;
         struct Context* context;
         struct Context** contexts;
--- a/src/parallel_execution/examples/Add.cbc	Fri Jun 16 16:29:27 2017 +0900
+++ b/src/parallel_execution/examples/Add.cbc	Tue Jun 20 03:02:29 2017 +0900
@@ -1,7 +1,7 @@
 #include "../../context.h"
 #include <stdio.h>
 __code add(struct Integer* input1, struct Integer* input2, __code next(struct Integer* output, ...)) {
-    struct Integer* output = NULL;
+    struct Integer* output = *O_output;
     output->value = input1->value + input2->value;
     printf("%d + %d = %d\n", input1->value, input2->value, output->value);
     *O_output = output;
--- a/src/parallel_execution/examples/Mult.cbc	Fri Jun 16 16:29:27 2017 +0900
+++ b/src/parallel_execution/examples/Mult.cbc	Tue Jun 20 03:02:29 2017 +0900
@@ -1,7 +1,7 @@
 #include "../../context.h"
 #include <stdio.h>
 __code mult(struct Integer* input1, struct Integer* input2, __code next(struct Integer* output, ...)) {
-    struct Integer* output = NULL;
+    struct Integer* output = *O_output;
     output->value = input1->value * input2->value;
     printf("%d + %d = %d\n", input1->value, input2->value, output->value);
     *O_output = output;
--- a/src/parallel_execution/examples/calc.cbc	Fri Jun 16 16:29:27 2017 +0900
+++ b/src/parallel_execution/examples/calc.cbc	Tue Jun 20 03:02:29 2017 +0900
@@ -100,11 +100,12 @@
 
     loopCounter->i = 0;
     taskManager->next = C_code1;
+    sleep(5);
     goto meta(context, taskManager->taskManager->TaskManager.shutdown);
 }
 
 __code createTask2(struct LoopCounter* loopCounter, struct TaskManager* taskManager) {
-    struct Context** tasks = (struct Context**)ALLOC_ARRAY(context, Context, 2);
+    struct Context** tasks = (struct Context**)ALLOC_ARRAY(context, Context, 3);
 
     Integer* integer1 = &ALLOCATE_DATA_GEAR(context, Integer)->Integer;
     Integer* integer2 = &ALLOCATE_DATA_GEAR(context, Integer)->Integer;
@@ -141,11 +142,26 @@
     task->maxOdg = task->odg + 1;
     tasks[1] = task;
 
+    // par goto initIntegerDataGears(integer2, integer4, integer5, __exit);
+    task = NEW(struct Context);
+    initContext(task);
+    task->taskManager = &taskManager->taskManager->TaskManager;
+    task->next = C_initIntegerDataGears;
+    task->idgCount = 0;
+    task->idg = task->dataNum;
+    task->maxIdg = task->idg;
+    task->odg = task->maxIdg;
+    task->data[task->odg] = (union Data*)integer2;
+    task->data[task->odg+1] = (union Data*)integer4;
+    task->data[task->odg+2] = (union Data*)integer5;
+    task->maxOdg = task->odg + 3;
+    tasks[2] = task;
+
     //goto taskManager->setWaitTask(createTask1);
     taskManager->contexts = tasks;
     // goto crateTask1();
     taskManager->next = C_createTask1;
-    goto meta(context, taskManager->taskManager->TaskManager.setWaitTask);
+    goto meta(context, taskManager->taskManager->TaskManager.spawnTasks);
 }
 
 void init(int argc, char** argv) {
--- a/src/parallel_execution/generate_stub.pl	Fri Jun 16 16:29:27 2017 +0900
+++ b/src/parallel_execution/generate_stub.pl	Tue Jun 20 03:02:29 2017 +0900
@@ -41,54 +41,18 @@
 my $implementation;
 my $interface;
 
-# interface definision
-#
-# typedef struct Stack<Type, Impl>{
-#         Type* stack;
-#         Type* data;
-#         Type* data1;
-#         __code whenEmpty(...);
-#         __code clear(Impl* stack,__code next(...));
-#         __code push(Impl* stack,Type* data, __code next(...));
-#         __code pop(Impl* stack, __code next(Type*, ...));
-#         __code pop2(Impl* stack, Type** data, Type** data1, __code next(Type**, Type**, ...));
-#         __code isEmpty(Impl* stack, __code next(...), __code whenEmpty(...));
-#         __code get(Impl* stack, Type** data, __code next(...));
-#         __code get2(Impl* stack,..., __code next(...));
-#         __code next(...);
-# } Stack;
-#
-# calling example
-#
-# goto nodeStack->push((union Data*)node, stackTest3);
-#
-# generated meta level code
-#
-# Gearef(context, Stack)->stack = nodeStack->stack;
-# Gearef(context, Stack)->data = (union Data*)node;
-# Gearef(context, Stack)->next = C_stackTest3;
-# goto meta(context, nodeStack->push);
-
 sub getDataGear {
     my ($filename) = @_;
     my ($codeGearName, $name, $inTypedef);
     open my $fd,"<",$filename or die("can't open $filename $!");
     while (<$fd>) {
         if (! $inTypedef) {
-            if (/^typedef struct (\w+)\s*<(.*)>/) {
+            if (/^typedef struct (\w+)/) {
                 $inTypedef = 1;
                 $name = $1;
                 $dataGear{$name} = $_;
                 $var{$name} = {};
                 $code{$name} = {};
-                $generic{$name} = \split(/,/,$2);
-            } elsif (/^typedef struct (\w+)/) {
-                $inTypedef = 1;
-                $name = $1;
-                $dataGear{$name} = $_;
-                $var{$name} = {};
-                $code{$name} = {};
-                $generic{$name} = [];
             } elsif (/^(\w+)(\*)+ create(\w+)\(/) {
                 if (defined $interface) {
                    die "duplicate interface $interface\n"; 
@@ -110,29 +74,8 @@
                 $ttype = $2;
             }
             $var{$name}->{$tname} = $ttype;
-        } elsif (/^\_\_code (\w+)\((.*)\)(.*)/) {
-            my $args = $2;
-            my $method = $1;
-            $code{$name}->{$method} = [];
-            while($args) {
-                if ($args =~ s/(^\s*,\s*)//) {
-                }
-                # continuation case
-                if ($args =~ s/^(\s)*\_\_code\s+(\w+)\(([^)]*)\)//) {
-                    my $next = $2;
-                    my @args = split(/,/,$3);
-                    push(@{$code{$name}->{$method}},$next); 
-                } elsif ($args =~ s/^(struct|union) (\w+)(\*)+\s(\w+)//) {
-                    my $structType = $1;
-                    my $typeName = $2;
-                    my $varName = $4;
-                    my $typeField = lcfirst($typeName);
-                    push(@{$code{$name}->{$method}},$varName); 
-                } elsif ($args =~ s/(.*,)//) {
-                } else {
-                    last;
-                }
-            }
+	} elsif (/\_\_code (\w+)\(/) {
+            $code{$name}->{$1} = 1;
         }
         if (/^}/) {
             $inTypedef = 0;
@@ -156,7 +99,6 @@
         return 0 if ( $n eq $varname1);
     }
     push @{$dataGearVar{$codeGearName}}, $varname1;
-    push @{$dataGearVarType{$codeGearName}}, $typeName;
     if ($typeName eq $implementation) {
         # get implementation
         $dataGearName{$codeGearName} .= "\t$typeName* $varName = ($typeName*)GearImpl(context, $interface, $varName);\n";
@@ -311,31 +253,6 @@
                     print $fd $outputVar{$codeGearName};
                 }
                 next;
-            } elsif (/^(.*)goto (\w+)\-\>(\w+)\((.*)\);/) {
-                # handling goto statement  
-                # convert it to the meta call form with two arugments, that is context and enum Code
-                my $prev = $1;
-                my $next = $2;
-                my $method = $3;
-                my @args = split(/,/,$4);
-                my @types = @$dataGearVarType;
-                my $ntype;
-                for my $v (@$dataGearVar) {
-                    my $t = shift @types;
-                    if ($v eq $next) {
-                        $ntype = $t;
-                    }
-                }
-                print $fd "\tGearef(context, $ntype)->$next = $next->$next;\n";
-                # Put interface argument 
-                my $prot = $code->{$method};
-                for my $arg (@args) {
-                    my $p = shift @$prot;
-                    next if ($p eq $arg);
-                    print $fd "\tGearef(context, $ntype)->$p = $arg;\n";
-                }
-                print $fd "${prev}goto meta(context, $next->$next->$ntype.$method);\n";
-                next;
             } elsif (/^(.*)goto (\w+)\((.*)\);/) {
                 # handling goto statement  
                 # convert it to the meta call form with two arugments, that is context and enum Code