title: Efficiency of using omp parallel for inside omp parallel omp task
body: <p>I am trying to improve the efficiency of a complicated project using OpenMP. I will use a toy example to show the situation I am facing.</p>
  <p>First, I have a recursion function and I use <code>omp task</code> to improve it, like this:</p>
  <pre><code>void CollectMsg(Node *n) {
  #pragma omp task
  {
      CollectMsg(n-&gt;child1);
      ProcessMsg1(n-&gt;array, n-&gt;child1-&gt;array);
  }
  
  #pragma omp task
  {
      CollectMsg(n-&gt;child2);
      ProcessMsg1(n-&gt;array, n-&gt;child2-&gt;array);
  }
  
  #pragma omp taskwait
      ProcessMsg2(n-&gt;array);
  }
  
  void main() {
  
      #pragma omp parallel num_threads(2)
      {
  #pragma omp single
          {
              CollectMsg(root);
          }
      }
  }
  </code></pre>
  <p>Then, however, the function <code>ProcessMsg1</code> contains some computations and I want to use <code>omp parallel for</code> on it. The following code shows a simple example. In this way, the project runs correctly, but the efficiency of it is just slightly improved. I am wondering the working mechanism of this process. Does it work if I use <code>omp parallel for</code> inside <code>omp task</code>? I have this question because there is only a slight difference on efficiency between using and not using <code>omp parallel for</code> in function <code>ProcessMsg1</code>. If the answer is yes, first I created 2 threads for <code>omp task</code>, and in the processing of each task, I created 2 threads for <code>omp parallel for</code>, then should I think of this parallel process as 2*2=4 threaded parallelism? If the answer is no, then how can I further improve the efficiency of this case?</p>
  <pre><code>void ProcessMsg1(int *a1, int *a2) {
  #pragma omp parallel for num_threads(2)
      for (int i = 0; i &lt; size; ++i) {
          a1[i] *= a2[i];
      }
  }
  </code></pre>