| 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 |
* Print out topology and which MPI processes are where. |
| 14 |
* Printing out the MPI ranks may not be possible |
| 15 |
* if hwloc_get_cpubind() doesn't work or if it |
| 16 |
* works but gives us a cpuset containing more than one cpu. |
| 17 |
* |
| 18 |
* Here we are assuming homogeneous topologies. At any level, |
| 19 |
* each child of that level has identical topology. |
| 20 |
* |
| 21 |
* When running this code make sure the path to the hwloc shared library |
| 22 |
* is in your environment. |
| 23 |
*/ |
| 24 |
|
| 25 |
#include <hwloc.h> |
| 26 |
#include <stdlib.h> |
| 27 |
#include <stdio.h> |
| 28 |
#include <string.h> |
| 29 |
#include <inttypes.h> |
| 30 |
#include <mpi.h> |
| 31 |
|
| 32 |
#define MEMORY_ERROR(buf) { \ |
| 33 |
if (buf == NULL){ \ |
| 34 |
fprintf(stderr,"Memory error\n"); \ |
| 35 |
exit(1); \ |
| 36 |
} \ |
| 37 |
} |
| 38 |
|
| 39 |
#define MAX_NAME_LEN 64 /* what should this be? */ |
| 40 |
#define MAX_NUM_LEVELS 12 /* ditto */ |
| 41 |
|
| 42 |
static char *which_mpi(); |
| 43 |
|
| 44 |
int main(int argc, char *argv[]) |
| 45 |
{ |
| 46 |
int have_my_cpu=1; /* We know the one cpu that each MPI process is on */ |
| 47 |
char *recvbuf=NULL; |
| 48 |
char type_name[MAX_NAME_LEN]; /* "machine", "node", "socket", ... */ |
| 49 |
char mask[MAX_NAME_LEN]; |
| 50 |
hwloc_topology_t topology; |
| 51 |
const struct hwloc_topology_support *support=NULL; |
| 52 |
hwloc_obj_t obj; |
| 53 |
hwloc_cpuset_t *cpuset=NULL; |
| 54 |
hwloc_cpuset_t binding; |
| 55 |
uint64_t local_memory; |
| 56 |
uint64_t total_memory; |
| 57 |
int size, rank, depth; |
| 58 |
int i, j, p; |
| 59 |
int rc, num; |
| 60 |
|
| 61 |
MPI_Init(&argc, &argv); |
| 62 |
MPI_Comm_size(zoltan_get_global_comm(), &size); |
| 63 |
MPI_Comm_rank(zoltan_get_global_comm(), &rank); |
| 64 |
|
| 65 |
/* allocate & initialize topology object */ |
| 66 |
|
| 67 |
hwloc_topology_init(&topology); |
| 68 |
|
| 69 |
#if 0 |
| 70 |
/* configure detection - skip levels with only one child or which are only children */ |
| 71 |
hwloc_topology_ignore_all_keep_structure(topology); |
| 72 |
#endif |
| 73 |
|
| 74 |
/* detect topology */ |
| 75 |
|
| 76 |
hwloc_topology_load(topology); |
| 77 |
|
| 78 |
/* capabilities of hwloc on this machine */ |
| 79 |
|
| 80 |
support = hwloc_topology_get_support((struct hwloc_topology *)topology); |
| 81 |
|
| 82 |
/* |
| 83 |
* Get the depth of topology and the root. |
| 84 |
* The root typically is a node on a multi-node machine, not the collection of nodes |
| 85 |
* in the application. |
| 86 |
*/ |
| 87 |
|
| 88 |
depth = hwloc_topology_get_depth(topology); |
| 89 |
|
| 90 |
/* |
| 91 |
* Which cpu am I running on? |
| 92 |
* |
| 93 |
* HWLOC_CPUBIND_STRICT - says assume each process is running on one processor and won't be moved |
| 94 |
* (may ensure we get a cpuset containing no more than one cpu - doesn't work on glory) |
| 95 |
*/ |
| 96 |
|
| 97 |
binding = hwloc_cpuset_alloc(); |
| 98 |
hwloc_cpuset_zero(binding); |
| 99 |
|
| 100 |
if (support->cpubind->get_thisproc_cpubind != 1){ |
| 101 |
have_my_cpu = 0; |
| 102 |
} |
| 103 |
else{ |
| 104 |
rc = hwloc_get_cpubind(topology, binding, HWLOC_CPUBIND_STRICT); |
| 105 |
|
| 106 |
if ((rc < 0) || (hwloc_cpuset_weight(binding) > 1)){ |
| 107 |
have_my_cpu = 0; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
MPI_Barrier(zoltan_get_global_comm()); |
| 112 |
|
| 113 |
if (!have_my_cpu && (rank == 0)){ |
| 114 |
printf("Warning: Unable to identify each MPI process with its CPU\n"); |
| 115 |
} |
| 116 |
|
| 117 |
if (rank == 0){ |
| 118 |
recvbuf = (char *)calloc(size , MAX_NAME_LEN); |
| 119 |
} |
| 120 |
else{ |
| 121 |
recvbuf = NULL; |
| 122 |
} |
| 123 |
|
| 124 |
if (have_my_cpu){ |
| 125 |
|
| 126 |
hwloc_cpuset_snprintf(mask, MAX_NAME_LEN-1, binding); |
| 127 |
MPI_Gather(mask, MAX_NAME_LEN, MPI_CHAR, recvbuf, MAX_NAME_LEN, MPI_CHAR, 0, zoltan_get_global_comm()); |
| 128 |
|
| 129 |
if (rank == 0){ |
| 130 |
cpuset = (hwloc_cpuset_t*)malloc(sizeof(hwloc_cpuset_t) * size); |
| 131 |
|
| 132 |
for (p=0; p < size; p++){ |
| 133 |
cpuset[p] = hwloc_cpuset_alloc(); |
| 134 |
hwloc_cpuset_from_string(cpuset[p], recvbuf + (p * MAX_NAME_LEN)); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
} |
| 139 |
|
| 140 |
if (rank == 0){ |
| 141 |
|
| 142 |
/* Get topology information. |
| 143 |
* Which of the MPI processes are each socket, cache, etc? |
| 144 |
*/ |
| 145 |
|
| 146 |
for (i=0; i < depth; i++){ |
| 147 |
|
| 148 |
num = hwloc_get_nbobjs_by_depth(topology, i); |
| 149 |
|
| 150 |
for (j = 0; j < num; j++){ |
| 151 |
|
| 152 |
obj = hwloc_get_obj_by_depth(topology, i, j); |
| 153 |
|
| 154 |
if (j==0){ |
| 155 |
hwloc_obj_type_snprintf(type_name, MAX_NAME_LEN-1, obj, 1); |
| 156 |
printf("\n%d %s%s:\n",num,type_name, ((num> 1) ? "s" : "")); |
| 157 |
} |
| 158 |
|
| 159 |
hwloc_cpuset_snprintf(mask, MAX_NAME_LEN - 1, obj->cpuset); |
| 160 |
local_memory = obj->memory.local_memory; |
| 161 |
total_memory= obj->memory.total_memory; |
| 162 |
|
| 163 |
printf(" cpu set mask %s, total memory %10.0fKB, local memory %10.0fKB", |
| 164 |
mask, (float)total_memory/1024.0, (float)local_memory/1024.0); |
| 165 |
|
| 166 |
if (have_my_cpu){ |
| 167 |
printf(", ( MPI ranks "); |
| 168 |
for (p=0; p < size; p++){ |
| 169 |
if (hwloc_cpuset_isincluded(cpuset[p], obj->cpuset)){ |
| 170 |
printf("%d ",p); |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
printf(")\n"); |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
if (cpuset){ |
| 179 |
for (p=0; p < size; p++){ |
| 180 |
hwloc_cpuset_free(cpuset[p]); |
| 181 |
} |
| 182 |
free(cpuset); |
| 183 |
} |
| 184 |
free(recvbuf); |
| 185 |
printf("\n%s\n",which_mpi()); |
| 186 |
} |
| 187 |
|
| 188 |
hwloc_cpuset_free(binding); |
| 189 |
|
| 190 |
MPI_Finalize(); |
| 191 |
|
| 192 |
return 0; |
| 193 |
} |
| 194 |
|
| 195 |
/* Curious - do different mpi implementations lay out processes differently |
| 196 |
* on tiered multicore architectures? |
| 197 |
*/ |
| 198 |
static char *which_mpi() |
| 199 |
{ |
| 200 |
char *info=NULL; |
| 201 |
|
| 202 |
info = (char *)calloc(64, 1); |
| 203 |
|
| 204 |
#ifdef OMPI_MAJOR_VERSION |
| 205 |
sprintf(info,"Open MPI %d %d %d", OMPI_MAJOR_VERSION, OMPI_MINOR_VERSION, OMPI_RELEASE_VERSION); |
| 206 |
#endif |
| 207 |
|
| 208 |
#ifdef LAM_MAJOR_VERSION |
| 209 |
sprintf(info,"LAM/MPI %d %d %d", LAM_MAJOR_VERSION, LAM_MINOR_VERSION, LAM_RELEASE_VERSION); |
| 210 |
#endif |
| 211 |
|
| 212 |
#ifdef MPICH2_VERSION |
| 213 |
sprintf(info,"MVAPICH2 or MPICH2 %s", MPICH2_VERSION); |
| 214 |
#endif |
| 215 |
|
| 216 |
#ifdef MPICH_VERSION |
| 217 |
sprintf(info,"MVAPICH or MPICH %s", MPICH_VERSION); |
| 218 |
#endif |
| 219 |
|
| 220 |
if (info[0] == 0){ |
| 221 |
sprintf(info,"unidentified"); |
| 222 |
} |
| 223 |
|
| 224 |
return info; |
| 225 |
} |
| 226 |
|