Skip to content

Commit

Permalink
fear: Add automatic mesh generator
Browse files Browse the repository at this point in the history
Cr. to Gosh.
  • Loading branch information
xusine committed Oct 3, 2024
1 parent b5cd711 commit dc22d17
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 2 deletions.
7 changes: 6 additions & 1 deletion components/NetShim/MemoryNetworkImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,12 @@ class FLEXUS_COMPONENT(MemoryNetwork)
}

nc = new NetContainer();
if (nc->buildNetwork(cfg.NetworkTopologyFile.c_str())) {

if (cfg.NetworkTopologyFile == "BuildMesh") {
if (nc->buildMesh()) {
throw Flexus::Core::FlexusException("Error building the network");
}
} else if (nc->buildNetwork(cfg.NetworkTopologyFile.c_str())) {
throw Flexus::Core::FlexusException("Error building the network");
}
}
Expand Down
5 changes: 4 additions & 1 deletion components/NetShim/NetShimImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ class FLEXUS_COMPONENT(NetShim)
}

nc = new NetContainer();
if (nc->buildNetwork(cfg.NetworkTopologyFile.c_str())) {
if (cfg.NetworkTopologyFile == "BuildMesh") {
if (nc->buildMesh())
throw Flexus::Core::FlexusException("Error building the network");
} else if (nc->buildNetwork(cfg.NetworkTopologyFile.c_str())) {
throw Flexus::Core::FlexusException("Error building the network");
}
}
Expand Down
138 changes: 138 additions & 0 deletions components/NetShim/netcontainer.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

#include <core/component.hpp>
#include "netcontainer.hpp"

#include <fstream>
Expand Down Expand Up @@ -74,6 +75,143 @@ NetContainer::NetContainer(void)
{
}

bool NetContainer::buildMesh() {
// fixed for now
channelLatency = 3;
channelLatencyData = 4;
channelLatencyControl = 1;
localChannelLatencyDivider = 1;
switchInputBuffers = 6;
switchOutputBuffers = 6;
switchInternalBuffersPerVC = 6;

numSwitches = Flexus::Core::ComponentManager::getComponentManager().systemWidth();
numNodes = numSwitches * 3;
switchPorts = 7; // 3 ports from/to node and 4 ports for up/down/left/right
switchBandwidth = 4;

if (allocateNetworkStructures())
return true;

// nodes to switches
for (int i = 0; i < numNodes; i++) {
auto s = i % numSwitches;
auto p = i / numSwitches;

if (attachNodeChannels(this, i))
return true;
if (attachSwitchChannels(this, s, p, true))
return true;
if (switches[s]->setLocalDelayOnly(p))
return true;

maxChannelIndex += 2;
}

#define U 3
#define D 4
#define L 5
#define R 6

// switches to switches
int col = -1;
int row = -1;

// ad hoc
for (int i = 1; i < 9; i++)
for (int j = 1; j < 3; j++)
if (i * i * j == numSwitches) {
col = i * j;
row = i;
break;
}

assert(col >= 0);

int col_m1 = col - 1;
int row_m1 = row - 1;

for (int i = 0; i < row; i++)
for (int j = 0; j < col; j++) {
auto s = i * col + j;

if (j < col_m1) {
auto r = s + 1;

if (attachSwitchChannels(this, s, R, false))
return true;
if (attachSwitchChannels(this, r, L, true))
return true;

maxChannelIndex += 2;
}

if (i < row_m1) {
auto d = s + col;

if (attachSwitchChannels(this, s, D, false))
return true;
if (attachSwitchChannels(this, d, U, true))
return true;

maxChannelIndex += 2;
}
}

// routes
for (int i = 0; i < numSwitches; i++) {
auto c = i % col;
auto r = i / col;

for (int j = 0; j < numNodes; j++) {
auto s = j % numSwitches;
auto p = j / numSwitches;

auto cc = s % col;
auto rr = s / col;

// x-y
if (c < cc) {
if (switches[i]->addRoutingEntry(j, R, 0))
return true;
} else if (c > cc) {
if (switches[i]->addRoutingEntry(j, L, 0))
return true;
} else if (r < rr) {
if (switches[i]->addRoutingEntry(j, D, 0))
return true;
} else if (r > rr) {
if (switches[i]->addRoutingEntry(j, U, 0))
return true;
} else {
if (switches[i]->addRoutingEntry(j, p, 0))
return true;
}

// y-x
if (r < rr) {
if (switches[i]->addRoutingEntry(j, D, 1))
return true;
} else if (r > rr) {
if (switches[i]->addRoutingEntry(j, U, 1))
return true;
} else if (c < cc) {
if (switches[i]->addRoutingEntry(j, R, 1))
return true;
} else if (c > cc) {
if (switches[i]->addRoutingEntry(j, L, 1))
return true;
} else {
if (switches[i]->addRoutingEntry(j, p, 1))
return true;
}
}
}

return false;
}


bool
NetContainer::buildNetwork(const char* filename)
{
Expand Down
1 change: 1 addition & 0 deletions components/NetShim/netcontainer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class NetContainer
NetContainer(void);

public:
bool buildMesh();
bool buildNetwork(const char* filename);

bool drive(void);
Expand Down

0 comments on commit dc22d17

Please sign in to comment.