| 1 |
// @HEADER |
| 2 |
// ***************************************************************************** |
| 3 |
// Zoltan Toolkit for Load-balancing, Partitioning, Ordering and Coloring |
| 4 |
// |
| 5 |
// Copyright 2012 NTESS and the Zoltan contributors. |
| 6 |
// SPDX-License-Identifier: BSD-3-Clause |
| 7 |
// ***************************************************************************** |
| 8 |
// @HEADER |
| 9 |
/* |
| 10 |
* Discover hardware topology using hwloc. |
| 11 |
* Tested with hwloc 1.0.2: http://www.open-mpi.org/software/hwloc |
| 12 |
* |
| 13 |
* This function returns hierarchy on a node or on a machine. |
| 14 |
* A dual socket quad core might return the following: |
| 15 |
* |
| 16 |
* depth = Zoltan_Get_Toplogy(branch_degrees, num_cpus, names, tmem, lmem) |
| 17 |
* |
| 18 |
* depth = 3 |
| 19 |
* degrees = {2, 4, 0} |
| 20 |
* num_cpus = {8, 4, 1} |
| 21 |
* names = {"MACHINE", "SOCKET", "PU"} |
| 22 |
* tmem = {34359738368, 17179869184, 4294967296} |
| 23 |
* lmem = {34359738368, 17179869184, 4294967296} |
| 24 |
* |
| 25 |
* Because a machine (a node to us) contains 2 sockets with 8 total cpus. |
| 26 |
* A socket contains 4 cores with 4 total cpus. |
| 27 |
* A processing unit is always the "leaf node" of the topology. |
| 28 |
* |
| 29 |
* tmem is the total memory of that object and its children |
| 30 |
* lmem is the total memory of that object |
| 31 |
* |
| 32 |
* If memory information is not available it will be zero. |
| 33 |
* |
| 34 |
* If any parameter is NULL it will be ignored. |
| 35 |
*/ |
| 36 |
|
| 37 |
#include <hwloc.h> |
| 38 |
#include <stdlib.h> |
| 39 |
#include <stdio.h> |
| 40 |
#include <string.h> |
| 41 |
#include <inttypes.h> |
| 42 |
#include <mpi.h> |
| 43 |
|
| 44 |
#define MEMORY_ERROR(buf) { \ |
| 45 |
if (buf == NULL){ \ |
| 46 |
fprintf(stderr,"Memory error\n"); \ |
| 47 |
exit(1); \ |
| 48 |
} \ |
| 49 |
} |
| 50 |
|
| 51 |
/* |
| 52 |
* Define BUILD_MAIN to create a test application |
| 53 |
*/ |
| 54 |
#define BUILD_MAIN |
| 55 |
|
| 56 |
#define MAX_NAME_LEN 64 /* what should this be? */ |
| 57 |
#define MAX_NUM_LEVELS 12 /* ditto */ |
| 58 |
|
| 59 |
int Zoltan_Get_Topology(int **branch_degree, |
| 60 |
int **num_cpus, |
| 61 |
char ***level_name, |
| 62 |
uint64_t **total_memory, |
| 63 |
uint64_t **local_memory) |
| 64 |
{ |
| 65 |
hwloc_topology_t topology; |
| 66 |
hwloc_obj_t rootobj, obj, prev_obj; |
| 67 |
char description[4096]; |
| 68 |
int size, rank, depth; |
| 69 |
int i, j; |
| 70 |
|
| 71 |
int real_depth, next, from, to, num; |
| 72 |
int real_level[MAX_NUM_LEVELS]; |
| 73 |
|
| 74 |
char type_name[MAX_NUM_LEVELS][MAX_NAME_LEN]; /* "machine", "node", "socket", ... */ |
| 75 |
int type_size[MAX_NUM_LEVELS]; /* how many (dual node is 2, quad socket is 4) */ |
| 76 |
hwloc_cpuset_t ancestor_cpuset[MAX_NUM_LEVELS]; /* mask representing cpus in this object */ |
| 77 |
uint64_t lmem[MAX_NUM_LEVELS]; /* memory owned by this object */ |
| 78 |
uint64_t tmem[MAX_NUM_LEVELS]; /* owned by this object and all its children */ |
| 79 |
|
| 80 |
MPI_Comm_size(zoltan_get_global_comm(), &size); |
| 81 |
MPI_Comm_rank(zoltan_get_global_comm(), &rank); |
| 82 |
|
| 83 |
/* allocate & initialize topology object */ |
| 84 |
|
| 85 |
hwloc_topology_init(&topology); |
| 86 |
|
| 87 |
#if 0 |
| 88 |
/* Configure detection to skip levels with only one child or which are only children. |
| 89 |
* |
| 90 |
* If we only want to know branch degrees, we can skip the levels in the hierarchy |
| 91 |
* that don't branch, but for now it's interesting to see what they are. |
| 92 |
*/ |
| 93 |
|
| 94 |
hwloc_topology_ignore_all_keep_structure(topology); |
| 95 |
#endif |
| 96 |
|
| 97 |
/* detect topology */ |
| 98 |
|
| 99 |
hwloc_topology_load(topology); |
| 100 |
|
| 101 |
/* |
| 102 |
* Get the depth of topology and the root. |
| 103 |
*/ |
| 104 |
|
| 105 |
depth = hwloc_topology_get_depth(topology); |
| 106 |
|
| 107 |
for (i=0; i < depth; i++){ |
| 108 |
ancestor_cpuset[i] = hwloc_cpuset_alloc(); |
| 109 |
} |
| 110 |
|
| 111 |
/* |
| 112 |
* Get topology information. We'll use sibling 0 at each level of the topology. |
| 113 |
* |
| 114 |
* The root object typically is a node on a multi-node machine, not the |
| 115 |
* collection of nodes in the application. |
| 116 |
*/ |
| 117 |
|
| 118 |
rootobj = hwloc_get_root_obj(topology); |
| 119 |
|
| 120 |
hwloc_obj_type_snprintf(type_name[0], MAX_NAME_LEN, rootobj, 1); |
| 121 |
hwloc_cpuset_copy(ancestor_cpuset[0], rootobj->cpuset); |
| 122 |
type_size[0] = 1; |
| 123 |
lmem[0] = rootobj->memory.local_memory; /*0 if not available */ |
| 124 |
tmem[0] = rootobj->memory.total_memory; |
| 125 |
|
| 126 |
real_level[0] = 0; |
| 127 |
next = 1; |
| 128 |
|
| 129 |
prev_obj = rootobj; |
| 130 |
|
| 131 |
for (i=1; i < depth; i++){ |
| 132 |
|
| 133 |
type_size[i] = prev_obj->arity; /* parent's number of children */ |
| 134 |
|
| 135 |
if ( (type_size[i] > 1) || (i == depth-1)){ |
| 136 |
/* |
| 137 |
* Significant levels are processing units (the leaf nodes) and levels |
| 138 |
* that are genuine branches - not the only child of parent. |
| 139 |
*/ |
| 140 |
real_level[next++] = i; |
| 141 |
} |
| 142 |
|
| 143 |
obj = prev_obj->children[0]; |
| 144 |
hwloc_obj_type_snprintf(type_name[i], MAX_NAME_LEN, obj, 1); |
| 145 |
hwloc_cpuset_copy(ancestor_cpuset[i], obj->cpuset); |
| 146 |
lmem[i] = obj->memory.local_memory; |
| 147 |
tmem[i] = obj->memory.total_memory; |
| 148 |
|
| 149 |
prev_obj = obj; |
| 150 |
} |
| 151 |
|
| 152 |
real_depth = next; |
| 153 |
|
| 154 |
if (branch_degree){ |
| 155 |
*branch_degree = (int *)malloc(sizeof(int) * real_depth); |
| 156 |
MEMORY_ERROR(*branch_degree); |
| 157 |
} |
| 158 |
if (num_cpus){ |
| 159 |
*num_cpus = (int *)malloc(sizeof(int) * real_depth); |
| 160 |
MEMORY_ERROR(*num_cpus); |
| 161 |
} |
| 162 |
if (level_name){ |
| 163 |
*level_name = (char **)malloc(sizeof(char*) * real_depth); |
| 164 |
MEMORY_ERROR(*level_name); |
| 165 |
} |
| 166 |
if (total_memory){ |
| 167 |
*total_memory = (uint64_t *)malloc(sizeof(uint64_t) * real_depth); |
| 168 |
MEMORY_ERROR(*total_memory); |
| 169 |
} |
| 170 |
if (local_memory){ |
| 171 |
*local_memory = (uint64_t *)malloc(sizeof(uint64_t) * real_depth); |
| 172 |
MEMORY_ERROR(*local_memory); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
for (i=0; i < real_depth; i++){ |
| 177 |
from = real_level[i]; |
| 178 |
|
| 179 |
if (i < real_depth-1) /* branch node */ |
| 180 |
to = real_level[i+1]; |
| 181 |
else |
| 182 |
to = 0; /* leaf node */ |
| 183 |
|
| 184 |
/* Total number of CPUs within this object |
| 185 |
*/ |
| 186 |
|
| 187 |
if (num_cpus){ |
| 188 |
(*num_cpus)[i] = hwloc_cpuset_weight(ancestor_cpuset[from]); |
| 189 |
} |
| 190 |
|
| 191 |
/* number of next level down objects in one of these objects |
| 192 |
*/ |
| 193 |
|
| 194 |
if (branch_degree){ |
| 195 |
if (to > 0){ |
| 196 |
(*branch_degree)[i] = type_size[to]; |
| 197 |
} |
| 198 |
else{ |
| 199 |
(*branch_degree)[i] = 0; |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
/* |
| 204 |
* name of this level in the topology and the succession of children |
| 205 |
* level that have only one parent. |
| 206 |
*/ |
| 207 |
|
| 208 |
if (level_name){ |
| 209 |
description[0] = 0; |
| 210 |
if (to){ |
| 211 |
num = type_size[from]; |
| 212 |
for (j=from; j < to; j++){ |
| 213 |
strcat(description, type_name[j]); |
| 214 |
if (num > 1){ |
| 215 |
strcat(description,"s"); |
| 216 |
} |
| 217 |
if (j < (to - 1)){ |
| 218 |
strcat(description, "/"); |
| 219 |
} |
| 220 |
} |
| 221 |
} |
| 222 |
else{ |
| 223 |
strcat(description, type_name[from]); |
| 224 |
} |
| 225 |
|
| 226 |
(*level_name)[i] = (char *)malloc(strlen(description) +1); |
| 227 |
MEMORY_ERROR((*level_name)[i]); |
| 228 |
strcpy((*level_name)[i], description); |
| 229 |
} |
| 230 |
|
| 231 |
if (local_memory){ |
| 232 |
(*local_memory)[i] = lmem[from]; |
| 233 |
if ((lmem[from] == 0) && (to > from)){ |
| 234 |
for (j=from+1; j < to; j++){ |
| 235 |
if (lmem[j] > 0){ |
| 236 |
(*local_memory)[j] = lmem[from]; |
| 237 |
break; |
| 238 |
} |
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
if (total_memory){ |
| 244 |
(*total_memory)[i] = tmem[from]; |
| 245 |
if ((tmem[from]) == 0 && (to > from)){ |
| 246 |
for (j=from+1; j < to; j++){ |
| 247 |
if (tmem[j] > 0){ |
| 248 |
(*total_memory)[j] = tmem[from]; |
| 249 |
break; |
| 250 |
} |
| 251 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
} |
| 256 |
|
| 257 |
for (i=0; i < depth; i++){ |
| 258 |
hwloc_cpuset_free(ancestor_cpuset[i]); |
| 259 |
} |
| 260 |
|
| 261 |
return real_depth; |
| 262 |
} |
| 263 |
|
| 264 |
#ifdef BUILD_MAIN |
| 265 |
|
| 266 |
int main(int argc, char *argv[]) |
| 267 |
{ |
| 268 |
int rank, depth; |
| 269 |
int i, go; |
| 270 |
int *branching_degree=NULL, *num_cpus=NULL; |
| 271 |
uint64_t *local_memory=NULL, *total_memory=NULL; |
| 272 |
char **type_name=NULL; |
| 273 |
|
| 274 |
MPI_Init(&argc, &argv); |
| 275 |
MPI_Comm_rank(zoltan_get_global_comm(), &rank); |
| 276 |
|
| 277 |
depth = Zoltan_Get_Topology(&branching_degree, &num_cpus, &type_name, &total_memory, &local_memory); |
| 278 |
|
| 279 |
MPI_Barrier(zoltan_get_global_comm()); |
| 280 |
|
| 281 |
if (rank == 0){ |
| 282 |
|
| 283 |
|
| 284 |
printf("A %s with %d CPUs has:\n",type_name[0],num_cpus[0]); |
| 285 |
|
| 286 |
for (i=1; i < depth; i++){ |
| 287 |
if (i < depth-1){ |
| 288 |
printf(" %d %s (%d total CPUs) %swith\n", |
| 289 |
branching_degree[i-1], type_name[i],num_cpus[i], |
| 290 |
((num_cpus[i] > 1) ? "each " : "")); |
| 291 |
} |
| 292 |
else{ |
| 293 |
printf(" %d %s\n", branching_degree[i-1], type_name[i]); |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
printf("\n"); |
| 298 |
|
| 299 |
go = 0; |
| 300 |
for (i=0; i <depth; i++){ |
| 301 |
if ((total_memory[i] > 0) || (local_memory[i] > 0 )){ |
| 302 |
go = 1; |
| 303 |
break; |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
if (go){ |
| 308 |
printf("Total memory (for all children), Local memory (for object at level)\n"); |
| 309 |
for (i=0; i < depth; i++){ |
| 310 |
printf("Memory at level %s: (%f10.0KB, %f10.0KB)\n", |
| 311 |
type_name[i],(float)total_memory[i]/1024.0,(float)local_memory[i]/1024.0); |
| 312 |
} |
| 313 |
} |
| 314 |
else{ |
| 315 |
printf("Memory available at each level is not available\n"); |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
/* Allocated cpusets can be freed with hwloc_cpuset_free() */ |
| 320 |
|
| 321 |
MPI_Barrier(zoltan_get_global_comm()); |
| 322 |
MPI_Finalize(); |
| 323 |
|
| 324 |
return 0; |
| 325 |
} |
| 326 |
#endif |