Skip to content

Instantly share code, notes, and snippets.

@thundergnat
Last active August 14, 2021 11:22
Show Gist options
  • Save thundergnat/56753a641e20128619505502efde9133 to your computer and use it in GitHub Desktop.
Save thundergnat/56753a641e20128619505502efde9133 to your computer and use it in GitHub Desktop.
Concurrency / variable locking woes
>$ time raku julia-set.raku
MoarVM oops: MVM_str_hash_fetch_nocheck called with a stale hashtable pointer
MoarVM oops: MVM_str_hash_fetch_nocheck called with a stale hashtable pointer
at julia-set.raku:26 (<ephemeral file>:hsv2rgb)
at julia-set.raku:26 (<ephemeral file>:hsv2rgb)
from julia-set.raku:17 (<ephemeral file>:)
from julia-set.raku:16 (<ephemeral file>:)
from julia-set.raku:17 (<ephemeral file>:)
from SETTING::src/core.c/Rakudo/Internals/HyperRaceSharedImpl.pm6:63 (/home/steve/.perl6/share/perl6/runtime/CORE.c.setting.moarvm:process-batch)
from julia-set.raku:16 (<ephemeral file>:)
from SETTING::src/core.c/Rakudo/Internals/HyperPipeline.pm6:74 (/home/steve/.perl6/share/perl6/runtime/CORE.c.setting.moarvm:)
from SETTING::src/core.c/Rakudo/Internals/HyperRaceSharedImpl.pm6:63 (/home/steve/.perl6/share/perl6/runtime/CORE.c.setting.moarvm:process-batch)
from SETTING::src/core.c/Rakudo/Internals/HyperPipeline.pm6:73 (/home/steve/.perl6/share/perl6/runtime/CORE.c.setting.moarvm:)
from SETTING::src/core.c/Rakudo/Internals/HyperPipeline.pm6:74 (/home/steve/.perl6/share/perl6/runtime/CORE.c.setting.moarvm:)
from SETTING::src/core.c/Rakudo/Internals/HyperPipeline.pm6:70 (/home/steve/.perl6/share/perl6/runtime/CORE.c.setting.moarvm:)
from SETTING::src/core.c/Rakudo/Internals/HyperPipeline.pm6:73 (/home/steve/.perl6/share/perl6/runtime/CORE.c.setting.moarvm:)
from SETTING::src/core.c/Promise.pm6:263 (/home/steve/.perl6/share/perl6/runtime/CORE.c.setting.moarvm:)
from SETTING::src/core.c/Rakudo/Internals/HyperPipeline.pm6:70 (/home/steve/.perl6/share/perl6/runtime/CORE.c.setting.moarvm:)
from SETTING::src/core.c/ThreadPoolScheduler.pm6:883 (/home/steve/.perl6/share/perl6/runtime/CORE.c.setting.moarvm:)
from SETTING::src/core.c/Promise.pm6:263 (/home/steve/.perl6/share/perl6/runtime/CORE.c.setting.moarvm:)
from SETTING::src/core.c/ThreadPoolScheduler.pm6:251 (/home/steve/.perl6/share/perl6/runtime/CORE.c.setting.moarvm:)
from SETTING::src/core.c/ThreadPoolScheduler.pm6:883 (/home/steve/.perl6/share/perl6/runtime/CORE.c.setting.moarvm:)
from SETTING::src/core.c/ThreadPoolScheduler.pm6:239 (/home/steve/.perl6/share/perl6/runtime/CORE.c.setting.moarvm:run-one)
from SETTING::src/core.c/ThreadPoolScheduler.pm6:251 (/home/steve/.perl6/share/perl6/runtime/CORE.c.setting.moarvm:)
from SETTING::src/core.c/ThreadPoolScheduler.pm6:281 (/home/steve/.perl6/share/perl6/runtime/CORE.c.setting.moarvm:)
from SETTING::src/core.c/ThreadPoolScheduler.pm6:242 (/home/steve/.perl6/share/perl6/runtime/CORE.c.setting.moarvm:run-one)
from SETTING::src/core.c/Thread.pm6:54 (/home/steve/.perl6/share/perl6/runtime/CORE.c.setting.moarvm:THREAD-ENTRY)
from SETTING::src/core.c/ThreadPoolScheduler.pm6:284 (/home/steve/.perl6/share/perl6/runtime/CORE.c.setting.moarvm:)
from SETTING::src/core.c/Thread.pm6:54 (/home/steve/.perl6/share/perl6/runtime/CORE.c.setting.moarvm:THREAD-ENTRY)
#`[[
Trying to fix concurrency issues for a Rosettacode entry.
https://rosettacode.org/wiki/Julia_set#Raku
2020.09 and earlier worked flawlessly with no variable locking.
This has become unreliable under recent Raku. I've been attempting
to add appropriate locks to make it reliable again but have not been
succesful. The below works no better than than version with no variable
locking. Falts with:
MoarVM oops: MVM_str_hash_fetch_nocheck called with a stale hashtable pointer
Any hints or pointers?
$ raku -v
Welcome to ๐‘๐š๐ค๐ฎ๐๐จโ„ข v2021.07.
Implementing the ๐‘๐š๐ค๐ฎโ„ข programming language v6.d.
Built on MoarVM version 2021.07.
Incidentally, when it _does_ run, it is about 30% slower now than under 2020.09
~13 seconds vs ~10 seconds on my system.
]]
use Image::PNG::Portable;
my ($w, $h) = 800, 600;
my $out = Image::PNG::Portable.new: :width($w), :height($h);
my $maxIter = 300;
my $c = -0.7 + 0.27015i;
my $lock = Lock::Async.new; # Added for caching troubles
julia($out);
$out.write: 'Julia-set-raku.png';
sub julia ( $png ) {
^$w .race.map: -> $x {
for ^$h -> $y {
my $z = Complex.new(($x - $w / 2) / $w * 3, ($y - $h / 2) / $h * 2);
my $i = $maxIter;
$z = $z*$z + $c while abs($z) < 2 and --$i;
$png.set: $x, $y, |hsv2rgb($i / $maxIter, 1, ?$i).reverse;
}
}
}
sub hsv2rgb ( $h, $s, $v ){
state %cache;
%cache{"$h|$s|$v"} //= do {
$lock.protect: { # Added for caching troubles
my $c = $v * $s;
my $x = $c * (1 - abs( (($h*6) % 2) - 1 ) );
my $m = $v - $c;
[(do given $h {
when 0..^1/6 { $c, $x, 0 }
when 1/6..^1/3 { $x, $c, 0 }
when 1/3..^1/2 { 0, $c, $x }
when 1/2..^2/3 { 0, $x, $c }
when 2/3..^5/6 { $x, 0, $c }
when 5/6..1 { $c, 0, $x }
} ).map: ((*+$m) * 255).Int ]
} # Added for caching troubles
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment