LLVM OpenMP 19.0.0git
affinity_values.c
Go to the documentation of this file.
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
7// REQUIRES: affinity
8
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12#include <omp.h>
13
14#define XSTR(x) #x
15#define STR(x) XSTR(x)
16
17#define streqls(s1, s2) (!strcmp(s1, s2))
18
19#define check(condition) \
20 if (!(condition)) { \
21 fprintf(stderr, "error: %s: %d: " STR(condition) "\n", __FILE__, \
22 __LINE__); \
23 exit(1); \
24 }
25
26#define DEBUG 0
27
28#if DEBUG
29#include <stdarg.h>
30#endif
31
32#define BUFFER_SIZE 1024
33
35#pragma omp threadprivate(buf)
36
37static int debug_printf(const char* format, ...) {
38 int retval = 0;
39#if DEBUG
40 va_list args;
41 va_start(args, format);
42 retval = vprintf(format, args);
43 va_end(args);
44#endif
45 return retval;
46}
47
49#if DEBUG
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"));
54#endif
55}
56
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]
60void 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);
65 ids_index = 0;
66 begin = end = aff;
67 while (end < absolute_end) {
68 end = begin;
69 while (*end != '\0' && *end != ',')
70 end++;
71 *end = '\0';
72 if (strchr(begin, '-') != NULL) {
73 // Range
74 sscanf(begin, "%d-%d", &b, &e);
75 } else {
76 // Single Number
77 sscanf(begin, "%d", &b);
78 e = b;
79 }
80 for (id = b; id <= e; ++id) {
81 ids[ids_index++] = id;
82 if (ids_index >= ids_size) {
83 free(aff);
84 return;
85 }
86 }
87 begin = end + 1;
88 }
89 free(aff);
90}
91
93 int i;
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]);
97 #pragma omp parallel
98 {
99 int j, k;
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);
104 char buf[256];
105 size_t n = omp_capture_affinity(buf, 256, NULL);
106 check(n <= 256);
107 omp_get_place_proc_ids(place, ids);
108 list_to_ids(buf, ids2, num_procs);
109
110 #pragma omp for schedule(static) ordered
111 for (k = 0; k < omp_get_num_threads(); ++k) {
112 #pragma omp ordered
113 {
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]);
120 }
121 }
122 }
123
124 free(ids);
125 free(ids2);
126 }
127 }
128}
129
130int main(int argc, char** argv) {
134 return 0;
135}
char buf[BUFFER_SIZE]
#define BUFFER_SIZE
void list_to_ids(const char *affinity, int *ids, int ids_size)
void check_thread_affinity()
static void display_affinity_environment()
#define check(condition)
static int debug_printf(const char *format,...)
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void ITT_FORMAT p void ITT_FORMAT p void size_t ITT_FORMAT d void ITT_FORMAT p const wchar_t ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s no args void ITT_FORMAT p size_t ITT_FORMAT d no args const wchar_t const wchar_t ITT_FORMAT s __itt_heap_function void size_t int ITT_FORMAT d __itt_heap_function void ITT_FORMAT p __itt_heap_function void void size_t int ITT_FORMAT d no args no args unsigned int ITT_FORMAT u const __itt_domain __itt_id ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain __itt_id ITT_FORMAT p const __itt_domain __itt_id __itt_timestamp __itt_timestamp end
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void ITT_FORMAT p void ITT_FORMAT p void size_t ITT_FORMAT d void ITT_FORMAT p const wchar_t ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s no args void ITT_FORMAT p size_t ITT_FORMAT d no args const wchar_t const wchar_t ITT_FORMAT s __itt_heap_function void size_t int ITT_FORMAT d __itt_heap_function void ITT_FORMAT p __itt_heap_function void void size_t int ITT_FORMAT d no args no args unsigned int ITT_FORMAT u const __itt_domain __itt_id ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain __itt_id ITT_FORMAT p const __itt_domain __itt_id __itt_timestamp begin
#define args
#define i
Definition: kmp_stub.cpp:87
#define omp_set_affinity_format
Definition: kmp_stub.cpp:37
#define omp_capture_affinity
Definition: kmp_stub.cpp:40
#define omp_set_nested
Definition: kmp_stub.cpp:36
int omp_get_num_threads()
int main()
Definition: test-touch.c:21