changeset 760:73e300d2a868

add AtomicTImpl.h
author ikkun <ikkun@cr.ie.u-ryukyu.ac.jp>
date Wed, 06 Jan 2021 14:18:56 +0900
parents c9cd8b1af70f (current diff) 21698f275162 (diff)
children 9ed383f5e77e
files src/parallel_execution/AtomicTImpl.h src/parallel_execution/CMakeLists.txt
diffstat 6 files changed, 165 insertions(+), 159 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/parallel_execution/AtomicTImpl.h	Wed Jan 06 14:18:56 2021 +0900
@@ -0,0 +1,5 @@
+typedef struct AtomicTImpl <T,> impl AtomicT {
+  __code next(...);
+  T atomic;
+  T init;
+} AtomicTImpl;
--- a/src/parallel_execution/examples/pop_and_push/main.cbc	Wed Jan 06 14:02:50 2021 +0900
+++ b/src/parallel_execution/examples/pop_and_push/main.cbc	Wed Jan 06 14:18:56 2021 +0900
@@ -1,74 +1,9 @@
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <unistd.h>
-
-#include "../../../context.h"
 #interface "Stack.h"
 #interface "StackTest.h"
 #interface "TaskManager.h"
 
-int cpu_num = 1;
-int length = 102400;
-int split = 8;
-int* array_ptr;
-int gpu_num = 0;
-int CPU_ANY = -1;
-int CPU_CUDA = -1;
-
-__code initDataGears(struct LoopCounter* loopCounter, struct TaskManager* taskManager) {
-    // loopCounter->tree = createRedBlackTree(context);
-    loopCounter->i = 0;
-    taskManager->taskManager = (union Data*)createTaskManagerImpl(context, cpu_num, gpu_num, 0);
-    goto code1();
-}
-
-__code code1(struct LoopCounter* loopCounter) {
-    printf("cpus:\t\t%d\n", cpu_num);
-    printf("gpus:\t\t%d\n", gpu_num);
-    printf("length:\t\t%d\n", length);
-    printf("length/task:\t%d\n", length/split);
-    /* puts("queue"); */
-    /* print_queue(context->data[ActiveQueue]->queue.first); */
-    /* puts("tree"); */
-    /* print_tree(context->data[Tree]->tree.root); */
-    /* puts("result"); */
-    goto createTask1();
-}
-
-
-__code createTask1(struct LoopCounter* loopCounter, struct TaskManager* taskManager) {
+__code gmain(){
     Stack* stack = createSingleLinkedStack(context);
     StackTest* stackTest = createStackTestImpl3(context);
-    goto stackTest->insertTest1(stack, code2);
-}
-
-__code code2(struct TaskManager* taskManager) {
-    goto taskManager->shutdown(exit_code);
-}
-
-__code code2_stub(struct Context* context) {
-    goto code2(context, &Gearef(context, TaskManager)->taskManager->TaskManager);
+    goto stackTest->insertTest1(stack, shutdown);
 }
-
-
-
-void init(int argc, char** argv) {
-    for (int i = 1; argv[i]; ++i) {
-        if (strcmp(argv[i], "-cpu") == 0)
-            cpu_num = (int)atoi(argv[i+1]);
-        else if (strcmp(argv[i], "-l") == 0)
-            length = (int)atoi(argv[i+1]);
-        else if (strcmp(argv[i], "-s") == 0)
-            split = (int)atoi(argv[i+1]);
-        else if (strcmp(argv[i], "-cuda") == 0) {
-            gpu_num = 1;
-            CPU_CUDA = 0;
-        }
-    }
-}
-
-int main(int argc, char** argv) {
-    init(argc, argv);
-    goto initDataGears();
-}
--- a/src/parallel_execution/generate_stub.pl	Wed Jan 06 14:02:50 2021 +0900
+++ b/src/parallel_execution/generate_stub.pl	Wed Jan 06 14:18:56 2021 +0900
@@ -72,16 +72,21 @@
 my %filename2EachCodeGearArgs;
 my %stub;
 
-my $implInterfaceInfo = {isImpl => undef, implementation => undef, interface => undef, parsedInterfaceInfo => undef, genConstructor => undef};
-my $generateHaveOutputStub = { counter => undef, list => undef };
+my $implInterfaceInfo      = {isImpl   => undef, implementation => undef, interface => undef, parsedInterfaceInfo => undef, genConstructor => undef};
+my $generateHaveOutputStub = { counter => undef, list           => undef };
 my $replaceCodeGearNames = {};
+my $isGmain;
 
 
 # this for statement is main routine
 for my $fn (@ARGV) {
     next if ($fn !~ /\.cbc$/);
     getDataGear($fn);
-    generateDataGear($fn, slurp_cbc_files($fn));
+    my $cbc_content = slurpCbCFiles($fn);
+    if ($isGmain) {
+      $cbc_content = Gears::Stub->generate_new_main(cbc_path => $fn,  cbc_content => $cbc_content);
+    }
+    generateDataGear($fn, $cbc_content);
 }
 
 
@@ -212,6 +217,9 @@
                   $codeGearInfo->{$codeGearName}->{arg}->{$vname} = $vname2type->{$vname};
                 }
                 $currentCodeGear = $codeGearName;
