1// RUN: %libomp-compile -D_GNU_SOURCE
2// RUN: env KMP_AFFINITY=granularity=thread,compact %libomp-run
3// RUN: env KMP_AFFINITY=granularity=core,compact %libomp-run
4// RUN: env KMP_AFFINITY=granularity=socket,compact %libomp-run
5// REQUIRES: linux
6
7#include <stdio.h>
8#include <stdlib.h>
9#include <string.h>
10#include "libomp_test_affinity.h"
11#include "libomp_test_topology.h"
12
13// Compare place lists. Make sure every place in p1 is in p2.
14static int compare_places(const place_list_t *p1, const place_list_t *p2) {
15 int i, j;
16 for (i = 0; i < p1->num_places; ++i) {
17 int found = 0;
18 for (j = 0; j < p2->num_places; ++j) {
19 if (affinity_mask_equal(mask1: p1->masks[i], mask2: p2->masks[j])) {
20 found = 1;
21 break;
22 }
23 }
24 if (!found) {
25 printf(format: "Found place in p1 not in p2!\n");
26 printf(format: "p1 places:\n");
27 topology_print_places(p: p1);
28 printf(format: "\n");
29 printf(format: "p2 places:\n");
30 topology_print_places(p: p2);
31 return EXIT_FAILURE;
32 }
33 }
34 return EXIT_SUCCESS;
35}
36
37static int check_places() {
38 int status;
39 const char *value = getenv(name: "KMP_AFFINITY");
40 if (!value) {
41 fprintf(stderr, format: "error: must set OMP_PLACES envirable for this test!\n");
42 return EXIT_FAILURE;
43 }
44 place_list_t *places, *openmp_places;
45 if (strstr(haystack: value, needle: "socket")) {
46 places = topology_alloc_type_places(type: TOPOLOGY_OBJ_SOCKET);
47 } else if (strstr(haystack: value, needle: "core")) {
48 places = topology_alloc_type_places(type: TOPOLOGY_OBJ_CORE);
49 } else if (strstr(haystack: value, needle: "thread")) {
50 places = topology_alloc_type_places(type: TOPOLOGY_OBJ_THREAD);
51 } else {
52 fprintf(
53 stderr,
54 format: "error: KMP_AFFINITY granularity must be one of thread,core,socket!\n");
55 return EXIT_FAILURE;
56 }
57 openmp_places = topology_alloc_openmp_places();
58 status = compare_places(p1: openmp_places, p2: places);
59 topology_free_places(places);
60 topology_free_places(places: openmp_places);
61 return status;
62}
63
64int main() {
65 if (!topology_using_full_mask()) {
66 printf(format: "Thread does not have access to all logical processors. Skipping "
67 "test.\n");
68 return EXIT_SUCCESS;
69 }
70 return check_places();
71}
72

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