1 /* Copyright (c) 2007-2011, 2014. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 //GTNETS_Link, GTNETS_Node, GTNETS_Topology:
8 //Temporary classes for generating GTNetS topology
10 #include "gtnets_topology.h"
15 #include "xbt/asserts.h"
16 #include <stdio.h> //for fflush in print_topology
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(surf_network_gtnets_topology, surf_network_gtnets,
19 "Logging specific to the SURF network GTNetS simulator");
26 GTNETS_Node::GTNETS_Node(int id):ID_(id),is_router_(false){}
28 GTNETS_Node::GTNETS_Node(const GTNETS_Node& node){
30 is_router_ = node.is_router_;
34 GTNETS_Node::~GTNETS_Node(){
38 // hostid = network_card_id
39 int GTNETS_Node::add_host(int hostid){
40 xbt_assert(!(is_router_), "Cannot add a host to a router node");
41 hosts_.insert(hostid);
45 // Add a router. If this node already has a router/host,
47 int GTNETS_Node::add_router(int routerid){
48 xbt_assert(!(hosts_.size() > 1), "Router node should have only one router");
49 if (hosts_.size() == 1){
50 xbt_assert((hosts_.find(routerid) != hosts_.end()), "Node %d is a different router", routerid);
54 hosts_.insert(routerid);
58 bool GTNETS_Node::is_router(){
62 bool GTNETS_Node::include(int hostid){
63 if (hosts_.find(hostid) == hosts_.end()) return false;
67 void GTNETS_Node::print_hosts(){
68 set<int>::iterator it;
69 for (it = hosts_.begin(); it != hosts_.end(); it++){
70 XBT_DEBUG("host id %d", *it);
79 GTNETS_Link::GTNETS_Link(){
84 GTNETS_Link::GTNETS_Link(int id):ID_(id), src_node_(0), dst_node_(0){}
87 GTNETS_Link::GTNETS_Link(const GTNETS_Link& link){
89 src_node_ = link.src_node_;
90 dst_node_ = link.dst_node_;
93 GTNETS_Link::~GTNETS_Link(){
97 void GTNETS_Link::print_link_status(){
98 XBT_DEBUG("****** link id: %d", ID_);
100 XBT_DEBUG("[src] id: %d, is it router?: %d",src_node_->id(), src_node_->is_router());
104 XBT_DEBUG("[dst] id: %d, is it router?: %d",dst_node_->id(), dst_node_->is_router());
108 GTNETS_Node* GTNETS_Link::src_node(){
112 GTNETS_Node* GTNETS_Link::dst_node(){
116 bool GTNETS_Link::route_exists(){
117 if (src_node_ && dst_node_) return true;
121 // return the peer node id
122 int GTNETS_Link::peer_node(int cur_id){
123 xbt_assert(((cur_id == src_node_->id())||(cur_id == dst_node_->id())), "Node not found");
125 if (cur_id == src_node_->id()) return dst_node_->id();
126 else if (cur_id == dst_node_->id()) return src_node_->id();
129 int GTNETS_Link::add_src(GTNETS_Node* src){
133 int GTNETS_Link::add_dst(GTNETS_Node* dst){
143 GTNETS_Topology::GTNETS_Topology(){
148 GTNETS_Topology::~GTNETS_Topology(){
149 map<int, GTNETS_Link*>::iterator it1;
150 for (it1 = links_.begin(); it1 != links_.end(); it1++){
153 vector<GTNETS_Node*>::iterator it2;
154 for (it2 = nodes_.begin(); it2 != nodes_.end(); it2++){
160 int GTNETS_Topology::link_size(){
161 return links_.size();
164 int GTNETS_Topology::node_size(){
165 return nodes_.size();
168 int GTNETS_Topology::add_link(int id){
169 map<int,GTNETS_Link*>::iterator iter = links_.find(id);
170 xbt_assert((iter == links_.end()), "Link %d already exists", id);
172 if(iter == links_.end()) {
173 GTNETS_Link* link= new GTNETS_Link(id);
179 int GTNETS_Topology::add_router(int id){
180 set<int>::iterator iter = routers_.find(id);
181 if(iter == routers_.end()){
184 XBT_DEBUG("Router (#%d) already exists", id);
189 bool GTNETS_Topology::is_router(int id){
190 set<int>::iterator iter = routers_.find(id);
191 if(iter == routers_.end()) return false;
195 //return the node id of the peer of cur_id by linkid.
196 int GTNETS_Topology::peer_node_id(int linkid, int cur_id){
197 GTNETS_Link* link = links_[linkid];
198 xbt_assert((link), "Link %d not found", linkid);
199 xbt_assert(!((cur_id < 0) || (cur_id > nodes_.size()-1)), "Node %d not found", cur_id);
201 int peer = link->peer_node(nodes_[cur_id]->id());
202 xbt_assert(!(peer < 0), "Peer not found");
207 int GTNETS_Topology::add_onehop_route(int src, int dst, int linkid){
210 map<int, GTNETS_Link*>::iterator iter = links_.find(linkid);
212 xbt_assert(!(iter == links_.end()), "Link %d not found", linkid);
215 XBT_DEBUG("Add onehop route, src (#%d), dst (#%d), linkid %d:(#%d)",src, dst, linkid, link->id());
217 GTNETS_Node *src_node, *dst_node;
218 src_node = link->src_node();
219 dst_node = link->dst_node();
221 // If not exists a route, add one.
222 if (!link->route_exists()){
223 //check whether there exists a node for the src.
224 int s_node_id = nodeid_from_hostid(src);
227 if (s_node_id < 0){//not exist, create one.
229 GTNETS_Node* node1 = new GTNETS_Node(s_node_id);
230 nodes_.push_back(node1);
231 hosts_[src] = nodes_[s_node_id]->id();
237 nodes_[s_node_id]->add_router(src);
239 nodes_[s_node_id]->add_host(src);
241 link->add_src(nodes_[s_node_id]);
243 //check whether there exists a node for the dst host/router.
244 int d_node_id = nodeid_from_hostid(dst);
245 if (d_node_id < 0){//not exist, create one.
247 GTNETS_Node* node2 = new GTNETS_Node(d_node_id);
248 nodes_.push_back(node2);
249 hosts_[dst] = nodes_[d_node_id]->id();
254 nodes_[d_node_id]->add_router(dst);
256 nodes_[d_node_id]->add_host(dst);
258 link->add_dst(nodes_[d_node_id]);
259 }else if (!(src_node && dst_node)){
260 xbt_assert((src_node && dst_node), "Either src or dst is null");
263 // case 1: link has two routers
264 else if (src_node->is_router() && dst_node->is_router()){
265 int tmpsrc1 = src_node->id();
266 int tmpsrc2 = nodeid_from_hostid(src);
267 int tmpdst1 = dst_node->id();
268 int tmpdst2 = nodeid_from_hostid(dst);
269 xbt_assert( (((tmpsrc1 == tmpsrc2) && (tmpdst1 == tmpdst2)) ||
270 ((tmpsrc1 == tmpdst2) && (tmpdst1 == tmpsrc2))), "Different one hop route defined");
273 // case 2: link has one router and one host
274 else if (src_node->is_router() && !dst_node->is_router()){
276 xbt_assert( ((is_router(src))||(is_router(dst))), "One of nodes should be a router");
281 }else if (is_router(dst)){
286 xbt_assert(!(src_node->id() != nodeid_from_hostid(newsrc)), "The router should be identical");
288 //now, to add dst to dst_node, dst should be a host.
289 xbt_assert(!(is_router(newdst)), "Dst %d is not an endpoint. cannot add it to dst_node", newdst);
291 if (!dst_node->include(newdst)){
292 dst_node->add_host(newdst);
293 hosts_[newdst] = dst_node->id();
296 else if (!src_node->is_router() && dst_node->is_router()){
298 xbt_assert(((is_router(src))||(is_router(dst))), "One of nodes should be a router");
303 }else if (is_router(dst)){
308 xbt_assert(!(dst_node->id() != hosts_[newdst]), "The router should be identical");
309 //now, to add dst to src_node, dst should be a host.
310 xbt_assert(!(is_router(newsrc)), "Src %d is not an endpoint. cannot add it to src_node", newsrc);
312 if (!src_node->include(newsrc)){
313 src_node->add_host(newsrc);
314 hosts_[newsrc] = src_node->id();
318 // case 3: link has two hosts
319 else if (!src_node->is_router() && !dst_node->is_router()){
320 xbt_assert(!(is_router(src) || is_router(dst)), "Cannot add a router to host-host link");
322 //if both are hosts, the order doesn't matter.
323 if (src_node->include(src)){
324 if (dst_node->include(dst)){
327 dst_node->add_host(dst);
328 hosts_[dst] = dst_node->id();
330 }else if (src_node->include(dst)){
331 if (dst_node->include(src)){
334 dst_node->add_host(src);
335 hosts_[src] = dst_node->id();
337 }else if (dst_node->include(src)){
338 if (src_node->include(dst)){
341 src_node->add_host(dst);
342 hosts_[dst] = src_node->id();
344 }else if (dst_node->include(dst)){
345 if (src_node->include(src)){
348 src_node->add_host(src);
349 hosts_[src] = src_node->id();
352 src_node->add_host(src);
353 dst_node->add_host(dst);
354 hosts_[src] = src_node->id();
355 hosts_[dst] = dst_node->id();
360 xbt_die("Shouldn't be here");
366 int GTNETS_Topology::nodeid_from_hostid(int hostid){
367 map<int,int>::iterator it = hosts_.find(hostid);
368 if (it == hosts_.end())
370 else return it->second;
373 void GTNETS_Topology::print_topology(){
374 XBT_DEBUG("<<<<<================================>>>>>");
375 XBT_DEBUG("Dumping GTNETS topollogy information");
376 map<int, GTNETS_Link*>::iterator it;
377 for (it = links_.begin(); it != links_.end(); it++){
378 it->second->print_link_status();
380 XBT_DEBUG(">>>>>================================<<<<<");
384 const vector<GTNETS_Node*>& GTNETS_Topology::nodes(){
388 const map<int, GTNETS_Link*>& GTNETS_Topology::links(){