olafmar has asked for the wisdom of the Perl Monks concerning the following question:
Conceptually, I read with the master thread the data of the different files, one at time, and I interleave them in an array. Later I start multiple threads, each one working on a different part of the initial array. I use Statistics::Basic to calculate the standard deviation of the values of a small slice of the initial array and I put the results in a temporary array, then copied to the array of the results, that is the only shared array I use.
This is the code of the main thread managing the work:
Each worker thread executes the following sub:my %mythreads; my $work_package = int($elements / $number_threads + 0.5); my @ReturnData :shared; my $start = 0; my $stop = $start + $work_package * $files - 1; $stop = $elements * $files - 1 if ($stop >= $elements * $files); for (my $i = 0; $i < $number_threads; $i++) { $mythreads{$i} = threads->create(\&do_work); $start = $stop + 1; $stop += $work_package * $files; $stop = $elements * $files - 1 if ($stop >= $elements * $files); } # collect data foreach (sort(keys(%mythreads))) { $mythreads{$_}->join(); }
I timed the execution and the results are found here: http://img62.imageshack.us/i/perlthreading.png/ 6 seconds are taken by the other parts of the script, the remaining is for the code displayed in this post.sub do_work { my @working_block = @input_data[$start .. $stop]; my @partial_output_data; $, = undef; $\ = undef; my $output_line = 0; for (my $i = 0; $i < @working_block; $i += $files) { # calculates the stddev of each slice and outputs the formatte +d resuls if (max(@working_block[$i .. $i + $files - 1]) == 0) { $partial_output_data[$output_line] = '0.00000E+00'; } else { $partial_output_data[$output_line] = sprintf('%.5E', stddev(@working_block[$i .. $i + $file +s - 1]) / mean(@working_block[$i .. $i + $files - 1])); } $output_line++; } @ReturnData[$start / $files .. $stop / $files] = @partial_output_d +ata; return; }
Is this behaviour normal? what could I do to improve the performances? this was more a test than a real need, but I would like to know for the future. I thought threads were easy enough not to increase too much the code size/effort, while processes would have required IPC or something else.
Thank you very much.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Poor performances with threads
by BrowserUk (Patriarch) on Oct 23, 2009 at 11:18 UTC | |
by moritz (Cardinal) on Oct 23, 2009 at 11:50 UTC | |
by BrowserUk (Patriarch) on Oct 23, 2009 at 12:06 UTC | |
by olafmar (Novice) on Oct 26, 2009 at 10:01 UTC | |
by olafmar (Novice) on Oct 26, 2009 at 12:18 UTC | |
|
Re: Poor performances with threads
by BrowserUk (Patriarch) on Oct 23, 2009 at 09:59 UTC | |
by olafmar (Novice) on Oct 23, 2009 at 10:20 UTC | |
|
Re: Poor performances with threads
by markuhs (Scribe) on Oct 23, 2009 at 11:27 UTC | |
|
Re: Poor performances with threads
by zentara (Cardinal) on Oct 23, 2009 at 12:52 UTC |