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?
答案 0 :(得分:3)
经过一天的调试和捕获数据竞争并阅读源代码(here)后,我可以得出结论index.knnSearch
不是线程安全的。
在内部,indexTree
正在knnSearch
来电更新。我通过为每个线程创建index
的副本来解决这个问题(是的,它很昂贵,但仍然比顺序代码更快)。