File: omp_sections_nowait.c

package info (click to toggle)
llvm-toolchain-13 1%3A13.0.1-11
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,418,840 kB
  • sloc: cpp: 5,290,826; ansic: 996,570; asm: 544,593; python: 188,212; objc: 72,027; lisp: 30,291; f90: 25,395; sh: 24,898; javascript: 9,780; pascal: 9,398; perl: 7,484; ml: 5,432; awk: 3,523; makefile: 2,913; xml: 953; cs: 573; fortran: 539
file content (104 lines) | stat: -rw-r--r-- 2,277 bytes parent folder | download | duplicates (27)
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
// RUN: %libomp-compile-and-run
#include <stdio.h>
#include "omp_testsuite.h"

/*
 * This test will hang if the nowait is not working properly
 *
 * It relies on a thread skipping to the second sections construct to
 * release the threads in the first sections construct
 *
 * Also, since scheduling of sections is implementation defined, it is
 * necessary to have all four sections in the second sections construct
 * release the threads since we can't guarantee which section a single thread
 * will execute.
 */
volatile int release;
volatile int count;

void wait_for_release_then_increment(int rank)
{
  fprintf(stderr, "Thread nr %d enters first section"
    " and waits.\n", rank);
  while (release == 0);
  #pragma omp atomic
  count++;
}

void release_and_increment(int rank)
{
  fprintf(stderr, "Thread nr %d sets release to 1\n", rank);
  release = 1;
  #pragma omp flush(release)
  #pragma omp atomic
  count++;
}

int test_omp_sections_nowait()
{
  release = 0;
  count = 0;

  #pragma omp parallel num_threads(4)
  {
    int rank;
    rank = omp_get_thread_num ();
    #pragma omp sections nowait
    {
      #pragma omp section
      {
        wait_for_release_then_increment(rank);
      }
      #pragma omp section
      {
        wait_for_release_then_increment(rank);
      }
      #pragma omp section
      {
        wait_for_release_then_increment(rank);
      }
      #pragma omp section
      {
        fprintf(stderr, "Thread nr %d enters first sections and goes "
          "immediately to next sections construct to release.\n", rank);
        #pragma omp atomic
        count++;
      }
    }
    /* Begin of second sections environment */
    #pragma omp sections
    {
      #pragma omp section
      {
        release_and_increment(rank);
      }
      #pragma omp section
      {
        release_and_increment(rank);
      }
      #pragma omp section
      {
        release_and_increment(rank);
      }
      #pragma omp section
      {
        release_and_increment(rank);
      }
    }
  }
  // Check to make sure all eight sections were executed
  return (count==8);
}

int main()
{
  int i;
  int num_failed=0;

  for(i = 0; i < REPETITIONS; i++) {
    if(!test_omp_sections_nowait()) {
      num_failed++;
    }
  }
  return num_failed;
}