omp_workshare2.f 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. C******************************************************************************
  2. C FILE: omp_workshare2.f
  3. C DESCRIPTION:
  4. C OpenMP Example - Sections Work-sharing - Fortran Version
  5. C In this example, the OpenMP SECTION directive is used to assign
  6. C different array operations to threads that execute a SECTION. Each
  7. C thread receives its own copy of the result array to work with.
  8. C AUTHOR: Blaise Barney 5/99
  9. C LAST REVISED: 01/09/04
  10. C******************************************************************************
  11. PROGRAM WORKSHARE2
  12. INTEGER N, I, NTHREADS, TID, OMP_GET_NUM_THREADS,
  13. + OMP_GET_THREAD_NUM
  14. PARAMETER (N=50)
  15. REAL A(N), B(N), C(N)
  16. ! Some initializations
  17. DO I = 1, N
  18. A(I) = I * 1.0
  19. B(I) = A(I)
  20. ENDDO
  21. !$OMP PARALLEL SHARED(A,B,NTHREADS), PRIVATE(C,I,TID)
  22. TID = OMP_GET_THREAD_NUM()
  23. IF (TID .EQ. 0) THEN
  24. NTHREADS = OMP_GET_NUM_THREADS()
  25. PRINT *, 'Number of threads =', NTHREADS
  26. END IF
  27. PRINT *, 'Thread',TID,' starting...'
  28. !$OMP SECTIONS
  29. !$OMP SECTION
  30. PRINT *, 'Thread',TID,' doing section 1'
  31. DO I = 1, N
  32. C(I) = A(I) + B(I)
  33. WRITE(*,100) TID,I,C(I)
  34. 100 FORMAT(' Thread',I2,': C(',I2,')=',F8.2)
  35. ENDDO
  36. !$OMP SECTION
  37. PRINT *, 'Thread',TID,' doing section 2'
  38. DO I = 1+N/2, N
  39. C(I) = A(I) * B(I)
  40. WRITE(*,100) TID,I,C(I)
  41. ENDDO
  42. !$OMP END SECTIONS NOWAIT
  43. PRINT *, 'Thread',TID,' done.'
  44. !$OMP END PARALLEL
  45. END