On Sun, Aug 5, 2012 at 4:41 PM, Denis Gonzalez <[email protected]>wrote:

> Hi everyone,
>
> I would like to learn how to work with parallel processing. I need fill a
> global "F" matrix of 100x100 with values of a function f(x,y). How can I do
> that in a 2 core computer?
>
> I am thinking in to build a code similar to this:
>
> $F = joint fill_F(1,50) with fill_F(51,100)   # how can I do this using
> parallel processing???
>
> sub fill_F {
>    my ($xa,$xb) = @_;
>    my $Faux = zeroes(50,100);
>    ------------------------------------------
>    Here I fill $Faux with values for f(x,y)
>    by using
>    x=[$xa...xb]
>    y=[1..100]
>    -------------------------------------------
>    return $Faux
> }
> Thanks,
> Denis
>
> _______________________________________________
> Perldl mailing list
> [email protected]
> http://mailman.jach.hawaii.edu/mailman/listinfo/perldl
>
>
If you're willing to download and install PDL::Parallel::threads, you can
try something like this. Mind you, it may not be fast (haven't benchmarked
and don't have time at the moment), but it will do what you want. (WARNING:
code not tested)

__EXAMPLE_1__
use strict;
use warnings;
use threads;
use PDL;
use PDL::Parallel::threads 'retrieve_pdls';
use PDL::NiceSlice;

my $data = zeroes(100, 100)->share_as('Some::Data');

# Fill the second half in another thread
async {
    my $data = retrieve_pdls('Some::Data');
    $data(50:) .= values();
};
# Fill the first half in the main thread
$data(0:49) .= other_values();
__END__

If you prefer a SIMD approach, you can use the PDL::Parallel::threads::SIMD
module. It's not documented, but most of the examples in the distribution
use it:

__EXAMPLE_2__
use strict;
use warnings;
use threads;
use PDL;
use PDL::Parallel::threads 'retrieve_pdls';
use PDL::Parallel::threads::SIMD 'launch_simd;
use PDL::NiceSlice;

zeroes(100, 100)->share_as('Some::Data');

launch_simd(2, sub {
    my $tid = shift;
    my $data = retrieve_pdl('Some::Data');
    my $start_idx = $tid * 50;
    my $stop_idx = $start_idx + 50;
    $data($start_idx:$stop_idx) .= values($tid);
});
__END__

I hope that gets you started. :-)

David

-- 
 "Debugging is twice as hard as writing the code in the first place.
  Therefore, if you write the code as cleverly as possible, you are,
  by definition, not smart enough to debug it." -- Brian Kernighan
_______________________________________________
Perldl mailing list
[email protected]
http://mailman.jach.hawaii.edu/mailman/listinfo/perldl

Reply via email to