1 // RUN: %libomp-compile
2 // RUN: env OMP_DISPLAY_AFFINITY=TRUE OMP_NUM_THREADS=4 OMP_PLACES='{0,1},{2,3},{4,5},{6,7}' %libomp-run | %python %S/check.py -c 'CHECK' %s
4 // Affinity Display examples
6 #include <stdlib.h> // also null is in <stddef.h>
12 // OMP_DISPLAY_AFFINITY=TRUE
14 // OMP_PLACES='{0,1},{2,3},{4,5},{6,7}'
16 // CHECK: num_threads=1 OMP: pid [0-9]+ tid [0-9]+ thread [0-4] bound to OS proc set \{([0-7])|(0,1)|(undefined)\}
17 // CHECK: num_threads=4 Thread id [0-3] reporting in
18 // CHECK: num_threads=4 OMP: pid [0-9]+ tid [0-9]+ thread [0-4] bound to OS proc set \{([0-7])|([0246],[1357])|(undefined)\}
19 // CHECK: num_threads=1 Default Affinity Format is:
20 // CHECK: num_threads=1 Affinity Format set to: host=%20H tid=%0.4n binds_to=%A
21 // CHECK: num_threads=4 tid=[0-3] affinity:host=[a-zA-Z0-9_.-]+[ ]+tid=000[0-4][ ]+binds_to=(([0-7])|([0246],[1357])|(undefined))
23 #define FORMAT_STORE 80
24 #define BUFFER_STORE 80
26 int main(int argc
, char** argv
) {
27 int i
, n
, tid
, max_req_store
= 0;
29 char default_format
[FORMAT_STORE
];
30 char my_format
[] = "host=%20H tid=%0.4n binds_to=%A";
33 // CODE SEGMENT 1 AFFINITY DISPLAY
34 omp_display_affinity(NULL
);
36 // OMP_DISPLAY_AFFINITY=TRUE,
37 // Affinity reported for 1 parallel region
40 printf("Thread id %d reporting in.\n", omp_get_thread_num());
43 // Get and Display Default Affinity Format
44 nchars
= omp_get_affinity_format(default_format
, (size_t)FORMAT_STORE
);
45 printf("Default Affinity Format is: %s\n", default_format
);
47 if (nchars
> FORMAT_STORE
) {
48 printf("Caution: Reported Format is truncated. Increase\n");
49 printf(" FORMAT_STORE by %d.\n", (int)nchars
- FORMAT_STORE
);
52 // Set Affinity Format
53 omp_set_affinity_format(my_format
);
54 printf("Affinity Format set to: %s\n", my_format
);
56 // CODE SEGMENT 3 CAPTURE AFFINITY
57 // Set up buffer for affinity of n threads
58 n
= omp_get_max_threads();
59 buffer
= (char **)malloc(sizeof(char *) * n
);
60 for (i
= 0; i
< n
; i
++) {
61 buffer
[i
] = (char *)malloc(sizeof(char) * BUFFER_STORE
);
64 // Capture Affinity using Affinity Format set above.
65 // Use critical reduction to check size of buffer areas
66 #pragma omp parallel private(tid, nchars)
68 tid
= omp_get_thread_num();
69 nchars
= omp_capture_affinity(buffer
[tid
], (size_t)BUFFER_STORE
, NULL
);
72 if (nchars
> max_req_store
)
73 max_req_store
= nchars
;
77 for (i
= 0; i
< n
; i
++) {
78 printf("tid=%d affinity:%s:\n", i
, buffer
[i
]);
80 // for 4 threads with OMP_PLACES='{0,1},{2,3},{4,5},{6,7}'
81 // host=%20H tid=%0.4n binds_to=%A
82 // host=<hostname> tid=0000 binds_to=0,1
83 // host=<hostname> tid=0001 binds_to=2,3
84 // host=<hostname> tid=0002 binds_to=4,5
85 // host=<hostname> tid=0003 binds_to=6,7
87 if (max_req_store
> BUFFER_STORE
) {
88 printf("Caution: Affinity string truncated. Increase\n");
89 printf(" BUFFER_STORE by %d\n", max_req_store
- BUFFER_STORE
);