Jump to content

Parallel calculations: Difference between revisions

m
→‎{{header|Perl 6}}: Use a smaller deterministic sample to return verifiable results. rearrange and expand commentary.
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 a smaller deterministic sample to return verifiable results. rearrange and expand commentary.)
Line 1,399:
 
=={{header|Perl 6}}==
The second line takesTakes the list of numbers and converts thenthem to a <tt>HyperSeq</tt> that is stored in a raw variable. Any <tt>HyperSeq</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>HyperSeq</tt> is lazy, so the computation of values will happen in chunks as they are requested in the third line.
 
Note that in this specific example (prime decomposition of these 15 numbers), the overhead from setting up threading eats up any gain from doing it in parallel. Probably wouldn't see any real speed up until there are 50 or more numbers to decompose, but this example is deterministic, compact and verifiable.
 
Using the <tt>prime-factors</tt> routine as defined in the [[Prime_decomposition#Perl_6 |prime decomposition]] task:
<lang perl6>my @nums = (1_000_00064921987050997300559, .. 100_000_000).pick:70251412046988563035, 10000; 71774104902986066597,
83448083465633593921, 84209429893632345702, 87001033462961102237,
87762379890959854011, 89538854889623608177, 98421229882942378967,
259826672618677756753, 262872058330672763871, 267440136898665274575,
278352769033314050117, 281398154745309057242, 292057004737291582187;
 
my \factories = @nums.hyper.map: *.&prime-factors.cache;
say my $gmf = {}.append(factories»[0] »=>« @nums).max: {+.key};
Line 1,427 ⟶ 1,436:
$factor;
}</lang>
{{out}}
The second line takes the list of numbers and converts then to a <tt>HyperSeq</tt> that is stored in a raw variable. Any <tt>HyperSeq</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>HyperSeq</tt> is lazy, so the computation of values will happen in chunks as they are requested in the third line.
<pre>736717 => [64921987050997300559 71774104902986066597 83448083465633593921 87001033462961102237 89538854889623608177 98421229882942378967]</pre>
 
Beside <tt>HyperSeq</tt> and its (allowed to be) out-of-order equivalent <tt>RaceSeq</tt>, [[Rakudo]] supports primitive threads, locks and highlevel promises. Using channels and supplies values can be move thread-safely from one thread to another. A react-block can be used as a central hub for message passing.
10,343

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.