xref: /llvm-project/openmp/runtime/test/teams/teams-atomic.c (revision 7e89420116c91647db340a4457b8ad0d60be1d5e)
1 // Check that omp atomic is permitted and behaves when strictly nested within
2 // omp teams.  This is an extension to OpenMP 5.2 and is enabled by default.
3 
4 // RUN: %libomp-compile-and-run | FileCheck %s
5 // GCC has really limited OpenMP 5.2 support yet.
6 // UNSUPPORTED: gcc
7 
8 #include <omp.h>
9 #include <stdbool.h>
10 #include <stdio.h>
11 #include <string.h>
12 
13 // High parallelism increases our chances of detecting a lack of atomicity.
14 #define NUM_TEAMS_TRY 256
15 
main()16 int main() {
17   //      CHECK: update: num_teams=[[#NUM_TEAMS:]]{{$}}
18   // CHECK-NEXT: update: x=[[#NUM_TEAMS]]{{$}}
19   int x = 0;
20   int numTeams;
21   #pragma omp teams num_teams(NUM_TEAMS_TRY)
22   {
23     #pragma omp atomic update
24     ++x;
25     if (omp_get_team_num() == 0)
26       numTeams = omp_get_num_teams();
27   }
28   printf("update: num_teams=%d\n", numTeams);
29   printf("update: x=%d\n", x);
30 
31   // CHECK-NEXT: capture: x=[[#NUM_TEAMS]]{{$}}
32   // CHECK-NEXT: capture: xCapturedCount=[[#NUM_TEAMS]]{{$}}
33   bool xCaptured[numTeams];
34   memset(xCaptured, 0, sizeof xCaptured);
35   x = 0;
36   #pragma omp teams num_teams(NUM_TEAMS_TRY)
37   {
38     int v;
39     #pragma omp atomic capture
40     v = x++;
41     xCaptured[v] = true;
42   }
43   printf("capture: x=%d\n", x);
44   int xCapturedCount = 0;
45   for (int i = 0; i < numTeams; ++i) {
46     if (xCaptured[i])
47       ++xCapturedCount;
48   }
49   printf("capture: xCapturedCount=%d\n", xCapturedCount);
50   return 0;
51 }
52