xref: /llvm-project/openmp/runtime/test/affinity/kmp-affinity.c (revision b49d3e50e353e7ab58c2e6622f8cf78e8db7f51e)
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.
compare_places(const place_list_t * p1,const place_list_t * p2)14 static 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(p1->masks[i], p2->masks[j])) {
20         found = 1;
21         break;
22       }
23     }
24     if (!found) {
25       printf("Found place in p1 not in p2!\n");
26       printf("p1 places:\n");
27       topology_print_places(p1);
28       printf("\n");
29       printf("p2 places:\n");
30       topology_print_places(p2);
31       return EXIT_FAILURE;
32     }
33   }
34   return EXIT_SUCCESS;
35 }
36 
check_places()37 static int check_places() {
38   int status;
39   const char *value = getenv("KMP_AFFINITY");
40   if (!value) {
41     fprintf(stderr, "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(value, "socket")) {
46     places = topology_alloc_type_places(TOPOLOGY_OBJ_SOCKET);
47   } else if (strstr(value, "core")) {
48     places = topology_alloc_type_places(TOPOLOGY_OBJ_CORE);
49   } else if (strstr(value, "thread")) {
50     places = topology_alloc_type_places(TOPOLOGY_OBJ_THREAD);
51   } else {
52     fprintf(
53         stderr,
54         "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(openmp_places, places);
59   topology_free_places(places);
60   topology_free_places(openmp_places);
61   return status;
62 }
63 
main()64 int main() {
65   if (!topology_using_full_mask()) {
66     printf("Thread does not have access to all logical processors. Skipping "
67            "test.\n");
68     return EXIT_SUCCESS;
69   }
70   return check_places();
71 }
72