| 1 |
/* |
| 2 |
* This file is part of MUSIC. |
| 3 |
* Copyright (C) 2008, 2009, 2012 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 <algorithm> |
| 20 |
|
| 21 |
#include "music/music-config.hh" |
| 22 |
|
| 23 |
#include "music/debug.hh" |
| 24 |
|
| 25 |
#include "music/connectivity.hh" |
| 26 |
#include "music/ioutils.hh" |
| 27 |
#include "music/error.hh" |
| 28 |
|
| 29 |
namespace MUSIC { |
| 30 |
|
| 31 |
int ConnectorInfo::maxPortCode_; |
| 32 |
|
| 33 |
#if __cplusplus <=199711L |
| 34 |
ConnectivityInfo* const Connectivity::NO_CONNECTIVITY = NULL; |
| 35 |
#endif |
| 36 |
|
| 37 |
void |
| 38 |
ConnectorInfo::registerPortCode (int portCode) |
| 39 |
{ |
| 40 |
ConnectorInfo::maxPortCode_ = std::max (portCode, |
| 41 |
ConnectorInfo::maxPortCode_); |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
void |
| 46 |
ConnectivityInfo::addConnection (std::string recApp, |
| 47 |
std::string recName, |
| 48 |
int recCode, |
| 49 |
int rLeader, |
| 50 |
int nProc, |
| 51 |
int commType, |
| 52 |
int procMethod |
| 53 |
) |
| 54 |
{ |
| 55 |
portConnections_.push_back (ConnectorInfo (recApp, |
| 56 |
recName, |
| 57 |
recCode, |
| 58 |
rLeader, |
| 59 |
nProc, |
| 60 |
commType, |
| 61 |
procMethod)); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
void |
| 66 |
Connectivity::add (std::string localPort, |
| 67 |
ConnectivityInfo::PortDirection dir, |
| 68 |
int width, |
| 69 |
std::string recApp, |
| 70 |
std::string recPort, |
| 71 |
int recPortCode, |
| 72 |
int remoteLeader, |
| 73 |
int remoteNProc, |
| 74 |
int commType, |
| 75 |
int procMethod |
| 76 |
) |
| 77 |
{ |
| 78 |
std::map<std::string, int>::iterator cmapInfo |
| 79 |
= connectivityMap.find (localPort); |
| 80 |
ConnectivityInfo* info; |
| 81 |
if (cmapInfo == connectivityMap.end ()) |
| 82 |
{ |
| 83 |
MUSIC_LOG ("creating new entry for " << localPort); |
| 84 |
int index = connections_.size (); |
| 85 |
connections_.push_back (ConnectivityInfo (localPort, dir, width)); |
| 86 |
info = &connections_.back (); |
| 87 |
MUSIC_LOG ("ci = " << info); |
| 88 |
connectivityMap.insert (std::make_pair (localPort, index)); |
| 89 |
} |
| 90 |
else |
| 91 |
{ |
| 92 |
MUSIC_LOG ("found old entry for " << localPort); |
| 93 |
info = &connections_[cmapInfo->second]; |
| 94 |
if (info->direction () != dir) |
| 95 |
error ("port " + localPort + " used both as output and input"); |
| 96 |
} |
| 97 |
info->addConnection (recApp, |
| 98 |
recPort, |
| 99 |
recPortCode, |
| 100 |
remoteLeader, |
| 101 |
remoteNProc, |
| 102 |
commType, |
| 103 |
procMethod |
| 104 |
); |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
ConnectivityInfo* |
| 109 |
Connectivity::info (std::string portName) |
| 110 |
{ |
| 111 |
std::map<std::string, int>::iterator info |
| 112 |
= connectivityMap.find (portName); |
| 113 |
if (info == connectivityMap.end ()) |
| 114 |
return NO_CONNECTIVITY; |
| 115 |
else |
| 116 |
return &connections_[info->second]; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
bool |
| 121 |
Connectivity::isConnected (std::string portName) |
| 122 |
{ |
| 123 |
return connectivityMap.find (portName) != connectivityMap.end (); |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
ConnectivityInfo::PortDirection |
| 128 |
Connectivity::direction (std::string portName) |
| 129 |
{ |
| 130 |
return connections_[connectivityMap[portName]].direction (); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
int |
| 135 |
Connectivity::width (std::string portName) |
| 136 |
{ |
| 137 |
return connections_[connectivityMap[portName]].width (); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
PortConnectorInfo |
| 142 |
Connectivity::connections (std::string portName) |
| 143 |
{ |
| 144 |
return connections_[connectivityMap[portName]].connections (); |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
void |
| 149 |
Connectivity::write (std::ostringstream& out) |
| 150 |
{ |
| 151 |
out << connectivityMap.size (); |
| 152 |
std::map<std::string, int>::iterator i; |
| 153 |
for (i = connectivityMap.begin (); |
| 154 |
i != connectivityMap.end (); |
| 155 |
++i) |
| 156 |
{ |
| 157 |
out << ':' << i->first << ':'; |
| 158 |
ConnectivityInfo* ci = &connections_[i->second]; |
| 159 |
out << ci->direction () << ':' << ci->width () << ':'; |
| 160 |
PortConnectorInfo conns = ci->connections (); |
| 161 |
out << conns.size (); |
| 162 |
PortConnectorInfo::iterator c; |
| 163 |
for (c = conns.begin (); c != conns.end (); ++c) |
| 164 |
{ |
| 165 |
out << ':' << c->receiverAppName (); |
| 166 |
out << ':' << c->receiverPortName (); |
| 167 |
out << ':' << c->receiverPortCode (); |
| 168 |
out << ':' << c->remoteLeader (); |
| 169 |
out << ':' << c->nProcesses (); |
| 170 |
out << ':' << c->communicationType(); |
| 171 |
out << ':' << c->processingMethod(); |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
void |
| 178 |
Connectivity::read (std::istringstream& in, std::map<int, int> leaders) |
| 179 |
{ |
| 180 |
int nPorts; |
| 181 |
in >> nPorts; |
| 182 |
for (int i = 0; i < nPorts; ++i) |
| 183 |
{ |
| 184 |
in.ignore (); |
| 185 |
std::string portName = IOUtils::read (in); |
| 186 |
in.ignore (); |
| 187 |
int dir; |
| 188 |
in >> dir; |
| 189 |
ConnectivityInfo::PortDirection pdir |
| 190 |
= static_cast<ConnectivityInfo::PortDirection> (dir); |
| 191 |
in.ignore (); |
| 192 |
int width; |
| 193 |
in >> width; |
| 194 |
in.ignore (); |
| 195 |
int nConnections; |
| 196 |
in >> nConnections; |
| 197 |
for (int i = 0; i < nConnections; ++i) |
| 198 |
{ |
| 199 |
in.ignore (); |
| 200 |
std::string recApp = IOUtils::read (in); |
| 201 |
in.ignore (); |
| 202 |
std::string recPort = IOUtils::read (in); |
| 203 |
in.ignore (); |
| 204 |
int recPortCode; |
| 205 |
in >> recPortCode; |
| 206 |
ConnectorInfo::registerPortCode (recPortCode); |
| 207 |
in.ignore (); |
| 208 |
// leader information is not available through configuration string |
| 209 |
// application color is used instead |
| 210 |
int color; |
| 211 |
in >> color; |
| 212 |
in.ignore (); |
| 213 |
int nProc; |
| 214 |
in >> nProc; |
| 215 |
in.ignore (); |
| 216 |
int commType; |
| 217 |
in >> commType; |
| 218 |
in.ignore (); |
| 219 |
int procMethod; |
| 220 |
in >> procMethod; |
| 221 |
add (portName, |
| 222 |
pdir, |
| 223 |
width, |
| 224 |
recApp, |
| 225 |
recPort, |
| 226 |
recPortCode, |
| 227 |
leaders[color], |
| 228 |
nProc, |
| 229 |
commType, |
| 230 |
procMethod |
| 231 |
); |
| 232 |
MUSIC_LOG ("add (portName = " << portName |
| 233 |
<< ", pdir = " << pdir |
| 234 |
<< ", width = " << width |
| 235 |
<< ", recApp = " << recApp |
| 236 |
<< ", recPort = " << recPort |
| 237 |
<< ", rLeader = " << leaders[color] |
| 238 |
<< ", nProc = " << nProc |
| 239 |
<< ", commType = " << commType |
| 240 |
<< ", procMethod = " << procMethod |
| 241 |
<< ")"); |
| 242 |
} |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
} |