File: transformations.f90

package info (click to toggle)
opari 1.1%2Bdfsg-7
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 728 kB
  • sloc: cpp: 2,005; ansic: 901; f90: 252; makefile: 128; sh: 86; fortran: 50
file content (166 lines) | stat: -rw-r--r-- 4,209 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
      module xxx
      contains
        subroutine writelog(msg, val)
          character(len=*), intent(in)  :: msg
          integer, optional, intent(in) :: val
          integer, external             :: omp_get_thread_num

          if ( present(val) ) then
            write(*,*) "---", omp_get_thread_num(), ": ", msg, val
          else
            write(*,*) "---", omp_get_thread_num(), ": ", msg
          endif
          return
        end subroutine writelog
      end module xxx

      program trans
      use xxx
      integer, parameter :: iterations = 4
      integer            :: i, k = 0
      integer            :: lck
      logical, external  :: omp_test_lock

      !$omp inst init

      call writelog("sequential", 0);

      ! ---- plain parallel region
      !$omp parallel
        call writelog("parallel", 0);
      !$omp end parallel
 
      call writelog("sequential", 1)

      ! ---- parallel region
      !$omp parallel
        call writelog("parallel", 1)

        !$omp inst begin(worksharing)

        ! ---- worksharing for loop without synchronisation
        !$omp do
        do i=1,iterations
          call writelog("for nowait iteration", i)
        enddo
        !$omp end do nowait

        ! ---- user specified barrier
        !$omp barrier

        ! ---- worksharing for loop with implicit synchronisation
        !$omp do
        do i=1,iterations
          call writelog("for iteration", i)
        enddo
        !$omp end do
 
        ! ---- worksharing tasks without synchronisation
        !$omp sections
          !$omp section
            call writelog("section nowait", 1)
          !$omp section
            call writelog("section nowait", 2)
        !$omp end sections nowait

        ! ---- worksharing tasks with implicit synchronisation
        !$omp sections
          !$omp section
            call writelog("section", 1)
          !$omp section
            call writelog("section", 2)
        !$omp end sections

        !$omp inst end(worksharing)

        !$omp inst begin(synchronisation)

        ! ---- critical section
        !$omp critical
          call writelog("critical\n")
          k = k+1
        !$omp end critical

        ! ---- named critical section
        !$omp critical(kincr)
          call writelog("critical\n")
          k = k+1
        !$omp end critical(kincr)

        ! ---- atomic expression
        !$omp atomic
          k = k+1

        ! ---- update k just once without synchronisation
        !$omp single
          call writelog("single nowait\n")
          k = k+1
        !$omp end single nowait

        ! ---- update k just once with implicit synchronisation
        !$omp single
          call writelog("single\n")
          k = k+1
        !$omp end single

        !$omp master
          call writelog("master\n")
          write(*,*) "k = ", k
          k = 0
        !$omp end master

        !$omp inst end(synchronisation)
      !$omp end parallel

      call writelog("sequential", 2)

      !$omp inst begin(parallelworksharing)

      ! ---- combined parallel worksharing for loop
      !$omp parallel do         &
      !$omp   reduction(+:k)    &
      !$omp   private(i)        &
      !$omp   schedule(dynamic)
      do i=1,iterations
        call writelog("pfor", i)
      enddo
      !$omp end parallel do

      call writelog("sequential", 3)

      ! ---- combined parallel worksharing tasks
      !$omp parallel sections
        !$omp section
          call writelog("psection", 1)
        !$omp section
          call writelog("psection", 2)
      !$omp end parallel sections

      call writelog("sequential", 4)

      !$omp inst end(parallelworksharing)

      ! ---- OpenMP locking API
      !$omp inst begin(locking)


      call omp_init_lock(lck)

      !$omp parallel shared(lck)
        call omp_set_lock(lck)
        call writelog("got lock")
        call omp_unset_lock(lck)

        do while (.not. omp_test_lock(lck))
          call writelog("skipping")
        enddo
        call writelog("working")
        call omp_unset_lock(lck)
      !$omp end parallel

      call omp_destroy_lock(lck)

      !$omp flush(k)
      !$omp inst end(locking)

      end program trans