|
| 1 | +// RUN: %compile-run-and-check |
| 2 | + |
| 3 | +#include <omp.h> |
| 4 | +#include <stdio.h> |
| 5 | + |
| 6 | +const int MaxThreads = 1024; |
| 7 | +const int NumThreads = 64; |
| 8 | + |
| 9 | +int main(int argc, char *argv[]) { |
| 10 | + int inParallel = -1, numThreads = -1, threadNum = -1; |
| 11 | + int check1[MaxThreads]; |
| 12 | + int check2[MaxThreads]; |
| 13 | + for (int i = 0; i < MaxThreads; i++) { |
| 14 | + check1[i] = check2[i] = 0; |
| 15 | + } |
| 16 | + |
| 17 | + #pragma omp target map(inParallel, numThreads, threadNum, check1[:], check2[:]) |
| 18 | + { |
| 19 | + inParallel = omp_in_parallel(); |
| 20 | + numThreads = omp_get_num_threads(); |
| 21 | + threadNum = omp_get_thread_num(); |
| 22 | + |
| 23 | + // Expecting active parallel region. |
| 24 | + #pragma omp parallel num_threads(NumThreads) |
| 25 | + { |
| 26 | + int id = omp_get_thread_num(); |
| 27 | + check1[id] += omp_get_num_threads() + omp_in_parallel(); |
| 28 | + |
| 29 | + // Expecting serialized parallel region. |
| 30 | + #pragma omp parallel |
| 31 | + { |
| 32 | + // Expected to be 1. |
| 33 | + int nestedInParallel = omp_in_parallel(); |
| 34 | + // Expected to be 1. |
| 35 | + int nestedNumThreads = omp_get_num_threads(); |
| 36 | + // Expected to be 0. |
| 37 | + int nestedThreadNum = omp_get_thread_num(); |
| 38 | + #pragma omp atomic |
| 39 | + check2[id] += nestedInParallel + nestedNumThreads + nestedThreadNum; |
| 40 | + } |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + // CHECK: target: inParallel = 0, numThreads = 1, threadNum = 0 |
| 45 | + printf("target: inParallel = %d, numThreads = %d, threadNum = %d\n", |
| 46 | + inParallel, numThreads, threadNum); |
| 47 | + |
| 48 | + // CHECK-NOT: invalid |
| 49 | + for (int i = 0; i < MaxThreads; i++) { |
| 50 | + // Check that all threads reported |
| 51 | + // omp_get_num_threads() = 64, omp_in_parallel() = 1. |
| 52 | + int Expected = NumThreads + 1; |
| 53 | + if (i < NumThreads) { |
| 54 | + if (check1[i] != Expected) { |
| 55 | + printf("invalid: check1[%d] should be %d, is %d\n", i, Expected, check1[i]); |
| 56 | + } |
| 57 | + } else if (check1[i] != 0) { |
| 58 | + printf("invalid: check1[%d] should be 0, is %d\n", i, check1[i]); |
| 59 | + } |
| 60 | + |
| 61 | + // Check serialized parallel region. |
| 62 | + if (i < NumThreads) { |
| 63 | + if (check2[i] != 2) { |
| 64 | + printf("invalid: check2[%d] should be 2, is %d\n", i, check2[i]); |
| 65 | + } |
| 66 | + } else if (check2[i] != 0) { |
| 67 | + printf("invalid: check2[%d] should be 0, is %d\n", i, check2[i]); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + return 0; |
| 72 | +} |
0 commit comments