Alex Bikfalvi
SimStream Documentation
TopoBrite.cpp
00001 #include "Headers.h" 00002 #include "TopoBrite.h" 00003 00004 CTopoBrite::CTopoBrite(const char* fileName) 00005 { 00006 this->numNodes = 0; 00007 this->numEdges = 0; 00008 00009 this->nodes = NULL; 00010 this->edges = NULL; 00011 00012 this->Read(fileName); 00013 00014 this->route = new CTopoBriteRoute(this->numNodes, this->numEdges, this->nodes, this->edges); 00015 assert(this->route); 00016 } 00017 00018 CTopoBrite::~CTopoBrite() 00019 { 00020 if(this->nodes) 00021 { 00022 for(__uint32 index = 0; index < this->numNodes; index++) delete this->nodes[index]; 00023 delete[] this->nodes; 00024 } 00025 if(this->edges) 00026 { 00027 for(__uint32 index = 0; index < this->numEdges; index++) delete this->edges[index]; 00028 delete[] this->edges; 00029 } 00030 delete this->route; 00031 } 00032 00033 void CTopoBrite::Read(const char* fileName) 00034 { 00035 // Open file 00036 FILE* file = NULL; 00037 if(FILE_OPEN(file, fileName, "r")) 00038 { 00039 printf("\nCannot open Brite topology file: %s", fileName); 00040 exit(-1); 00041 } 00042 00043 // Read header 00044 #ifdef _WINDOWS 00045 if(fscanf_s(file, "Topology: ( %u Nodes, %u Edges )\n", &this->numNodes, &this->numEdges) != 2) 00046 throw "I/O Error"; 00047 if(fscanf_s(file, "Model (%u - %s ", 00048 &this->model, 00049 this->modelName, 00050 32) != 2) 00051 throw "I/O Error"; 00052 #else 00053 if(fscanf(file, "Topology: ( %u Nodes, %u Edges )\n", &this->numNodes, &this->numEdges) != 2) 00054 throw "I/O Error"; 00055 if(fscanf(file, "Model (%u - %s ", 00056 &this->model, 00057 this->modelName) != 2) 00058 throw "I/O Error"; 00059 #endif 00060 00061 __uint32 numNodes; 00062 00063 double bwMin; 00064 double bwMax; 00065 00066 switch(model) 00067 { 00068 case TOPO_BRITE_MODEL_WAXMAN: 00069 #ifdef _WINDOWS 00070 if(fscanf_s(file, "%u %u %u %u %u %lf %lf %u %u %lf %lf \n\n", 00071 &numNodes, 00072 &this->hs, 00073 &this->ls, 00074 &this->nodePlacement, 00075 &this->nodeLinks, 00076 &this->alpha, 00077 &this->beta, 00078 &this->growthType, 00079 &this->bwDist, 00080 &bwMin, 00081 &bwMax) != 11) throw "I/O error"; 00082 #else 00083 if(fscanf(file, "%u %u %u %u %u %lf %lf %u %u %lf %lf \n\n", 00084 &numNodes, 00085 &this->hs, 00086 &this->ls, 00087 &this->nodePlacement, 00088 &this->nodeLinks, 00089 &this->alpha, 00090 &this->beta, 00091 &this->growthType, 00092 &this->bwDist, 00093 &bwMin, 00094 &bwMax) != 11) throw "I/O error"; 00095 #endif 00096 break; 00097 default: throw "Not supported"; 00098 } 00099 assert(this->numNodes == numNodes); 00100 00101 this->bwMax = (__bitrate)ceil(bwMax); 00102 this->bwMin = (__bitrate)ceil(bwMin); 00103 00104 // Read nodes 00105 #ifdef _WINDOWS 00106 if(fscanf_s(file, "Nodes: ( %u )\n", &numNodes) != 1) throw "I/O error"; 00107 #else 00108 if(fscanf(file, "Nodes: ( %u )\n", &numNodes) != 1) throw "I/O error"; 00109 #endif 00110 assert(this->numNodes == numNodes); 00111 00112 this->nodes = new CTopoBriteNode*[this->numNodes]; 00113 assert(this->nodes); 00114 00115 __uint32 id; 00116 __uint32 xpos; 00117 __uint32 ypos; 00118 __uint32 inDegree; 00119 __uint32 outDegree; 00120 int as; 00121 char type[16]; 00122 00123 for(__uint32 index = 0; index < this->numNodes; index++) 00124 { 00125 #ifdef _WINDOWS 00126 if(fscanf_s(file, "%u %u %u %u %u %d %s\n", &id, &xpos, &ypos, &inDegree, &outDegree, &as, type, 16) != 7) throw "I/O error"; 00127 #else 00128 if(fscanf(file, "%u %u %u %u %u %d %s\n", &id, &xpos, &ypos, &inDegree, &outDegree, &as, type) != 7) throw "I/O error"; 00129 #endif 00130 assert(id == index); 00131 assert(inDegree == outDegree); 00132 00133 this->nodes[index] = new CTopoBriteNode( 00134 index, 00135 xpos, 00136 ypos, 00137 inDegree 00138 ); 00139 assert(this->nodes[index]); 00140 } 00141 00142 // Read edges 00143 __uint32 numEdges; 00144 #ifdef _WINDOWS 00145 if(fscanf_s(file, "\n\nEdges: ( %u )\n", &numEdges) != 1) throw "I/O error"; 00146 #else 00147 if(fscanf(file, "\n\nEdges: ( %u )\n", &numEdges) != 1) throw "I/O error"; 00148 #endif 00149 assert(this->numEdges == numEdges); 00150 00151 this->edges = new CTopoBriteEdge*[this->numEdges]; 00152 assert(this->edges); 00153 00154 __uint32 from; 00155 __uint32 to; 00156 double length; 00157 double delay; 00158 double bw; 00159 int asFrom; 00160 int asTo; 00161 char val; 00162 00163 for(__uint32 index = 0; index < this->numEdges; index++) 00164 { 00165 #ifdef _WINDOWS 00166 if(fscanf_s(file, "%u %u %u %lf %lf %lf %d %d %s %c\n", &id, &from, &to, &length, &delay, &bw, &asFrom, &asTo, type, 16, &val) != 10) throw "I/O error"; 00167 #else 00168 if(fscanf(file, "%u %u %u %lf %lf %lf %d %d %s %c\n", &id, &from, &to, &length, &delay, &bw, &asFrom, &asTo, type, &val) != 10) throw "I/O error"; 00169 #endif 00170 assert(id == index); 00171 00172 if(bw > this->bwMax) bw = (double)this->bwMax; 00173 if(bw < this->bwMin) bw = (double)this->bwMin; 00174 00175 // Create the edge 00176 this->edges[index] = new CTopoBriteEdge(index, from, to, length, delay / 1000, (__bitrate)ceil(bw)); 00177 assert(this->edges[index]); 00178 00179 // Set the edge for the nodes 00180 this->nodes[from]->SetEdge(index); 00181 this->nodes[to]->SetEdge(index); 00182 } 00183 00184 // Close file 00185 fclose(file); 00186 } 00187 00188 //void CTopoBrite::AddUnicast(__uint32 src, __uint32 dst, __bitrate bw, __uint32& length) 00189 //{ 00190 // // Add a flow from the specified source to destination 00191 // length = 0; 00192 // for(__uint32 nextNode = src; nextNode != dst;) 00193 // { 00194 // CTopoBriteEdge* edge = this->edges[this->route->NextEdge(nextNode, dst)]; 00195 // assert(edge); 00196 // 00197 // edge->AddFlow(bw); 00198 // 00199 // assert(this->bwUsed + bw <= this->bwTotal); 00200 // this->bwUsed += bw; 00201 // 00202 // nextNode = this->route->NextNode(nextNode, dst); 00203 // 00204 // length++; 00205 // } 00206 //} 00207 // 00208 //void CTopoBrite::RemoveUnicast(__uint32 src, __uint32 dst, __bitrate bw) 00209 //{ 00210 // // Remove a flow from the specified source to destination 00211 // for(__uint32 nextNode = src; nextNode != dst;) 00212 // { 00213 // CTopoBriteEdge* edge = this->edges[this->route->NextEdge(nextNode, dst)]; 00214 // assert(edge); 00215 // 00216 // edge->RemoveFlow(bw); 00217 // 00218 // assert(this->bwUsed >= bw); 00219 // this->bwUsed -= bw; 00220 // 00221 // nextNode = this->route->NextNode(nextNode, dst); 00222 // } 00223 //} 00224 // 00225 //void CTopoBrite::AddMulticast(__uint32 group, __uint32 src, __uint32 dst, __bitrate bw, int& mcastSizeDelta, int& mcastGroupDelta) 00226 //{ 00227 // // Add a multicast flow from the specified source to destination 00228 // mcastSizeDelta = 0; 00229 // 00230 // // Calculate the group size delta: if the destination node already in the group then the delta is 0, otherwise 1 00231 // CTopoBriteNode* node = this->nodes[dst]; 00232 // assert(node); 00233 // 00234 // mcastGroupDelta = (node->McastAccess(group)?0:1); 00235 // 00236 // // Start from the destination towards the source 00237 // __uint32 nextNode = dst; 00238 // for(; nextNode != src;) 00239 // { 00240 // node = this->nodes[nextNode]; 00241 // assert(node); 00242 // 00243 // // Add the group to the node 00244 // assert(this->mcastUsed >= node->Mcast()); 00245 // this->mcastUsed -= node->Mcast(); 00246 // __uint32 con = node->AddMulticast(group, nextNode == dst); 00247 // this->mcastUsed += node->Mcast(); 00248 // 00249 // // There must be at least one group entry at the node 00250 // assert(con); 00251 // 00252 // // If the added group is the first for this node, also add the flow to the edge going from node 00253 // if(1 == con) 00254 // { 00255 // CTopoBriteEdge* edge = this->edges[this->route->NextEdge(nextNode, src)]; 00256 // assert(edge); 00257 // 00258 // assert(this->bwUsed + bw <= this->bwTotal); 00259 // this->bwUsed += bw; 00260 // 00261 // edge->AddFlow(bw); 00262 // 00263 // mcastSizeDelta++; 00264 // 00265 // // Select next node towards the source 00266 // nextNode = this->route->NextNode(nextNode, src); 00267 // } 00268 // else 00269 // // The node has more group entries: stop here 00270 // break; 00271 // } 00272 // 00273 // // If the next node is the source 00274 // if(src == nextNode) 00275 // { 00276 // // Add the group to the node 00277 // assert(this->mcastUsed >= this->nodes[src]->Mcast()); 00278 // this->mcastUsed -= this->nodes[src]->Mcast(); 00279 // this->nodes[src]->AddMulticast(group, nextNode == dst); 00280 // this->mcastUsed += this->nodes[src]->Mcast(); 00281 // } 00282 //} 00283 // 00284 //void CTopoBrite::RemoveMulticast(__uint32 group, __uint32 src, __uint32 dst, __bitrate bw, int& mcastSizeDelta, int& mcastGroupDelta) 00285 //{ 00286 // // Remove a multicast flow from the specified source to destination 00287 // mcastSizeDelta = 0; 00288 // 00289 // // Calculate the group size delta: if the destination node has the mcast greater than 1 then the delta is 0, otherwise -1 00290 // CTopoBriteNode* node = this->nodes[dst]; 00291 // assert(node); 00292 // 00293 // mcastGroupDelta = (node->McastAccess(group) > 1)?0:1; 00294 // 00295 // // Start from the destination towards the source 00296 // __uint32 nextNode = dst; 00297 // for(;nextNode != src;) 00298 // { 00299 // node = this->nodes[nextNode]; 00300 // assert(node); 00301 // 00302 // // Remove the group entry from the node 00303 // assert(this->mcastUsed >= node->Mcast()); 00304 // this->mcastUsed -= node->Mcast(); 00305 // __uint32 con = node->RemoveMulticast(group, nextNode == dst); 00306 // this->mcastUsed += node->Mcast(); 00307 // 00308 // // If the node does not have the group (no more destinations) 00309 // // also remove the group from the link 00310 // if(0 == con) 00311 // { 00312 // CTopoBriteEdge* edge = this->edges[this->route->NextEdge(nextNode, src)]; 00313 // assert(edge); 00314 // 00315 // assert(this->bwUsed >= bw); 00316 // this->bwUsed -= bw; 00317 // 00318 // edge->RemoveFlow(bw); 00319 // 00320 // mcastSizeDelta++; 00321 // 00322 // // Select next node towards the source 00323 // nextNode = this->route->NextNode(nextNode, src); 00324 // } 00325 // else 00326 // break; 00327 // } 00328 // 00329 // // If the next node is the source 00330 // if(src == nextNode) 00331 // { 00332 // // Remove the group from the node 00333 // assert(this->mcastUsed >= this->nodes[src]->Mcast()); 00334 // this->mcastUsed -= this->nodes[src]->Mcast(); 00335 // this->nodes[src]->RemoveMulticast(group, nextNode == dst); 00336 // this->mcastUsed += this->nodes[src]->Mcast(); 00337 // } 00338 //} 00339 // 00340 //void CTopoBrite::Clear() 00341 //{ 00342 // this->bwUsed = 0; 00343 // this->mcastUsed = 0; 00344 // 00345 // for(__uint32 index = 0; index < this->numNodes; index++) 00346 // this->nodes[index]->Clear(); 00347 // for(__uint32 index = 0; index < this->numEdges; index++) 00348 // this->edges[index]->Clear(); 00349 //}
Last updated: February 8, 2011