I need to build a binary tree, and when branching the tree from list of points, I need to randomly pick a branch point, and I want to use openMP
to accelerate my program, below is how I am doing it. It seems that the random engine gg
would be shared and accessed by all threads, will this make the number it generated less random or slow down my program due to data race? If so, how could I avoid that? should I declare the std::mt19937_64 gg
inside the build function?
#include<random>
#include<omp.h>
std::mt19937_64 gg(std::random_device {}());
int main()
{
omp_set_num_threads(128);
#pragma omp parallel
{
#pragma omp single nowait
buildFromPoints(0, 0, 1000000);
}
return 0;
}
void buildFromPoints(int idx, int lower, int upper)
{
std::uniform_int_distribution<int> distr(lower, upper - 1);
int povotIdx = distr(gg);
// do sth with the random pivotIdx
if (problemSizeIsSmall)
{
buildFromPoints(subPara1, subPara2, subPara3);
buildFromPoints(subPara4, subPara5, subPara6);
}
else
{
#pragma omp task
{
// sub-tree left
buildFromPoints(subPara1, subPara2, subPara3);
}
#pragma omp task
{
// sub-tree right
buildFromPoints(subPara4, subPara5, subPara6);
}
}
}