I am trying to parallelize my ML problem with flann Index. My code in nutshell looks like this:
Index index(dict, KDTreeIndexParams(TREE_NUM)); // HUGE dict, very expensive to construct -- prefer to create it once.
#pragma omp parallel for
for (int i = 0; i < featuresize; i++) {
// creating thread-local params
auto denseTF = index.knnSearch(<thread-local params>);
// not relevant code
}
I looked up documentation here but there is nothing about thread safety there. My concern is whether this snippet is thread safe?
After a day of debugging and catching a data race and reading through source code (here ) I can conclude that index.knnSearch
is NOT thread safe.
Internally, indexTree
is being updated on knnSearch
call. I solved this issue by creating a copy of index
for each thread (yes, it is expensive, but still faster than sequential code).