| 1 |
/* |
| 2 |
* This file is part of MUSIC. |
| 3 |
* Copyright (C) 2007, 2008, 2009, 2017, 2022 INCF |
| 4 |
* |
| 5 |
* MUSIC is free software; you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation; either version 3 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* MUSIC is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <string> |
| 20 |
#include <fstream> |
| 21 |
|
| 22 |
#include "config.h" |
| 23 |
|
| 24 |
#include <music/version.hh> |
| 25 |
|
| 26 |
#include "music/application_mapper.hh" |
| 27 |
#include "mpidep/mpidep.hh" |
| 28 |
|
| 29 |
extern "C" { |
| 30 |
#include <sys/types.h> |
| 31 |
#include <unistd.h> |
| 32 |
#include <getopt.h> |
| 33 |
#include <stdlib.h> |
| 34 |
#include <stdio.h> |
| 35 |
} |
| 36 |
using std::string; |
| 37 |
|
| 38 |
/* The preprocessor macro MUSIC_HANDLE_MAP is currently used to disable |
| 39 |
* broken functionality which previously tried to predict which ranks |
| 40 |
* ran which applications. Such prediction can only be safely made using |
| 41 |
* MPI. This could be implemented at a later date. |
| 42 |
*/ |
| 43 |
|
| 44 |
void |
| 45 |
usage (int rank) |
| 46 |
{ |
| 47 |
if (rank <= 0) |
| 48 |
{ |
| 49 |
std::cout << "Usage: mpirun ... music [OPTION...] CONFIG" << std::endl |
| 50 |
<< "`music' launches an application as part of a multi-simulation job." << std::endl << std::endl |
| 51 |
<< " -h, --help print this help message" << std::endl |
| 52 |
#ifdef MUSIC_HANDLE_MAP |
| 53 |
<< " -m, --map print application rank map" << std::endl |
| 54 |
// << " -f, --file-map creates a file with the list of environment variable of each application" << std::endl |
| 55 |
// << " -e, --export-scripts export launcher scripts" << std::endl |
| 56 |
#endif |
| 57 |
<< " -v, --version prints version of MUSIC library" << std::endl |
| 58 |
<< std::endl |
| 59 |
<< "Report bugs to <[email protected]>." << std::endl; |
| 60 |
} |
| 61 |
exit (0); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
void |
| 66 |
print_map (MUSIC::Configuration* config) |
| 67 |
{ |
| 68 |
MUSIC::ApplicationMap* a = config->applications (); |
| 69 |
std::cout << "rank\tapplication" << std::endl; |
| 70 |
for (MUSIC::ApplicationMap::iterator i = a->begin (); i != a->end (); ++i) |
| 71 |
{ |
| 72 |
int first = i->leader (); |
| 73 |
int nProc = i->nProc (); |
| 74 |
std::cout << first; |
| 75 |
if (nProc > 1) |
| 76 |
std::cout << "-" << first + nProc - 1; |
| 77 |
std::cout << '\t' << i->name () << std::endl; |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
/*const static char* const configEnvVarName = "_MUSIC_CONFIG_"; |
| 82 |
const static char* const mapFName = "music.map"; |
| 83 |
void |
| 84 |
export_scripts (MUSIC::ApplicationMapper* map) |
| 85 |
{ |
| 86 |
MUSIC::Configuration* config = map->config (); |
| 87 |
MUSIC::ApplicationMap* a = config->applications (); |
| 88 |
for (MUSIC::ApplicationMap::iterator i = a->begin (); i != a->end (); ++i) |
| 89 |
{ |
| 90 |
std::string name = i->name (); |
| 91 |
std::string fname = name + ".sh"; |
| 92 |
std::ofstream script (fname.c_str ()); |
| 93 |
script << "#!/bin/sh" << std::endl << std::endl; |
| 94 |
map->mapConnectivity (name); |
| 95 |
MUSIC::Configuration* config = map->config (i->color()); |
| 96 |
config->writeEnv (); |
| 97 |
script << "export " << configEnvVarName |
| 98 |
<< "=\"" << getenv (configEnvVarName) |
| 99 |
<< '"' << std::endl; |
| 100 |
std::string binary; |
| 101 |
config->lookup ("binary", &binary); |
| 102 |
std::string args; |
| 103 |
config->lookup ("args", &args); |
| 104 |
script << binary << ' ' << args << std::endl; |
| 105 |
script.close (); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
void export_map(MUSIC::ApplicationMapper* map) |
| 110 |
{ |
| 111 |
MUSIC::Configuration* config = map->config (); |
| 112 |
MUSIC::ApplicationMap* a = config->applications (); |
| 113 |
std::ofstream map_file (mapFName); |
| 114 |
for (MUSIC::ApplicationMap::iterator i = a->begin (); i != a->end (); ++i) |
| 115 |
{ |
| 116 |
map->mapConnectivity (i->name()); |
| 117 |
MUSIC::Configuration* config = map->config (i->color()); |
| 118 |
config->writeEnv (); |
| 119 |
map_file << getenv (configEnvVarName) << std::endl; |
| 120 |
} |
| 121 |
map_file.close (); |
| 122 |
}*/ |
| 123 |
|
| 124 |
|
| 125 |
void |
| 126 |
print_version (int rank) |
| 127 |
{ |
| 128 |
if (rank <= 0) |
| 129 |
{ |
| 130 |
std::cout << "MUSIC " << MUSIC::version () << std::endl |
| 131 |
<< "Copyright (C) 2007-2012 INCF." << std::endl |
| 132 |
<< "You may redistribute copies of MUSIC" << std::endl |
| 133 |
<< "under the terms of the GNU General Public License." << std::endl |
| 134 |
<< "For more information about these matters, see the file named COPYING." << std::endl; |
| 135 |
} |
| 136 |
exit (0); |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
void |
| 141 |
launch (MUSIC::Configuration* config, char** argv) |
| 142 |
{ |
| 143 |
string binary; |
| 144 |
config->lookup ("binary", &binary); |
| 145 |
config->writeEnv (); |
| 146 |
string wd; |
| 147 |
if (config->lookup ("wd", &wd)) |
| 148 |
{ |
| 149 |
if (chdir (wd.c_str ())) |
| 150 |
goto error_exit; |
| 151 |
} |
| 152 |
execvp (binary.c_str (), argv); |
| 153 |
|
| 154 |
error_exit: |
| 155 |
// if we get here, something is wrong |
| 156 |
std::cerr << "Error during launching of binary " << binary << std::endl; |
| 157 |
perror ("MUSIC"); |
| 158 |
exit (1); |
| 159 |
} |
| 160 |
|
| 161 |
/* remedius |
| 162 |
* option -f to the music launcher tool is added. This option causes the |
| 163 |
* music launcher tool to write a file that contains mapping information of the rank (rank range) |
| 164 |
* to the according launching environment variable. |
| 165 |
* This file can be used while launching multiple applications in MPMD mode. |
| 166 |
*/ |
| 167 |
int |
| 168 |
main (int argc, char *argv[]) |
| 169 |
{ |
| 170 |
// predict the rank MPI_Init will give us using |
| 171 |
// mpi implementation dependent code from ../mpidep |
| 172 |
int rank = -1; |
| 173 |
#if MUSIC_USE_MPI |
| 174 |
rank = getRank (argc, argv); |
| 175 |
#endif |
| 176 |
|
| 177 |
bool do_print_map = false; |
| 178 |
bool do_export_scripts = false; |
| 179 |
bool do_export_map = false; |
| 180 |
|
| 181 |
opterr = 0; // handle errors ourselves |
| 182 |
while (1) |
| 183 |
{ |
| 184 |
static struct option longOptions[] = |
| 185 |
{ |
| 186 |
{"help", no_argument, 0, 'h'}, |
| 187 |
#ifdef MUSIC_HANDLE_MAP |
| 188 |
{"map", required_argument, 0, 'm'}, |
| 189 |
// {"export-scripts", required_argument, 0, 'e'}, |
| 190 |
// {"file-map", required_argument, 0, 'f'}, |
| 191 |
#endif |
| 192 |
{"version", no_argument, 0, 'v'}, |
| 193 |
{0, 0, 0, 0} |
| 194 |
}; |
| 195 |
/* `getopt_long' stores the option index here. */ |
| 196 |
int option_index = 0; |
| 197 |
|
| 198 |
// the + below tells getopt_long not to reorder argv |
| 199 |
int c = getopt_long (argc, argv, "+hm:v", longOptions, &option_index); |
| 200 |
/* detect the end of the options */ |
| 201 |
if (c == -1) |
| 202 |
break; |
| 203 |
switch (c) |
| 204 |
{ |
| 205 |
case '?': |
| 206 |
break; // ignore unknown options |
| 207 |
case 'h': |
| 208 |
usage (rank); |
| 209 |
case 'm': |
| 210 |
do_print_map = true; |
| 211 |
continue; |
| 212 |
/* case 'e': |
| 213 |
do_export_scripts = true; |
| 214 |
continue; |
| 215 |
case 'f': |
| 216 |
do_export_map = true; |
| 217 |
continue;*/ |
| 218 |
case 'v': |
| 219 |
print_version (rank); |
| 220 |
|
| 221 |
default: |
| 222 |
abort (); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
// extract the configuration file name using |
| 227 |
// mpi implementation dependent code from ../mpidep |
| 228 |
std::istream* configFile = getConfig (rank, argc, argv); |
| 229 |
|
| 230 |
if (!*configFile) |
| 231 |
{ |
| 232 |
if (rank <= 0) |
| 233 |
std::cerr << "MUSIC: Couldn't open configuration file " |
| 234 |
<< argv[1] << std::endl; |
| 235 |
exit (1); |
| 236 |
} |
| 237 |
|
| 238 |
MUSIC::Configuration config; |
| 239 |
MUSIC::ApplicationMapper mapper (&config); |
| 240 |
if (rank >= 0) // Only try to map applications if rank prediction worked |
| 241 |
mapper.map (configFile, rank); |
| 242 |
|
| 243 |
#ifdef MUSIC_HANDLE_MAP |
| 244 |
if (do_print_map) |
| 245 |
{ |
| 246 |
if (rank <= 0) |
| 247 |
print_map (&config); |
| 248 |
} |
| 249 |
|
| 250 |
/* if (do_export_scripts) |
| 251 |
{ |
| 252 |
if (rank <= 0) |
| 253 |
export_scripts (&map); |
| 254 |
} |
| 255 |
if(do_export_map) |
| 256 |
{ |
| 257 |
if (rank <=0) |
| 258 |
export_map(&map); |
| 259 |
|
| 260 |
}*/ |
| 261 |
if (do_print_map) //|| do_export_scripts || do_export_map) |
| 262 |
return 0; |
| 263 |
#endif |
| 264 |
|
| 265 |
if (rank == -1) |
| 266 |
{ |
| 267 |
std::cerr << "MUSIC: Unable to determine process rank." << std::endl |
| 268 |
<< "MUSIC: Did you launch music using mpirun?" << std::endl |
| 269 |
<< "MUSIC: If so, check the comments about porting in README." << std::endl; |
| 270 |
exit (1); |
| 271 |
} |
| 272 |
|
| 273 |
launch (&config, argv); |
| 274 |
|
| 275 |
return 0; |
| 276 |
} |