1 // RUN: %libomp-compile-and-run 2 3 #include <stdio.h> 4 #include <stdlib.h> 5 #include <string.h> 6 #include <omp.h> 7 8 #define XSTR(x) #x 9 #define STR(x) XSTR(x) 10 11 #define streqls(s1, s2) (!strcmp(s1, s2)) 12 13 #define check(condition) \ 14 if (!(condition)) { \ 15 fprintf(stderr, "error: %s: %d: " STR(condition) "\n", __FILE__, \ 16 __LINE__); \ 17 exit(1); \ 18 } 19 20 #define BUFFER_SIZE 1024 21 22 char buf[BUFFER_SIZE]; 23 #pragma omp threadprivate(buf) 24 25 char* get_string(size_t check_needed) { 26 size_t needed = omp_capture_affinity(buf, BUFFER_SIZE, NULL); 27 //printf("buf = %s\n", buf); 28 check(needed < BUFFER_SIZE); 29 if (check_needed != 0) { 30 check(needed == check_needed); 31 } 32 return buf; 33 } 34 35 void check_thread_num_padded_rjustified() { 36 int i; 37 const char* formats[2] = {"%0.8{thread_num}", "%0.8n"}; 38 for (i = 0; i < sizeof(formats)/sizeof(formats[0]); ++i) { 39 omp_set_affinity_format(formats[i]); 40 #pragma omp parallel num_threads(8) 41 { 42 int j; 43 int tid = omp_get_thread_num(); 44 char ctid = '0' + (char)tid; 45 char* s = get_string(8); 46 for (j = 0; j < 7; ++j) { 47 check(s[j] == '0'); 48 } 49 check(s[j] == ctid); 50 } 51 } 52 } 53 54 void check_thread_num_rjustified() { 55 int i; 56 const char* formats[2] = {"%.12{thread_num}", "%.12n"}; 57 for (i = 0; i < sizeof(formats)/sizeof(formats[0]); ++i) { 58 omp_set_affinity_format(formats[i]); 59 #pragma omp parallel num_threads(8) 60 { 61 int j; 62 int tid = omp_get_thread_num(); 63 char ctid = '0' + (char)tid; 64 char* s = get_string(12); 65 for (j = 0; j < 11; ++j) { 66 check(s[j] == ' '); 67 } 68 check(s[j] == ctid); 69 } 70 } 71 } 72 73 void check_thread_num_ljustified() { 74 int i; 75 const char* formats[2] = {"%5{thread_num}", "%5n"}; 76 for (i = 0; i < sizeof(formats)/sizeof(formats[0]); ++i) { 77 omp_set_affinity_format(formats[i]); 78 #pragma omp parallel num_threads(8) 79 { 80 int j; 81 int tid = omp_get_thread_num(); 82 char ctid = '0' + (char)tid; 83 char* s = get_string(5); 84 check(s[0] == ctid); 85 for (j = 1; j < 5; ++j) { 86 check(s[j] == ' '); 87 } 88 } 89 } 90 } 91 92 void check_thread_num_padded_ljustified() { 93 int i; 94 const char* formats[2] = {"%018{thread_num}", "%018n"}; 95 for (i = 0; i < sizeof(formats)/sizeof(formats[0]); ++i) { 96 omp_set_affinity_format(formats[i]); 97 #pragma omp parallel num_threads(8) 98 { 99 int j; 100 int tid = omp_get_thread_num(); 101 char ctid = '0' + (char)tid; 102 char* s = get_string(18); 103 check(s[0] == ctid); 104 for (j = 1; j < 18; ++j) { 105 check(s[j] == ' '); 106 } 107 } 108 } 109 } 110 111 int main(int argc, char** argv) { 112 check_thread_num_ljustified(); 113 check_thread_num_rjustified(); 114 check_thread_num_padded_ljustified(); 115 check_thread_num_padded_rjustified(); 116 return 0; 117 } 118