[llvm] Stop including unordered_map (NFC)
[llvm-project.git] / openmp / runtime / test / affinity / format / api2.c
blobcda3a628d0c44d975726f2e1765bff2a84dfb9fd
1 // RUN: %libomp-compile-and-run
2 // RUN: %libomp-run | %python %S/check.py -c 'CHECK' %s
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <omp.h>
9 #define XSTR(x) #x
10 #define STR(x) XSTR(x)
12 #define streqls(s1, s2) (!strcmp(s1, s2))
14 #define check(condition) \
15 if (!(condition)) { \
16 fprintf(stderr, "error: %s: %d: %s\n", __FILE__, __LINE__, \
17 STR(condition)); \
18 exit(1); \
21 #if defined(_WIN32)
22 #define snprintf _snprintf
23 #endif
25 #define BUFFER_SIZE 1024
27 int main(int argc, char** argv) {
28 char buf[BUFFER_SIZE];
29 size_t needed, length;
30 const char* format = "tl:%L tn:%n nt:%N an:%a";
31 const char* second_format = "nesting_level:%{nesting_level} thread_num:%{thread_num} num_threads:%{num_threads} ancestor_tnum:%{ancestor_tnum}";
33 length = strlen(format);
34 omp_set_affinity_format(format);
36 needed = omp_get_affinity_format(buf, BUFFER_SIZE);
37 check(streqls(buf, format));
38 check(needed == length)
40 // Check that it is truncated properly
41 omp_get_affinity_format(buf, 5);
42 check(streqls(buf, "tl:%"));
44 #pragma omp parallel
46 char my_buf[512];
47 char supposed[512];
48 int tl, tn, nt, an;
49 size_t needed, needed2;
50 tl = omp_get_level();
51 tn = omp_get_thread_num();
52 nt = omp_get_num_threads();
53 an = omp_get_ancestor_thread_num(omp_get_level()-1);
54 needed = omp_capture_affinity(my_buf, 512, NULL);
55 needed2 = (size_t)snprintf(supposed, 512, "tl:%d tn:%d nt:%d an:%d", tl, tn, nt, an);
56 check(streqls(my_buf, supposed));
57 check(needed == needed2);
58 // Check that it is truncated properly
59 supposed[4] = '\0';
60 omp_capture_affinity(my_buf, 5, NULL);
61 check(streqls(my_buf, supposed));
63 needed = omp_capture_affinity(my_buf, 512, second_format);
64 needed2 = (size_t)snprintf(supposed, 512, "nesting_level:%d thread_num:%d num_threads:%d ancestor_tnum:%d", tl, tn, nt, an);
65 check(streqls(my_buf, supposed));
66 check(needed == needed2);
68 // Check that it is truncated properly
69 supposed[25] = '\0';
70 omp_capture_affinity(my_buf, 26, second_format);
71 check(streqls(my_buf, supposed));
74 #pragma omp parallel num_threads(4)
76 omp_display_affinity(NULL);
77 omp_display_affinity(second_format);
80 return 0;
83 // CHECK: num_threads=4 tl:[0-9]+ tn:[0-9]+ nt:[0-9]+ an:[0-9]+
84 // CHECK: num_threads=4 nesting_level:[0-9]+ thread_num:[0-9]+ num_threads:[0-9]+ ancestor_tnum:[0-9]+