OpenMP: Difference between revisions

Jump to navigation Jump to search
1,520 bytes added ,  7 years ago
no edit summary
No edit summary
No edit summary
Line 170: Line 170:
Environment variables specific to the Intel compiler start with <tt>KMP_</tt> whereas those specific to Gnu start with <tt>GOMP_</tt>. For optimal performance regarding memory access, it is important to set the
Environment variables specific to the Intel compiler start with <tt>KMP_</tt> whereas those specific to Gnu start with <tt>GOMP_</tt>. For optimal performance regarding memory access, it is important to set the
<tt>OMP_PROC_BIND</tt> variable as well as the affinity variables, <tt>KMP_AFFINITY</tt> for Intel, and <tt>GOMP_CPU_AFFINITY</tt> for GNU compilers. This prevents the movement of OpenMP threads between processors by the operating system. This is particularly important in a [http://en.wikipedia.org/wiki/Non_Uniform_Memory_Access NUMA] architecture found in most modern computers.
<tt>OMP_PROC_BIND</tt> variable as well as the affinity variables, <tt>KMP_AFFINITY</tt> for Intel, and <tt>GOMP_CPU_AFFINITY</tt> for GNU compilers. This prevents the movement of OpenMP threads between processors by the operating system. This is particularly important in a [http://en.wikipedia.org/wiki/Non_Uniform_Memory_Access NUMA] architecture found in most modern computers.
== Example ==
Here is a ''hello world'' example that shows the use of openMP.
{| border="0" cellpadding="5" cellspacing="0" align="center"
! style="background:#8AA8E5;" | '''C CODE''': <tt>ompHello.c</tt>
! style="background:#ECCF98;" | '''FORTRAN CODE''': <tt>ompHello.f90</tt>
|-valign="top"
|<source lang="c">
#include <stdio.h>
#include <omp.h>
int main() {
  #pragma omp parallel
  {
      printf("Hello world from thread %d out of %d\n",
              omp_get_thread_num(),omp_get_num_threads());
  }
  return 0;
}
</source>
|<source lang="fortran">
program omphello
  implicit none
  integer omp_get_thread_num,omp_get_num_threads
  !$omp parallel
  print *, 'Hello world from thread',omp_get_thread_num(), &
            'out of',omp_get_num_threads()
  !$omp end parallel
end
</source>
|}
Compiling and running the C code goes as follows:
litai10:~$ gcc -O3 -fopenmp ompHello.c -o ompHello
litai10:~$ export OMP_NUM_THREADS=4
litai10:~$ ./ompHello
Hello world from thread 0 out of 4
Hello world from thread 2 out of 4
Hello world from thread 1 out of 4
Hello world from thread 3 out of 4
Compiling and running the fortran 90 code is as follows:
litai10:~$ gfortran -O3 -fopenmp ompHello.f90 -o fomphello
litai10:~$ export OMP_NUM_THREADS=4
litai10:~$ ./fomphello
Hello world from thread          0 out of          4
Hello world from thread          2 out of          4
Hello world from thread          1 out of          4
Hello world from thread          3 out of          4
cc_staff
30

edits

Navigation menu