changeset 396:bba401f93dcd

Add handle par goto statement
author Tatsuki IHA <innparusu@cr.ie.u-ryukyu.ac.jp>
date Thu, 24 Aug 2017 15:32:38 +0900
parents 864cd4e346e9
children c43ec0e3fa84
files src/parallel_execution/examples/calc/calc.cbc src/parallel_execution/generate_stub.pl
diffstat 2 files changed, 87 insertions(+), 46 deletions(-) [+]
line wrap: on
line diff
--- a/src/parallel_execution/examples/calc/calc.cbc	Tue Aug 01 20:09:33 2017 +0900
+++ b/src/parallel_execution/examples/calc/calc.cbc	Thu Aug 24 15:32:38 2017 +0900
@@ -110,49 +110,13 @@
     Integer* integer1 = &ALLOCATE_DATA_GEAR(context, Integer)->Integer;
     Integer* integer2 = &ALLOCATE_DATA_GEAR(context, Integer)->Integer;
     Integer* integer3 = &ALLOCATE_DATA_GEAR(context, Integer)->Integer;
-    // par goto mult(integer1, integer2, integer3, __exit);
-    struct Context* task = NEW(struct Context);
-    initContext(task);
-    task->next = C_mult;
-    task->idgCount = 2;
-    task->idg = task->dataNum;
-    task->data[task->idg] = (union Data*)integer1;
-    task->data[task->idg+1] = (union Data*)integer2;
-    task->maxIdg = task->idg + 2;
-    task->odg = task->maxIdg;
-    task->data[task->odg] = (union Data*)integer3;
-    task->maxOdg = task->odg + 1;
-    tasks[0] = task;
+    par goto mult(integer1, integer2, integer3, __exit);
 
     Integer* integer4 = &ALLOCATE_DATA_GEAR(context, Integer)->Integer;
     Integer* integer5 = &ALLOCATE_DATA_GEAR(context, Integer)->Integer;
-    // par goto add(integer4, integer5, integer1, __exit);
-    task = NEW(struct Context);
-    initContext(task);
-    task->next = C_add;
-    task->idgCount = 2;
-    task->idg = task->dataNum;
-    task->data[task->idg] = (union Data*)integer4;
-    task->data[task->idg+1] = (union Data*)integer5;
-    task->maxIdg = task->idg + 2;
-    task->odg = task->maxIdg;
-    task->data[task->odg] = (union Data*)integer1;
-    task->maxOdg = task->odg + 1;
-    tasks[1] = task;
+    par goto add(integer4, integer5, integer1, __exit);
 
-    // par goto initIntegerDataGears(integer2, integer4, integer5, __exit);
-    task = NEW(struct Context);
-    initContext(task);
-    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;
+    par goto initIntegerDataGears(integer2, integer4, integer5, __exit);
 
     taskManager->contexts = tasks;
     // goto crateTask1();
--- a/src/parallel_execution/generate_stub.pl	Tue Aug 01 20:09:33 2017 +0900
+++ b/src/parallel_execution/generate_stub.pl	Thu Aug 24 15:32:38 2017 +0900
@@ -41,6 +41,7 @@
 my %dataGearName;
 my %generic;
 my %dataGearVarType;
+my %codeGear;
 my $implementation;
 my $interface;
 
@@ -107,6 +108,14 @@
                 if ( -f "$impln.cbc") {
                     &getCodeGear("$impln.cbc");
                 }
+            } elsif(/^(.*)par goto (\w+)\((.*)\)/) {
+                my $codeGearName = $2;
+                if ($filename =~ /^(.*)\/(.*)/) {
+                    $codeGearName = "$1/$codeGearName";
+                }
+                if ( -f "$codeGearName.cbc") {
+                    &getCodeGear("$codeGearName.cbc");
+                }
             }
             next;
         }
@@ -160,6 +169,31 @@
                     }
                 }
             }
+        } elsif (/^\_\_code (\w+)\((.*)\)(.*)/) {
+            my $codeGearName = $1;
+            my $args = $2;
+            my $inputCount = 0;
+            my $outputCount = 0;
+            my $inputIncFlag = 1;
+            while($args) {
+                if ($args =~ s/(^\s*,\s*)//) {
+                }
+                if ($args =~ s/^(\s)*\_\_code\s+(\w+)\(//) {
+                    $inputIncFlag = 0;
+                } elsif ($args =~ s/^(struct|union) (\w+)(\*)+\s(\w+)//) {
+                    if($inputIncFlag) {
+                        $inputCount++;
+                    }
+                    else {
+                        $outputCount++;
+                    }
+                } elsif ($args =~ s/(.*,)//) {
+                } else {
+                    last;
+                }
+            }
+            $codeGear{$codeGearName}->{"input"} = $inputCount;
+            $codeGear{$codeGearName}->{"output"} = $outputCount;
         }
     }
 }
