Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^2: a look at Sub::Genius with the debugger, and use v in 2021

by Aldebaran (Curate)
on Jun 25, 2021 at 21:31 UTC ( #11134302=note: print w/replies, xml ) Need Help??


in reply to Re: a look at Sub::Genius with the debugger, and use v in 2021
in thread a look at Sub::Genius with the debugger, and use v in 2021

Hi Brett,

Welcome to the monastery. I hope your friendly presence makes it feel less cloistered here. I've taken the opportunity to look and listen to your presentations, and I'm so glad that you took the time you needed to get through the initial arc of it. I've been in conversations with people who do representations that take 1, 3, 6 hours, and you seem to be of this variety, ideas coming out of your ideas like the florets of a cauliflower("...and in the infinite case....") You should know that I'm only intermediate as a practioner of perl, despite having been at it for a while now.

I have some pretty grandiose ideas of where this software could take us, but it would have to be an "us" thing. By that I mean, you, me, and others would have to get their paws on this if it's gonna realize its goal. One might think that there's fewer perl practioners to help with anything, but what if it offered the carrot of not having bad forks nor race conditions, provided that the user doesn't embellish and add them? The last perl script I wrote with fork was equal amounts hideous and faulty.

I've worked through several examples. The five older ones are solid, but the last 3 pi ones need some more spitshine. My theory is that you wrote over your previous work. Let's take a look at the second one first. I will list source, output from stdout, source text from debugger, then debugger output:

#!/usr/bin/env perl use strict; use warnings; use feature 'state'; use Sub::Genius; my $NUM_THREADS = 5; my $preplan = q{ ( step0 & step1 & step2 & step3 & step4 & step5 ) reduce }; my $final_scope = Sub::Genius->new( preplan => $preplan )->run_any( sc +ope => { sum => 0.0, num_steps => 1_000_000, pi => undef } ); printf qq{pi = %f\n}, $final_scope->{pi}; sub AUTOLOAD { our $AUTOLOAD; my $sub = $AUTOLOAD; $sub =~ s/.*:://; die if $sub !~ m/^step([\d]+)/; my $step_id = $1; # deal with 'step' my $old_scope = shift; my $new_scope = _snep( $step_id, $old_scope ); return $new_scope; } sub _snep { my ( $step_id, $scope ) = @_; $scope->{sum} = _do_calc( $step_id, $scope ); return $scope; } sub reduce { my $scope = shift; my $num_steps = $scope->{num_steps}; my $step = 1 / $num_steps; $scope->{pi} = $scope->{sum} * $step; return $scope; } sub _do_calc { my ( $id, $scope ) = @_; my $sum = $scope->{sum}; my $num_steps = $scope->{num_steps}; my $step = 1 / $num_steps; for ( my $i = $id ; $i < $num_steps ; $i += $NUM_THREADS ) { my $x = ( $i + 0.5 ) * $step; $sum += 4.0 / ( 1 + $x * $x ); } return $sum; }

Stdout has:

$ ./pi2.pl Died at ./pi2.pl line 36. $

This is my debugger source text, 3.scope.txt:

#source 2.scope.txt f Genius.pm b 282 s n n p $sub v s v s v b 35 c n p $sub p $sub !~ m/^step([\d]+)/ #save 3.scope.txt

Debug output:

$ perl -d pi2.pl Loading DB routines from perl5db.pl version 1.55 Editor support available. Enter h or 'h h' for help, or 'man perldebug' for more help. main::(pi2.pl:9): my $NUM_THREADS = 5; DB<1> source 3.scope.txt >> #source 2.scope.txt >> f Genius.pm Choosing /usr/local/share/perl/5.30.0/Sub/Genius.pm matching 'Genius.p +m': >> b 282 >> s main::(pi2.pl:11): my $preplan = q{ main::(pi2.pl:12): ( main::(pi2.pl:13): step0 main::(pi2.pl:14): & main::(pi2.pl:15): step1 main::(pi2.pl:16): & main::(pi2.pl:17): step2 main::(pi2.pl:18): & main::(pi2.pl:19): step3 main::(pi2.pl:20): & main::(pi2.pl:21): step4 main::(pi2.pl:22): & main::(pi2.pl:23): step5 main::(pi2.pl:24): ) main::(pi2.pl:25): reduce >> n main::(pi2.pl:28): my $final_scope = Sub::Genius->new( preplan => $ +preplan )->run_any( scope => { sum => 0.0, num_steps => 1_000_000, pi + => undef } ); >> n Sub::Genius::run_once(/usr/local/share/perl/5.30.0/Sub/Genius.pm:282): 282: eval sprintf( qq{%s%s(\$opts{scope});}, $opts{ns}, + $sub ); >> p $sub step >> v 279 # main run loop - run once 280: local $@; 281: foreach my $sub (@seq) { 282==>b eval sprintf( qq{%s%s(\$opts{scope});}, $opts{n +s}, $sub ); 283: die $@ if $@; # be nice and die for easier debu +ggering 284 } 285 } 286: return $opts{scope}; 287 } 288 >> >> s Sub::Genius::run_once((eval 44)[/usr/local/share/perl/5.30.0/Sub/Geniu +s.pm:282]:1): 1: main::step($opts{scope}); >> v 1==> main::step($opts{scope}); 2 ; >> s main::AUTOLOAD(pi2.pl:33): our $AUTOLOAD; >> v 30: printf qq{pi = %f\n}, $final_scope->{pi}; 31 32 sub AUTOLOAD { 33==> our $AUTOLOAD; 34: my $sub = $AUTOLOAD; 35: $sub =~ s/.*:://; 36: die if $sub !~ m/^step([\d]+)/; 37: my $step_id = $1; 38 39 # deal with 'step' >> b 35 >> c main::AUTOLOAD(pi2.pl:35): $sub =~ s/.*:://; >> n main::AUTOLOAD(pi2.pl:36): die if $sub !~ m/^step([\d]+)/; >> p $sub step >> p $sub !~ m/^step([\d]+)/ 1 >> #save 3.scope.txt DB<10> s + Sub::Genius::run_once(/usr/local/share/perl/5.30.0/Sub/Genius.pm:283): 283: die $@ if $@; # be nice and die for easier debu +ggering DB<10> s + Died at pi2.pl line 36. at /usr/local/share/perl/5.30.0/Sub/Genius.pm line 283. Sub::Genius::run_once(Sub::Genius=HASH(0x564d07b619b8), "scope", H +ASH(0x564d07e0f210)) called at /usr/local/share/perl/5.30.0/Sub/Geniu +s.pm line 245 Sub::Genius::run_any(Sub::Genius=HASH(0x564d07b619b8), "scope", HA +SH(0x564d07e0f210)) called at pi2.pl line 28 Debugged program terminated. Use q to quit or R to restart, use o inhibit_exit to avoid stopping after program termination, h q, h R or h o to get additional info. DB<10>

The truth is that I don't get what goes wrong here, but I think I've framed where it happens. The scoping is hard for me to get my head around, but is it not the implementation of an AST?

I could take you through the first one in a similar way, but again, I think it got overwritten, so fix 2 then fix 1 makes sense in such a scenario to me. The third is tantalizing, but we seem to have some inflation there:

$ ./pi3.pl pi = 3.769908 $

Have you read Borwein and Borwein re pi calculations? (A classic.) Worth the interlibrary loan if you haven't.

Anyways, I'm gonna get this posted hoping that the surfeit of brilliant perl programmers who suddenly have more time on the their hands than usual can pick up where I tap out.

Also, I've seen how you attack a keyboard, and monastery markup might be tedious from scratch. This is what I use as a template for posts:

#!/usr/bin/perl -w use v5.030; use Path::Tiny; use POSIX qw(strftime); # initialization that must precede main data structure # User: enter a subdirectory you would like to create # enter a subdirectory of this^^^ for output my $ts = "perlmonks"; my $output = "writeups"; ## turning things to Path::Tiny my $abs = path(__FILE__)->absolute; my $path1 = Path::Tiny->cwd; my $path2 = path( $path1, $ts ); say "abs is $abs"; say "path1 is $path1"; say "path2 is $path2"; print "This script will build the above path2. Proceed? (y|n)"; my $prompt = <STDIN>; chomp $prompt; die unless ( $prompt eq "y" ); ## special do-hickeys I want at the end my @list = ( '[id://1177642]', '[mod://Astro::Coord::ECI::Sun]', '[|] +', '[|]', '[|]','&lt;', '&gt;', '&#91;', '&#93;' ,'<ul> <li>vanilla</li> <li>chocolate</li> </ul>'); @list = map { "$_\n" } @list; say "list is @list"; my $return1 = write_monk_tags(); say "return1 is $return1"; my $munge = strftime( "%d-%m-%Y-%H-%M-%S", localtime ); $munge .= ".monk.txt"; # use Path::Tiny to create and write to a text in relevant directory my $save_file = path( $path2, $output, $munge )->touchpath; my $return2 = $save_file->spew($return1); say "return2 is $return2"; my $return3 = $save_file->append(@list); say "return3 is $return3"; say "created file $save_file"; system( "gedit $save_file &"); sub write_monk_tags { my $tag_pair = '<x></x>'; my $return = ''; # User: change these quoted values for different order or tags my @buchstaben = qw/i p c pre readmore b/; for my $letter (@buchstaben) { print "How many $letter tag pairs would you like?: "; my $prompt = <STDIN>; chomp $prompt; while ( $prompt gt 0 ) { my $new_string = $tag_pair; $new_string =~ s/x/${letter}/g; say "new string is $new_string"; $return = $return . $new_string . "\n"; --$prompt; } } return $return; } __END__

Edit: Removed the use of an earlier version and redundant use warnings in a subroutine.

use warnings; use 5.011;

Have a great day. I like scripting in the AC when the alternative is an unforgiving sun....

Replies are listed 'Best First'.
Re^3: a look at Sub::Genius with the debugger, and use v in 2021
by oodler (Initiate) on Jun 28, 2021 at 11:31 UTC
    Aldebaran, thanks for the kind words and welcome. I'll reply to a few things to get a handle on what direction you're heading.
    The truth is that I don't get what goes wrong here, but I think I've framed where it happens. The scoping is hard for me to get my head around, but is it not the implementation of an AST?

    The AST is only relevant when it comes to the construction of the DFA that is ultimately used to produce a single valid line of execution. Sub::Genius just takes what FLAT is able to generate as a string derived from the PRE (by way of an equivalent DFA). Are you trying to figure out how to go from the PRE to the DFA? An AST is involved in the conversion from the PRE to the PFA, but after that it is not involved at all.

    Thank you for the pointer to the work of the Borweins, I am not a math guy but do enjoy reading about the subject and interesting people around it. Numerical errors notwithstanding, here are some suggestions that I think can help:
    • any word in the PRE represents a subroutine that is called when the "plan" is run; you have access to all the good stuff perl provides, such as AUTOLOAD; this was just a demo of handling subroutines that were not explicitly defined
    • the stubby utility has a new "list" subcommand that will take a PRE and list all valid orderings implied; I added this as a way to more easily see what execution orderings of the subroutines you were admitting
    • there is an error in the pi examples that are corrected in the latest release on CPAN - NUM_THREADS should be 6; but I think you figured that out
    • all parts of perl's memory model hold, see below for what I mean.
    Here's an example of running stubby to list all possible valid subroutine execution orderings:
    $ stubby list -p "begin ( sub1 & sub2 & sub3 ) end" begin sub1 sub3 sub2 end begin sub1 sub2 sub3 end begin sub2 sub1 sub3 end begin sub2 sub3 sub1 end begin sub3 sub1 sub2 end begin sub3 sub2 sub1 end
    Regarding perl's memory model, you may program all subroutines assuming that they can access:
    • globaly variables
    • state variables defined within each sub (to create coroutines)
    • an additional execution scope represented by $scope that can be initialized (or not), then treated as an accumulator that is passed into each sub call and returned via each sub call
    My contention is that using the right plan and correct use of the "memory" space, any implicitly shared memory algorithm may be implemented correctly. This remains to be seen, but that's my gut feeling. Note - everything is run sequentially in the perl runtime - thought, there is nothing stopping you from using fork; but that just gets us back to the realities of perl being fundamentally a uniprocess.
    Hope that helps! Feel free to send me links to some stuff you've played with here - obviously, PM is not ideal for really swapping code. :-)
Re^3: a look at Sub::Genius with the debugger, and use v in 2021
by Aldebaran (Curate) on Jun 26, 2021 at 23:44 UTC
    I could take you through the first one in a similar way,

    I have enough time on a hades hot afternoon to do a lazy write-up for the first pi script from examples for Sub::Genius, that, on my terminal, is tripped up at run_any which calls run_once . I think this frames the issue:

    $ cat 5.pi.txt b 10 c n s n n v b 283 c v s #save 5.pi.txt $ perl -d pi.pl Loading DB routines from perl5db.pl version 1.55 Editor support available. Enter h or 'h h' for help, or 'man perldebug' for more help. main::(pi.pl:8): my $NUM_THREADS = 5; DB<1> source 5.pi.txt + >> b 10 >> c main::(pi.pl:10): my $preplan = q{ main::(pi.pl:11): ( main::(pi.pl:12): step0 main::(pi.pl:13): & main::(pi.pl:14): step1 main::(pi.pl:15): & main::(pi.pl:16): step2 main::(pi.pl:17): & main::(pi.pl:18): step3 main::(pi.pl:19): & main::(pi.pl:20): step4 main::(pi.pl:21): & main::(pi.pl:22): step5 main::(pi.pl:23): ) main::(pi.pl:24): reduce >> n main::(pi.pl:27): my $final_scope = Sub::Genius->new( preplan => $p +replan )->run_any( scope => { sum => 0.0, num_steps => 1_000_000, pi +=> undef } ); >> s Sub::Genius::new(/usr/local/share/perl/5.30.0/Sub/Genius.pm:15): 15: my $pkg = shift; >> n Sub::Genius::new(/usr/local/share/perl/5.30.0/Sub/Genius.pm:16): 16: my %self = @_; >> n Sub::Genius::new(/usr/local/share/perl/5.30.0/Sub/Genius.pm:17): 17: my $self = \%self; >> v 14 sub new { 15: my $pkg = shift; 16: my %self = @_; 17==> my $self = \%self; 18: bless $self, $pkg; 19: die qq{'preplan' parameter required!\n} if not defined $sel +f->{preplan}; 20 21 # set to undef to disable preprocessing 22: if ( not exists $self->{preprocess} ) { 23: $self->{preprocess} = 1; >> b 283 >> c Sub::Genius::run_once(/usr/local/share/perl/5.30.0/Sub/Genius.pm:283): 283: die $@ if $@; # be nice and die for easier debu +ggering >> v 280: local $@; 281: foreach my $sub (@seq) { 282: eval sprintf( qq{%s%s(\$opts{scope});}, $opts{ns}, + $sub ); 283==>b die $@ if $@; # be nice and die for easier d +ebuggering 284 } 285 } 286: return $opts{scope}; 287 } 288 289 # >> s Undefined subroutine &main::step called at (eval 38)[/usr/local/share/ +perl/5.30.0/Sub/Genius.pm:282] line 1. at /usr/local/share/perl/5.30.0/Sub/Genius.pm line 283. Sub::Genius::run_once(Sub::Genius=HASH(0x5643068207b0), "scope", H +ASH(0x564306ab08c0)) called at /usr/local/share/perl/5.30.0/Sub/Geniu +s.pm line 245 Sub::Genius::run_any(Sub::Genius=HASH(0x5643068207b0), "scope", HA +SH(0x564306ab08c0)) called at pi.pl line 27 Debugged program terminated. Use q to quit or R to restart, use o inhibit_exit to avoid stopping after program termination, h q, h R or h o to get additional info. >> #save 5.pi.txt DB<5> q + $ cat pi.pl #!/usr/bin/env perl use strict; use warnings; use Sub::Genius; my $NUM_THREADS = 5; my $preplan = q{ ( step0 & step1 & step2 & step3 & step4 & step5 ) reduce }; my $final_scope = Sub::Genius->new( preplan => $preplan )->run_any( sc +ope => { sum => 0.0, num_steps => 1_000_000, pi => undef } ); printf qq{pi = %f\n}, $final_scope->{pi}; sub step0 { my $scope = shift; my $step_id = 0; $scope->{sum} = _do_calc( $step_id, $scope ); return $scope; } sub step1 { my $scope = shift; my $step_id = 1; $scope->{sum} = _do_calc( $step_id, $scope ); return $scope; } sub step2 { my $scope = shift; my $step_id = 2; $scope->{sum} = _do_calc( $step_id, $scope ); return $scope; } sub step3 { my $scope = shift; my $step_id = 3; $scope->{sum} = _do_calc( $step_id, $scope ); return $scope; } sub step4 { my $scope = shift; my $step_id = 4; $scope->{sum} = _do_calc( $step_id, $scope ); return $scope; } sub step5 { my $scope = shift; my $step_id = 5; $scope->{sum} = _do_calc( $step_id, $scope ); return $scope; } sub reduce { my $scope = shift; my $num_steps = $scope->{num_steps}; my $step = 1 / $num_steps; $scope->{pi} = $scope->{sum} * $step; return $scope; } sub _do_calc { my ( $id, $scope ) = @_; my $sum = $scope->{sum}; my $num_steps = $scope->{num_steps}; my $step = 1 / $num_steps; for ( my $i = $id ; $i < $num_steps ; $i += $NUM_THREADS ) { my $x = ( $i + 0.5 ) * $step; $sum += 4.0 / ( 1 + $x * $x ); } return $sum; } $

    Even with the number of threads bumped to 6, it goes no farther. I must say I'm puzzled, because I do not see how this source differs from what Brett shows to compile and behave on his screen at the one hour mark of his second youtube presentation.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11134302]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (1)
As of 2022-01-28 06:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:












    Results (73 votes). Check out past polls.

    Notices?