title: creating openmp threads for functions
body: <p>All the tutorial examples for openmp that I see are for creating threads for for loops. But I need to create threads for ordinary groups of statements which may be clustered into functions. eg, something like the following:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;omp.h&gt;
  int A() { printf("in A:%d\n", omp_get_thread_num()); }
  int B() { printf("in B:%d\n", omp_get_thread_num()); }
  int D() { printf("in D:%d\n", omp_get_thread_num()); }
  int E() { printf("in E:%d\n", omp_get_thread_num()); }
  int F() { printf("in F:%d\n", omp_get_thread_num()); }
  int G() { printf("in G:%d\n", omp_get_thread_num()); }
  int H() { printf("in H:%d\n", omp_get_thread_num()); }
  int C() {
      printf("in C:%d\n", omp_get_thread_num());
      #pragma omp parallel num_threads(2)
      {
          D(); // want to execute D,E in separate threads
          E();
      }
      F();
  }
  main() {
      omp_set_nested(1);
      printf("in main:%d\n", omp_get_thread_num());
      G();
      #pragma omp parallel num_threads(3)
      {
          A(); // want to execute A,B,C in separate threads
          B();
          C();
      }
      H();
  }
  </code></pre>
  
  <p>In the above code, I want each function to execute exactly once, but in different threads. (So I might be wrong in the above code with the usage of the directives, please correct it as needed.)</p>
  
  <p>How do I code this kind of nested parallelism of functions with openmp? Will these functions share all the global variables that are available, or is there a way to specify which variables will be shared by which functions?</p>
  
  <p><strong>EDITS:</strong> After reading Jorge Bellon's answer below, I coded the following, and its output is shown after the code. It looks like thread-0 is being used for many of the functions, which is not what I intended - I want the functions to be executed in parallel. Also, I want only one execution for G, so looks like I have to delete the "num_threads(3)" line. Let me know what is the fix for this problem.</p>
  
  <pre><code>// compile this with: g++ -fopenmp
  int A() { printf("in H:%d\n", omp_get_thread_num()); sleep(1); }
  // similarly for B, D, E, F, G, H
  int C() {
      printf("in C:%d\n", omp_get_thread_num()); sleep(1);
      #pragma omp task
      D();
      #pragma omp task
      E();
      #pragma omp taskwait
      F(); sleep(1);
  }
  main() {
      omp_set_nested(1);
      printf("in main:%d\n", omp_get_thread_num());
      #pragma omp parallel num_threads(3)
      G();
      #pragma omp task
      A();
      #pragma omp task
      B();
      #pragma omp task
      C();
      #pragma omp taskwait
      H();
  }
  // outputs:
  in main:0
  in G:1
  in G:0
  in G:2
  in A:0
  in B:0
  in C:0
  in D:0
  in E:0
  in F:0
  in H:0
  </code></pre>