Skip to content
This repository has been archived by the owner on Nov 2, 2024. It is now read-only.

Commit

Permalink
whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
mohawk2 committed Oct 24, 2024
1 parent 33eee9b commit 61e0b07
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 70 deletions.
16 changes: 8 additions & 8 deletions examples/barrier-sync.pl
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,29 @@ END
parallelize {
my $tid = parallel_id;
my $pdl = retrieve_pdls('test');

print "Thread id $tid says the ndarray is $pdl\n";
parallel_sync;

my $N_data_to_fix = $pdl->nelem / $N_threads;
my $idx = sequence($N_data_to_fix) * $N_threads + $tid;
$pdl($idx) .= $tid;
parallel_sync;

print "After set, thread id $tid says the ndarray is $pdl\n";
parallel_sync;

$pdl->set($tid, 0);
parallel_sync;

print "Thread id $tid says the ndarray is now $pdl\n";
} $N_threads;

print "mmap is $mmap\n";
parallelize {
my $tid = parallel_id;
my $mmap = retrieve_pdls('mmap');

$mmap($tid) .= $tid;
} $N_threads;

Expand All @@ -59,14 +59,14 @@ END
parallelize {
my $tid = parallel_id;
my $pdl = retrieve_pdls('test');

print "Thread id is $tid\n";

my $N_data_to_fix = $pdl->nelem / $N_threads;
my $idx = sequence($N_data_to_fix - 1) * $N_threads + $tid;
use PDL::NiceSlice;
$pdl($idx) .= -$tid;

my $slice = retrieve_pdls('slice');
$slice($tid) .= -10 * $tid;
} $N_threads;
Expand Down
12 changes: 6 additions & 6 deletions examples/perl-barrier.pl
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@

