Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Greetings vr,

Cool gradient background! I tried adding frames simultaneously while running by modifying the gather code block.

gather => sub { if (@_ == 1) { print "\r", $count++; } else { if (!defined $gifdata) { $image = GD::Image-> newFromGifData( shift @{ $_[1] } ); $gifdata = $image-> gifanimbegin( 0,0 ); $gifdata .= $image-> gifanimadd( 1,0,0,1,1 ); } while ( my $data = shift @{ $_[1] } ) { my $frame = GD::Image-> newFromGifData( $data ); $gifdata .= $frame-> gifanimadd( 1,0,0,1,1 ); } } },

That results in workers waiting for the manager process which is fine (i.e. the manager process is now the bottleneck). In that case chunk_size 1 may be faster to have the manager process quickly add one frame. Thereby able to respond to other workers with lesser latency.

use strict; use warnings; use feature 'say'; use Imager; use MCE::Loop; use Time::HiRes 'time'; use GD; STDOUT->autoflush; my $start = time; my $count = 0; my $bkg; my $image; my $gifdata; MCE::Loop->init( max_workers => MCE::Util::get_ncpu(), chunk_size => 1, init_relay => '', gather => sub { if (@_ == 1) { print "\r", $count++; } else { if (!defined $gifdata) { $image = GD::Image-> newFromGifData( shift @{ $_[1] } ); $gifdata = $image-> gifanimbegin( 0,0 ); $gifdata .= $image-> gifanimadd( 1,0,0,1,1 ); } while ( my $data = shift @{ $_[1] } ) { my $frame = GD::Image-> newFromGifData( $data ); $gifdata .= $frame-> gifanimadd( 1,0,0,1,1 ); } } }, user_begin => sub { $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) }, ); mce_loop { 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 { MCE->gather($chunk_id, \@i_data) }; } [ 0 .. 319 ]; MCE::Loop->finish; print " frame GIF done!\n"; printf "compute time: %0.3fs\n", time - $start; $gifdata .= $image-> gifanimend; open my $fh, '>:raw', 'gd.gif'; print $fh $gifdata; close $fh; printf "Total: %0.3fs\n", time - $start;

Regards, Mario


In reply to Re^6: MCE segmentation fault 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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-04-16 03:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found