SimpleNetwork.cc revision 6145:15cca6ab723a
1
2/*
3 * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution;
13 * neither the name of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30/*
31 * SimpleNetwork.C
32 *
33 * Description: See SimpleNetwork.h
34 *
35 * $Id$
36 *
37 */
38
39#include "SimpleNetwork.hh"
40#include "Profiler.hh"
41#include "System.hh"
42#include "Switch.hh"
43#include "NetDest.hh"
44#include "Topology.hh"
45#include "TopologyType.hh"
46#include "MachineType.hh"
47#include "MessageBuffer.hh"
48#include "Protocol.hh"
49#include "Map.hh"
50
51// ***BIG HACK*** - This is actually code that _should_ be in Network.C
52
53// Note: Moved to Princeton Network
54// calls new to abstract away from the network
55/*
56Network* Network::createNetwork(int nodes)
57{
58  return new SimpleNetwork(nodes);
59}
60*/
61
62SimpleNetwork::SimpleNetwork(int nodes)
63{
64  m_nodes = MachineType_base_number(MachineType_NUM);
65
66  m_virtual_networks = NUMBER_OF_VIRTUAL_NETWORKS;
67  m_endpoint_switches.setSize(m_nodes);
68
69  m_in_use.setSize(m_virtual_networks);
70  m_ordered.setSize(m_virtual_networks);
71  for (int i = 0; i < m_virtual_networks; i++) {
72    m_in_use[i] = false;
73    m_ordered[i] = false;
74  }
75
76  // Allocate to and from queues
77  m_toNetQueues.setSize(m_nodes);
78  m_fromNetQueues.setSize(m_nodes);
79  for (int node = 0; node < m_nodes; node++) {
80    m_toNetQueues[node].setSize(m_virtual_networks);
81    m_fromNetQueues[node].setSize(m_virtual_networks);
82    for (int j = 0; j < m_virtual_networks; j++) {
83      m_toNetQueues[node][j] = new MessageBuffer;
84      m_fromNetQueues[node][j] = new MessageBuffer;
85    }
86  }
87
88  // Setup the network switches
89  m_topology_ptr = new Topology(this, m_nodes);
90  int number_of_switches = m_topology_ptr->numSwitches();
91  for (int i=0; i<number_of_switches; i++) {
92    m_switch_ptr_vector.insertAtBottom(new Switch(i, this));
93  }
94  m_topology_ptr->createLinks(false);  // false because this isn't a reconfiguration
95}
96
97void SimpleNetwork::reset()
98{
99  for (int node = 0; node < m_nodes; node++) {
100    for (int j = 0; j < m_virtual_networks; j++) {
101      m_toNetQueues[node][j]->clear();
102      m_fromNetQueues[node][j]->clear();
103    }
104  }
105
106  for(int i=0; i<m_switch_ptr_vector.size(); i++){
107    m_switch_ptr_vector[i]->clearBuffers();
108  }
109}
110
111SimpleNetwork::~SimpleNetwork()
112{
113  for (int i = 0; i < m_nodes; i++) {
114    m_toNetQueues[i].deletePointers();
115    m_fromNetQueues[i].deletePointers();
116  }
117  m_switch_ptr_vector.deletePointers();
118  m_buffers_to_free.deletePointers();
119  delete m_topology_ptr;
120}
121
122// From a switch to an endpoint node
123void SimpleNetwork::makeOutLink(SwitchID src, NodeID dest, const NetDest& routing_table_entry, int link_latency, int link_weight, int bw_multiplier, bool isReconfiguration)
124{
125  assert(dest < m_nodes);
126  assert(src < m_switch_ptr_vector.size());
127  assert(m_switch_ptr_vector[src] != NULL);
128  if(!isReconfiguration){
129    m_switch_ptr_vector[src]->addOutPort(m_fromNetQueues[dest], routing_table_entry, link_latency, bw_multiplier);
130    m_endpoint_switches[dest] = m_switch_ptr_vector[src];
131  } else {
132    m_switch_ptr_vector[src]->reconfigureOutPort(routing_table_entry);
133  }
134}
135
136// From an endpoint node to a switch
137void SimpleNetwork::makeInLink(NodeID src, SwitchID dest, const NetDest& routing_table_entry, int link_latency, int bw_multiplier, bool isReconfiguration)
138{
139  assert(src < m_nodes);
140  if(!isReconfiguration){
141    m_switch_ptr_vector[dest]->addInPort(m_toNetQueues[src]);
142  } else {
143    // do nothing
144  }
145}
146
147// From a switch to a switch
148void SimpleNetwork::makeInternalLink(SwitchID src, SwitchID dest, const NetDest& routing_table_entry, int link_latency, int link_weight, int bw_multiplier, bool isReconfiguration)
149{
150  if(!isReconfiguration){
151    // Create a set of new MessageBuffers
152    Vector<MessageBuffer*> queues;
153    for (int i = 0; i < m_virtual_networks; i++) {
154      // allocate a buffer
155      MessageBuffer* buffer_ptr = new MessageBuffer;
156      buffer_ptr->setOrdering(true);
157      if(FINITE_BUFFERING) {
158        buffer_ptr->setSize(FINITE_BUFFER_SIZE);
159      }
160      queues.insertAtBottom(buffer_ptr);
161      // remember to deallocate it
162      m_buffers_to_free.insertAtBottom(buffer_ptr);
163    }
164
165    // Connect it to the two switches
166    m_switch_ptr_vector[dest]->addInPort(queues);
167    m_switch_ptr_vector[src]->addOutPort(queues, routing_table_entry, link_latency, bw_multiplier);
168  } else {
169    m_switch_ptr_vector[src]->reconfigureOutPort(routing_table_entry);
170  }
171}
172
173void SimpleNetwork::checkNetworkAllocation(NodeID id, bool ordered, int network_num)
174{
175  ASSERT(id < m_nodes);
176  ASSERT(network_num < m_virtual_networks);
177
178  if (ordered) {
179    m_ordered[network_num] = true;
180  }
181  m_in_use[network_num] = true;
182}
183
184MessageBuffer* SimpleNetwork::getToNetQueue(NodeID id, bool ordered, int network_num)
185{
186  checkNetworkAllocation(id, ordered, network_num);
187  return m_toNetQueues[id][network_num];
188}
189
190MessageBuffer* SimpleNetwork::getFromNetQueue(NodeID id, bool ordered, int network_num)
191{
192  checkNetworkAllocation(id, ordered, network_num);
193  return m_fromNetQueues[id][network_num];
194}
195
196const Vector<Throttle*>* SimpleNetwork::getThrottles(NodeID id) const
197{
198  assert(id >= 0);
199  assert(id < m_nodes);
200  assert(m_endpoint_switches[id] != NULL);
201  return m_endpoint_switches[id]->getThrottles();
202}
203
204void SimpleNetwork::printStats(ostream& out) const
205{
206  out << endl;
207  out << "Network Stats" << endl;
208  out << "-------------" << endl;
209  out << endl;
210  for(int i=0; i<m_switch_ptr_vector.size(); i++) {
211    m_switch_ptr_vector[i]->printStats(out);
212  }
213}
214
215void SimpleNetwork::clearStats()
216{
217  for(int i=0; i<m_switch_ptr_vector.size(); i++) {
218    m_switch_ptr_vector[i]->clearStats();
219  }
220}
221
222void SimpleNetwork::printConfig(ostream& out) const
223{
224  out << endl;
225  out << "Network Configuration" << endl;
226  out << "---------------------" << endl;
227  out << "network: SIMPLE_NETWORK" << endl;
228  out << "topology: " << g_NETWORK_TOPOLOGY << endl;
229  out << endl;
230
231  for (int i = 0; i < m_virtual_networks; i++) {
232    out << "virtual_net_" << i << ": ";
233    if (m_in_use[i]) {
234      out << "active, ";
235      if (m_ordered[i]) {
236        out << "ordered" << endl;
237      } else {
238        out << "unordered" << endl;
239      }
240    } else {
241      out << "inactive" << endl;
242    }
243  }
244  out << endl;
245  for(int i=0; i<m_switch_ptr_vector.size(); i++) {
246    m_switch_ptr_vector[i]->printConfig(out);
247  }
248
249  if (g_PRINT_TOPOLOGY) {
250    m_topology_ptr->printConfig(out);
251  }
252}
253
254void SimpleNetwork::print(ostream& out) const
255{
256  out << "[SimpleNetwork]";
257}
258