| 1 |
/* |
| 2 |
* This file is part of MUSIC. |
| 3 |
* Copyright (C) 2007, 2008, 2009, 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 |
#include "music/setup.hh" |
| 19 |
#if MUSIC_USE_MPI |
| 20 |
#include "music/mpi_utils.hh" |
| 21 |
#include "music/runtime.hh" |
| 22 |
#include "music/parse.hh" |
| 23 |
#include "music/error.hh" |
| 24 |
#include "music/application_mapper.hh" |
| 25 |
#include "music/ioutils.hh" |
| 26 |
#include <strings.h> |
| 27 |
#include <fstream> |
| 28 |
|
| 29 |
namespace MUSIC { |
| 30 |
|
| 31 |
bool Setup::isInstantiated_ = false; |
| 32 |
static std::string err_MPI_Init = "MPI_Init was called before the Setup constructor"; |
| 33 |
const char* const Setup::opConfigFileName = "--music-config"; |
| 34 |
const char* const Setup::opAppLabel = "--app-label"; |
| 35 |
|
| 36 |
Setup::Setup (int& argc, char**& argv) |
| 37 |
: argc_ (argc), argv_ (argv) |
| 38 |
{ |
| 39 |
checkInstantiatedOnce (isInstantiated_, "Setup"); |
| 40 |
if (mpi_is_initialized ()) |
| 41 |
errorRank (err_MPI_Init); |
| 42 |
maybeProcessMusicArgv (argc, argv); |
| 43 |
MPI_Init (&argc, &argv); |
| 44 |
|
| 45 |
init (argc, argv); |
| 46 |
|
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
Setup::Setup (int& argc, char**& argv, int required, int* provided) |
| 51 |
: argc_ (argc), argv_ (argv) |
| 52 |
{ |
| 53 |
checkInstantiatedOnce (isInstantiated_, "Setup"); |
| 54 |
if (mpi_is_initialized ()) |
| 55 |
errorRank (err_MPI_Init); |
| 56 |
maybeProcessMusicArgv (argc, argv); |
| 57 |
MPI_Init_thread (&argc, &argv, required, provided); |
| 58 |
init (argc, argv); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
Setup::~Setup () |
| 63 |
{ |
| 64 |
for (std::vector<Port*>::iterator i = ports_.begin (); |
| 65 |
i != ports_.end (); |
| 66 |
++i) |
| 67 |
(*i)->setupCleanup (); |
| 68 |
|
| 69 |
if (launchedByMusic ()) |
| 70 |
delete temporalNegotiator_; |
| 71 |
|
| 72 |
// delete connection objects |
| 73 |
for (std::vector<Connection*>::iterator i = connections_->begin (); |
| 74 |
i != connections_->end (); |
| 75 |
++i) |
| 76 |
delete *i; |
| 77 |
|
| 78 |
delete connections_; |
| 79 |
|
| 80 |
delete config_; |
| 81 |
|
| 82 |
isInstantiated_ = false; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
void |
| 87 |
Setup::init (int& argc, char**& argv) |
| 88 |
{ |
| 89 |
int myRank = mpi_get_rank (MPI_COMM_WORLD); |
| 90 |
std::string config = ""; |
| 91 |
launchedByMusic_ = false; |
| 92 |
postponeSetup_ = false; |
| 93 |
|
| 94 |
if (launchedWithExec (config)) |
| 95 |
{ |
| 96 |
assert(config.length() > 0); |
| 97 |
launchedByMusic_ = true; |
| 98 |
if (!config.compare (0, 8, "POSTPONE")) |
| 99 |
{ |
| 100 |
postponeSetup_ = true; |
| 101 |
// *fixme* Error checking |
| 102 |
std::istringstream in (config); |
| 103 |
IOUtils::read (in); // POSTPONE |
| 104 |
in.ignore (); // delim |
| 105 |
std::string colorString = IOUtils::read (in); |
| 106 |
color_ = atoi (colorString.c_str ()); |
| 107 |
} |
| 108 |
else |
| 109 |
config_ = new Configuration (config); |
| 110 |
} |
| 111 |
else if (launchedMPMD (argc, argv, config)) |
| 112 |
{ |
| 113 |
launchedByMusic_ = true; |
| 114 |
std::string config_file; |
| 115 |
loadConfigFile (config, config_file); |
| 116 |
|
| 117 |
std::string app_label; |
| 118 |
std::string binary (argv[0]); |
| 119 |
// argv[0] is the name of the program, |
| 120 |
// or an empty string if the name is not available |
| 121 |
if (!getOption (argc, argv, opAppLabel, app_label) |
| 122 |
&& binary.length () == 0) |
| 123 |
{ |
| 124 |
std::ostringstream oss; |
| 125 |
oss << "MUSIC: use --app-label to specify application label"; |
| 126 |
error0 (oss.str ()); |
| 127 |
} |
| 128 |
|
| 129 |
std::istringstream config_istream (config_file); |
| 130 |
config_ = new Configuration (); |
| 131 |
ApplicationMapper app_mapper(config_); |
| 132 |
app_mapper.map(&config_istream, binary, app_label); |
| 133 |
} |
| 134 |
else |
| 135 |
config_ = new Configuration (); |
| 136 |
|
| 137 |
|
| 138 |
connections_ = new std::vector<Connection*>; // destroyed by runtime |
| 139 |
if (launchedByMusic ()) |
| 140 |
{ |
| 141 |
// launched by the music utility |
| 142 |
if (!postponeSetup_) |
| 143 |
{ |
| 144 |
fullInit (); |
| 145 |
argc = argc_; |
| 146 |
argv = argv_; |
| 147 |
} |
| 148 |
MPI_Comm_split (MPI_COMM_WORLD, postponeSetup_ ? color_ : config_->Color (), myRank, &comm); |
| 149 |
} |
| 150 |
else |
| 151 |
{ |
| 152 |
// launched with mpirun |
| 153 |
comm = MPI_COMM_WORLD; |
| 154 |
timebase_ = MUSIC_DEFAULT_TIMEBASE; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
bool |
| 160 |
Setup::getOption (int argc, char** argv, std::string option, std::string& result) |
| 161 |
{ |
| 162 |
result.assign(""); |
| 163 |
for (int i = 1; i < argc; ++i) |
| 164 |
if (option.compare(argv[i]) == 0 && argc > i){ |
| 165 |
result.assign(argv[i+1]); // skip options |
| 166 |
return true; |
| 167 |
} |
| 168 |
return false; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
bool |
| 173 |
Setup::launchedWithExec (std::string &result) |
| 174 |
{ |
| 175 |
// is _MUSIC_CONFIG_ env variable is set ? |
| 176 |
char* res = getenv (Configuration::configEnvVarName); |
| 177 |
if (res != NULL) |
| 178 |
{ |
| 179 |
result.assign (res); |
| 180 |
return true; |
| 181 |
} |
| 182 |
else |
| 183 |
return false; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
bool |
| 188 |
Setup::launchedMPMD (int argc, char** argv, std::string& config) |
| 189 |
{ |
| 190 |
// if given option --music-config, |
| 191 |
// the launch is categorized as MPMD |
| 192 |
if (!getOption(argc, argv, opConfigFileName, config) ) |
| 193 |
return false; |
| 194 |
else |
| 195 |
return true; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
void |
| 200 |
Setup::loadConfigFile (std::string filename, std::string &result) |
| 201 |
{ |
| 202 |
std::ifstream config; |
| 203 |
char* buffer; |
| 204 |
int size = 0; |
| 205 |
int myRank = mpi_get_rank (MPI_COMM_WORLD); |
| 206 |
// Rank #0 is reading a file and broadcast it to each rank in the launch |
| 207 |
if (myRank == 0) |
| 208 |
{ |
| 209 |
config.open (filename.c_str ()); |
| 210 |
if (!config.is_open ()) |
| 211 |
{ |
| 212 |
std::ostringstream oss; |
| 213 |
oss << "MUSIC: Couldn't open configuration file: " << filename << '\n'; |
| 214 |
error0 (oss.str ()); |
| 215 |
} |
| 216 |
|
| 217 |
size = config.tellg (); |
| 218 |
config.seekg (0, std::ios_base::end); |
| 219 |
long cur_pos = config.tellg (); |
| 220 |
size = cur_pos - size; |
| 221 |
config.seekg (0, std::ios_base::beg); |
| 222 |
} |
| 223 |
// first broadcast the size of the file |
| 224 |
MPI_Bcast (&size, 1, MPI_INT, 0, MPI_COMM_WORLD); |
| 225 |
buffer = new char[size]; |
| 226 |
|
| 227 |
if (myRank == 0) |
| 228 |
config.read (buffer, size); |
| 229 |
// then broadcast the file but itself |
| 230 |
MPI_Bcast (buffer, size, MPI_BYTE, 0, MPI_COMM_WORLD); |
| 231 |
// parseMapFile (app_name, std::string (buffer, size), result); |
| 232 |
if (myRank == 0) |
| 233 |
config.close (); |
| 234 |
|
| 235 |
result.assign (buffer); |
| 236 |
delete[] buffer; |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
void |
| 241 |
Setup::maybeProcessMusicArgv (int& argc, char**& argv) |
| 242 |
{ |
| 243 |
char* MUSIC_ARGV = getenv ("MUSIC_ARGV"); |
| 244 |
if (MUSIC_ARGV != NULL) |
| 245 |
{ |
| 246 |
std::string cmd; |
| 247 |
std::string argstring; |
| 248 |
char* s = index (MUSIC_ARGV, ' '); |
| 249 |
if (s == NULL) |
| 250 |
{ |
| 251 |
cmd = std::string (MUSIC_ARGV); |
| 252 |
argstring = ""; |
| 253 |
} |
| 254 |
else |
| 255 |
{ |
| 256 |
cmd = std::string (MUSIC_ARGV, s - MUSIC_ARGV); |
| 257 |
argstring = std::string (s + 1); |
| 258 |
} |
| 259 |
argv = parseArgs (cmd, argstring, &argc); |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
void |
| 265 |
Setup::maybePostponedSetup () |
| 266 |
{ |
| 267 |
if (postponeSetup_) |
| 268 |
{ |
| 269 |
postponeSetup_ = false; |
| 270 |
std::string config = ""; |
| 271 |
launchedWithExec (config); |
| 272 |
config_ = new Configuration (config); |
| 273 |
fullInit (); |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
void |
| 279 |
Setup::errorChecks () |
| 280 |
{ |
| 281 |
ApplicationMap* apps = applicationMap (); |
| 282 |
int nRequestedProc = apps->nProcesses (); |
| 283 |
int nMPIProc = mpi_get_comm_size (MPI_COMM_WORLD); |
| 284 |
if (nMPIProc != nRequestedProc) |
| 285 |
{ |
| 286 |
std::ostringstream msg; |
| 287 |
msg << "configuration file specifies " << nRequestedProc |
| 288 |
<< " MPI processes but MUSIC was given " << nMPIProc |
| 289 |
<< std::endl; |
| 290 |
error0 (msg.str ()); |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
void |
| 296 |
Setup::fullInit () |
| 297 |
{ |
| 298 |
errorChecks (); |
| 299 |
if (!config ("timebase", &timebase_)) |
| 300 |
timebase_ = MUSIC_DEFAULT_TIMEBASE; // default timebase |
| 301 |
string binary; |
| 302 |
config_->lookup ("binary", &binary); |
| 303 |
string args; |
| 304 |
config_->lookup ("args", &args); |
| 305 |
argv_ = parseArgs (binary, args, &argc_); |
| 306 |
temporalNegotiator_ = new TemporalNegotiator (this); |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
bool |
| 311 |
Setup::launchedByMusic () |
| 312 |
{ |
| 313 |
return launchedByMusic_; |
| 314 |
} |
| 315 |
|
| 316 |
|
| 317 |
MPI_Comm |
| 318 |
Setup::communicator () |
| 319 |
{ |
| 320 |
return comm; |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
ConnectivityInfo* |
| 325 |
Setup::portConnectivity (const std::string localName) |
| 326 |
{ |
| 327 |
return config_->connectivityMap ()->info (localName); |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
ApplicationMap* |
| 332 |
Setup::applicationMap () |
| 333 |
{ |
| 334 |
return config_->applications (); |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
int |
| 339 |
Setup::applicationColor () |
| 340 |
{ |
| 341 |
return config_->Color(); |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
std::string |
| 346 |
Setup::applicationName() |
| 347 |
{ |
| 348 |
return config_->Name(); |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
int |
| 353 |
Setup::leader () |
| 354 |
{ |
| 355 |
return config_->Leader (); |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
int |
| 360 |
Setup::nProcs () |
| 361 |
{ |
| 362 |
return mpi_get_comm_size (comm); |
| 363 |
} |
| 364 |
|
| 365 |
|
| 366 |
ConnectivityInfo::PortDirection |
| 367 |
Setup::portDirection (const std::string localName) |
| 368 |
{ |
| 369 |
return config_->connectivityMap ()->direction (localName); |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
int |
| 374 |
Setup::portWidth (const std::string localName) |
| 375 |
{ |
| 376 |
return config_->connectivityMap ()->width (localName); |
| 377 |
} |
| 378 |
|
| 379 |
|
| 380 |
PortConnectorInfo |
| 381 |
Setup::portConnections (const std::string localName) |
| 382 |
{ |
| 383 |
return config_->connectivityMap ()->connections (localName); |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
bool |
| 388 |
Setup::config (string var, string* result) |
| 389 |
{ |
| 390 |
return config_->lookup (var, result); |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
bool |
| 395 |
Setup::config (string var, int* result) |
| 396 |
{ |
| 397 |
return config_->lookup (var, result); |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
bool |
| 402 |
Setup::config (string var, double* result) |
| 403 |
{ |
| 404 |
return config_->lookup (var, result); |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
bool |
| 409 |
Setup::config (string var, bool* result) |
| 410 |
{ |
| 411 |
return config_->lookup (var, result); |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
ContInputPort* |
| 416 |
Setup::publishContInput (std::string identifier) |
| 417 |
{ |
| 418 |
return new ContInputPort (this, identifier); |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
ContOutputPort* |
| 423 |
Setup::publishContOutput (std::string identifier) |
| 424 |
{ |
| 425 |
return new ContOutputPort (this, identifier); |
| 426 |
} |
| 427 |
|
| 428 |
|
| 429 |
EventInputPort* |
| 430 |
Setup::publishEventInput (std::string identifier) |
| 431 |
{ |
| 432 |
return new EventInputPort (this, identifier); |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
EventOutputPort* |
| 437 |
Setup::publishEventOutput (std::string identifier) |
| 438 |
{ |
| 439 |
return new EventOutputPort (this, identifier); |
| 440 |
} |
| 441 |
|
| 442 |
|
| 443 |
MessageInputPort* |
| 444 |
Setup::publishMessageInput (std::string identifier) |
| 445 |
{ |
| 446 |
return new MessageInputPort (this, identifier); |
| 447 |
} |
| 448 |
|
| 449 |
|
| 450 |
MessageOutputPort* |
| 451 |
Setup::publishMessageOutput (std::string identifier) |
| 452 |
{ |
| 453 |
return new MessageOutputPort (this, identifier); |
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
void Setup::addPort (Port* p) |
| 458 |
{ |
| 459 |
ports_.push_back (p); |
| 460 |
} |
| 461 |
|
| 462 |
|
| 463 |
void Setup::addConnection (Connection* c) |
| 464 |
{ |
| 465 |
connections_->push_back (c); |
| 466 |
} |
| 467 |
|
| 468 |
} |
| 469 |
#endif |