1 // RUN: %libomp-compile
2 // RUN: env OMP_PROC_BIND=close OMP_PLACES=threads %libomp-run
3 // RUN: env OMP_PROC_BIND=close OMP_PLACES=cores %libomp-run
4 // RUN: env OMP_PROC_BIND=close OMP_PLACES=sockets %libomp-run
5 // RUN: env KMP_AFFINITY=compact %libomp-run
6 // RUN: env KMP_AFFINITY=scatter %libomp-run
15 #define STR(x) XSTR(x)
17 #define streqls(s1, s2) (!strcmp(s1, s2))
19 #define check(condition) \
21 fprintf(stderr, "error: %s: %d: " STR(condition) "\n", __FILE__, \
32 #define BUFFER_SIZE 1024
34 char buf
[BUFFER_SIZE
];
35 #pragma omp threadprivate(buf)
37 static int debug_printf(const char* format
, ...) {
41 va_start(args
, format
);
42 retval
= vprintf(format
, args
);
48 static void display_affinity_environment() {
50 printf("Affinity Environment:\n");
51 printf(" OMP_PROC_BIND=%s\n", getenv("OMP_PROC_BIND"));
52 printf(" OMP_PLACES=%s\n", getenv("OMP_PLACES"));
53 printf(" KMP_AFFINITY=%s\n", getenv("KMP_AFFINITY"));
57 // Reads in a list of integers into ids array (not going past ids_size)
58 // e.g., if affinity = "0-4,6,8-10,14,16,17-20,23"
59 // then ids = [0,1,2,3,4,6,8,9,10,14,16,17,18,19,20,23]
60 void list_to_ids(const char* affinity
, int* ids
, int ids_size
) {
61 int id
, b
, e
, ids_index
;
62 char *aff
, *begin
, *end
, *absolute_end
;
63 aff
= strdup(affinity
);
64 absolute_end
= aff
+ strlen(aff
);
67 while (end
< absolute_end
) {
69 while (*end
!= '\0' && *end
!= ',')
72 if (strchr(begin
, '-') != NULL
) {
74 sscanf(begin
, "%d-%d", &b
, &e
);
77 sscanf(begin
, "%d", &b
);
80 for (id
= b
; id
<= e
; ++id
) {
81 ids
[ids_index
++] = id
;
82 if (ids_index
>= ids_size
) {
92 void check_thread_affinity() {
94 const char *formats
[2] = {"%{thread_affinity}", "%A"};
95 for (i
= 0; i
< sizeof(formats
) / sizeof(formats
[0]); ++i
) {
96 omp_set_affinity_format(formats
[i
]);
100 int place
= omp_get_place_num();
101 int num_procs
= omp_get_place_num_procs(place
);
102 int *ids
= (int *)malloc(sizeof(int) * num_procs
);
103 int *ids2
= (int *)malloc(sizeof(int) * num_procs
);
105 size_t n
= omp_capture_affinity(buf
, 256, NULL
);
107 omp_get_place_proc_ids(place
, ids
);
108 list_to_ids(buf
, ids2
, num_procs
);
110 #pragma omp for schedule(static) ordered
111 for (k
= 0; k
< omp_get_num_threads(); ++k
) {
114 debug_printf("Thread %d: captured affinity = %s\n",
115 omp_get_thread_num(), buf
);
116 for (j
= 0; j
< num_procs
; ++j
) {
117 debug_printf("Thread %d: ids[%d] = %d ids2[%d] = %d\n",
118 omp_get_thread_num(), j
, ids
[j
], j
, ids2
[j
]);
119 check(ids
[j
] == ids2
[j
]);
130 int main(int argc
, char** argv
) {
132 display_affinity_environment();
133 check_thread_affinity();