| 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 |
| 3 | |
| 4 | // Affinity Display examples |
| 5 | #include <stdio.h> |
| 6 | #include <stdlib.h> // also null is in <stddef.h> |
| 7 | #include <stddef.h> |
| 8 | #include <omp.h> |
| 9 | #include <string.h> |
| 10 | |
| 11 | // ENVIRONMENT |
| 12 | // OMP_DISPLAY_AFFINITY=TRUE |
| 13 | // OMP_NUM_THREADS=4 |
| 14 | // OMP_PLACES='{0,1},{2,3},{4,5},{6,7}' |
| 15 | |
| 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)) |
| 22 | |
| 23 | #define FORMAT_STORE 80 |
| 24 | #define BUFFER_STORE 80 |
| 25 | |
| 26 | int main(int argc, char** argv) { |
| 27 | int i, n, tid, max_req_store = 0; |
| 28 | size_t nchars; |
| 29 | char default_format[FORMAT_STORE]; |
| 30 | char my_format[] = "host=%20H tid=%0.4n binds_to=%A" ; |
| 31 | char **buffer; |
| 32 | |
| 33 | // CODE SEGMENT 1 AFFINITY DISPLAY |
| 34 | omp_display_affinity(NULL); |
| 35 | |
| 36 | // OMP_DISPLAY_AFFINITY=TRUE, |
| 37 | // Affinity reported for 1 parallel region |
| 38 | #pragma omp parallel |
| 39 | { |
| 40 | printf("Thread id %d reporting in.\n" , omp_get_thread_num()); |
| 41 | } |
| 42 | |
| 43 | // Get and Display Default Affinity Format |
| 44 | nchars = omp_get_affinity_format(default_format, (size_t)FORMAT_STORE); |
| 45 | printf(format: "Default Affinity Format is: %s\n" , default_format); |
| 46 | |
| 47 | if (nchars > FORMAT_STORE) { |
| 48 | printf(format: "Caution: Reported Format is truncated. Increase\n" ); |
| 49 | printf(format: " FORMAT_STORE by %d.\n" , (int)nchars - FORMAT_STORE); |
| 50 | } |
| 51 | |
| 52 | // Set Affinity Format |
| 53 | omp_set_affinity_format(my_format); |
| 54 | printf(format: "Affinity Format set to: %s\n" , my_format); |
| 55 | |
| 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(size: sizeof(char *) * n); |
| 60 | for (i = 0; i < n; i++) { |
| 61 | buffer[i] = (char *)malloc(size: sizeof(char) * BUFFER_STORE); |
| 62 | } |
| 63 | |
| 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) |
| 67 | { |
| 68 | tid = omp_get_thread_num(); |
| 69 | nchars = omp_capture_affinity(buffer[tid], (size_t)BUFFER_STORE, NULL); |
| 70 | #pragma omp critical |
| 71 | { |
| 72 | if (nchars > max_req_store) |
| 73 | max_req_store = nchars; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | for (i = 0; i < n; i++) { |
| 78 | printf(format: "tid=%d affinity:%s:\n" , i, buffer[i]); |
| 79 | } |
| 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 |
| 86 | |
| 87 | if (max_req_store > BUFFER_STORE) { |
| 88 | printf(format: "Caution: Affinity string truncated. Increase\n" ); |
| 89 | printf(format: " BUFFER_STORE by %d\n" , max_req_store - BUFFER_STORE); |
| 90 | } |
| 91 | return 0; |
| 92 | } |
| 93 | |