|
| 1 | +/** |
| 2 | + * Copyright (c) 2010-2012 Los Alamos National Security, LLC. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This program was prepared by Los Alamos National Security, LLC at Los Alamos |
| 6 | + * National Laboratory (LANL) under contract No. DE-AC52-06NA25396 with the U.S. |
| 7 | + * Department of Energy (DOE). All rights in the program are reserved by the DOE |
| 8 | + * and Los Alamos National Security, LLC. Permission is granted to the public to |
| 9 | + * copy and use this software without charge, provided that this Notice and any |
| 10 | + * statement of authorship are reproduced on all copies. Neither the U.S. |
| 11 | + * Government nor LANS makes any warranty, express or implied, or assumes any |
| 12 | + * liability or responsibility for the use of this software. |
| 13 | + */ |
| 14 | + |
| 15 | +/** |
| 16 | + * @author Samuel K. Gutierrez |
| 17 | + */ |
| 18 | + |
| 19 | +/* ///////////////////////////////////////////////////////////////////////////// |
| 20 | +o CHANGE LOG |
| 21 | +
|
| 22 | +2010-11-05 Samuel K. Gutierrez |
| 23 | + * initial implementation. |
| 24 | +2010-11-08 Samuel K. Gutierrez |
| 25 | + * updated algorithm - last version didn't work. |
| 26 | +2011-04-22 Samuel K. Gutierrez |
| 27 | + * updated macros. |
| 28 | +2012-10-10 Samuel K. Gutierrez |
| 29 | + * updates for release. |
| 30 | +///////////////////////////////////////////////////////////////////////////// */ |
| 31 | + |
| 32 | +#include <stdlib.h> |
| 33 | +#include <stdio.h> |
| 34 | +#include <unistd.h> |
| 35 | +#include <stdint.h> |
| 36 | +#include <netdb.h> |
| 37 | +#include <arpa/inet.h> |
| 38 | +#include "mpi.h" |
| 39 | + |
| 40 | +/* application name */ |
| 41 | +#define SMPCOMM_NAME "mpi-smp-comm" |
| 42 | +/* current version */ |
| 43 | +#define SMPCOMM_VER "0.0.5" |
| 44 | + |
| 45 | +/* ////////////////////////////////////////////////////////////////////////// */ |
| 46 | +/* convenience macros */ |
| 47 | +/* ////////////////////////////////////////////////////////////////////////// */ |
| 48 | +/* "master" rank */ |
| 49 | +#define SMPCOMM_MASTER_RANK 0 |
| 50 | + |
| 51 | +#define SMPCOMM_FAILURE 0 |
| 52 | +#define SMPCOMM_SUCCESS 1 |
| 53 | + |
| 54 | +#define SMPCOMM_STRINGIFY(x) #x |
| 55 | +#define SMPCOMM_TOSTRING(x) SMPCOMM_STRINGIFY(x) |
| 56 | + |
| 57 | +#define SMPCOMM_ERR_AT __FILE__ " ("SMPCOMM_TOSTRING(__LINE__)")" |
| 58 | +#define SMPCOMM_ERR_PREFIX "-[SMPCOMM ERROR: "SMPCOMM_ERR_AT"]- " |
| 59 | + |
| 60 | +/* error message */ |
| 61 | +#define SMPCOMM_ERR_MSG(pfargs...) \ |
| 62 | +do { \ |
| 63 | + fprintf(stderr, SMPCOMM_ERR_PREFIX); \ |
| 64 | + fprintf(stderr, pfargs); \ |
| 65 | +} while (0) |
| 66 | + |
| 67 | +/* mpi check */ |
| 68 | +#define SMPCOMM_MPICHK(_ret_,_gt_) \ |
| 69 | +do { \ |
| 70 | + if (MPI_SUCCESS != (_ret_)) { \ |
| 71 | + MPI_Error_string((_ret_), \ |
| 72 | + err_str, \ |
| 73 | + &err_str_len); \ |
| 74 | + SMPCOMM_ERR_MSG("mpi success not returned on %s... %s (errno: %d)\n", \ |
| 75 | + host_name_buff, \ |
| 76 | + err_str, \ |
| 77 | + (_ret_)); \ |
| 78 | + goto _gt_; \ |
| 79 | + } \ |
| 80 | +} while (0) |
| 81 | + |
| 82 | +/* printf with flush */ |
| 83 | +#define SMPCOMM_PF(pfargs...) \ |
| 84 | +do { \ |
| 85 | + fprintf(stdout, pfargs); \ |
| 86 | + fflush(stdout); \ |
| 87 | +} while (0) |
| 88 | + |
| 89 | +/* master rank printf */ |
| 90 | +/* master rank printf */ |
| 91 | +#define SMPCOMM_MPF(pfargs...) \ |
| 92 | +do { \ |
| 93 | + if (my_rank == (SMPCOMM_MASTER_RANK)) { \ |
| 94 | + fprintf(stdout, pfargs); \ |
| 95 | + fflush(stdout); \ |
| 96 | + } \ |
| 97 | +} while (0) |
| 98 | + |
| 99 | +/* memory alloc check */ |
| 100 | +#define SMPCOMM_MEMCHK(_ptr_,_gt_) \ |
| 101 | +do { \ |
| 102 | + if (NULL == (_ptr_)) { \ |
| 103 | + SMPCOMM_ERR_MSG("memory allocation error on %s\n", host_name_buff); \ |
| 104 | + goto _gt_; \ |
| 105 | + } \ |
| 106 | +} while (0) |
| 107 | + |
| 108 | +/* error string length */ |
| 109 | +static int err_str_len; |
| 110 | +/* error string buffer */ |
| 111 | +static char err_str[MPI_MAX_ERROR_STRING]; |
| 112 | +/* host name buffer */ |
| 113 | +static char host_name_buff[MPI_MAX_PROCESSOR_NAME]; |
| 114 | + |
| 115 | +/* ////////////////////////////////////////////////////////////////////////// */ |
| 116 | +/* static forward declarations */ |
| 117 | +/* ////////////////////////////////////////////////////////////////////////// */ |
| 118 | + |
| 119 | +/* ////////////////////////////////////////////////////////////////////////// */ |
| 120 | +/* helper functions */ |
| 121 | +/* ////////////////////////////////////////////////////////////////////////// */ |
| 122 | + |
| 123 | +/* ////////////////////////////////////////////////////////////////////////// */ |
| 124 | +static int |
| 125 | +get_net_num(char *hstn, |
| 126 | + unsigned long int *net_num) |
| 127 | +{ |
| 128 | + struct hostent *host = NULL; |
| 129 | + |
| 130 | + if (NULL == (host = gethostbyname(hstn))) { |
| 131 | + SMPCOMM_ERR_MSG("gethostbyname error\n"); |
| 132 | + /* epic fail! */ |
| 133 | + goto err; |
| 134 | + } |
| 135 | + /* htonl used here because nodes could be different architectures */ |
| 136 | + *net_num = htonl(inet_network(inet_ntoa(*(struct in_addr *)host->h_addr))); |
| 137 | + |
| 138 | + return SMPCOMM_SUCCESS; |
| 139 | +err: |
| 140 | + return SMPCOMM_FAILURE; |
| 141 | +} |
| 142 | + |
| 143 | +/* ////////////////////////////////////////////////////////////////////////// */ |
| 144 | +static int |
| 145 | +cmp_uli(const void *p1, |
| 146 | + const void *p2) |
| 147 | +{ |
| 148 | + return (*(unsigned long int *)p1 - *(unsigned long int *)p2); |
| 149 | +} |
| 150 | + |
| 151 | +/* ////////////////////////////////////////////////////////////////////////// */ |
| 152 | +static void |
| 153 | +get_my_color(unsigned long int *net_nums /* in */, |
| 154 | + int net_num_len /* in */, |
| 155 | + const unsigned long int *my_net_num /* in */, |
| 156 | + int *my_color /* out */) |
| 157 | +{ |
| 158 | + int i = 0; |
| 159 | + int node_i = 0; |
| 160 | + unsigned long int prev_num; |
| 161 | + |
| 162 | + qsort(net_nums, (size_t)net_num_len, sizeof(unsigned long int), cmp_uli); |
| 163 | + |
| 164 | + prev_num = net_nums[0]; |
| 165 | + |
| 166 | + while (i < net_num_len && prev_num != *my_net_num) { |
| 167 | + while (net_nums[i] == prev_num) { |
| 168 | + ++i; |
| 169 | + } |
| 170 | + ++node_i; |
| 171 | + prev_num = net_nums[i]; |
| 172 | + } |
| 173 | + |
| 174 | + *my_color = node_i; |
| 175 | +} |
| 176 | + |
| 177 | +/* ////////////////////////////////////////////////////////////////////////// */ |
| 178 | +/* main */ |
| 179 | +/* ////////////////////////////////////////////////////////////////////////// */ |
| 180 | +int |
| 181 | +main(int argc, |
| 182 | + char **argv) |
| 183 | +{ |
| 184 | + /* pointer to array that holds net nums for all rank processes */ |
| 185 | + unsigned long int *net_nums = NULL; |
| 186 | + /* my color */ |
| 187 | + int my_color; |
| 188 | + /* my rank */ |
| 189 | + int my_rank; |
| 190 | + /* size of mpi_comm_world */ |
| 191 | + int num_ranks; |
| 192 | + /* hostname length */ |
| 193 | + int hostname_len; |
| 194 | + /* local rank communicator */ |
| 195 | + MPI_Comm local_comm; |
| 196 | + /* holds mpi return codes */ |
| 197 | + int mpi_ret_code; |
| 198 | + /* number of local ranks */ |
| 199 | + int num_local_procs; |
| 200 | + /* my local rank number */ |
| 201 | + int my_local_rank; |
| 202 | + unsigned long int net_num; |
| 203 | + |
| 204 | + /* init MPI */ |
| 205 | + mpi_ret_code = MPI_Init(&argc, &argv); |
| 206 | + SMPCOMM_MPICHK(mpi_ret_code, error); |
| 207 | + /* get comm size */ |
| 208 | + mpi_ret_code = MPI_Comm_size(MPI_COMM_WORLD, &num_ranks); |
| 209 | + SMPCOMM_MPICHK(mpi_ret_code, error); |
| 210 | + /* get my rank */ |
| 211 | + mpi_ret_code = MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); |
| 212 | + SMPCOMM_MPICHK(mpi_ret_code, error); |
| 213 | + /* get my host's name */ |
| 214 | + mpi_ret_code = MPI_Get_processor_name(host_name_buff, &hostname_len); |
| 215 | + SMPCOMM_MPICHK(mpi_ret_code, error); |
| 216 | + |
| 217 | + /* get my network number */ |
| 218 | + if (SMPCOMM_SUCCESS != get_net_num(host_name_buff, &net_num)) { |
| 219 | + goto error; |
| 220 | + } |
| 221 | + |
| 222 | + net_nums = (unsigned long int *)malloc(sizeof(unsigned long int) * |
| 223 | + num_ranks); |
| 224 | + SMPCOMM_MEMCHK(net_nums, error); |
| 225 | + |
| 226 | + /* get everyone else's net_num value */ |
| 227 | + mpi_ret_code = MPI_Allgather(&net_num, |
| 228 | + 1, |
| 229 | + MPI_UNSIGNED_LONG, |
| 230 | + net_nums, |
| 231 | + 1, |
| 232 | + MPI_UNSIGNED_LONG, |
| 233 | + MPI_COMM_WORLD); |
| 234 | + SMPCOMM_MPICHK(mpi_ret_code, error); |
| 235 | + |
| 236 | + get_my_color(net_nums, num_ranks, &net_num, &my_color); |
| 237 | + |
| 238 | + /* free up some resources - no longer needed */ |
| 239 | + free(net_nums); |
| 240 | + |
| 241 | + /* split into local node groups */ |
| 242 | + mpi_ret_code = MPI_Comm_split(MPI_COMM_WORLD, |
| 243 | + my_color, |
| 244 | + my_rank, |
| 245 | + &local_comm); |
| 246 | + SMPCOMM_MPICHK(mpi_ret_code, error); |
| 247 | + |
| 248 | + /* get local comm size */ |
| 249 | + mpi_ret_code = MPI_Comm_size(local_comm, &num_local_procs); |
| 250 | + SMPCOMM_MPICHK(mpi_ret_code, error); |
| 251 | + |
| 252 | + /* get my local comm rank */ |
| 253 | + mpi_ret_code = MPI_Comm_rank(local_comm, &my_local_rank); |
| 254 | + SMPCOMM_MPICHK(mpi_ret_code, error); |
| 255 | + |
| 256 | + /* let the "master process" print out some header stuff */ |
| 257 | + SMPCOMM_MPF("# %s %s\n", SMPCOMM_NAME, SMPCOMM_VER); |
| 258 | + SMPCOMM_MPF("# numpe %d\n", num_ranks); |
| 259 | + |
| 260 | + /** |
| 261 | + * not needed... just used to make sure that the |
| 262 | + * header stuff is flushed before following info |
| 263 | + */ |
| 264 | + MPI_Barrier(MPI_COMM_WORLD); |
| 265 | + |
| 266 | + if (0 == my_local_rank) { |
| 267 | + SMPCOMM_PF("# host %s has %d local rank process%s\n", |
| 268 | + host_name_buff, |
| 269 | + num_local_procs, |
| 270 | + num_local_procs> 1 && 0 != num_local_procs ? "es ":" "); |
| 271 | + } |
| 272 | + |
| 273 | + mpi_ret_code = MPI_Comm_free(&local_comm); |
| 274 | + SMPCOMM_MPICHK(mpi_ret_code, error); |
| 275 | + |
| 276 | + /* done! */ |
| 277 | + mpi_ret_code = MPI_Finalize(); |
| 278 | + SMPCOMM_MPICHK(mpi_ret_code, error); |
| 279 | + |
| 280 | + return EXIT_SUCCESS; |
| 281 | +error: |
| 282 | + MPI_Abort(MPI_COMM_WORLD, mpi_ret_code); |
| 283 | + return EXIT_FAILURE; |
| 284 | +} |
0 commit comments