! http://www.llnl.gov/computing/tutorials/openMP/exercise.html !****************************************************************************** ! FILE: omp_hello.f ! DESCRIPTION: ! OpenMP Example - Hello World - Fortran Version ! In this simple example, the master thread forks a parallel region. ! All threads in the team obtain their unique thread number and print it. ! The master thread only prints the total number of threads. Two OpenMP ! library routines are used to obtain the number of threads and each ! thread's number. ! AUTHOR: Blaise Barney 5/99 ! LAST REVISED: !****************************************************************************** PROGRAM HELLO INTEGER NTHREADS, TID, OMP_GET_NUM_THREADS, & OMP_GET_THREAD_NUM ! Fork a team of threads giving them their own copies of variables !$OMP PARALLEL PRIVATE(NTHREADS, TID) ! Obtain thread number TID = OMP_GET_THREAD_NUM() PRINT *, 'Hello World from thread = ', TID ! Only master thread does this IF (TID .EQ. 0) THEN NTHREADS = OMP_GET_NUM_THREADS() PRINT *, 'Number of threads = ', NTHREADS END IF ! All threads join master thread and disband !$OMP END PARALLEL END !***************** ! ! f90 omp_hello.f90 <-- Compiling this OpenMP file ! ./a.out <-- Running this OpenMP executable ! Hello World from thread = 0 ! Hello World from thread = 1 ! Number of threads = 4 ! Hello World from thread = 2 ! Hello World from thread = 3 ! OMP_NUM_THREADS=8 <-- Changing the number of threads ! ./a.out <-- Running the executable again ! Hello World from thread = 0 ! Hello World from thread = 1 ! Number of threads = 8 ! Hello World from thread = 2 ! Hello World from thread = 3 ! Hello World from thread = 4 ! Hello World from thread = 5 ! Hello World from thread = 6 ! Hello World from thread = 7