changeset 754:60e5f3c55d02

gmain template
author anatofuz <anatofuz@cr.ie.u-ryukyu.ac.jp>
date Mon, 04 Jan 2021 15:04:33 +0900
parents 6f2341eb225a
children f37759c62604
files src/parallel_execution/lib/Gears/Stub.pm src/parallel_execution/lib/Gears/Template/Gmain.pm
diffstat 2 files changed, 87 insertions(+), 84 deletions(-) [+]
line wrap: on
line diff
--- a/src/parallel_execution/lib/Gears/Stub.pm	Sat Jan 02 16:56:27 2021 +0900
+++ b/src/parallel_execution/lib/Gears/Stub.pm	Mon Jan 04 15:04:33 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	Sat Jan 02 16:56:27 2021 +0900
+++ b/src/parallel_execution/lib/Gears/Template/Gmain.pm	Mon Jan 04 15:04:33 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";
 }
 
@@ -67,90 +66,92 @@
 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;
+  push(@template_content, '#include <stdio.h> ' );
+  push(@template_content, '#include <string.h> ' );
+  push(@template_content, '#include <stdlib.h> ' );
+  push(@template_content, '#include <unistd.h> ' );
+  push(@template_content, ' ' );
 
-  my $msg = <<"EOFEOF";
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-#include <unistd.h>
+  push(@template_content, @$includes);
 
-$includes
+  push(@template_content, ' ' );
+  push(@template_content, 'int cpu_num = 1; ' );
+  push(@template_content, 'int length = 102400; ' );
+  push(@template_content, 'int split = 8; ' );
+  push(@template_content, 'int* array_ptr; ' );
+  push(@template_content, 'int gpu_num = 0; ' );
+  push(@template_content, 'int CPU_ANY = -1; ' );
+  push(@template_content, 'int CPU_CUDA = -1; ' );
+  push(@template_content, ' ' );
+  push(@template_content, '__code initDataGears(struct LoopCounter* loopCounter, struct TaskManager* taskManager) { ' );
+  push(@template_content, '    // loopCounter->tree = createRedBlackTree(context); ' );
+  push(@template_content, '    loopCounter->i = 0; ' );
+  push(@template_content, '    taskManager->taskManager = (union Data*)createTaskManagerImpl(context, cpu_num, gpu_num, 0); ' );
+  push(@template_content, '    goto prevTask(); ' );
+  push(@template_content, '} ' );
+  push(@template_content, ' ' );
+  push(@template_content, '__code prevTask(struct LoopCounter* loopCounter) { ' );
+  push(@template_content, '    printf("cpus:\t\t%d\n", cpu_num); ' );
+  push(@template_content, '    printf("gpus:\t\t%d\n", gpu_num); ' );
+  push(@template_content, '    printf("length:\t\t%d\n", length); ' );
+  push(@template_content, '    printf("length/task:\t%d\n", length/split); ' );
+  push(@template_content, '    /* puts("queue"); */ ' );
+  push(@template_content, '    /* print_queue(context->data[ActiveQueue]->queue.first); */ ' );
+  push(@template_content, '    /* puts("tree"); */ ' );
+  push(@template_content, '    /* print_tree(context->data[Tree]->tree.root); */ ' );
+  push(@template_content, '    /* puts("result"); */ ' );
+  push(@template_content, '    goto createTask(); ' );
+  push(@template_content, '} ' );
+  push(@template_content, ' ' );
+  push(@template_content, ' ' );
+  push(@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;
+  push(@template_content, '} ' );
+  push(@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();
+  push(@template_content, '__code shutdown(struct TaskManager* taskManager) { ' );
+  push(@template_content, '    goto taskManager->shutdown(exit_code); ' );
+  push(@template_content, '} ' );
+  push(@template_content, ' ' );
+  push(@template_content, '__code shutdown_stub(struct Context* context) { ' );
+  push(@template_content, '    goto shutdown(context, &Gearef(context, TaskManager)->taskManager->TaskManager); ' );
+  push(@template_content, '} ' );
+  push(@template_content, ' ' );
+  push(@template_content, ' ' );
+  push(@template_content, ' ' );
+  push(@template_content, 'void init(int argc, char** argv) { ' );
+  push(@template_content, '    for (int i = 1; argv[i]; ++i) { ' );
+  push(@template_content, '        if (strcmp(argv[i], "-cpu") == 0) ' );
+  push(@template_content, '            cpu_num = (int)atoi(argv[i+1]); ' );
+  push(@template_content, '        else if (strcmp(argv[i], "-l") == 0) ' );
+  push(@template_content, '            length = (int)atoi(argv[i+1]); ' );
+  push(@template_content, '        else if (strcmp(argv[i], "-s") == 0) ' );
+  push(@template_content, '            split = (int)atoi(argv[i+1]); ' );
+  push(@template_content, '        else if (strcmp(argv[i], "-cuda") == 0) { ' );
+  push(@template_content, '            gpu_num = 1; ' );
+  push(@template_content, '            CPU_CUDA = 0; ' );
+  push(@template_content, '        } ' );
+  push(@template_content, '    } ' );
+  push(@template_content, '} ' );
+  push(@template_content, ' ' );
+  push(@template_content, 'int main(int argc, char** argv) { ' );
+  push(@template_content, '    init(argc, argv); ' );
+  push(@template_content, '    goto initDataGears(); ' );
+  push(@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;