Greetings, fellow monks,

This one is a MCE::Relay demonstration. Well my friends, MCE::Relay boggles my mind. What a treat :)

See kcott's example for the input file.

use strict; use warnings; use autodie; use MCE::Loop; use Data::Dumper; my $data_file = 'DATA_F.dat'; my (%global_hash1, %global_hash2); my ($global_counter1, $global_counter2) = (0, 0); MCE::Loop::init { use_slurpio => 1, max_workers => 8, init_relay => 0, gather => sub { my ($counter, $hash_ref) = @_; $global_counter2 += $counter; while (my ($k,$v) = each %{$hash_ref}) { $global_hash2{$k} = $v; } }, }; print "# printing counter1\n"; { open (my $fh, '<', $data_file); while (<$fh>) { my ($k,$v) = split; $global_hash1{$k} = $v; print ++$global_counter1, $/; } } print "# done printing counter1\n"; print "# printing counter2\n"; mce_loop_f { my ($mce, $chunk_file, $chunk_id) = @_; my ($wid, $counter, %hash) = (MCE->wid, 0); my $output = "# worker $wid\n"; my $numlines = ${ $chunk_file } =~ tr/\n//; my $relaycount = MCE->relay_recv; MCE::relay { $_ += $numlines }; open my $fh, "<", $chunk_file or die "$!"; while (<$fh>) { my ($k, $v) = split; $hash{$k} = $v; $output .= (++$counter + $relaycount).$/; } close $fh; MCE->gather($counter, \%hash); MCE->print($output); } $data_file; MCE::Loop->finish(); print "# done printing counter2\n"; print "counter1 final: ", $global_counter1, $/; print "counter2 final: ", $global_counter2, $/; print " relay final: ", MCE->relay_final, $/; print "hash1: ", Dumper(\%global_hash1); print "hash2: ", Dumper(\%global_hash2);

Output

# printing counter1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 # done printing counter1 # printing counter2 # worker 2 1 2 # worker 8 5 6 # worker 5 3 4 # worker 7 7 8 # worker 8 17 18 # worker 7 19 20 # worker 4 11 12 # worker 8 21 22 # worker 1 9 10 # worker 6 13 14 # worker 2 25 26 # worker 7 23 24 # worker 3 15 16 # done printing counter2 counter1 final: 26 counter2 final: 26 relay final: 26 hash1: $VAR1 = { '8' => 'h', '3' => 'c', '10' => 'j', '24' => 'x', '13' => 'm', '22' => 'v', '18' => 'r', '7' => 'g', '19' => 's', '21' => 'u', '12' => 'l', '14' => 'n', '23' => 'w', '20' => 't', '11' => 'k', '15' => 'o', '1' => 'a', '6' => 'f', '26' => 'z', '4' => 'd', '9' => 'i', '2' => 'b', '17' => 'q', '5' => 'e', '25' => 'y', '16' => 'p' }; hash2: $VAR1 = { '18' => 'r', '19' => 's', '7' => 'g', '21' => 'u', '8' => 'h', '10' => 'j', '3' => 'c', '13' => 'm', '24' => 'x', '22' => 'v', '11' => 'k', '12' => 'l', '14' => 'n', '23' => 'w', '20' => 't', '4' => 'd', '26' => 'z', '15' => 'o', '1' => 'a', '6' => 'f', '16' => 'p', '9' => 'i', '2' => 'b', '17' => 'q', '5' => 'e', '25' => 'y' };

In reply to Re^2: MCE: How to access variables globally by marioroy
in thread MCE: How to access variables globally by biohisham

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.