Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Threads From Hell #3: Missing Some Basic Prerequisites

by FreeBeerReekingMonk (Deacon)
on May 29, 2015 at 20:13 UTC ( [id://1128339]=note: print w/replies, xml ) Need Help??


in reply to Threads From Hell #3: Missing Some Basic Prerequisites [Solved]

You are queueing pixel by pixel. But the threads can calculate (x,y) by themselves. Why not do the following:

my $width = 1280 * 4; my $height = 1024 * 4; $picturesize = $width * $height; for(my $i=0; $i< $picturesize-1; $i+=32){ $start = $i; $end = $i+32; $x1 = int(($start % $width) /4); $y1 = int(($start / $width) /4); $x2 = int(($end % $width) /4); $y2 = int(($end / $width) /4); printf("(%4d,%4d)..(%4d,%4d) %d\n", $x1,$y1,$x2,$y2, $i); }

This way, the main program only uses the picture size (a large integer), and increases it by 32 units (4 pixels with four byte RGBA perl pixel, I guess). And you give it a start and stop range: $i .. $i+32. The thread can then use those numbers to calculate back the pixels (x,y) themselves, in parallel.
startthread($from,$to,$width)

Replies are listed 'Best First'.
Re^2: Threads From Hell #3: Missing Some Basic Prerequisites
by karlgoethebier (Abbot) on May 30, 2015 at 14:48 UTC

    Thank you very much for your reply FreeBeerReekingMonk.

    "...use those numbers to calculate back the pixels (x,y)..."

    The problem is that i don't understand these numbers:

    ( 0, 0)..( 8, 0) 0 ( 8, 0)..( 16, 0) 32 ( 16, 0)..( 24, 0) 64 ( 24, 0)..( 32, 0) 96 ( 32, 0)..( 40, 0) 128 ...

    Ich stehe gerade auf dem Schlauch.

    Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

      Instead of enqueueing pixels, you enqueue an offset, and a length of 32 (and the thread will also need the width of the image).
      With that data, each thread is able to calculate itself its tasked pixels.

      Thus, if I have offset=0 the formula give calculates (x=0,y=0) to (x=8,y=0).
      If I have offset=32 the formula give calculates (x=8,y=0) to (x=16,y=0).

      Those are 8 pixels of 4 bytes each (RGBA).

      you can avoid that thread-save problem with imagemagick.
      You read an image and $blob=$original->ImageToBlob(); then manipulate all pixels in the blob, and BlobToImage() back.
      See this imagemagick post.
      And also this Permonk post: Re: reading in raw data into perl's ImageMagick

      At least these primitives are ok with threads? (although not your eyes, you see I used seizure inducing red and green lines... sorry for that)

      #! perl use warnings; use strict; use threads; use Image::Magick; my $image = Image::Magick->new( size => "600x600", ); $image->Read("xc:white"); for my $i (200..400){ async{ my $color = $i % 2 ? '#f00' : '#0f0'; $image->Draw( primitive => 'line', points => "$i,100 $i,500", stroke => $color, ); }->join; } $image->Set(magick=>'gif'); my $blob = $image->ImageToBlob(); open(FH,"> $0.gif")or die "$!\n"; print FH $blob; close FH;

      perldoc threads

        Blue rectangle over striped pattern (writing to same pixels), threaded. The Rectangle does not survive intact...

        #! perl use warnings; use strict; use threads; use Image::Magick; my $image = Image::Magick->new( size => "600x600", ); $image->Read("xc:white"); for my $i (200..400){ async{ my $color = $i % 2 ? '#f00' : '#0f0'; $image->Draw( primitive => 'line', points => "$i,100 $i,500", stroke => $color, ); }->join; async{ my $y = 100 + $i; $image->Draw( primitive => 'line', points => "200,$y 400,$y", stroke => '#00f' ); }->join; } $image->Set(magick=>'gif'); my $blob = $image->ImageToBlob(); open(FH,"> $0.gif")or die "$!\n"; print FH $blob; close FH;

        The example in the previous post with and without threading:

        Rate threaded non-threaded threaded 1.41/s -- -94% non-threaded 24.7/s 1652% --

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1128339]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-03-29 06:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found