Mercurial > hg > Gears > Gears
changeset 753:6f2341eb225a
(none)
author | ikkun <ikkun@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Sat, 02 Jan 2021 16:56:27 +0900 |
parents | 1d24435ab9b8 (current diff) 179fde29e4c0 (diff) |
children | 60e5f3c55d02 3f3cf13153d1 |
files | |
diffstat | 2 files changed, 184 insertions(+), 6 deletions(-) [+] |
line wrap: on
line diff
--- a/src/parallel_execution/lib/Gears/Stub.pm Sat Jan 02 16:55:48 2021 +0900 +++ b/src/parallel_execution/lib/Gears/Stub.pm Sat Jan 02 16:56:27 2021 +0900 @@ -5,29 +5,51 @@ use Carp qw/croak/; use Gears::Interface; +use Gears::Template::Gmain; -sub parsed_main { - my ($self, $cbc_content) = @_; +sub generate_new_main { + my ($class, %args) = @_; + + my $cbc_content = $args{cbc_content}; - my @replace_cbc_content = (); - my @headers = (); + my $template = Gears::Template::Gmain->new( + cbc_path => $args{cbc_path}, + cbc_content => $args{cbc_content}, + ); + + + my @includes = (); + my @main = (); + my @other = (); my $in_gmain = undef; for my $line (@$cbc_content){ if ($in_gmain) { + if ($line =~ /^}$/) { + $in_gmain = undef; + next; + } + push(@main, $line); } if ($line =~ /^#/) { - push(@headers, $line); + push(@includes, $line); next; } if ($line =~ /__code\s+gmain/) { $in_gmain = 1; } + + push(@other, $line); } - return \@replace_cbc_content; + my $new_content = $template->generate_gmain( + includes => \@includes, gmain => \@main, other => \@other + ); + + my @new_content = split /\n/, $new_content; + return \@new_content; } sub generate_constructor {
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/parallel_execution/lib/Gears/Template/Gmain.pm Sat Jan 02 16:56:27 2021 +0900 @@ -0,0 +1,156 @@ +package Gears::Template::Gmain; +use strict; +use warnings; +use Carp qw/cluck/; + +use FindBin; +use File::Spec; + +sub new { + my ($class, %args) = @_; + + my $self = {}; + $self->{cbc_path} = $args{cbc_path} // cluck "require cbc_file"; + $self->{cbc_content} = $args{cbc_content} // slurp($args{cbc_file}); + return bless $self, $class; +} + +sub slurp { + my $cbc_file = shift; + local $/ = undef; + open my $fh, '<', $cbc_file; + my $content = <$fh>; + close $fh; + return $content; +} + +sub _get_context_header_path { + my $self = shift; + + 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"; + + if ($distance <= 0 ) { + return $context; + } + + my $context_path = join "/", map { ".." } (1..$distance); + return "$context_path/$context"; +} + + +sub get_word_count_in_str { + my ($str, $pattern) = @_; + my $count = () = $str =~ m/$pattern/g; + return $count; +} + +sub _get_generate_stub_path { + return $FindBin::Bin; +} + +sub _generate_include_section { + my ($self, $headers) = @_; + + unless (grep { $_ =~ /#include "[.\/]+context\.h"/ } @$headers) { + my $context_header = $self->_get_context_header_path(); + push(@$headers, '#include "' . $context_header . '"' . "\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"; + + $self->_generate_include_section($includes_list); + + my $includes = join "", @$includes_list; + my $gmain = join "", @$gmain_list; + my $other = join "", @$other_list; + + my $msg = <<"EOFEOF"; +#include <stdio.h> +#include <string.h> +#include <stdlib.h> +#include <unistd.h> + +$includes + +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 prevTask(); +} + +__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;