1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
|
#ifndef LIBOMP_TEST_TOPOLOGY_H
#define LIBOMP_TEST_TOPOLOGY_H
#include "libomp_test_affinity.h"
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <errno.h>
#include <ctype.h>
#include <omp.h>
#include <stdarg.h>
typedef enum topology_obj_type_t {
TOPOLOGY_OBJ_THREAD,
TOPOLOGY_OBJ_CORE,
TOPOLOGY_OBJ_SOCKET,
TOPOLOGY_OBJ_MAX
} topology_obj_type_t;
typedef struct place_list_t {
int num_places;
int current_place;
int *place_nums;
affinity_mask_t **masks;
} place_list_t;
// Return the first character in file 'f' that is not a whitespace character
// including newlines and carriage returns
static int get_first_nonspace_from_file(FILE *f) {
int c;
do {
c = fgetc(f);
} while (c != EOF && (isspace(c) || c == '\n' || c == '\r'));
return c;
}
// Read an integer from file 'f' into 'number'
// Return 1 on successful read of integer,
// 0 on unsuccessful read of integer,
// EOF on end of file.
static int get_integer_from_file(FILE *f, int *number) {
int n;
n = fscanf(f, "%d", number);
if (feof(f))
return EOF;
if (n != 1)
return 0;
return 1;
}
// Read a siblings list file from Linux /sys/devices/system/cpu/cpu?/topology/*
static affinity_mask_t *topology_get_mask_from_file(const char *filename) {
int status = EXIT_SUCCESS;
FILE *f = fopen(filename, "r");
if (!f) {
perror(filename);
exit(EXIT_FAILURE);
}
affinity_mask_t *mask = affinity_mask_alloc();
while (1) {
int c, i, n, lower, upper;
// Read the first integer
n = get_integer_from_file(f, &lower);
if (n == EOF) {
break;
} else if (n == 0) {
fprintf(stderr, "syntax error: expected integer\n");
status = EXIT_FAILURE;
break;
}
// Now either a , or -
c = get_first_nonspace_from_file(f);
if (c == EOF || c == ',') {
affinity_mask_set(mask, lower);
if (c == EOF)
break;
} else if (c == '-') {
n = get_integer_from_file(f, &upper);
if (n == EOF || n == 0) {
fprintf(stderr, "syntax error: expected integer\n");
status = EXIT_FAILURE;
break;
}
for (i = lower; i <= upper; ++i)
affinity_mask_set(mask, i);
c = get_first_nonspace_from_file(f);
if (c == EOF) {
break;
} else if (c == ',') {
continue;
} else {
fprintf(stderr, "syntax error: unexpected character: '%c (%d)'\n", c,
c);
status = EXIT_FAILURE;
break;
}
} else {
fprintf(stderr, "syntax error: unexpected character: '%c (%d)'\n", c, c);
status = EXIT_FAILURE;
break;
}
}
fclose(f);
if (status == EXIT_FAILURE) {
affinity_mask_free(mask);
mask = NULL;
}
return mask;
}
static int topology_get_num_cpus() {
char buf[1024];
// Count the number of cpus
int cpu = 0;
while (1) {
snprintf(buf, sizeof(buf), "/sys/devices/system/cpu/cpu%d", cpu);
DIR *dir = opendir(buf);
if (dir) {
closedir(dir);
cpu++;
} else {
break;
}
}
if (cpu == 0)
cpu = 1;
return cpu;
}
// Return whether the current thread has access to all logical processors
static int topology_using_full_mask() {
int cpu;
int has_all = 1;
int num_cpus = topology_get_num_cpus();
affinity_mask_t *mask = affinity_mask_alloc();
get_thread_affinity(mask);
for (cpu = 0; cpu < num_cpus; ++cpu) {
if (!affinity_mask_isset(mask, cpu)) {
has_all = 0;
break;
}
}
affinity_mask_free(mask);
return has_all;
}
// Return array of masks representing OMP_PLACES keyword (e.g., sockets, cores,
// threads)
static place_list_t *topology_alloc_type_places(topology_obj_type_t type) {
char buf[1024];
int i, cpu, num_places, num_unique;
int *place_nums;
int num_cpus = topology_get_num_cpus();
place_list_t *places = (place_list_t *)malloc(sizeof(place_list_t));
affinity_mask_t **masks =
(affinity_mask_t **)malloc(sizeof(affinity_mask_t *) * num_cpus);
num_unique = 0;
for (cpu = 0; cpu < num_cpus; ++cpu) {
affinity_mask_t *mask;
if (type == TOPOLOGY_OBJ_CORE) {
snprintf(buf, sizeof(buf),
"/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list",
cpu);
mask = topology_get_mask_from_file(buf);
} else if (type == TOPOLOGY_OBJ_SOCKET) {
snprintf(buf, sizeof(buf),
"/sys/devices/system/cpu/cpu%d/topology/core_siblings_list",
cpu);
mask = topology_get_mask_from_file(buf);
} else if (type == TOPOLOGY_OBJ_THREAD) {
mask = affinity_mask_alloc();
affinity_mask_set(mask, cpu);
} else {
fprintf(stderr, "Unknown topology type (%d)\n", (int)type);
exit(EXIT_FAILURE);
}
// Check for unique topology objects above the thread level
if (type != TOPOLOGY_OBJ_THREAD) {
for (i = 0; i < num_unique; ++i) {
if (affinity_mask_equal(masks[i], mask)) {
affinity_mask_free(mask);
mask = NULL;
break;
}
}
}
if (mask)
masks[num_unique++] = mask;
}
place_nums = (int *)malloc(sizeof(int) * num_unique);
for (i = 0; i < num_unique; ++i)
place_nums[i] = i;
places->num_places = num_unique;
places->masks = masks;
places->place_nums = place_nums;
places->current_place = -1;
return places;
}
static place_list_t *topology_alloc_openmp_places() {
int place, i;
int num_places = omp_get_num_places();
place_list_t *places = (place_list_t *)malloc(sizeof(place_list_t));
affinity_mask_t **masks =
(affinity_mask_t **)malloc(sizeof(affinity_mask_t *) * num_places);
int *place_nums = (int *)malloc(sizeof(int) * num_places);
for (place = 0; place < num_places; ++place) {
int num_procs = omp_get_place_num_procs(place);
int *ids = (int *)malloc(sizeof(int) * num_procs);
omp_get_place_proc_ids(place, ids);
affinity_mask_t *mask = affinity_mask_alloc();
for (i = 0; i < num_procs; ++i)
affinity_mask_set(mask, ids[i]);
masks[place] = mask;
place_nums[place] = place;
}
places->num_places = num_places;
places->place_nums = place_nums;
places->masks = masks;
places->current_place = omp_get_place_num();
return places;
}
static place_list_t *topology_alloc_openmp_partition() {
int p, i;
int num_places = omp_get_partition_num_places();
place_list_t *places = (place_list_t *)malloc(sizeof(place_list_t));
int *place_nums = (int *)malloc(sizeof(int) * num_places);
affinity_mask_t **masks =
(affinity_mask_t **)malloc(sizeof(affinity_mask_t *) * num_places);
omp_get_partition_place_nums(place_nums);
for (p = 0; p < num_places; ++p) {
int place = place_nums[p];
int num_procs = omp_get_place_num_procs(place);
int *ids = (int *)malloc(sizeof(int) * num_procs);
if (num_procs == 0) {
fprintf(stderr, "place %d has 0 procs?\n", place);
exit(EXIT_FAILURE);
}
omp_get_place_proc_ids(place, ids);
affinity_mask_t *mask = affinity_mask_alloc();
for (i = 0; i < num_procs; ++i)
affinity_mask_set(mask, ids[i]);
if (affinity_mask_count(mask) == 0) {
fprintf(stderr, "place %d has 0 procs set?\n", place);
exit(EXIT_FAILURE);
}
masks[p] = mask;
}
places->num_places = num_places;
places->place_nums = place_nums;
places->masks = masks;
places->current_place = omp_get_place_num();
return places;
}
// Free the array of masks from one of: topology_alloc_type_masks()
// or topology_alloc_openmp_masks()
static void topology_free_places(place_list_t *places) {
int i;
for (i = 0; i < places->num_places; ++i)
affinity_mask_free(places->masks[i]);
free(places->masks);
free(places->place_nums);
free(places);
}
static void topology_print_places(const place_list_t *p) {
int i;
char buf[1024];
for (i = 0; i < p->num_places; ++i) {
affinity_mask_snprintf(buf, sizeof(buf), p->masks[i]);
printf("Place %d: %s\n", p->place_nums[i], buf);
}
}
// Print out an error message, possibly with two problem place lists,
// and then exit with failure
static void proc_bind_die(omp_proc_bind_t proc_bind, int T, int P,
const char *format, ...) {
va_list args;
va_start(args, format);
const char *pb;
switch (proc_bind) {
case omp_proc_bind_false:
pb = "False";
break;
case omp_proc_bind_true:
pb = "True";
break;
case omp_proc_bind_master:
pb = "Master (Primary)";
break;
case omp_proc_bind_close:
pb = "Close";
break;
case omp_proc_bind_spread:
pb = "Spread";
break;
default:
pb = "(Unknown Proc Bind Type)";
break;
}
if (proc_bind == omp_proc_bind_spread || proc_bind == omp_proc_bind_close) {
if (T <= P) {
fprintf(stderr, "%s : (T(%d) <= P(%d)) : ", pb, T, P);
} else {
fprintf(stderr, "%s : (T(%d) > P(%d)) : ", pb, T, P);
}
} else {
fprintf(stderr, "%s : T = %d, P = %d : ", pb, T, P);
}
vfprintf(stderr, format, args);
va_end(args);
exit(EXIT_FAILURE);
}
// Return 1 on failure, 0 on success.
static void proc_bind_check(omp_proc_bind_t proc_bind,
const place_list_t *parent, place_list_t **children,
int nchildren) {
place_list_t *partition;
int T, i, j, place, low, high, first, last, count, current_place, num_places;
const int *place_nums;
int P = parent->num_places;
// Find the correct T (there could be null entries in children)
place_list_t **partitions =
(place_list_t **)malloc(sizeof(place_list_t *) * nchildren);
T = 0;
for (i = 0; i < nchildren; ++i)
if (children[i])
partitions[T++] = children[i];
// Only able to check spread, close, master (primary)
if (proc_bind != omp_proc_bind_spread && proc_bind != omp_proc_bind_close &&
proc_bind != omp_proc_bind_master)
proc_bind_die(proc_bind, T, P, NULL, NULL,
"Cannot check this proc bind type\n");
if (proc_bind == omp_proc_bind_spread) {
if (T <= P) {
// Run through each subpartition
for (i = 0; i < T; ++i) {
partition = partitions[i];
place_nums = partition->place_nums;
num_places = partition->num_places;
current_place = partition->current_place;
// Correct count?
low = P / T;
high = P / T + (P % T ? 1 : 0);
if (num_places != low && num_places != high) {
proc_bind_die(proc_bind, T, P,
"Incorrect number of places for thread %d: %d. "
"Expecting between %d and %d\n",
i, num_places, low, high);
}
// Consecutive places?
for (j = 1; j < num_places; ++j) {
if (place_nums[j] != (place_nums[j - 1] + 1) % P) {
proc_bind_die(proc_bind, T, P,
"Not consecutive places: %d, %d in partition\n",
place_nums[j - 1], place_nums[j]);
}
}
first = place_nums[0];
last = place_nums[num_places - 1];
// Primary thread executes on place of the parent thread?
if (i == 0) {
if (current_place != parent->current_place) {
proc_bind_die(
proc_bind, T, P,
"Primary thread not on same place (%d) as parent thread (%d)\n",
current_place, parent->current_place);
}
} else {
// Thread's current place is first place within it's partition?
if (current_place != first) {
proc_bind_die(proc_bind, T, P,
"Thread's current place (%d) is not the first place "
"in its partition [%d, %d]\n",
current_place, first, last);
}
}
// Partitions don't have intersections?
int f1 = first;
int l1 = last;
for (j = 0; j < i; ++j) {
int f2 = partitions[j]->place_nums[0];
int l2 = partitions[j]->place_nums[partitions[j]->num_places - 1];
if (f1 > l1 && f2 > l2) {
proc_bind_die(proc_bind, T, P,
"partitions intersect. [%d, %d] and [%d, %d]\n", f1,
l1, f2, l2);
}
if (f1 > l1 && f2 <= l2)
if (f1 < l2 || l1 > f2) {
proc_bind_die(proc_bind, T, P,
"partitions intersect. [%d, %d] and [%d, %d]\n", f1,
l1, f2, l2);
}
if (f1 <= l1 && f2 > l2)
if (f2 < l1 || l2 > f1) {
proc_bind_die(proc_bind, T, P,
"partitions intersect. [%d, %d] and [%d, %d]\n", f1,
l1, f2, l2);
}
if (f1 <= l1 && f2 <= l2)
if (!(f2 > l1 || l2 < f1)) {
proc_bind_die(proc_bind, T, P,
"partitions intersect. [%d, %d] and [%d, %d]\n", f1,
l1, f2, l2);
}
}
}
} else {
// T > P
// Each partition has only one place?
for (i = 0; i < T; ++i) {
if (partitions[i]->num_places != 1) {
proc_bind_die(
proc_bind, T, P,
"Incorrect number of places for thread %d: %d. Expecting 1\n", i,
partitions[i]->num_places);
}
}
// Correct number of consecutive threads per partition?
low = T / P;
high = T / P + (T % P ? 1 : 0);
for (i = 1, count = 1; i < T; ++i) {
if (partitions[i]->place_nums[0] == partitions[i - 1]->place_nums[0]) {
count++;
if (count > high) {
proc_bind_die(
proc_bind, T, P,
"Too many threads have place %d for their partition\n",
partitions[i]->place_nums[0]);
}
} else {
if (count < low) {
proc_bind_die(
proc_bind, T, P,
"Not enough threads have place %d for their partition\n",
partitions[i]->place_nums[0]);
}
count = 1;
}
}
// Primary thread executes on place of the parent thread?
current_place = partitions[0]->place_nums[0];
if (parent->current_place != -1 &&
current_place != parent->current_place) {
proc_bind_die(
proc_bind, T, P,
"Primary thread not on same place (%d) as parent thread (%d)\n",
current_place, parent->current_place);
}
}
} else if (proc_bind == omp_proc_bind_close ||
proc_bind == omp_proc_bind_master) {
// Check that each subpartition is the same as the parent
for (i = 0; i < T; ++i) {
partition = partitions[i];
place_nums = partition->place_nums;
num_places = partition->num_places;
current_place = partition->current_place;
if (parent->num_places != num_places) {
proc_bind_die(proc_bind, T, P,
"Number of places in subpartition (%d) does not match "
"parent (%d)\n",
num_places, parent->num_places);
}
for (j = 0; j < num_places; ++j) {
if (parent->place_nums[j] != place_nums[j]) {
proc_bind_die(proc_bind, T, P,
"Subpartition place (%d) does not match "
"parent partition place (%d)\n",
place_nums[j], parent->place_nums[j]);
}
}
}
// Find index into place_nums of current place for parent
for (j = 0; j < parent->num_places; ++j)
if (parent->place_nums[j] == parent->current_place)
break;
if (proc_bind == omp_proc_bind_close) {
if (T <= P) {
// close T <= P
// check place assignment for each thread
for (i = 0; i < T; ++i) {
partition = partitions[i];
current_place = partition->current_place;
if (current_place != parent->place_nums[j]) {
proc_bind_die(
proc_bind, T, P,
"Thread %d's current place (%d) is incorrect. expected %d\n", i,
current_place, parent->place_nums[j]);
}
j = (j + 1) % parent->num_places;
}
} else {
// close T > P
// check place assignment for each thread
low = T / P;
high = T / P + (T % P ? 1 : 0);
count = 1;
if (partitions[0]->current_place != parent->current_place) {
proc_bind_die(
proc_bind, T, P,
"Primary thread's place (%d) is not parent thread's place (%d)\n",
partitions[0]->current_place, parent->current_place);
}
for (i = 1; i < T; ++i) {
current_place = partitions[i]->current_place;
if (current_place == parent->place_nums[j]) {
count++;
if (count > high) {
proc_bind_die(
proc_bind, T, P,
"Too many threads have place %d for their current place\n",
current_place);
}
} else {
if (count < low) {
proc_bind_die(
proc_bind, T, P,
"Not enough threads have place %d for their current place\n",
parent->place_nums[j]);
}
j = (j + 1) % parent->num_places;
if (current_place != parent->place_nums[j]) {
proc_bind_die(
proc_bind, T, P,
"Thread %d's place (%d) is not corret. Expected %d\n", i,
partitions[i]->current_place, parent->place_nums[j]);
}
count = 1;
}
}
}
} else {
// proc_bind_primary
// Every thread should be assigned to the primary thread's place
for (i = 0; i < T; ++i) {
if (partitions[i]->current_place != parent->current_place) {
proc_bind_die(
proc_bind, T, P,
"Thread %d's place (%d) is not the primary thread's place (%d)\n",
i, partitions[i]->current_place, parent->current_place);
}
}
}
}
// Check that each partition's current place is within the partition
for (i = 0; i < T; ++i) {
current_place = partitions[i]->current_place;
num_places = partitions[i]->num_places;
first = partitions[i]->place_nums[0];
last = partitions[i]->place_nums[num_places - 1];
for (j = 0; j < num_places; ++j)
if (partitions[i]->place_nums[j] == current_place)
break;
if (j == num_places) {
proc_bind_die(proc_bind, T, P,
"Thread %d's current place (%d) is not within its "
"partition [%d, %d]\n",
i, current_place, first, last);
}
}
free(partitions);
}
#endif
|