xref: /llvm-project/openmp/runtime/test/affinity/omp-places.c (revision b49d3e50e353e7ab58c2e6622f8cf78e8db7f51e)
1 // RUN: %libomp-compile -D_GNU_SOURCE
2 // RUN: env OMP_PLACES=threads %libomp-run
3 // RUN: env OMP_PLACES=cores %libomp-run
4 // RUN: env OMP_PLACES=sockets %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. The order is not taken into consideration here.
14 // The OS detection might have the cores/sockets in a different
15 // order from the runtime.
compare_places(const place_list_t * p1,const place_list_t * p2)16 static int compare_places(const place_list_t *p1, const place_list_t *p2) {
17   int i, j;
18   if (p1->num_places != p2->num_places) {
19     fprintf(stderr, "error: places do not have same number of places! (p1 has "
20                     "%d, p2 has %d)\n",
21             p1->num_places, p2->num_places);
22     printf("p1 places:\n");
23     topology_print_places(p1);
24     printf("\n");
25     printf("p2 places:\n");
26     topology_print_places(p2);
27     return EXIT_FAILURE;
28   }
29   for (i = 0; i < p1->num_places; ++i) {
30     int found = 0;
31     for (j = 0; j < p2->num_places; ++j) {
32       if (affinity_mask_equal(p1->masks[i], p2->masks[j])) {
33         found = 1;
34         break;
35       }
36     }
37     if (!found) {
38       printf("Found difference in places!\n");
39       printf("p1 places:\n");
40       topology_print_places(p1);
41       printf("\n");
42       printf("p2 places:\n");
43       topology_print_places(p2);
44       return EXIT_FAILURE;
45     }
46   }
47   return EXIT_SUCCESS;
48 }
49 
check_places()50 static int check_places() {
51   int status;
52   const char *value = getenv("OMP_PLACES");
53   if (!value) {
54     fprintf(stderr, "error: must set OMP_PLACES envirable for this test!\n");
55     return EXIT_FAILURE;
56   }
57   place_list_t *places, *openmp_places;
58   if (strcmp(value, "sockets") == 0) {
59     places = topology_alloc_type_places(TOPOLOGY_OBJ_SOCKET);
60   } else if (strcmp(value, "cores") == 0) {
61     places = topology_alloc_type_places(TOPOLOGY_OBJ_CORE);
62   } else if (strcmp(value, "threads") == 0) {
63     places = topology_alloc_type_places(TOPOLOGY_OBJ_THREAD);
64   } else {
65     fprintf(stderr,
66             "error: OMP_PLACES must be one of threads,cores,sockets!\n");
67     return EXIT_FAILURE;
68   }
69   openmp_places = topology_alloc_openmp_places();
70   status = compare_places(places, openmp_places);
71   topology_free_places(places);
72   topology_free_places(openmp_places);
73   return status;
74 }
75 
main()76 int main() {
77   if (!topology_using_full_mask()) {
78     printf("Thread does not have access to all logical processors. Skipping "
79            "test.\n");
80     return EXIT_SUCCESS;
81   }
82   return check_places();
83 }
84