+                if ($codeGearName =~ /gmain/ ) {
+                  $isGmain = 1;
+                }
               } elsif ((/^\s*(union|struct|const|enum)?\s*(\w+)(\*)\s+(\w+)\s+=/) && $currentCodeGear) { # collect local variables
                 my $structType = $1;
                 my $interfaceName = $2;
@@ -778,6 +786,28 @@
                 # Put interface argument
                 my $prot = $code{$ntype}->{$method};
                 my $i = 1;
+                my $nextType = $currentCodeGearInfo->{localVar}->{$next}  // $currentCodeGearInfo->{arg}->{$next};
+                #  use DDP {deparse => 1};
+                #  p $currentCodeGearInfo;
+                my $nextTypePath = $headerNameToInfo->{$nextType}->{path};
+                my $parsedNextTypePath = Gears::Interface->detailed_parse($nextTypePath);
+                #p $parsedNextTypePath;
+                #print "method\n";
+                #p $method;
+                unless (exists $parsedNextTypePath->{codeName}->{$method}) {
+                  die "[ERROR] not found $next definition at $_ in $fn\n";
+                }
+                my $nextMethodInfo = $parsedNextTypePath->{codeName}->{$method};
+                my $nextMethodWantArgc = $nextMethodInfo->{argc};
+
+                if ($nextMethodWantArgc != scalar(@args)) {
+                  #use DDP {deparse => 1};
+                  #  p $nextMethodWantArgc;
+                  #  p $nextMethodInfo;
+                  #  p @args;
+                  #die "[EROR] invalid arg $_  you shoud impl $nextMethodInfo->{args}\n";
+                }
+
                 for my $arg (@args) {
                     my $pType;
                     my $pName;
@@ -1111,7 +1141,7 @@
 }
 
 
-sub slurp_cbc_files  {
+sub slurpCbCFiles  {
   my $cbcFile = shift;
 
   my @emitCbCFileArray;
--- a/src/parallel_execution/lib/Gears/Interface.pm	Wed Jan 06 14:02:50 2021 +0900
+++ b/src/parallel_execution/lib/Gears/Interface.pm	Wed Jan 06 14:18:56 2021 +0900
@@ -144,10 +144,38 @@
 
       if ($line =~ m|__code $codeGearName\(([()\.\*\s\w,_]+)\)|) {
         my $arg = $1;
-        push(@output_code_gears, {
-          name => $codeGearName,
-          args => $arg,
-        });
+
+        # check individual argument
+        # eg. (Impl* self, Int a, char* b, struct hoge* h, __code next(out* o, out* o2, ...))
+        # indivisual argument is  self, a, b, h, next  ( $argc == 5)
+        my @comma_split_arg =  split /,/, $arg;
+        my $inParen = undef;
+        my $argc = 0;
+        for my $tmpArg (@comma_split_arg) {
+          #ignore inner code gear (eg next)
+          if ($tmpArg =~ /\(/ ) {
+            $inParen = 1;
+          }
+
+          # want to __code next(...) <- right paren
+          if ($tmpArg =~ /\)/) {
+            $inParen = undef;
+          }
+
+          next if ($inParen);
+          $argc++;
+        }
+        #ignore self
+        # ex upper case, before $argc == 5, after $argc == 4
+        if (@comma_split_arg) {
+          $argc-- if ($comma_split_arg[0] =~ /Impl/);
+        }
+        my $element = { name => $codeGearName, args => $arg, argc => $argc };
+        push(@output_code_gears, $element);
+
+        #code gear name to hash
+        $ir->{codeName}->{$codeGearName} = $element;
+
         # args  eq "Impl* stack, __code next(Type* data, Type* data1, ...)",
         if ($arg =~ /__code \w+\((.+),\s*\.\.\.\s*\)/) {
           my $outputArgs = $1;
--- a/src/parallel_execution/lib/Gears/Stub.pm	Wed Jan 06 14:02:50 2021 +0900
+++ b/src/parallel_execution/lib/Gears/Stub.pm	Wed Jan 06 14:18:56 2021 +0900
@@ -30,6 +30,7 @@
         next;
       }
       push(@main, $line);
+      next;
     }
 
     if ($line =~ /^#/) {
@@ -39,6 +40,7 @@
 
     if ($line =~ /__code\s+gmain/) {
         $in_gmain = 1;
+        next;
     }
 
     push(@other, $line);
@@ -48,8 +50,8 @@
     includes => \@includes, gmain => \@main, other => \@other
   );
 
-  my @new_content = split /\n/, $new_content;
-  return \@new_content;
+
+  return $new_content;
 }
 
 sub generate_constructor {
--- a/src/parallel_execution/lib/Gears/Template/Gmain.pm	Wed Jan 06 14:02:50 2021 +0900
+++ b/src/parallel_execution/lib/Gears/Template/Gmain.pm	Wed Jan 06 14:18:56 2021 +0900
@@ -27,20 +27,19 @@
 sub _get_context_header_path {
   my $self = shift;
 
-  my $main_cbc_path = File::Spec->rel2abs($self->{cbc_path});
+  my $main_cbc_path      = File::Spec->rel2abs($self->{cbc_path});
   my $generate_stub_path = _get_generate_stub_path();
 
   #print "path: $generate_stub_path", get_word_count_in_str($generate_stub_path, "/"), "\n";
   #print "cbc: $main_cbc_path", get_word_count_in_str($main_cbc_path, "/"), "\n";
 
   my $distance = get_word_count_in_str($main_cbc_path, "/") - get_word_count_in_str($generate_stub_path, "/");
-  my $context = "context.h";
-
+  my $context  = "context.h";
   if ($distance <= 0 ) {
     return $context;
   }
+  my $context_path = join "/", map { ".." } (1..$distance);
 
-  my $context_path = join "/", map { ".." } (1..$distance);
   return "$context_path/$context";
 }
 
@@ -64,93 +63,100 @@
   }
 }
 
+sub npush {
+  my ($array, $arg) = @_;
+  push(@$array, "$arg\n");
+}
+
 sub generate_gmain {
   my ($self, %args) = @_;
 
-  my $includes_list = $args{includes} // cluck "require includes";
-  my $gmain_list    = $args{gmain}    // cluck "require gmain";
-  my $other_list    = $args{other}    // cluck "require other";
+  my $includes = $args{includes} // cluck "require includes";
+  my $gmain    = $args{gmain}    // cluck "require gmain";
+  my $other    = $args{other}    // cluck "require other";
 
-  $self->_generate_include_section($includes_list);
+  $self->_generate_include_section($includes);
+
 
-  my $includes = join "", @$includes_list;
-  my $gmain    = join "", @$gmain_list;
-  my $other    = join "", @$other_list;
+  my @template_content;
+  npush(\@template_content, '#include <stdio.h> ');
+  npush(\@template_content, '#include <string.h> ');
+  npush(\@template_content, '#include <stdlib.h> ');
+  npush(\@template_content, '#include <unistd.h> ');
+  npush(\@template_content, ' ' );
 
-  my $msg = <<"EOFEOF";
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <unistd.h>
+  push(@template_content, @$includes);
 
-$includes
+  npush(\@template_content, ' ' );
+  npush(\@template_content, 'int cpu_num = 1; ' );
+  npush(\@template_content, 'int length = 102400; ' );
+  npush(\@template_content, 'int split = 8; ' );
+  npush(\@template_content, 'int* array_ptr; ' );
+  npush(\@template_content, 'int gpu_num = 0; ' );
+  npush(\@template_content, 'int CPU_ANY = -1; ' );
+  npush(\@template_content, 'int CPU_CUDA = -1; ' );
+  npush(\@template_content, ' ' );
+  npush(\@template_content, '__code initDataGears(struct LoopCounter* loopCounter, struct TaskManager* taskManager) { ' );
+  npush(\@template_content, '    // loopCounter->tree = createRedBlackTree(context); ' );
+  npush(\@template_content, '    loopCounter->i = 0; ' );
+  npush(\@template_content, '    taskManager->taskManager = (union Data*)createTaskManagerImpl(context, cpu_num, gpu_num, 0); ' );
+  npush(\@template_content, '    goto prevTask(); ' );
+  npush(\@template_content, '} ' );
+  npush(\@template_content, ' ' );
+  npush(\@template_content, '__code prevTask(struct LoopCounter* loopCounter) { ' );
+  npush(\@template_content, '    printf("cpus:\t\t%d\n", cpu_num); ' );
+  npush(\@template_content, '    printf("gpus:\t\t%d\n", gpu_num); ' );
+  npush(\@template_content, '    printf("length:\t\t%d\n", length); ' );
+  npush(\@template_content, '    printf("length/task:\t%d\n", length/split); ' );
+  npush(\@template_content, '    /* puts("queue"); */ ' );
+  npush(\@template_content, '    /* print_queue(context->data[ActiveQueue]->queue.first); */ ' );
+  npush(\@template_content, '    /* puts("tree"); */ ' );
+  npush(\@template_content, '    /* print_tree(context->data[Tree]->tree.root); */ ' );
+  npush(\@template_content, '    /* puts("result"); */ ' );
+  npush(\@template_content, '    goto createTask(); ' );
+  npush(\@template_content, '} ' );
+  npush(\@template_content, ' ' );
+  npush(\@template_content, ' ' );
+  npush(\@template_content, '__code createTask(struct LoopCounter* loopCounter, struct TaskManager* taskManager) { ' );
+
+  push(@template_content, @$gmain);
 
-int cpu_num = 1;
-int length = 102400;
-int split = 8;
-int* array_ptr;
-int gpu_num = 0;
-int CPU_ANY = -1;
-int CPU_CUDA = -1;
+  npush(\@template_content, '} ' );
+  npush(\@template_content, ' ' );
 
-__code initDataGears(struct LoopCounter* loopCounter, struct TaskManager* taskManager) {
-    // loopCounter->tree = createRedBlackTree(context);
-    loopCounter->i = 0;
-    taskManager->taskManager = (union Data*)createTaskManagerImpl(context, cpu_num, gpu_num, 0);
-    goto prevTask();
+  npush(\@template_content, '__code shutdown(struct TaskManager* taskManager) { ' );
+  npush(\@template_content, '    goto taskManager->shutdown(exit_code); ' );
+  npush(\@template_content, '} ' );
+  npush(\@template_content, ' ' );
+  npush(\@template_content, '__code shutdown_stub(struct Context* context) { ' );
+  npush(\@template_content, '    goto shutdown(context, &Gearef(context, TaskManager)->taskManager->TaskManager); ' );
+  npush(\@template_content, '} ' );
+  npush(\@template_content, ' ' );
+  npush(\@template_content, ' ' );
+  npush(\@template_content, ' ' );
+  npush(\@template_content, 'void init(int argc, char** argv) { ' );
+  npush(\@template_content, '    for (int i = 1; argv[i]; ++i) { ' );
+  npush(\@template_content, '        if (strcmp(argv[i], "-cpu") == 0) ' );
+  npush(\@template_content, '            cpu_num = (int)atoi(argv[i+1]); ' );
+  npush(\@template_content, '        else if (strcmp(argv[i], "-l") == 0) ' );
+  npush(\@template_content, '            length = (int)atoi(argv[i+1]); ' );
+  npush(\@template_content, '        else if (strcmp(argv[i], "-s") == 0) ' );
+  npush(\@template_content, '            split = (int)atoi(argv[i+1]); ' );
+  npush(\@template_content, '        else if (strcmp(argv[i], "-cuda") == 0) { ' );
+  npush(\@template_content, '            gpu_num = 1; ' );
+  npush(\@template_content, '            CPU_CUDA = 0; ' );
+  npush(\@template_content, '        } ' );
+  npush(\@template_content, '    } ' );
+  npush(\@template_content, '} ' );
+  npush(\@template_content, ' ' );
+  npush(\@template_content, 'int main(int argc, char** argv) { ' );
+  npush(\@template_content, '    init(argc, argv); ' );
+  npush(\@template_content, '    goto initDataGears(); ' );
+  npush(\@template_content, '} ' );
+
+  push(@template_content, @$other);
+
+  return \@template_content;
 }
 
-__code prevTask(struct LoopCounter* loopCounter) {
-    printf("cpus:\t\t%d\n", cpu_num);
-    printf("gpus:\t\t%d\n", gpu_num);
-    printf("length:\t\t%d\n", length);
-    printf("length/task:\t%d\n", length/split);
-    /* puts("queue"); */
-    /* print_queue(context->data[ActiveQueue]->queue.first); */
-    /* puts("tree"); */
-    /* print_tree(context->data[Tree]->tree.root); */
-    /* puts("result"); */
-    goto createTask();
-}
-
-
-__code createTask(struct LoopCounter* loopCounter, struct TaskManager* taskManager) {
-  $gmain
-}
-
-__code shutdown(struct TaskManager* taskManager) {
-    goto taskManager->shutdown(exit_code);
-}
-
-__code shutdown_stub(struct Context* context) {
-    goto shutdown(context, &Gearef(context, TaskManager)->taskManager->TaskManager);
-}
-
-
-
-void init(int argc, char** argv) {
-    for (int i = 1; argv[i]; ++i) {
-        if (strcmp(argv[i], "-cpu") == 0)
-            cpu_num = (int)atoi(argv[i+1]);
-        else if (strcmp(argv[i], "-l") == 0)
-            length = (int)atoi(argv[i+1]);
-        else if (strcmp(argv[i], "-s") == 0)
-            split = (int)atoi(argv[i+1]);
-        else if (strcmp(argv[i], "-cuda") == 0) {
-            gpu_num = 1;
-            CPU_CUDA = 0;
-        }
-    }
-}
-
-int main(int argc, char** argv) {
-    init(argc, argv);
-    goto initDataGears();
-}
-$other
-EOFEOF
-}
-
-
-
 1;