Dear Monks, I need to parallelize the process of finding positions of various patterns which has been collected and stored into a hash (refer the script which i have attached). if I dont parallelized the process of find patterns stored in a hash then all patterns has to stay in the queue until the preceding pattern is completed. So here I dont know whether to use threads or forks for achieving parallelization.
I have attached my script, where I have tried to implement perl ithreads to accomplish the parallelization (at line 25, inside the foreach loop). I dont think that the threads I have implemented works properly, because there's no visible improvement in the script as used without threads.
here in this paragraph, i have tried to explain the objective of my work. i get in a string and break it down into a length specified substrings (which may vary) and find the positions of those substrings.
use threads ; use feature "say"; use warnings; my $input_file = "text"; #Contains a single lined strin +g without any newline character open my $hd, "<", $input_file or die "Couldn't open '$input_file' - $! +"; my $text=<$hd>; chomp($text); my $window=5; #Length of the window my $i=0; LOOP:seek $hd,$i,0; #Here seek and read give us the + first $window lengthed substing runned over loop read ($hd,my $substring,$window); my %hash; if(length($substring)==$window) { $hash{$substring}=$i; #$substring pushed into a has +h, here I have avoided array has there may be repeated substrings in +the $text $i++; goto LOOP; } my $count = keys %hash; my $j=0; foreach my $pattern (keys %hash) { if($j<=$count) { my $thr="thread".$j; $$thr=threads->create(\&position,$text,$pattern); $j++; } } for($ii=0;$ii<=$count;$ii++) { $thr="thread".$ii; print $thr,"\n"; $$thr->join(); } sub position { my ($text,$pattern) = @_ ; my $offset = 0; print "\n$pattern ($window)\n"; print '~' x $window,"\n"; my $pos=index $text,$pattern,$offset; while ($pos != -1) { print $pos+1," to ",$pos+$window,"\n"; $offset = $pos + 99; $pos = index($text, $pattern, $offset); } }
In reply to Trouble implementing threads by saranrsm
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |