aboutsummaryrefslogtreecommitdiff
path: root/final/runtime/test/worksharing/for/omp_for_nowait.c
blob: 95a477538f2ad3e14776d9bcbcec37359307412c (plain)
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
// 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 for construct to
 * release the threads in the first for construct.
 *
 * Also, we use static scheduling to guarantee that one
 * thread will make it to the second for construct.
 */
volatile int release;
volatile int count;

void wait_for_release_then_increment(int rank)
{
  fprintf(stderr, "Thread nr %d enters first for construct"
    " 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 atomic
  count++;
}

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

  #pragma omp parallel num_threads(4)
  {
    int rank;
    int i;

    rank = omp_get_thread_num();

    #pragma omp for schedule(static) nowait
    for (i = 0; i < 4; i++) {
      if (i < 3)
        wait_for_release_then_increment(rank);
      else {
        fprintf(stderr, "Thread nr %d enters first for and goes "
          "immediately to the next for construct to release.\n", rank);
        #pragma omp atomic
        count++;
      }
    }

    #pragma omp for schedule(static)
    for (i = 0; i < 4; i++) {
        release_and_increment(rank);
    }
  }
  return (count==8);
}

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

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