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
|
#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>
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;
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 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;
}
places->num_places = num_unique;
places->masks = masks;
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);
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;
}
places->num_places = num_places;
places->masks = masks;
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);
}
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", i, buf);
}
}
#endif
|