sub barrier_sync {
yield until $barrier_state eq 'ready' or $barrier_state eq 'up';

lock($barrier_count);
$barrier_state = 'up';
$barrier_count++;

if ($barrier_count == $N_threads) {
$barrier_count--;
$barrier_state = 'down';
Expand All @@ -44,16 +44,16 @@ sub barrier_sync {
# This is the code that actually does calculations
sub main {
my $tid = threads->self->tid;

$data[$tid] = $tid;
barrier_sync;

print "Thread id $tid says the array is ", join(', ', @data), "\n";
barrier_sync;

$data[$tid] = 0;
barrier_sync;

print "Thread id $tid says the array is now ", join(', ', @data), "\n";
}

2 changes: 1 addition & 1 deletion examples/test-memory-consumption.pl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
parallelize {
my $tid = parallel_id;
my ($pdl, $mapped) = retrieve_pdls('test', 'mapped');

print "Thread id $tid is about to sleep for 5 seconds\n";
parallel_sync;
sleep 5;
Expand Down
60 changes: 30 additions & 30 deletions lib/PDL/Parallel/threads/SIMD.pm
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ sub parallel_sync () {
carp("Cannot call parallel_sync outside of a parallelized block");
return;
}

yield until $barrier_state eq 'ready' or $barrier_state eq 'up';

lock($barrier_count);
$barrier_state = 'up';
$barrier_count++;

if ($barrier_count == $N_threads) {
$barrier_count--;
$barrier_state = 'down';
Expand Down Expand Up @@ -83,14 +83,14 @@ sub parallelize (&$) {
local $N_threads = $requested_threads;
# local $barrier_count = 0;
# local $barrier_state = 'ready';

# Launch N-1 threads...
my @to_join = map {
threads->create(\&run_it, $_, $sub)
} (1..$N_threads-1);
# ... and execute the last thread in this, the main thread
run_it(0, $sub);

# Reap the threads
for my $thr (@to_join) {
$thr->join;
Expand All @@ -113,32 +113,32 @@ This documentation describes version 0.02 of PDL::Parallel::threads::SIMD.
=head1 SYNOPSIS
use PDL::Parallel::threads::SIMD qw(parallelize parallel_sync parallel_id);
# Launch five threads that all print a statement
parallelize {
my $pid = parallel_id;
print "Hello from parallel thread $pid\n";
} 5;
my @positions :shared;
# Run 47 time steps, performing the calculations
# for the time steps in parallel
my $size = 100;
my $N_threads = 10;
my $stride = $size / $N_threads;
parallelize {
my $pid = parallel_id;
# Set this thread's portion of the positions to zero
my $start = $stride * $pid;
my $end = $start + $stride - 1;
@positions[$start..$end] = (0) x $stride;
for (1..47) {
# First make sure all the threads are lined up
parallel_sync;
# Now calculate the next positions
$positions[$_] += $velocities[$_] for $start .. $end;
}
Expand Down Expand Up @@ -218,45 +218,45 @@ from its Perl-assigned thread id? The reason is that having such unique,
sequential, and normalized numbers makes it very easy for you to divide the
work between the threads in a simple and predictable way. For example, in
the code shown below, the bounds for the slice are calculated in a
thread-specific fashion based on the parallel thread id.
thread-specific fashion based on the parallel thread id.
use PDL;
use PDL::Parallel::threads;
use PDL::Parallel::threads:SIMD qw(parallelize parallel);
use PDL::NiceSlice;
# Load the data with 7 million elements into $to_sum...
# ...
# Share it.
$to_sum->share_as('to-sum');
# Also allocate some shared, temproary memory:
my $N_threads = 10;
zeroes($N_threads)->share_as('workspace');
my $stride = $to_sum->nelem / $N_threads;
# Parallelize the sum:
parallelize {
my $pid = parallel_id;
my ($to_sum, $temporary)
= retrieve_pdls('to-sum', 'workspace');
# Calculate the thread-specific slice bounds
my $start = $stride * $pid;
my $end = $start + $stride - 1;
$end = $to_sum->nelem - 1 if $end >= $to_sum->nelem;
# Perform this thread's sum
$temporary($pid)
.= $to_sum($start:$end)->sum;
});
# This code will not run until that launch has returned
# for all threads, so at this point we can assume the
# workspace memory has been filled.
my $final_sum = retrieve_pdls('workspace')->sum;
print "The sum is $final_sum\n";
As mentioned in the last comment in that example code, the last
Expand Down Expand Up @@ -284,7 +284,7 @@ follows:
PDL::Parallel::threads::SIMD Execution
======================================
main thread
|
|
Expand All @@ -306,7 +306,7 @@ wait for the GPU to finish:
CUDA Execution
==============
main thread
|
|
Expand Down Expand Up @@ -334,9 +334,9 @@ that could lead to I<VERY> confusing apparent errors in logic. For example:
...
parallelize {
my $pid = parallel_id;
# do some calculations
# Do some thread-specific work
if ($pid < 5) {
# Notice the *two* barriers set up here:
Expand Down Expand Up @@ -395,11 +395,11 @@ Usage:
parallelize {
# ...
parallel_sync;
# ...
} $N_threads;
This function enforces barrier synchronization among all the threads in your
Expand All @@ -424,11 +424,11 @@ Usage:
parallelize {
# ...
my $pid = parallel_id;
# ...
} $N_threads;
From within the L</parallelize> block, you obtain the current thread's
Expand Down
2 changes: 1 addition & 1 deletion t/02_non_threaded.t
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ is_deeply([$to_compare->dims], [$data->dims], 'Retrieved dims is correct')
. " and retrieved dims are " . join', ', $to_compare->dims);

ok($data->type == $to_compare->type, 'Retrieved type is correct')
or diag("Original type is " . $data->type
or diag("Original type is " . $data->type
. " and retrieved type is " . $to_compare->type);

ok(all($to_compare == $data), 'Retrieved value exactly equals original')
Expand Down
16 changes: 8 additions & 8 deletions t/10_physical_piddles.t
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,17 @@ use PDL::Parallel::threads qw(retrieve_pdls);
# Here we allocate shared work space for each PDL data type. We then create
# a collection of threads and have each thread modify the contents of one
# part of the shared memory.
#
#
# While there, each thread does a number of things. It sets a value in the
# shared memory, it confirms that the now-set value is correct, and it
# builds the hash of expected values from such checks. That last part need
# not be done in the threads explicitly, but it makes it easier to write. :-)
#
#
# After all the threads return, we check that all the values agree with what
# we expect, which is fairly easy (though not entirely trivial) to construct
# by hand. I encorporate square-roots into the calculations to ensure good
# bit coverage of the tests, at least for the floating point numbers.
#
#
# The last step simply confirms that sharing slices croaks, a pretty easy
# pair of tests.

Expand Down Expand Up @@ -68,14 +68,14 @@ my @success : shared;
my @expected : shared;
threads->create(sub {
my $tid = shift;

my (%expected_hash, %success_hash, %bits_hash);
for my $type_letter (keys %workspaces) {
my $workspace = retrieve_pdls("workspace_$type_letter");

# Build this up one thread at a time
$expected_hash{$type_letter} = 1;

# Have this thread touch one of the values, and have it double-check
# that the value is correctly set
my $tid_plus_1 = double($tid + 1);
Expand All @@ -86,11 +86,11 @@ threads->create(sub {
$success_hash{$type_letter}
= ($workspace->at($tid,0) == $to_test->at(0));
}

# Make sure the results for each type have a space in shared memory
$expected[$tid] = shared_clone(\%expected_hash);
$success[$tid] = shared_clone(\%success_hash);

}, $_) for 0..$N_threads-1;

# Reap the threads
Expand Down
16 changes: 8 additions & 8 deletions t/20_simd.t
Original file line number Diff line number Diff line change
Expand Up @@ -41,34 +41,34 @@ parallelize {
# Get the pid and log the presence
my $pid = parallel_id;
$pids[$pid] = 1;

$workspace[$pid] = $pid + 1;

# First barrier sync: make sure everybody has updated workspace
parallel_sync;

# Make sure that the previosu pid set the correct value before we reached
# this point.
my $pid_to_check = $pid - 1;
$pid_to_check = $N_threads - 1 if $pid_to_check < 0;
$after_first_block[$pid] = 1;
$after_first_block[$pid] = 0
if $workspace[$pid_to_check] != $pid_to_check + 1;

# Update the workspace value
$workspace[$pid_to_check] = -$pid;

# Second barrier sync: make sure we could perform the first check and
# the assignment
parallel_sync;

# Make sure that the newly changed value, from the other thread, is
# correct here.
$pid_to_check = $pid + 1;
$pid_to_check = 0 if $pid_to_check == $N_threads;
$after_second_block[$pid] = 1;
$after_second_block[$pid] = 0 if $workspace[$pid] != -$pid_to_check;

# Check recursive parallelized block
eval {
parallelize {
Expand All @@ -78,7 +78,7 @@ parallelize {
} or do {
$recursive_simd_allowed[$pid] = 0;
};

} $N_threads;

my @expected = (1) x $N_threads;
Expand Down
Loading

0 comments on commit 61e0b07

Please sign in to comment.