I completed the project (as complete as it will ever be). I ended up using one child thread with a queue to create two parallel processes. It was exactly what I wanted, and it was extremely simple to implement. The main process created the valid numbers; the child process paired them up. The two ran very much in parallel since the pairing algorithm actually ran more efficiently working on a growing list of numbers. If I had more processors, I'm not sure how I'd partition the logic.
I think I'd have to re-frame the overall algorithm to take advantage of many processors; then, it may have been more efficient to multi-thread and partition like (some of) you suggested.
The time dropped to 44% of the original (2.25 times faster). Some of the speed came not from the threading, but just learning how to write more efficient perl statements. I learned that there are expensive operations in perl and some very fast operations... I ended up tweaking many, and changing how I was storing the data to take advantage of these realities.
I am sure the program could be made even faster once I understand perl a bit better.
Here are the code snippets relevant to the threading:
# This child thread builds an encoded array of valid pairs.
# "valid pairs" do not have any of the same bits set.
# Output format is row1_integer:row2_integer
my $DataQueue = Thread::Queue->new();
my ($thr) = threads->create({'context' => 'list'}, sub
{
# I tried to use a shared hash of hashes, but the required work to
+ explicity define the hash of hashes in the manner required by thread
+s::shared was just too expensive.
use integer;
# This thread uses queues to get its parameter.
my @ValidPairs=();
my @output=();
while (my $validNum = $DataQueue->dequeue())
{
!($validNum & $_) and push @output, $validNum .':'.$_ foreach
+(@ValidPairs);
push (@ValidPairs, $validNum ); #must add number to this threa
+d's list of valid numbers
}
return (@output);
});
#... Inside loop and logic to build numbers
# send this new number to the child thread waiting on queued par
+ameters.
# The child thread builds the array of valid pairs
$DataQueue->enqueue($i);
#... After block that builds the numbers
$DataQueue->enqueue(undef); #tell the child process to finish what has
+ been queued and then exit.
my @encodedPairedNumbers = $thr->join();
#subsequent code decodes our encoded paired numbers into a hash of has
+hes
Thank you to everyone for your help!
-
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.