93 lines · c
1// RUN: %libomp-compile2// 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' %s3 4// Affinity Display examples5#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// ENVIRONMENT12// OMP_DISPLAY_AFFINITY=TRUE13// OMP_NUM_THREADS=414// 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 in18// 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=%A21// 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 8024#define BUFFER_STORE 8025 26int 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 DISPLAY34 omp_display_affinity(NULL);35 36 // OMP_DISPLAY_AFFINITY=TRUE,37 // Affinity reported for 1 parallel region38 #pragma omp parallel39 {40 printf("Thread id %d reporting in.\n", omp_get_thread_num());41 }42 43 // Get and Display Default Affinity Format44 nchars = omp_get_affinity_format(default_format, (size_t)FORMAT_STORE);45 printf("Default Affinity Format is: %s\n", default_format);46 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);50 }51 52 // Set Affinity Format53 omp_set_affinity_format(my_format);54 printf("Affinity Format set to: %s\n", my_format);55 56 // CODE SEGMENT 3 CAPTURE AFFINITY57 // Set up buffer for affinity of n threads58 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);62 }63 64 // Capture Affinity using Affinity Format set above.65 // Use critical reduction to check size of buffer areas66 #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 critical71 {72 if (nchars > max_req_store)73 max_req_store = nchars;74 }75 }76 77 for (i = 0; i < n; i++) {78 printf("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=%A82 // host=<hostname> tid=0000 binds_to=0,183 // host=<hostname> tid=0001 binds_to=2,384 // host=<hostname> tid=0002 binds_to=4,585 // host=<hostname> tid=0003 binds_to=6,786 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);90 }91 return 0;92}93