Since the find_end point is to find the last occurrence of the needle in the haystack, parallelization by splitting the haystack into adjacent segments often does not bring any benefit, because if the needle is actually in the last segment, the work performed by all processors other than that which is assigned to the last segment is wasted, and the time is exactly the same as with one processor. Theoretically, a parallel assessment allows you to limit the maximum search time, which is beneficial if (1) the processors do not compete for other tasks and (2) there are relatively few needle instances in the haystack.
In addition, you should be able to coordinate the completion of the process; each process can refuse a search when it finds a match or when its younger brother either found a match or refused a search. After process 0 has found a match or finished places to find them, the process with the lowest index with a match wins.
An alternative is to alternate the search. If you have processors k , then processor 0 is given sequences that end with last-0 , last-k , last-2k ..., processor 1 is given sequences that end with last-1 , last-k-1 , last-2k-1 ... and in general the processor i ( 0 โค i < k ) runs on last-i , last-ki , last-2k-i ...
The coordination of the process is slightly different from the first alternative. Again, every single process can stop as soon as it finds a match. In addition, any process may cease as soon as its current goal is less than the last match found by another process.
Although this should lead to a reasonable parallelization of the search, it is not clear to me that it will be better than a non-parallel linear time algorithm such as Knuth-Morris-Pratt or Boyer-Moore, any of which can be trivially modified to search from right to left. These algorithms will be especially useful in the unusual case when the needle is a compile-time constant that allows you to predict the necessary shift tables. Non-threaded parallelization may benefit from KMP or BM, with the same caution as above: it is likely that most of the involved process will be inappropriate.
source share