title: "hello world" C program using hybrid of OpenMP and MPI
body: <p>I'm trying to get a "hello world" program that uses both OpenMP and MPI working. I started with the example here</p>
  
  <p><a href="http://www.slac.stanford.edu/comp/unix/farm/mpi_and_openmp.html" rel="nofollow">http://www.slac.stanford.edu/comp/unix/farm/mpi_and_openmp.html</a></p>
  
  <p>but I can't reproduce the output. Here's the exact source I am using:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;mpi.h&gt;
  #include &lt;omp.h&gt;
  
  int main(int argc, char *argv[]) {
    int numprocs, rank, namelen;
    char processor_name[MPI_MAX_PROCESSOR_NAME];
    int iam = 0, np = 1;
  
    MPI_Init(&amp;argc, &amp;argv);
    MPI_Comm_size(MPI_COMM_WORLD, &amp;numprocs);
    MPI_Comm_rank(MPI_COMM_WORLD, &amp;rank);
    MPI_Get_processor_name(processor_name, &amp;namelen);
  
    //omp_set_num_threads(4);
  
  #pragma omp parallel default(shared) private(iam, np)
    {
      np = omp_get_num_threads();
      iam = omp_get_thread_num();
      printf("Hello from thread %d out of %d from process %d out of %d on %s\n",
             iam, np, rank, numprocs, processor_name);
    }
  
    MPI_Finalize();
  }
  </code></pre>
  
  <p>I am using a dual-processor Xeon workstation (2x6 cores) running Ubuntu 12.10. I have no trouble getting programs that use MPI or OpenMP (but not both) to work.</p>
  
  <p>I compiled the source above using the command</p>
  
  <pre><code>mpicc -fopenmp hello.c -o hello
  </code></pre>
  
  <p>then ran using </p>
  
  <pre><code>export OMP_NUM_THREADS=4
  mpirun ./hello -np 2 -x OMP_NUM_THREADS
  </code></pre>
  
  <p>Here is the output I am getting:</p>
  
  <pre><code>Hello from thread 0 out of 4 from process 0 out of 1 on SteinbergT5600Linux
  Hello from thread 2 out of 4 from process 0 out of 1 on SteinbergT5600Linux
  Hello from thread 1 out of 4 from process 0 out of 1 on SteinbergT5600Linux
  Hello from thread 3 out of 4 from process 0 out of 1 on SteinbergT5600Linux
  </code></pre>
  
  <p>I SHOULD, according to the example, be getting something like this:</p>
  
  <pre><code>Hello from thread 0 out of 4 from process 0 out of 2 on SteinbergT5600Linux
  Hello from thread 2 out of 4 from process 0 out of 2 on SteinbergT5600Linux
  Hello from thread 1 out of 4 from process 0 out of 2 on SteinbergT5600Linux
  Hello from thread 3 out of 4 from process 0 out of 2 on SteinbergT5600Linux
  Hello from thread 0 out of 4 from process 1 out of 2 on SteinbergT5600Linux
  Hello from thread 2 out of 4 from process 1 out of 2 on SteinbergT5600Linux
  Hello from thread 1 out of 4 from process 1 out of 2 on SteinbergT5600Linux
  Hello from thread 3 out of 4 from process 1 out of 2 on SteinbergT5600Linux
  </code></pre>
  
  <p>Can anyone give me a hint as to what I'm doing wrong? As far as I can tell I am exactly reproducing the example in the link above.</p>