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!


In reply to Closure & Results by remzak
in thread Need a faster way to find matches by remzak

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.