| 1 | // RUN: %libomp-compile-and-run |
| 2 | // RUN: %libomp-run | %python %S/check.py -c 'CHECK' %s |
| 3 | |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <string.h> |
| 7 | #include <omp.h> |
| 8 | |
| 9 | #define XSTR(x) #x |
| 10 | #define STR(x) XSTR(x) |
| 11 | |
| 12 | #define streqls(s1, s2) (!strcmp(s1, s2)) |
| 13 | |
| 14 | #define check(condition) \ |
| 15 | if (!(condition)) { \ |
| 16 | fprintf(stderr, "error: %s: %d: %s\n", __FILE__, __LINE__, \ |
| 17 | STR(condition)); \ |
| 18 | exit(1); \ |
| 19 | } |
| 20 | |
| 21 | #if defined(_WIN32) |
| 22 | #define snprintf _snprintf |
| 23 | #endif |
| 24 | |
| 25 | #define BUFFER_SIZE 1024 |
| 26 | |
| 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}" ; |
| 32 | |
| 33 | length = strlen(s: format); |
| 34 | omp_set_affinity_format(format); |
| 35 | |
| 36 | needed = omp_get_affinity_format(buf, BUFFER_SIZE); |
| 37 | check(streqls(buf, format)); |
| 38 | check(needed == length) |
| 39 | |
| 40 | // Check that it is truncated properly |
| 41 | omp_get_affinity_format(buf, 5); |
| 42 | check(streqls(buf, "tl:%" )); |
| 43 | |
| 44 | #pragma omp parallel |
| 45 | { |
| 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(s: supposed, maxlen: 512, format: "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)); |
| 62 | |
| 63 | needed = omp_capture_affinity(my_buf, 512, second_format); |
| 64 | needed2 = (size_t)snprintf(s: supposed, maxlen: 512, format: "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); |
| 67 | |
| 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)); |
| 72 | } |
| 73 | |
| 74 | #pragma omp parallel num_threads(4) |
| 75 | { |
| 76 | omp_display_affinity(NULL); |
| 77 | omp_display_affinity(second_format); |
| 78 | } |
| 79 | |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 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]+ |
| 85 | |