| 1 |
/* |
| 2 |
* This file is part of MUSIC. |
| 3 |
* Copyright (C) 2007, 2008, 2009, 2012, 2021 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/configuration.hh" // Must be included first on BG/L |
| 20 |
|
| 21 |
#include "music/debug.hh" |
| 22 |
|
| 23 |
extern "C" { |
| 24 |
#include <stdlib.h> |
| 25 |
} |
| 26 |
|
| 27 |
#include "music/ioutils.hh" |
| 28 |
#include "music/error.hh" |
| 29 |
#include <iostream> |
| 30 |
#include <fstream> |
| 31 |
|
| 32 |
namespace MUSIC { |
| 33 |
|
| 34 |
const char* const Configuration::configEnvVarName = "_MUSIC_CONFIG_"; |
| 35 |
// const char* const Configuration::mapFileName = "music.map"; |
| 36 |
|
| 37 |
/* |
| 38 |
* This is the syntax of the _MUSIC_CONFIG_ variable: |
| 39 |
* |
| 40 |
* POSTPONE:COLOR means that configuration is postponed until the |
| 41 |
* first port is created. At that time, the variable is expected to |
| 42 |
* contain the full configuration information: |
| 43 |
* |
| 44 |
* |
| 45 |
* APPLICATIONNAME:COLOR:APPLICATIONMAP:CONNECTIVITYMAP:CONFIGDICT |
| 46 |
* |
| 47 |
* APPLICATIONNAME = name of this application (string) |
| 48 |
* |
| 49 |
* COLOR = index of section in music configuration file |
| 50 |
* |
| 51 |
* APPLICATIONMAP = SIZE:...:NAMEk:NPROCk:... |
| 52 |
* |
| 53 |
* SIZE = number of applications (integer) |
| 54 |
* |
| 55 |
* NAMEk = name of application k |
| 56 |
* |
| 57 |
* NPROCk = number of processes in application k |
| 58 |
* |
| 59 |
* CONNECTIVITYMAP = SIZE:...:PORTNAMEk:DIRECTIONk:WIDTHk:CONNECTIONSk:... |
| 60 |
* |
| 61 |
* SIZE = number of ports of this application |
| 62 |
* |
| 63 |
* PORTNAMEk = port name |
| 64 |
* |
| 65 |
* DIRECTIONk = port direction, 0 = OUTPUT, 1 = INPUT |
| 66 |
* |
| 67 |
* WIDTHk = port width |
| 68 |
* |
| 69 |
* CONNECTIONSk = SIZE:...: |
| 70 |
* RECAPPNAME:RECPORTNAME:RECPORTCODE:REMOTELEADER:NREMOTEPROCS |
| 71 |
* :COMMTYPE:PROCMETHOD:... |
| 72 |
* |
| 73 |
* RECAPPNAME = name of receiving application |
| 74 |
* |
| 75 |
* RECPORTNAME = name of receiver port |
| 76 |
* |
| 77 |
* RECPORTCODE = code unique for every receiver port |
| 78 |
* |
| 79 |
* REMOTELEADER = lowest rank of the remote application |
| 80 |
* |
| 81 |
* NREMOTEPROCS = number of processes in the remote application |
| 82 |
* |
| 83 |
* COMMTYPE = communication algorithm, 0 = collective, 1 = point-to-point |
| 84 |
* |
| 85 |
* PROCMETHOD = processing method, 0 = tree, 1 = table |
| 86 |
* |
| 87 |
* CONFIGDICT = ...:VARNAMEk=VALUEk:... |
| 88 |
*/ |
| 89 |
|
| 90 |
Configuration::Configuration () |
| 91 |
{ |
| 92 |
init (); |
| 93 |
} |
| 94 |
|
| 95 |
#if MUSIC_USE_MPI |
| 96 |
Configuration::Configuration (std::string configStr) |
| 97 |
{ |
| 98 |
init (); |
| 99 |
parse (configStr); |
| 100 |
} |
| 101 |
#endif |
| 102 |
|
| 103 |
Configuration::~Configuration () |
| 104 |
{ |
| 105 |
delete connectivityMap_; |
| 106 |
delete applications_; |
| 107 |
if (defaultConfig_ != NULL) |
| 108 |
delete defaultConfig_; |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
void |
| 113 |
Configuration::init () |
| 114 |
{ |
| 115 |
applications_ = new ApplicationMap (); |
| 116 |
connectivityMap_ = new Connectivity (); |
| 117 |
defaultConfig_ = NULL; |
| 118 |
} |
| 119 |
|
| 120 |
#if MUSIC_USE_MPI |
| 121 |
void |
| 122 |
Configuration::parse(std::string configStr) |
| 123 |
{ |
| 124 |
std::istringstream env (configStr); |
| 125 |
setName (IOUtils::read (env)); |
| 126 |
env.ignore (); // delim |
| 127 |
// do not need color information, |
| 128 |
// but we can use for error check |
| 129 |
IOUtils::read (env); |
| 130 |
env.ignore (); // delim |
| 131 |
applications_->read (env); |
| 132 |
env.ignore (); |
| 133 |
std::map<int, int> leaders = applications_->assignLeaders( Name ()); |
| 134 |
connectivityMap_->read (env, leaders); |
| 135 |
// parse config string |
| 136 |
while (!env.eof ()) |
| 137 |
{ |
| 138 |
env.ignore (); |
| 139 |
std::string name = IOUtils::read (env, '='); |
| 140 |
env.ignore (); |
| 141 |
std::string v = IOUtils::read (env); |
| 142 |
// std::cerr << name << " " << v << std::endl; |
| 143 |
insert (name, v ); |
| 144 |
} |
| 145 |
|
| 146 |
} |
| 147 |
#endif |
| 148 |
|
| 149 |
void |
| 150 |
Configuration::write (std::ostringstream& env, Configuration* mask) |
| 151 |
{ |
| 152 |
std::map<std::string, std::string>::iterator pos; |
| 153 |
for (pos = dict_.begin (); pos != dict_.end (); ++pos) |
| 154 |
{ |
| 155 |
std::string name = pos->first; |
| 156 |
if (!(mask && mask->lookup (name))) |
| 157 |
{ |
| 158 |
env << ':' << name << '='; |
| 159 |
IOUtils::write (env, pos->second); |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
void |
| 166 |
Configuration::writeEnv () |
| 167 |
{ |
| 168 |
std::ostringstream env; |
| 169 |
env << Name () << ':' << Color () << ':'; |
| 170 |
applications_->write (env); |
| 171 |
env << ':'; |
| 172 |
connectivityMap_->write (env); |
| 173 |
write (env, 0); |
| 174 |
if (defaultConfig_ != NULL) |
| 175 |
defaultConfig_->write (env, this); |
| 176 |
setenv (configEnvVarName, env.str ().c_str (), 1); |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
bool |
| 181 |
Configuration::lookup(std::string name) |
| 182 |
{ |
| 183 |
return dict_.find(name) != dict_.end(); |
| 184 |
} |
| 185 |
|
| 186 |
bool |
| 187 |
Configuration::lookup(std::string name, std::string* result) |
| 188 |
{ |
| 189 |
std::map<std::string, std::string>::iterator pos = dict_.find(name); |
| 190 |
if (pos == dict_.end()) |
| 191 |
return defaultConfig_ && defaultConfig_->lookup(name, result); |
| 192 |
|
| 193 |
*result = pos->second; |
| 194 |
return true; |
| 195 |
} |
| 196 |
|
| 197 |
bool |
| 198 |
Configuration::lookup(std::string name, int* result) |
| 199 |
{ |
| 200 |
std::map<std::string, std::string>::iterator pos = dict_.find(name); |
| 201 |
if (pos == dict_.end()) |
| 202 |
return defaultConfig_ && defaultConfig_->lookup(name, result); |
| 203 |
|
| 204 |
std::istringstream iss(pos->second); |
| 205 |
if (! (iss >> *result).fail()) |
| 206 |
return true; |
| 207 |
|
| 208 |
std::ostringstream oss; |
| 209 |
oss << "var " << name << " given wrong type (" << pos->second |
| 210 |
<< "; expected int) in config file"; |
| 211 |
error(oss.str()); |
| 212 |
return true; // Doesn't happen! Just for compiler! |
| 213 |
} |
| 214 |
|
| 215 |
bool |
| 216 |
Configuration::lookup(std::string name, double* result) |
| 217 |
{ |
| 218 |
std::map<std::string, std::string>::iterator pos = dict_.find(name); |
| 219 |
if (pos == dict_.end()) |
| 220 |
return defaultConfig_ && defaultConfig_->lookup (name, result); |
| 221 |
|
| 222 |
std::istringstream iss(pos->second); |
| 223 |
if (! (iss >> *result).fail()) |
| 224 |
return true; |
| 225 |
|
| 226 |
std::ostringstream oss; |
| 227 |
oss << "var " << name << " given wrong type (" << pos->second |
| 228 |
<< "; expected double) in config file"; |
| 229 |
error(oss.str()); |
| 230 |
return true; // Doesn't happen! Just for compiler! |
| 231 |
} |
| 232 |
|
| 233 |
bool |
| 234 |
Configuration::lookup (std::string name, bool* result) |
| 235 |
{ |
| 236 |
std::map<std::string, std::string>::iterator pos = dict_.find (name); |
| 237 |
if (pos == dict_.end ()) |
| 238 |
return defaultConfig_ && defaultConfig_->lookup (name, result); |
| 239 |
|
| 240 |
std::istringstream iss(pos->second); |
| 241 |
if (! (iss >> std::boolalpha >> *result).fail ()) |
| 242 |
return true; |
| 243 |
|
| 244 |
std::ostringstream oss; |
| 245 |
oss << "var " << name << " given wrong type (" << pos->second |
| 246 |
<< "; expected bool) in config file"; |
| 247 |
error(oss.str ()); |
| 248 |
return true; // Doesn't happen! Just for compiler! |
| 249 |
} |
| 250 |
|
| 251 |
std::string |
| 252 |
Configuration::Name () |
| 253 |
{ |
| 254 |
return app_name_; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
void |
| 259 |
Configuration::setName (std::string app_name) |
| 260 |
{ |
| 261 |
app_name_ = app_name; |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
int |
| 266 |
Configuration::Color() |
| 267 |
{ |
| 268 |
return applications_->lookup (app_name_)->color (); |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
int |
| 273 |
Configuration::Leader() |
| 274 |
{ |
| 275 |
ApplicationInfo* info = applications_->lookup (app_name_); |
| 276 |
return info == 0 ? -1 : info->leader (); |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
void |
| 281 |
Configuration::insert (std::string name, std::string value) |
| 282 |
{ |
| 283 |
|
| 284 |
dict_.insert (std::make_pair (name, value)); |
| 285 |
} |
| 286 |
|
| 287 |
|
| 288 |
ApplicationMap* |
| 289 |
Configuration::applications () |
| 290 |
{ |
| 291 |
return applications_; |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
Configuration* |
| 296 |
Configuration::defaultConfig () |
| 297 |
{ |
| 298 |
if (defaultConfig_ == NULL) |
| 299 |
defaultConfig_ = new Configuration(); |
| 300 |
return defaultConfig_; |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
Connectivity* |
| 305 |
Configuration::connectivityMap () |
| 306 |
{ |
| 307 |
return connectivityMap_; |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
const Configuration::ConfigDict & |
| 312 |
Configuration::getDict() |
| 313 |
{ |
| 314 |
return dict_; |
| 315 |
} |
| 316 |
|
| 317 |
|
| 318 |
void |
| 319 |
Configuration::setDict(const Configuration::ConfigDict &dict) |
| 320 |
{ |
| 321 |
dict_ = dict; |
| 322 |
|
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
void |
| 327 |
Configuration::resetDict() |
| 328 |
{ |
| 329 |
dict_ = Configuration::ConfigDict (); |
| 330 |
} |
| 331 |
|
| 332 |
} |