From c1a515bc041cca8a74a8c4b53ba8f5837384201d Mon Sep 17 00:00:00 2001 From: Salim Suhet Mussi Date: Thu, 1 Oct 2026 20:57:24 -0300 Subject: [PATCH] Add -seed to htsim_uec_df / htsim_uec_sf and seed switch RNGs per switch Both binaries hardcoded seed 13 and had no -seed option, and every DragonflySwitch / SlimFlySwitch seeded its std::mt19937 with the same constant (33), so all switches draw identical random sequences (e.g. the same VALIANT intermediates). -seed N now reseeds srand/srandom and seeds each switch generator with std::seed_seq{N, switch id}. Without -seed the behaviour is unchanged. --- htsim/sim/datacenter/dragonfly_switch.cpp | 8 +++++++- htsim/sim/datacenter/dragonfly_switch.h | 3 +++ htsim/sim/datacenter/main_uec_df.cpp | 11 +++++++++++ htsim/sim/datacenter/main_uec_sf.cpp | 11 +++++++++++ htsim/sim/datacenter/slimfly_switch.cpp | 8 +++++++- htsim/sim/datacenter/slimfly_switch.h | 3 +++ 6 files changed, 42 insertions(+), 2 deletions(-) diff --git a/htsim/sim/datacenter/dragonfly_switch.cpp b/htsim/sim/datacenter/dragonfly_switch.cpp index 5f1c14801..566775b23 100644 --- a/htsim/sim/datacenter/dragonfly_switch.cpp +++ b/htsim/sim/datacenter/dragonfly_switch.cpp @@ -7,6 +7,7 @@ bool DragonflySwitch::_trim_disable = false; uint16_t DragonflySwitch::_trim_size = 0; +int64_t DragonflySwitch::_rng_seed = -1; DragonflySwitch::RoutingStrategy DragonflySwitch::_routing_strategy = DragonflySwitch::MINIMAL; string ntoa(double n); @@ -30,7 +31,12 @@ DragonflySwitch::DragonflySwitch(EventList& event_list, _pipe = new CallbackPipe(delay, event_list, this); - _generator = std::mt19937(33); + if (_rng_seed < 0) { + _generator = std::mt19937(33); + } else { + std::seed_seq seq{(uint32_t)_rng_seed, (uint32_t)_id}; + _generator = std::mt19937(seq); + } _dist_a = std::uniform_int_distribution<>(0, _a - 1); _dist_h = std::uniform_int_distribution<>(0, _h - 1); diff --git a/htsim/sim/datacenter/dragonfly_switch.h b/htsim/sim/datacenter/dragonfly_switch.h index 3212ad837..d987a1b99 100644 --- a/htsim/sim/datacenter/dragonfly_switch.h +++ b/htsim/sim/datacenter/dragonfly_switch.h @@ -37,6 +37,8 @@ class DragonflySwitch : public Switch { } static uint16_t get_trim_disable() { return _trim_disable; } static uint16_t get_trim_size() { return _trim_size; } + // -1 keeps the legacy behaviour (every switch seeded with 33). + static void set_rng_seed(int64_t seed) { _rng_seed = seed; } private: SwitchType _type; @@ -70,6 +72,7 @@ class DragonflySwitch : public Switch { static bool _trim_disable; static uint16_t _trim_size; static RoutingStrategy _routing_strategy; + static int64_t _rng_seed; }; #endif diff --git a/htsim/sim/datacenter/main_uec_df.cpp b/htsim/sim/datacenter/main_uec_df.cpp index a46d1d1ae..30ccd9564 100644 --- a/htsim/sim/datacenter/main_uec_df.cpp +++ b/htsim/sim/datacenter/main_uec_df.cpp @@ -32,6 +32,7 @@ int main(int argc, char** argv) { Clock c(timeFromSec(5 / 100.), eventlist); int seed = 13; + bool seed_set = false; srand(seed); srandom(seed); @@ -163,6 +164,10 @@ int main(int argc, char** argv) { else throw std::logic_error("LB not recognized"); i++; + } else if (!strcmp(argv[i], "-seed")) { + seed = std::stoi(argv[i + 1]); + seed_set = true; + i++; } else if (!strcmp(argv[i], "-paths")) { path_entropy_size = std::stoi(argv[i + 1]); i++; @@ -280,6 +285,12 @@ int main(int argc, char** argv) { } // Topology + if (seed_set) { + srand(seed); + srandom(seed); + DragonflySwitch::set_rng_seed(seed); + std::cout << "Random seed " << seed << std::endl; + } DragonflySwitch::set_config(routing_strategy, trim_disable, trim_size); DragonflyTopology* topo = new DragonflyTopology(qt, queuesize, qlf, &eventlist); topo->load_topology(topo_base_path); diff --git a/htsim/sim/datacenter/main_uec_sf.cpp b/htsim/sim/datacenter/main_uec_sf.cpp index 84bde6bcf..b1c45241e 100644 --- a/htsim/sim/datacenter/main_uec_sf.cpp +++ b/htsim/sim/datacenter/main_uec_sf.cpp @@ -32,6 +32,7 @@ int main(int argc, char** argv) { Clock c(timeFromSec(5 / 100.), eventlist); int seed = 13; + bool seed_set = false; srand(seed); srandom(seed); @@ -165,6 +166,10 @@ int main(int argc, char** argv) { else throw std::logic_error("LB not recognized"); i++; + } else if (!strcmp(argv[i], "-seed")) { + seed = std::stoi(argv[i + 1]); + seed_set = true; + i++; } else if (!strcmp(argv[i], "-paths")) { path_entropy_size = std::stoi(argv[i + 1]); i++; @@ -282,6 +287,12 @@ int main(int argc, char** argv) { } // Topology + if (seed_set) { + srand(seed); + srandom(seed); + SlimFlySwitch::set_rng_seed(seed); + std::cout << "Random seed " << seed << std::endl; + } SlimFlySwitch::set_config(routing_strategy, trim_disable, trim_size); SlimFlyTopology* topo = new SlimFlyTopology(topo_base_path, qt, queuesize, qlf, &eventlist); topo->load_topology(topo_base_path); diff --git a/htsim/sim/datacenter/slimfly_switch.cpp b/htsim/sim/datacenter/slimfly_switch.cpp index 67ab39770..0f0e4ab3c 100644 --- a/htsim/sim/datacenter/slimfly_switch.cpp +++ b/htsim/sim/datacenter/slimfly_switch.cpp @@ -9,6 +9,7 @@ extern void tokenize(std::string const& str, const char delim, std::vectorget_neighbours(_id); - _generator = mt19937(33); + if (_rng_seed < 0) { + _generator = std::mt19937(33); + } else { + std::seed_seq seq{(uint32_t)_rng_seed, (uint32_t)_id}; + _generator = std::mt19937(seq); + } _dist_neighbour = uniform_int_distribution<>(0, _neighbours->size() - 1); } diff --git a/htsim/sim/datacenter/slimfly_switch.h b/htsim/sim/datacenter/slimfly_switch.h index 0b87368dd..a38180352 100644 --- a/htsim/sim/datacenter/slimfly_switch.h +++ b/htsim/sim/datacenter/slimfly_switch.h @@ -42,6 +42,8 @@ class SlimFlySwitch : public Switch { } static uint16_t get_trim_disable() { return _trim_disable; } static uint16_t get_trim_size() { return _trim_size; } + // -1 keeps the legacy behaviour (every switch seeded with 33). + static void set_rng_seed(int64_t seed) { _rng_seed = seed; } private: std::string _path_fib; @@ -71,6 +73,7 @@ class SlimFlySwitch : public Switch { static bool _trim_disable; static uint16_t _trim_size; static RoutingStrategy _routing_strategy; + static int64_t _rng_seed; }; #endif \ No newline at end of file