问题
I am trying to build a tree with a fixed number of children as well as a fixed depth. I do not fully understand the underlying mechanics of openMP. Tree construction begins upon calling build(root_node, 0)
. Now let's suppose that maxDepth
is given an arbitrary number and that maxChildren
is equal to n
. When build(root_node, 0)
is called, n
threads are launched. I was under the impression that each of these n
threads would create n
threads. However, careful observation of top
revealed that there are never more than n
threads. I can saturate my cores only when maxChildren
is equal or higher than the number of cores I have. It seems that the parallel
blocks in subsequent levels in the recursion have no effet, limiting the number of available threads for subsequent use to what was needed in the initial call to build
.
Why does it behave this way? Has the recursion any part in this? And most importantly, what can I do to remedy this? Thanks in advance.
void
build(Node* pNode, unsigned int depth)
{
if (depth >= maxDepth)
return;
std::list<Node*> children;
std::list<Node*>::iterator it;
// This loop cannot be parallelized because each call to select_next_node
// is dependent on the previous one
for (unsigned i = 0; i < maxChildren; ++i)
{
Node* const p_candidate_node = select_next_node(...);
if (is_valid(p_candidate_node))
children.push_back(p_candidate_node);
}
#pragma omp parallel private(it)
for (it = children.begin(); it != children.end(); ++it)
#pragma omp single nowait
build(*it, depth + 1);
}
回答1:
Nested parallelism is disabled by default in almost all OpenMP run-times. You should explicitly enable it by one of those two methods:
- call
omp_set_nested(1);
- set environment variable
OMP_NESTED
toTRUE
Nested parallelism might not be what you want in that case. The number of threads could grow very fast and consume lots of system resources. You should rather use OpenMP tasks. They should be supported by all OpenMP 3.0 compliant compilers.
来源:https://stackoverflow.com/questions/10825860/how-to-spawn-subthreads-from-threads-in-openmp-c