in reply to Need a faster way to find matches
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!
|
|---|