File: transformations.c

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 (170 lines) | stat: -rw-r--r-- 3,356 bytes parent folder | download | duplicates (5)
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
167
168
169
170
#include <stdio.h>

#include "opari_omp.h"

static void log(const char* msg, int val) {
  printf("--- %3d: %s", omp_get_thread_num(), msg);
  if ( val != -1 ) printf(" %d", val);
  printf("\n");
  fflush(stdout);
}

const int iterations = 4;

int main() {
  int i, k = 0;
  omp_lock_t lck;

  #pragma omp inst init

  log("sequential", 0);

  /* ---- plain parallel region ---- */
  #pragma omp parallel
  {
    log("parallel", 0);
  }

  log("sequential", 1);

  /* ---- large parallel region ---- */
  #pragma omp parallel
  {
    log("parallel", 1);

    #pragma omp inst begin(worksharing)

    /* ---- worksharing for loop without synchronisation ---- */
    #pragma omp for nowait
    for(i=0; i<iterations; ++i) {
      log("for nowait iteration", i);
    }

    /* ---- user specified barrier ---- */
    #pragma omp barrier

    /* ---- worksharing for loop with implicit synchronisation ---- */
    #pragma omp for
    for(i=0; i<iterations; ++i)
      log("for iteration", i);
 
    /* ---- worksharing tasks without synchronisation ---- */
    #pragma omp sections nowait
    {
      #pragma omp section
        log("section nowait", 1);
      #pragma omp section
      {
        log("section nowait", 2);
      }
    }

    /* ---- worksharing tasks with implicit synchronisation ---- */
    #pragma omp sections
    {
      #pragma omp section
      {
        log("section", 1);
      }
      #pragma omp section
        log("section", 2);
    }

    #pragma omp inst end(worksharing)

    #pragma omp inst begin(synchronisation)

    /* ---- critical section ---- */
    #pragma omp critical
    {
      log("critical\n", -1);
      k += 1;
    }

    /* ---- named critical section ---- */
    #pragma omp critical(kincr)
    {
      log("critical\n", -1);
      k += 1;
    }

    /* ---- atomic expression ---- */
    #pragma omp atomic
      k += 1;

    /* ---- update k just once without synchronisation ---- */
    #pragma omp single nowait
    {
      log("single nowait\n", -1);
      k += 1;
    }

    /* ---- update k just once with implicit synchronisation ---- */
    #pragma omp single
    {
      log("single\n", -1);
      k += 1;
    }

    #pragma omp master
    {
      log("master\n", -1);
      printf("k = %d\n", k);
      k = 0;
    }

    #pragma omp inst end(synchronisation)
  } /* end parallel ---- */

  log("sequential", 2);

  #pragma omp inst begin(parallelworksharing)

  /* ---- combined parallel worksharing for loop ---- */
  #pragma omp parallel for \
          reduction(+:k)  \
          private(i)       \
          schedule(dynamic)
  for(i=0; i<iterations; ++i) {
    log("pfor", i);
  }

  log("sequential", 3);

  /* ---- combined parallel worksharing tasks ---- */
  #pragma omp parallel sections
  {
    #pragma omp section
      log("psection", 1);
    #pragma omp section
      log("psection", 2);
  }

  log("sequential", 4);

  #pragma omp inst end(parallelworksharing)

  /* ---- OpenMP locking API ---- */
  #pragma omp inst begin(locking)

  omp_init_lock(&lck);

  #pragma omp parallel shared(lck)
  {
    omp_set_lock(&lck);
    log("got lock", -1);
    omp_unset_lock(&lck);

    while (! omp_test_lock(&lck)) {
      log("skipping", -1);
    }
    log("working", -1);
    omp_unset_lock(&lck);
  }

  omp_destroy_lock(&lck);

  #pragma omp flush(k)

  #pragma omp inst end(locking)
}