| 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 |
|
| 19 |
#include "music/music-config.hh" |
| 20 |
|
| 21 |
#include "music/debug.hh" |
| 22 |
|
| 23 |
#include "music/error.hh" |
| 24 |
#include <iostream> |
| 25 |
#include <fstream> |
| 26 |
#include <stdlib.h> |
| 27 |
#include <string> |
| 28 |
#include <algorithm> |
| 29 |
#include <assert.h> |
| 30 |
#include "music/application_mapper.hh" |
| 31 |
|
| 32 |
#include "rudeconfig/src/config.h" |
| 33 |
|
| 34 |
#if MUSIC_USE_MPI |
| 35 |
#include "music/mpi_utils.hh" |
| 36 |
#endif |
| 37 |
|
| 38 |
#if HAVE_SYS_STAT_H |
| 39 |
#include <sys/stat.h> |
| 40 |
#endif |
| 41 |
|
| 42 |
namespace MUSIC { |
| 43 |
|
| 44 |
|
| 45 |
// This is where we parse the configuration file |
| 46 |
// NOTE: Could check here that obligatory parameters exists |
| 47 |
|
| 48 |
ApplicationMapper::ApplicationMapper (Configuration *config) |
| 49 |
{ |
| 50 |
config_ = config; |
| 51 |
applications_ = config->applications(); |
| 52 |
connectivityMap_ = config->connectivityMap(); |
| 53 |
cfile_ = new rude::Config (); |
| 54 |
app_selector_ = NULL; |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
ApplicationMapper::~ApplicationMapper() |
| 59 |
{ |
| 60 |
if (app_selector_ != NULL) |
| 61 |
delete app_selector_; |
| 62 |
delete cfile_; |
| 63 |
} |
| 64 |
|
| 65 |
|
| 66 |
Configuration* |
| 67 |
ApplicationMapper::map (std::istream* configFile, int rank) |
| 68 |
{ |
| 69 |
MUSIC_LOG("Co-simulation launched using music utility"); |
| 70 |
app_selector_ = new SelectorR (rank); |
| 71 |
map (configFile); |
| 72 |
return config_; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
#if MUSIC_USE_MPI |
| 77 |
Configuration* |
| 78 |
ApplicationMapper::map (std::istream* configFile, std::string binary, |
| 79 |
std::string appLabel) |
| 80 |
{ |
| 81 |
|
| 82 |
//how do we identify ourselves |
| 83 |
if (appLabel.length () != 0) |
| 84 |
{ |
| 85 |
MUSIC_LOG("MPMD launch with --app-label option:" << appLabel); |
| 86 |
app_selector_ = new SelectorOp (appLabel); |
| 87 |
} |
| 88 |
else |
| 89 |
{ |
| 90 |
int nRanks = mpi_get_comm_size (MPI_COMM_WORLD); |
| 91 |
int my_rank = mpi_get_rank (MPI_COMM_WORLD); |
| 92 |
|
| 93 |
#if HAVE_SYS_STAT_H |
| 94 |
|
| 95 |
long my_ino = ApplicationMapper::getApplicationINO (binary.c_str ()); |
| 96 |
// communicate st.st_ino |
| 97 |
long *inos = new long[nRanks]; |
| 98 |
inos[my_rank] = my_ino; |
| 99 |
MPI_Allgather (MPI_IN_PLACE, 0, MPI_LONG, inos, 1, |
| 100 |
MPI_LONG, MPI_COMM_WORLD); |
| 101 |
|
| 102 |
int index = 0; |
| 103 |
int i = 0; |
| 104 |
// among processors with the same ino number |
| 105 |
// assign current sequential order number. |
| 106 |
while (i < my_rank) |
| 107 |
if (inos[i++] == my_ino) |
| 108 |
index++; |
| 109 |
delete[] inos; |
| 110 |
|
| 111 |
MUSIC_LOG("MPMD launch. Application INO:" << my_ino); |
| 112 |
app_selector_ = new SelectorINO (my_ino, index); |
| 113 |
#else |
| 114 |
std::ostringstream oss; |
| 115 |
oss << "MUSIC: HAVE_SYS_STAT_H is node defined, use --app-label to specify application label"; |
| 116 |
error0 (oss.str ()); |
| 117 |
#endif |
| 118 |
|
| 119 |
} |
| 120 |
map (configFile); |
| 121 |
return config_; |
| 122 |
} |
| 123 |
#endif |
| 124 |
|
| 125 |
void |
| 126 |
ApplicationMapper::map (std::istream* configFile) |
| 127 |
{ |
| 128 |
|
| 129 |
if (cfile_->load (*configFile)) |
| 130 |
{ |
| 131 |
bool leader_avail = false; |
| 132 |
mapApplications (); |
| 133 |
#if MUSIC_USE_MPI |
| 134 |
if (mpi_is_initialized ()) |
| 135 |
{ |
| 136 |
applications_->assignLeaders (config_->Name ()); |
| 137 |
leader_avail = true; |
| 138 |
} |
| 139 |
#endif |
| 140 |
mapConnectivity (leader_avail); |
| 141 |
} |
| 142 |
else |
| 143 |
error0 ("Configuration file load: " + std::string (cfile_->getError ())); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
void |
| 148 |
ApplicationMapper::mapApplications () |
| 149 |
{ |
| 150 |
|
| 151 |
// retrieve default configurations for current object config_ |
| 152 |
mapSection ( 0, config_->defaultConfig()); |
| 153 |
|
| 154 |
// create temporal object with the default configurations |
| 155 |
Configuration *config = new Configuration (); |
| 156 |
config->defaultConfig ()->setDict(config_->defaultConfig ()->getDict ()); |
| 157 |
|
| 158 |
int n_selections = 0; |
| 159 |
int np; |
| 160 |
int nSections = cfile_->getNumSections (); |
| 161 |
// loop through all sections |
| 162 |
for (int s = 1; s < nSections; ++s) |
| 163 |
{ |
| 164 |
// retrieve configuration to the temporal object |
| 165 |
mapSection (s, config); |
| 166 |
|
| 167 |
// if retrieved section is current configuration |
| 168 |
if (app_selector_->selected(config)) |
| 169 |
{ |
| 170 |
++n_selections; |
| 171 |
// copy configuration settings to current object |
| 172 |
config_->setDict (config->getDict ()); |
| 173 |
|
| 174 |
config_->setName(config->Name()); |
| 175 |
|
| 176 |
} |
| 177 |
|
| 178 |
// save application information |
| 179 |
config->lookup ("np", &np); |
| 180 |
applications_->add (config->Name(), np, s - 1); |
| 181 |
// empty temporal object |
| 182 |
// NOTE: default configuration is preserved |
| 183 |
config->resetDict(); |
| 184 |
} |
| 185 |
delete config; |
| 186 |
assert(config_->getDict().size() != 0); |
| 187 |
|
| 188 |
// NOTE: given rank -1 causes an assertion failure |
| 189 |
assert(n_selections == 1); |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
void |
| 194 |
ApplicationMapper::mapSection (int index, Configuration* config) |
| 195 |
{ |
| 196 |
std::string name (cfile_->getSectionNameAt (index)); |
| 197 |
cfile_->setSection (name.c_str ()); |
| 198 |
int nMembers = cfile_->getNumDataMembers (); |
| 199 |
config->setName(name); |
| 200 |
for (int m = 0; m < nMembers; ++m) |
| 201 |
{ |
| 202 |
std::string n (cfile_->getDataNameAt (m)); |
| 203 |
std::string v (cfile_->getStringValue (n.c_str ())); |
| 204 |
config->insert (n, v); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
void |
| 210 |
ApplicationMapper::mapConnectivity (bool leaders) |
| 211 |
{ |
| 212 |
|
| 213 |
std::map<std::string, int> receiverPortCodes; |
| 214 |
int nextPortCode = 0; |
| 215 |
|
| 216 |
int nSections = cfile_->getNumSections (); |
| 217 |
for (int s = 0; s < nSections; ++s) |
| 218 |
{ |
| 219 |
std::string secName (cfile_->getSectionNameAt (s)); |
| 220 |
cfile_->setSection (secName.c_str ()); |
| 221 |
|
| 222 |
int nConnections = cfile_->getNumSourceDestMembers (); |
| 223 |
for (int c = 0; c < nConnections; ++c) |
| 224 |
{ |
| 225 |
std::string senderApp (cfile_->getSrcAppAt (c)); |
| 226 |
std::string senderPort (cfile_->getSrcObjAt (c)); |
| 227 |
std::string receiverApp (cfile_->getDestAppAt (c)); |
| 228 |
std::string receiverPort (cfile_->getDestObjAt (c)); |
| 229 |
std::string width (cfile_->getWidthAt (c)); |
| 230 |
std::string commType (cfile_->getCommTypeAt (c)); |
| 231 |
std::string procMethod (cfile_->getProcMethodAt (c)); |
| 232 |
|
| 233 |
if (senderApp == "") |
| 234 |
{ |
| 235 |
if (secName == "") |
| 236 |
error ( |
| 237 |
"sender application not specified for output port " |
| 238 |
+ senderPort); |
| 239 |
else |
| 240 |
senderApp = secName; |
| 241 |
} |
| 242 |
if (receiverApp == "") |
| 243 |
{ |
| 244 |
if (secName == "") |
| 245 |
error ( |
| 246 |
"receiver application not specified for input port " |
| 247 |
+ receiverPort); |
| 248 |
else |
| 249 |
receiverApp = secName; |
| 250 |
} |
| 251 |
if (senderApp == receiverApp) |
| 252 |
error ( |
| 253 |
"port " + senderPort + " of application " + senderApp |
| 254 |
+ " connected to the same application"); |
| 255 |
|
| 256 |
|
| 257 |
// Communication type option can be either *point-to-point* or |
| 258 |
// *collective* written in any case letters. |
| 259 |
std::transform (commType.begin (), commType.end (), |
| 260 |
commType.begin (), ::tolower); |
| 261 |
if (commType.length () > 0 && commType.compare ("collective") |
| 262 |
&& commType.compare ("point-to-point")) |
| 263 |
error ("communication type " + commType + " is not supported"); |
| 264 |
|
| 265 |
// Processing method can be either *tree* or |
| 266 |
// *table* written in any case letters. |
| 267 |
std::transform (procMethod.begin (), procMethod.end (), |
| 268 |
procMethod.begin (), ::tolower); |
| 269 |
if (procMethod.length () > 0 && procMethod.compare ("table") |
| 270 |
&& procMethod.compare ("tree")) |
| 271 |
error ("processing method " + procMethod + " is not supported"); |
| 272 |
|
| 273 |
// Generate a unique "port code" for each receiver port |
| 274 |
// name. This will later be used during temporal |
| 275 |
// negotiation since it easier to communicate integers, |
| 276 |
// which have constant size, than strings. |
| 277 |
// |
| 278 |
// NOTE: This code must be executed in the same order in |
| 279 |
// all MPI processes. |
| 280 |
std::string receiverPortFullName = receiverApp + "." + receiverPort; |
| 281 |
std::map<std::string, int>::iterator pos = receiverPortCodes.find ( |
| 282 |
receiverPortFullName); |
| 283 |
int portCode; |
| 284 |
if (pos == receiverPortCodes.end ()) |
| 285 |
{ |
| 286 |
portCode = nextPortCode++; |
| 287 |
receiverPortCodes.insert ( |
| 288 |
std::make_pair (receiverPortFullName, portCode)); |
| 289 |
} |
| 290 |
else |
| 291 |
portCode = pos->second; |
| 292 |
|
| 293 |
ConnectivityInfo::PortDirection dir; |
| 294 |
ApplicationInfo* remoteInfo; |
| 295 |
if (config_->Name () == senderApp) |
| 296 |
{ |
| 297 |
dir = ConnectivityInfo::OUTPUT; |
| 298 |
remoteInfo = applications_->lookup (receiverApp); |
| 299 |
} |
| 300 |
else if (config_->Name () == receiverApp) |
| 301 |
{ |
| 302 |
dir = ConnectivityInfo::INPUT; |
| 303 |
remoteInfo = applications_->lookup (senderApp); |
| 304 |
} |
| 305 |
else |
| 306 |
continue; |
| 307 |
int w; |
| 308 |
if (width == "") |
| 309 |
w = ConnectivityInfo::NO_WIDTH; |
| 310 |
else |
| 311 |
{ |
| 312 |
std::istringstream ws (width); |
| 313 |
if (! (ws >> w)) |
| 314 |
error ("could not interpret width"); |
| 315 |
} |
| 316 |
|
| 317 |
// default communication type is *point-to-point* |
| 318 |
int iCommType; |
| 319 |
if (commType.length () == 0 || !commType.compare ("point-to-point")) |
| 320 |
iCommType = ConnectorInfo::POINTTOPOINT; |
| 321 |
else |
| 322 |
iCommType = ConnectorInfo::COLLECTIVE; |
| 323 |
|
| 324 |
// default processing method is *tree* |
| 325 |
int iProcMethod; |
| 326 |
if (procMethod.length () == 0 || !procMethod.compare ("tree")) |
| 327 |
iProcMethod = ConnectorInfo::TREE; |
| 328 |
|
| 329 |
else |
| 330 |
iProcMethod = ConnectorInfo::TABLE; |
| 331 |
|
| 332 |
// NOTE: leader number can be not available at this stage, |
| 333 |
// if leader is not available, we write down the color of the application |
| 334 |
int leader = leaders ? remoteInfo->leader () : remoteInfo->color (); |
| 335 |
connectivityMap_->add ( |
| 336 |
dir == ConnectivityInfo::OUTPUT ? senderPort : receiverPort, |
| 337 |
dir, w, receiverApp, receiverPort, portCode, leader, |
| 338 |
remoteInfo->nProc (), iCommType, iProcMethod); |
| 339 |
} |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
#if HAVE_SYS_STAT_H |
| 345 |
long |
| 346 |
ApplicationMapper::getApplicationINO(const char * path) |
| 347 |
{ |
| 348 |
struct stat st; |
| 349 |
if (stat(path, &st) == -1) |
| 350 |
{ |
| 351 |
std::ostringstream oss; |
| 352 |
oss |
| 353 |
<< "MUSIC: Couldn't retrieve file status (stat()) about: " << path; |
| 354 |
error0 (oss.str ()); |
| 355 |
} |
| 356 |
return st.st_ino; |
| 357 |
} |
| 358 |
#endif |
| 359 |
|
| 360 |
|
| 361 |
bool |
| 362 |
SelectorR::selected (Configuration* config) |
| 363 |
{ |
| 364 |
#if MUSIC_USE_MPI |
| 365 |
int np; |
| 366 |
config->lookup ("np", &np); |
| 367 |
selected_ = rank_ < next_chunk_; |
| 368 |
next_chunk_ += np; |
| 369 |
return rank_ < next_chunk_ && !selected_; |
| 370 |
#endif |
| 371 |
|
| 372 |
return false; |
| 373 |
} |
| 374 |
|
| 375 |
|
| 376 |
bool |
| 377 |
SelectorOp::selected (Configuration* config) |
| 378 |
{ |
| 379 |
std::string sec_name = config->Name(); |
| 380 |
return !app_label_.compare(sec_name); |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
bool |
| 385 |
SelectorINO::selected (Configuration* config) |
| 386 |
{ |
| 387 |
#if HAVE_SYS_STAT_H |
| 388 |
std::string sec_name; |
| 389 |
config->lookup("appLabel", &sec_name); |
| 390 |
std::string binary; |
| 391 |
config->lookup("binary", &binary); |
| 392 |
long ino = ApplicationMapper::getApplicationINO (binary.c_str()); |
| 393 |
if (ino == ino_) |
| 394 |
{ |
| 395 |
int np; |
| 396 |
config->lookup("np", &np); |
| 397 |
selected_ = my_chunk_ < next_chunk_; |
| 398 |
next_chunk_+=np; |
| 399 |
} |
| 400 |
|
| 401 |
return ino == ino_ && my_chunk_ < next_chunk_ && !selected_; |
| 402 |
#endif |
| 403 |
|
| 404 |
return false; |
| 405 |
|
| 406 |
} |
| 407 |
|
| 408 |
} |