1// RUN: %libomp-compile -D_GNU_SOURCE
2// RUN: env OMP_NUM_THREADS=2,2 KMP_AFFINITY=reset,granularity=thread,compact %libomp-run
3// REQUIRES: linux
4
5#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
8#include <omp.h>
9#include "libomp_test_affinity.h"
10
11#define CHECK_EQUAL 0
12#define CHECK_NOT_EQUAL 1
13
14void check_primary_thread_affinity(int line, affinity_mask_t *other_aff,
15 int type) {
16 #pragma omp master
17 {
18 affinity_mask_t *primary_aff = affinity_mask_alloc();
19 get_thread_affinity(mask: primary_aff);
20 if (type == CHECK_EQUAL && !affinity_mask_equal(mask1: primary_aff, mask2: other_aff)) {
21 fprintf(stderr, format: "error: line %d: primary affinity was not equal\n", line);
22 exit(EXIT_FAILURE);
23 } else if (type == CHECK_NOT_EQUAL &&
24 affinity_mask_equal(mask1: primary_aff, mask2: other_aff)) {
25 fprintf(stderr, format: "error: line %d: primary affinity was equal\n", line);
26 exit(EXIT_FAILURE);
27 }
28 affinity_mask_free(mask: primary_aff);
29 }
30}
31
32#define CHECK_PRIMARY_THREAD_AFFINITY_EQUAL(other_aff) \
33 check_primary_thread_affinity(__LINE__, other_aff, CHECK_EQUAL)
34#define CHECK_PRIMARY_THREAD_AFFINITY_NOT_EQUAL(other_aff) \
35 check_primary_thread_affinity(__LINE__, other_aff, CHECK_NOT_EQUAL)
36
37int main() {
38 int i;
39 affinity_mask_t *initial_mask = affinity_mask_alloc();
40 get_thread_affinity(mask: initial_mask);
41
42 for (i = 0; i < 10; ++i) {
43 #pragma omp parallel
44 {
45 CHECK_PRIMARY_THREAD_AFFINITY_NOT_EQUAL(initial_mask);
46 }
47 CHECK_PRIMARY_THREAD_AFFINITY_EQUAL(initial_mask);
48 }
49
50 omp_set_max_active_levels(2);
51 for (i = 0; i < 10; ++i) {
52 #pragma omp parallel
53 {
54 CHECK_PRIMARY_THREAD_AFFINITY_NOT_EQUAL(initial_mask);
55
56 #pragma omp parallel
57 CHECK_PRIMARY_THREAD_AFFINITY_NOT_EQUAL(initial_mask);
58
59 CHECK_PRIMARY_THREAD_AFFINITY_NOT_EQUAL(initial_mask);
60 }
61 CHECK_PRIMARY_THREAD_AFFINITY_EQUAL(initial_mask);
62 }
63
64 affinity_mask_free(mask: initial_mask);
65 return EXIT_SUCCESS;
66}
67

source code of openmp/runtime/test/affinity/kmp-affinity-reset.c