以下例子来自https://computing.llnl.gov/tutorials/openMP/exercise.html网站
一、打印线程(Hello world)
C******************************************************************************
C FILE: omp_hello.f
C DESCRIPTION:
C OpenMP Example - Hello World - Fortran Version
C In this simple example, the master thread forks a parallel region.
C All threads in the team obtain their unique thread number and print it.
C The master thread only prints the total number of threads. Two OpenMP
C library routines are used to obtain the number of threads and each
C thread's number.
C AUTHOR: Blaise Barney 5/99
C LAST REVISED:
C******************************************************************************
PROGRAM HELLO
INTEGER NTHREADS, TID, OMP_GET_NUM_THREADS, OMP_GET_THREAD_NUM
C Fork a team of threads giving them their own copies of variables
!$OMP PARALLEL PRIVATE(NTHREADS, TID)
C Obtain thread number
TID = OMP_GET_THREAD_NUM()
PRINT *, 'Hello World from thread = ', TID
C Only master thread does this
IF (TID .EQ. 0) THEN
NTHREADS = OMP_GET_NUM_THREADS()
PRINT *, 'Number of threads = ', NTHREADS
END IF
C All threads join master thread and disband
!$OMP END PARALLEL
END
二、循环(Loop work-sharing)
1 C****************************************************************************** 2 C FILE: omp_workshare1.f 3 C DESCRIPTION: 4 C OpenMP Example - Loop Work-sharing - Fortran Version 5 C In this example, the iterations of a loop are scheduled dynamically 6 C across the team of threads. A thread will perform CHUNK iterations 7 C at a time before being scheduled for the next CHUNK of work. 8 C AUTHOR: Blaise Barney 5/99 9 C LAST REVISED: 01/09/04 10 C****************************************************************************** 11 12 PROGRAM WORKSHARE1 13 14 INTEGER NTHREADS, TID, OMP_GET_NUM_THREADS, 15 + OMP_GET_THREAD_NUM, N, CHUNKSIZE, CHUNK, I 16 PARAMETER (N=100) 17 PARAMETER (CHUNKSIZE=10) 18 REAL A(N), B(N), C(N) 19 20 ! Some initializations 21 DO I = 1, N 22 A(I) = I * 1.0 23 B(I) = A(I) 24 ENDDO 25 CHUNK = CHUNKSIZE 26 27 !$OMP PARALLEL SHARED(A,B,C,NTHREADS,CHUNK) PRIVATE(I,TID) 28 29 TID = OMP_GET_THREAD_NUM() 30 IF (TID .EQ. 0) THEN 31 NTHREADS = OMP_GET_NUM_THREADS() 32 PRINT *, 'Number of threads =', NTHREADS 33 END IF 34 PRINT *, 'Thread',TID,' starting...' 35 !$OMP DO SCHEDULE(DYNAMIC,CHUNK) 36 DO I = 1, N 37 C(I) = A(I) + B(I) 38 WRITE(*,100) TID,I,C(I) 39 100 FORMAT(' Thread',I2,': C(',I3,')=',F8.2) 40 ENDDO 41 !$OMP END DO NOWAIT 42 PRINT *, 'Thread',TID,' done.' 43 !$OMP END PARALLEL 44 45 END
三、Sections work-sharing
C****************************************************************************** C FILE: omp_workshare2.f C DESCRIPTION: C OpenMP Example - Sections Work-sharing - Fortran Version C In this example, the OpenMP SECTION directive is used to assign C different array operations to each thread that executes a SECTION. C AUTHOR: Blaise Barney 5/99 C LAST REVISED: 07/16/07 C****************************************************************************** PROGRAM WORKSHARE2 INTEGER N, I, NTHREADS, TID, OMP_GET_NUM_THREADS, + OMP_GET_THREAD_NUM PARAMETER (N=50) REAL A(N), B(N), C(N), D(N) ! Some initializations DO I = 1, N A(I) = I * 1.5 B(I) = I + 22.35 C(N) = 0.0 D(N) = 0.0 ENDDO !$OMP PARALLEL SHARED(A,B,C,D,NTHREADS), PRIVATE(I,TID) TID = OMP_GET_THREAD_NUM() IF (TID .EQ. 0) THEN NTHREADS = OMP_GET_NUM_THREADS() PRINT *, 'Number of threads =', NTHREADS END IF PRINT *, 'Thread',TID,' starting...' !$OMP SECTIONS !$OMP SECTION PRINT *, 'Thread',TID,' doing section 1' DO I = 1, N C(I) = A(I) + B(I) WRITE(*,100) TID,I,C(I) 100 FORMAT(' Thread',I2,': C(',I2,')=',F8.2) ENDDO !$OMP SECTION PRINT *, 'Thread',TID,' doing section 2' DO I = 1, N D(I) = A(I) * B(I) WRITE(*,100) TID,I,D(I) ENDDO !$OMP END SECTIONS NOWAIT PRINT *, 'Thread',TID,' done.' !$OMP END PARALLEL END
四、Combined parallel loop reduction
C****************************************************************************** C FILE: omp_reduction.f C DESCRIPTION: C OpenMP Example - Combined Parallel Loop Reduction - Fortran