SimpleNetwork.cc revision 7055
16145Snate@binkert.org/*
26145Snate@binkert.org * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
36145Snate@binkert.org * All rights reserved.
46145Snate@binkert.org *
56145Snate@binkert.org * Redistribution and use in source and binary forms, with or without
66145Snate@binkert.org * modification, are permitted provided that the following conditions are
76145Snate@binkert.org * met: redistributions of source code must retain the above copyright
86145Snate@binkert.org * notice, this list of conditions and the following disclaimer;
96145Snate@binkert.org * redistributions in binary form must reproduce the above copyright
106145Snate@binkert.org * notice, this list of conditions and the following disclaimer in the
116145Snate@binkert.org * documentation and/or other materials provided with the distribution;
126145Snate@binkert.org * neither the name of the copyright holders nor the names of its
136145Snate@binkert.org * contributors may be used to endorse or promote products derived from
146145Snate@binkert.org * this software without specific prior written permission.
156145Snate@binkert.org *
166145Snate@binkert.org * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
176145Snate@binkert.org * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
186145Snate@binkert.org * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
196145Snate@binkert.org * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
206145Snate@binkert.org * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
216145Snate@binkert.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
226145Snate@binkert.org * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
236145Snate@binkert.org * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
246145Snate@binkert.org * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
256145Snate@binkert.org * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
266145Snate@binkert.org * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
276145Snate@binkert.org */
286145Snate@binkert.org
297054Snate@binkert.org#include "mem/gems_common/Map.hh"
307054Snate@binkert.org#include "mem/protocol/MachineType.hh"
317054Snate@binkert.org#include "mem/protocol/Protocol.hh"
327054Snate@binkert.org#include "mem/protocol/TopologyType.hh"
337054Snate@binkert.org#include "mem/ruby/buffers/MessageBuffer.hh"
347054Snate@binkert.org#include "mem/ruby/common/NetDest.hh"
356154Snate@binkert.org#include "mem/ruby/network/simple/SimpleNetwork.hh"
367054Snate@binkert.org#include "mem/ruby/network/simple/Switch.hh"
377054Snate@binkert.org#include "mem/ruby/network/simple/Topology.hh"
386154Snate@binkert.org#include "mem/ruby/profiler/Profiler.hh"
396154Snate@binkert.org#include "mem/ruby/system/System.hh"
406145Snate@binkert.org
417055Snate@binkert.orgusing namespace std;
427055Snate@binkert.org
437054Snate@binkert.org#if 0
446284Snate@binkert.org// ***BIG HACK*** - This is actually code that _should_ be in Network.cc
456145Snate@binkert.org
466145Snate@binkert.org// Note: Moved to Princeton Network
476145Snate@binkert.org// calls new to abstract away from the network
487054Snate@binkert.orgNetwork*
497054Snate@binkert.orgNetwork::createNetwork(int nodes)
506145Snate@binkert.org{
517054Snate@binkert.org    return new SimpleNetwork(nodes);
526145Snate@binkert.org}
537054Snate@binkert.org#endif
546145Snate@binkert.org
556876Ssteve.reinhardt@amd.comSimpleNetwork::SimpleNetwork(const Params *p)
566876Ssteve.reinhardt@amd.com    : Network(p)
576285Snate@binkert.org{
587054Snate@binkert.org    // Note: the parent Network Object constructor is called before the
597054Snate@binkert.org    // SimpleNetwork child constructor.  Therefore, the member variables
607054Snate@binkert.org    // used below should already be initialized.
616285Snate@binkert.org
627054Snate@binkert.org    m_endpoint_switches.setSize(m_nodes);
636285Snate@binkert.org
647054Snate@binkert.org    m_in_use.setSize(m_virtual_networks);
657054Snate@binkert.org    m_ordered.setSize(m_virtual_networks);
667054Snate@binkert.org    for (int i = 0; i < m_virtual_networks; i++) {
677054Snate@binkert.org        m_in_use[i] = false;
687054Snate@binkert.org        m_ordered[i] = false;
696285Snate@binkert.org    }
707054Snate@binkert.org
717054Snate@binkert.org    // Allocate to and from queues
727054Snate@binkert.org    m_toNetQueues.setSize(m_nodes);
737054Snate@binkert.org    m_fromNetQueues.setSize(m_nodes);
747054Snate@binkert.org    for (int node = 0; node < m_nodes; node++) {
757054Snate@binkert.org        m_toNetQueues[node].setSize(m_virtual_networks);
767054Snate@binkert.org        m_fromNetQueues[node].setSize(m_virtual_networks);
777054Snate@binkert.org        for (int j = 0; j < m_virtual_networks; j++) {
787054Snate@binkert.org            m_toNetQueues[node][j] = new MessageBuffer(
797054Snate@binkert.org                "toNet node "+int_to_string(node)+" j "+int_to_string(j));
807054Snate@binkert.org            m_fromNetQueues[node][j] = new MessageBuffer(
817054Snate@binkert.org                "fromNet node "+int_to_string(node)+" j "+int_to_string(j));
827054Snate@binkert.org        }
837054Snate@binkert.org    }
846881SBrad.Beckmann@amd.com}
856285Snate@binkert.org
867054Snate@binkert.orgvoid
877054Snate@binkert.orgSimpleNetwork::init()
886881SBrad.Beckmann@amd.com{
897054Snate@binkert.org    Network::init();
906881SBrad.Beckmann@amd.com
917054Snate@binkert.org    // The topology pointer should have already been initialized in
927054Snate@binkert.org    // the parent class network constructor.
937054Snate@binkert.org    assert(m_topology_ptr != NULL);
947054Snate@binkert.org    int number_of_switches = m_topology_ptr->numSwitches();
957054Snate@binkert.org    for (int i = 0; i < number_of_switches; i++) {
967054Snate@binkert.org        m_switch_ptr_vector.insertAtBottom(new Switch(i, this));
977054Snate@binkert.org    }
986881SBrad.Beckmann@amd.com
997054Snate@binkert.org    // false because this isn't a reconfiguration
1007054Snate@binkert.org    m_topology_ptr->createLinks(this, false);
1016285Snate@binkert.org}
1026145Snate@binkert.org
1037054Snate@binkert.orgvoid
1047054Snate@binkert.orgSimpleNetwork::reset()
1056145Snate@binkert.org{
1067054Snate@binkert.org    for (int node = 0; node < m_nodes; node++) {
1077054Snate@binkert.org        for (int j = 0; j < m_virtual_networks; j++) {
1087054Snate@binkert.org            m_toNetQueues[node][j]->clear();
1097054Snate@binkert.org            m_fromNetQueues[node][j]->clear();
1107054Snate@binkert.org        }
1116145Snate@binkert.org    }
1126145Snate@binkert.org
1137054Snate@binkert.org    for(int i = 0; i < m_switch_ptr_vector.size(); i++){
1147054Snate@binkert.org        m_switch_ptr_vector[i]->clearBuffers();
1157054Snate@binkert.org    }
1166145Snate@binkert.org}
1176145Snate@binkert.org
1186145Snate@binkert.orgSimpleNetwork::~SimpleNetwork()
1196145Snate@binkert.org{
1207054Snate@binkert.org    for (int i = 0; i < m_nodes; i++) {
1217054Snate@binkert.org        m_toNetQueues[i].deletePointers();
1227054Snate@binkert.org        m_fromNetQueues[i].deletePointers();
1237054Snate@binkert.org    }
1247054Snate@binkert.org    m_switch_ptr_vector.deletePointers();
1257054Snate@binkert.org    m_buffers_to_free.deletePointers();
1267054Snate@binkert.org    // delete m_topology_ptr;
1276145Snate@binkert.org}
1286145Snate@binkert.org
1296145Snate@binkert.org// From a switch to an endpoint node
1307054Snate@binkert.orgvoid
1317054Snate@binkert.orgSimpleNetwork::makeOutLink(SwitchID src, NodeID dest,
1327054Snate@binkert.org    const NetDest& routing_table_entry, int link_latency, int link_weight,
1337054Snate@binkert.org    int bw_multiplier, bool isReconfiguration)
1346145Snate@binkert.org{
1357054Snate@binkert.org    assert(dest < m_nodes);
1367054Snate@binkert.org    assert(src < m_switch_ptr_vector.size());
1377054Snate@binkert.org    assert(m_switch_ptr_vector[src] != NULL);
1387054Snate@binkert.org
1397054Snate@binkert.org    if (isReconfiguration) {
1407054Snate@binkert.org        m_switch_ptr_vector[src]->reconfigureOutPort(routing_table_entry);
1417054Snate@binkert.org        return;
1427054Snate@binkert.org    }
1437054Snate@binkert.org
1447054Snate@binkert.org    m_switch_ptr_vector[src]->addOutPort(m_fromNetQueues[dest],
1457054Snate@binkert.org        routing_table_entry, link_latency, bw_multiplier);
1466145Snate@binkert.org    m_endpoint_switches[dest] = m_switch_ptr_vector[src];
1476145Snate@binkert.org}
1486145Snate@binkert.org
1496145Snate@binkert.org// From an endpoint node to a switch
1507054Snate@binkert.orgvoid
1517054Snate@binkert.orgSimpleNetwork::makeInLink(NodeID src, SwitchID dest,
1527054Snate@binkert.org    const NetDest& routing_table_entry, int link_latency, int bw_multiplier,
1537054Snate@binkert.org    bool isReconfiguration)
1546145Snate@binkert.org{
1557054Snate@binkert.org    assert(src < m_nodes);
1567054Snate@binkert.org    if (isReconfiguration) {
1577054Snate@binkert.org        // do nothing
1587054Snate@binkert.org        return;
1597054Snate@binkert.org    }
1607054Snate@binkert.org
1616145Snate@binkert.org    m_switch_ptr_vector[dest]->addInPort(m_toNetQueues[src]);
1626145Snate@binkert.org}
1636145Snate@binkert.org
1646145Snate@binkert.org// From a switch to a switch
1657054Snate@binkert.orgvoid
1667054Snate@binkert.orgSimpleNetwork::makeInternalLink(SwitchID src, SwitchID dest,
1677054Snate@binkert.org    const NetDest& routing_table_entry, int link_latency, int link_weight,
1687054Snate@binkert.org    int bw_multiplier, bool isReconfiguration)
1696145Snate@binkert.org{
1707054Snate@binkert.org    if (isReconfiguration) {
1717054Snate@binkert.org        m_switch_ptr_vector[src]->reconfigureOutPort(routing_table_entry);
1727054Snate@binkert.org        return;
1737054Snate@binkert.org    }
1747054Snate@binkert.org
1756145Snate@binkert.org    // Create a set of new MessageBuffers
1766145Snate@binkert.org    Vector<MessageBuffer*> queues;
1776145Snate@binkert.org    for (int i = 0; i < m_virtual_networks; i++) {
1787054Snate@binkert.org        // allocate a buffer
1797054Snate@binkert.org        MessageBuffer* buffer_ptr = new MessageBuffer;
1807054Snate@binkert.org        buffer_ptr->setOrdering(true);
1817054Snate@binkert.org        if (m_buffer_size > 0) {
1827054Snate@binkert.org            buffer_ptr->setSize(m_buffer_size);
1837054Snate@binkert.org        }
1847054Snate@binkert.org        queues.insertAtBottom(buffer_ptr);
1857054Snate@binkert.org        // remember to deallocate it
1867054Snate@binkert.org        m_buffers_to_free.insertAtBottom(buffer_ptr);
1877054Snate@binkert.org    }
1887054Snate@binkert.org    // Connect it to the two switches
1897054Snate@binkert.org    m_switch_ptr_vector[dest]->addInPort(queues);
1907054Snate@binkert.org    m_switch_ptr_vector[src]->addOutPort(queues, routing_table_entry,
1917054Snate@binkert.org        link_latency, bw_multiplier);
1927054Snate@binkert.org}
1937054Snate@binkert.org
1947054Snate@binkert.orgvoid
1957054Snate@binkert.orgSimpleNetwork::checkNetworkAllocation(NodeID id, bool ordered, int network_num)
1967054Snate@binkert.org{
1977054Snate@binkert.org    ASSERT(id < m_nodes);
1987054Snate@binkert.org    ASSERT(network_num < m_virtual_networks);
1997054Snate@binkert.org
2007054Snate@binkert.org    if (ordered) {
2017054Snate@binkert.org        m_ordered[network_num] = true;
2027054Snate@binkert.org    }
2037054Snate@binkert.org    m_in_use[network_num] = true;
2047054Snate@binkert.org}
2057054Snate@binkert.org
2067054Snate@binkert.orgMessageBuffer*
2077054Snate@binkert.orgSimpleNetwork::getToNetQueue(NodeID id, bool ordered, int network_num)
2087054Snate@binkert.org{
2097054Snate@binkert.org    checkNetworkAllocation(id, ordered, network_num);
2107054Snate@binkert.org    return m_toNetQueues[id][network_num];
2117054Snate@binkert.org}
2127054Snate@binkert.org
2137054Snate@binkert.orgMessageBuffer*
2147054Snate@binkert.orgSimpleNetwork::getFromNetQueue(NodeID id, bool ordered, int network_num)
2157054Snate@binkert.org{
2167054Snate@binkert.org    checkNetworkAllocation(id, ordered, network_num);
2177054Snate@binkert.org    return m_fromNetQueues[id][network_num];
2187054Snate@binkert.org}
2197054Snate@binkert.org
2207054Snate@binkert.orgconst Vector<Throttle*>*
2217054Snate@binkert.orgSimpleNetwork::getThrottles(NodeID id) const
2227054Snate@binkert.org{
2237054Snate@binkert.org    assert(id >= 0);
2247054Snate@binkert.org    assert(id < m_nodes);
2257054Snate@binkert.org    assert(m_endpoint_switches[id] != NULL);
2267054Snate@binkert.org    return m_endpoint_switches[id]->getThrottles();
2277054Snate@binkert.org}
2287054Snate@binkert.org
2297054Snate@binkert.orgvoid
2307054Snate@binkert.orgSimpleNetwork::printStats(ostream& out) const
2317054Snate@binkert.org{
2327054Snate@binkert.org    out << endl;
2337054Snate@binkert.org    out << "Network Stats" << endl;
2347054Snate@binkert.org    out << "-------------" << endl;
2357054Snate@binkert.org    out << endl;
2367054Snate@binkert.org    for (int i = 0; i < m_switch_ptr_vector.size(); i++) {
2377054Snate@binkert.org        m_switch_ptr_vector[i]->printStats(out);
2387054Snate@binkert.org    }
2397054Snate@binkert.org    m_topology_ptr->printStats(out);
2407054Snate@binkert.org}
2417054Snate@binkert.org
2427054Snate@binkert.orgvoid
2437054Snate@binkert.orgSimpleNetwork::clearStats()
2447054Snate@binkert.org{
2457054Snate@binkert.org    for (int i = 0; i < m_switch_ptr_vector.size(); i++) {
2467054Snate@binkert.org        m_switch_ptr_vector[i]->clearStats();
2477054Snate@binkert.org    }
2487054Snate@binkert.org    m_topology_ptr->clearStats();
2497054Snate@binkert.org}
2507054Snate@binkert.org
2517054Snate@binkert.orgvoid
2527054Snate@binkert.orgSimpleNetwork::printConfig(ostream& out) const
2537054Snate@binkert.org{
2547054Snate@binkert.org    out << endl;
2557054Snate@binkert.org    out << "Network Configuration" << endl;
2567054Snate@binkert.org    out << "---------------------" << endl;
2577054Snate@binkert.org    out << "network: SIMPLE_NETWORK" << endl;
2587054Snate@binkert.org    out << "topology: " << m_topology_ptr->getName() << endl;
2597054Snate@binkert.org    out << endl;
2607054Snate@binkert.org
2617054Snate@binkert.org    for (int i = 0; i < m_virtual_networks; i++) {
2627054Snate@binkert.org        out << "virtual_net_" << i << ": ";
2637054Snate@binkert.org        if (m_in_use[i]) {
2647054Snate@binkert.org            out << "active, ";
2657054Snate@binkert.org            if (m_ordered[i]) {
2667054Snate@binkert.org                out << "ordered" << endl;
2677054Snate@binkert.org            } else {
2687054Snate@binkert.org                out << "unordered" << endl;
2697054Snate@binkert.org            }
2707054Snate@binkert.org        } else {
2717054Snate@binkert.org            out << "inactive" << endl;
2727054Snate@binkert.org        }
2737054Snate@binkert.org    }
2747054Snate@binkert.org    out << endl;
2757054Snate@binkert.org
2767054Snate@binkert.org    for(int i = 0; i < m_switch_ptr_vector.size(); i++) {
2777054Snate@binkert.org        m_switch_ptr_vector[i]->printConfig(out);
2786145Snate@binkert.org    }
2796145Snate@binkert.org
2807054Snate@binkert.org    m_topology_ptr->printConfig(out);
2816145Snate@binkert.org}
2826145Snate@binkert.org
2837054Snate@binkert.orgvoid
2847054Snate@binkert.orgSimpleNetwork::print(ostream& out) const
2856145Snate@binkert.org{
2867054Snate@binkert.org    out << "[SimpleNetwork]";
2876145Snate@binkert.org}
2886876Ssteve.reinhardt@amd.com
2896876Ssteve.reinhardt@amd.com
2906876Ssteve.reinhardt@amd.comSimpleNetwork *
2916876Ssteve.reinhardt@amd.comSimpleNetworkParams::create()
2926876Ssteve.reinhardt@amd.com{
2936876Ssteve.reinhardt@amd.com    return new SimpleNetwork(this);
2946876Ssteve.reinhardt@amd.com}
295