title: How to spawn subthreads from threads in openMP (C++)
body: <p>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 <code>build(root_node, 0)</code>. Now let's suppose that <code>maxDepth</code> is given an arbitrary number and that <code>maxChildren</code> is equal to <code>n</code>. When <code>build(root_node, 0)</code> is called, <code>n</code> threads are launched. I was under the impression that each of these <code>n</code> threads would create <code>n</code> threads. However, careful observation of <code>top</code> revealed that there are never more than <code>n</code> threads. I can saturate my cores only when <code>maxChildren</code> is equal or higher than the number of cores I have. It seems that the <code>parallel</code> 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 <code>build</code>.</p>
  
  <p>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.</p>
  
  <pre><code>void
  build(Node* pNode, unsigned int depth)
  {
      if (depth &gt;= maxDepth)
          return;
      std::list&lt;Node*&gt; children;
      std::list&lt;Node*&gt;::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 &lt; 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);
  }
  </code></pre>