Parallel calculations: Difference between revisions

m
→‎{{header|Perl 6}}: Use Hyper instead of Race to match up the values correctly. Instead of just displaying the first number that has a particular glf, display an array of all found values.
m (→‎{{header|Perl 6}}: Use Hyper instead of Race to match up the values correctly. Instead of just displaying the first number that has a particular glf, display an array of all found values.)
Line 1,399:
 
=={{header|Perl 6}}==
AssumingUsing thatthe <tt>prime-factors</tt> is defined exactlyroutine as defined in the [[Prime_decomposition#Perl_6 |prime decomposition]] task:
<lang perl6>my @nums = (1_000_000 .. 100_000_000).pick: 10000010000;
<lang perl6>
my \factories = @nums.race(:batch(@nums / 32))hyper.map: *.&prime-factors.cache;
my @nums = (1_000_000 .. 100_000_000).pick: 100000;
say my $gmf = {}.append(factories»[0] »=>« @nums).reduce(&max): {+.valuekey};
my \factories = @nums.race(:batch(@nums / 32)).map: *.&prime-factors.cache;
my $gmf = (factories»[0] »=>« @nums).reduce(&max).value;
 
sub prime-factors ( Int $n where * > 0 ) {
Line 1,427 ⟶ 1,426:
$factor = find-factor( $n, $constant + 1 ) if $n == $factor;
$factor;
}</lang perl6>
}
The second line takes the list of numbers and converts then to a <tt>RaceSeqHyperSeq</tt> that is stored in a raw variable. Any <tt>RaceSeqHyperSeq</tt> overloads <tt>map</tt> and <tt>grep</tt> to convert and pick values in worker threads. The runtime will pick the number of OS-level threads and assign worker threads to them while avoiding stalling in any part of the program. A <tt>RaceSeqHyperSeq</tt> is lazy, so the computation of values will happen in chunks as they are requested in the third line.
</lang>
The second line takes the list of numbers and converts then to a <tt>RaceSeq</tt> that is stored in a raw variable. Any <tt>RaceSeq</tt> overloads <tt>map</tt> and <tt>grep</tt> to convert and pick values in worker threads. The runtime will pick the number of OS-level threads and assign worker threads to them while avoiding stalling in any part of the program. A <tt>RaceSeq</tt> is lazy, so the computation of values will happen in chunks as they are requested in the third line.
 
Beside <tt>RaceSeqHyperSeq</tt> and its in(allowed to be) out-of-order equivalent <tt>HyperSeqRaceSeq</tt>, [[Rakudo]] supports primitive threads, locks and highlevel promises. Using channels and supplies values can be move threadsafethread-safely from one thread to another. A react-block can be used as a central hub for message passing.
 
In [[Perl 6]] most errors are bottled up <tt>Exceptions</tt> inside <tt>Failure</tt> objects that remember where they are created and thrown when used. This is useful to pass errors from one thread to another without losing file and linennumber of the source file that caused the error.
10,333

edits