@@ -194,7 +228,7 @@
                         $outputVar{$codeGearName} .= "\t$typeName* $varName;\n";
                         return 1;
                     }
-                
+
                     $dataGearName{$codeGearName} .= "\t$typeName* $varName = Gearef(context, $interface)->$varName;\n";
                     return 1;
                 }
@@ -209,7 +243,7 @@
         }
         # global or local variable case
         if ($typeName eq "Code") {
-           $dataGearName{$codeGearName} .= "\tenum $typeName $varName = Gearef(context, $interface)->$varName;\n";
+            $dataGearName{$codeGearName} .= "\tenum $typeName $varName = Gearef(context, $interface)->$varName;\n";
             return 1;
         }
         $dataGearName{$codeGearName} .= "\t$typeName* $varName = Gearef(context, $typeName);\n";
@@ -223,7 +257,7 @@
 
     my $fn;
     if ($opt_o) {
-       $fn = $opt_o;
+        $fn = $opt_o;
     } else {
         my $fn1 = $filename;
         $fn1 =~ s/\.cbc/.c/;
@@ -244,6 +278,7 @@
     my $prevCodeGearName;
     my $inTypedef = 0;
     my $inStub = 0;
+    my $inParGoto = 0;
     my %stub;
     my $codeGearName;
 
@@ -376,6 +411,46 @@
                 }
                 print $fd "${prev}goto meta(context, $next->$next->$ntype.$method);\n";
                 next;
+            } elsif(/^(.*)par goto (\w+)\((.*)\);/) {
+                # handling par goto statement
+                # convert it to the parallel
+                my $prev = $1;
+                my $codeGearName = $2;
+                my @dataGears = split(/,\s*/, $3);
+                my $nextCodeGear = pop(@dataGears);
+                my $inputCount = $codeGear{$codeGearName}->{'input'};
+                my $outputCount = $codeGear{$codeGearName}->{'output'};
+                if (! $inParGoto) {
+                    $inParGoto = 1;
+                    my $initTasks = << "EOFEOF";
+                    ${prev}struct Context** tasks = (struct Context**)ALLOC_ARRAY(context, Context, ?);
+                    ${prev}int taskCount = 0;
+EOFEOF
+                    print $fd $initTasks;
+                }
+
+                my $initTask = << "EOFEOF";
+                ${prev}struct Context* task = NEW(struct Context);
+                ${prev}initContext(task);
+                ${prev}task->next = C_$codeGearName;
+                ${prev}task->idgCount = $inputCount;
+                ${prev}task->idg = task->dataNum;
+                ${prev}task->maxIdg = task->idg + $inputCount;
+                ${prev}task->odg = task->maxIdg;
+                ${prev}task->maxOdg = task->odg + $outputCount;
+EOFEOF
+                print $fd $initTask;
+                for my $i (0..$inputCount-1) {
+                    print $fd "${prev}task->data[task->idg+$i] = (union Data*)@dataGears[$i];\n";
+                }
+
+                for my $i (0..$outputCount-1) {
+                    print $fd "${prev}task->data[task->odg+$i] = (union Data*)@dataGears[$inputCount+$i];\n";
+                }
+
+                print $fd "${prev}tasks[taskCount] = task;\n";
+                print $fd "${prev}taskCount++;\n";
+                next;
             } elsif (/^(.*)goto (\w+)\((.*)\);/) {
                 # handling goto statement  
                 # convert it to the meta call form with two arugments, that is context and enum Code
@@ -384,10 +459,10 @@
                 my @args = split(/,/,$3);
                 my $v = 0;
                 for my $n ( @{$dataGearVar{$codeGearName}} ) {
-                     # continuation arguments 
+                    # continuation arguments 
                     $v = 1  if ( $n eq $next);
                 }
-                if ($v || defined $code{$interface}->{$next}) { 
+                if ($v || defined $code{$interface}->{$next}) {
                     # write continuation's arguments into the interface arguments
                     # we may need a commit for a shared DataGear
                     for my $arg ( @{$outputArgs{$codeGearName}->{$next}} ) {
@@ -397,11 +472,13 @@
                     print $fd "${prev}goto meta(context, $next);\n";
                     next;
                 }
-            } else {
+            }
+            else {
                 s/new\s+(\w+)\(\)/\&ALLOCATE(context, \1)->\1/g;   # replacing new
             }
-        # gather type name and type
+            # gather type name and type
         } elsif (/^}/) {
+            $inParGoto = 0;
             $inStub = 0;
             $inTypedef = 0;
         }