I am afraid that breaking the string will not do.
Generally speaking, early escaping is difficult, so you'd be better off breaking the text in chunks.
But let's ask Herb Sutter to explain searching with parallel algorithms first on Dr Dobbs. The idea is to use the non-uniformity of the distribution to have an early return. Of course Sutter is interested in any match, which is not the problem at hand, so let's adapt.
Here is my idea, let's say we have:
- Text of length
N
p
Processors
- heuristic:
max
is the maximum number of characters a chunk should contain, probably an order of magnitude greater than M
the length of the pattern.
Now, what you want is to split your text into k
equal chunks, where k
is is minimal and size(chunk)
is maximal yet inferior to max
.
Then, we have a classical Producer-Consumer
pattern: the p
processes are feeded with the chunks of text, each process looking for the pattern in the chunk it receives.
The early escape is done by having a flag. You can either set the index of the chunk in which you found the pattern (and its position), or you can just set a boolean, and store the result in the processes themselves (in which case you'll have to go through all the processes once they have stop). The point is that each time a chunk is requested, the producer checks the flag, and stop feeding the processes if a match has been found (since the processes have been given the chunks in order).
Let's have an example, with 3 processors:
[ 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 ]
x x
The chunks 6
and 8
both contain the string.
The producer will first feed 1, 2 and 3 to the processes, then each process will advance at its own rhythm (it depends on the similarity of the text searched and the pattern).
Let's say we find the pattern in 8
before we find it in 6
. Then the process that was working on 7
ends and tries to get another chunk, the producer stops it --> it would be irrelevant. Then the process working on 6
ends, with a result, and thus we know that the first occurrence was in 6
, and we have its position.
The key idea is that you don't want to look at the whole text! It's wasteful!