MCE::Channel is included in recent MCE releases. MCE::Flow allows running multiple tasks simultaneously.

So here is a great demonstration for MCE::Flow and MCE::Channel. The benefit is that the manager process does little work and better able to accommodate workers requesting input with minimum latency. The bottleneck is now the gifout task which is a separate worker. Notice user_begin and user_end called for the first task only by checking MCE->task_id. Notice also max_workers taking an array ref for specifying how many workers for each task.

The following demonstration was made possible by PerlMonks. Thanks vr for providing GD tips. Thank you Anonymous Monk for posting.

use strict; use warnings; use feature 'say'; use Imager; use MCE::Flow; use MCE::Channel; use Time::HiRes 'time'; use GD; STDOUT->autoflush; my $start = time; my $chnl = MCE::Channel->new(); my $count = 0; my $bkg; MCE::Flow->init( max_workers => [ MCE::Util::get_ncpu() - 1, 1 ], chunk_size => 1, init_relay => '', gather => sub { print "\r", $count++; }, user_begin => sub { if (MCE->task_id == 0) { $bkg = Imager->new(xsize=>800, ysize=>600); $bkg->filter(type=>"gradgen", xo=>[ 100, 300, 600 ], yo=>[ 100, 300, 100 ], colors=>[ qw(red blue green) ]); $bkg->filter(type=>"noise", amount=>12, subtype=>0) } }, user_end => sub { if (MCE->task_id == 0) { $chnl->end; } } ); sub task_frames { my ($mce, $chunk_ref, $chunk_id) = @_; my @i_data; for my $x (@{ $chunk_ref }) { my $i = $bkg-> copy; $i->string( text => $x, color => Imager::Color->new('ffffff'), font => Imager::Font->new( # file => '/usr/share/fonts/truetype/msttcorefonts/cour.ttf', # + Ubuntu # file => '/System/Library/Fonts/Courier.dfont', face => 'Courier New', # mswin size => 420, aa => 1), x => 25, y => 500, ); $i->write(data => \my $data, type => 'gif'); push @i_data, $data; MCE->gather($x); } MCE::relay { $chnl->send(\@i_data) }; } sub task_gifout { my $image; my $gifdata; while ( my $i_data_ref = $chnl->recv ) { if (!defined $gifdata) { $image = GD::Image-> newFromGifData( shift @{ $i_data_ref } ) +; $gifdata = $image-> gifanimbegin( 0,0 ); $gifdata .= $image-> gifanimadd( 1,0,0,1,1 ); } while ( my $data = shift @{ $i_data_ref } ) { my $frame = GD::Image-> newFromGifData( $data ); $gifdata .= $frame-> gifanimadd( 1,0,0,1,1 ); } } $gifdata .= $image-> gifanimend; open my $fh, '>:raw', 'gd.gif'; print $fh $gifdata; close $fh; } mce_flow \&task_frames, \&task_gifout, [ 0 .. 319 ]; MCE::Flow->finish; print " frame GIF done!\n"; printf "compute time: %0.3fs\n", time - $start;

There is a lot of parallel going on :) Although workers complete sooner, the manager process still waits for all tasks to finish before MCE::Flow exits.

Regards, Mario


In reply to Re^7: MCE segmentation fault - MCE::Flow + MCE::Channel by marioroy
in thread MCE segmentation fault by Anonymous Monk

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.