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.
16static 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, format: "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(format: "p1 places:\n");
23 topology_print_places(p: p1);
24 printf(format: "\n");
25 printf(format: "p2 places:\n");
26 topology_print_places(p: 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(mask1: p1->masks[i], mask2: p2->masks[j])) {
33 found = 1;
34 break;
35 }
36 }
37 if (!found) {
38 printf(format: "Found difference in places!\n");
39 printf(format: "p1 places:\n");
40 topology_print_places(p: p1);
41 printf(format: "\n");
42 printf(format: "p2 places:\n");
43 topology_print_places(p: p2);
44 return EXIT_FAILURE;
45 }
46 }
47 return EXIT_SUCCESS;
48}
49
50static int check_places() {
51 int status;
52 const char *value = getenv(name: "OMP_PLACES");
53 if (!value) {
54 fprintf(stderr, format: "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(s1: value, s2: "sockets") == 0) {
59 places = topology_alloc_type_places(type: TOPOLOGY_OBJ_SOCKET);
60 } else if (strcmp(s1: value, s2: "cores") == 0) {
61 places = topology_alloc_type_places(type: TOPOLOGY_OBJ_CORE);
62 } else if (strcmp(s1: value, s2: "threads") == 0) {
63 places = topology_alloc_type_places(type: TOPOLOGY_OBJ_THREAD);
64 } else {
65 fprintf(stderr,
66 format: "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(p1: places, p2: openmp_places);
71 topology_free_places(places);
72 topology_free_places(places: openmp_places);
73 return status;
74}
75
76int main() {
77 if (!topology_using_full_mask()) {
78 printf(format: "Thread does not have access to all logical processors. Skipping "
79 "test.\n");
80 return EXIT_SUCCESS;
81 }
82 return check_places();
83}
84

source code of openmp/runtime/test/affinity/omp-places.c