// tests/mgpu/device_set_test.cpp - the topology data model: ranking rule, link figures, JSON in and out, synthetic builders. CPU only, no GPU. #include "strata/mgpu/device_set.hpp" #include #include #include using namespace strata::mgpu; namespace { int g_fail = 0; void check(bool ok, const std::string& what) { if (ok) return; std::fprintf(stderr, "FAIL: %s\n", what.c_str()); ++g_fail; } bool near(double a, double b, double rel = 1e-3) { return std::fabs(a - b) <= rel * std::max(1.0, std::fabs(b)); } SyntheticDevice card(const char* name, int sms, double ghz, uint64_t gib) { SyntheticDevice d; d.name = name; d.sms = sms; d.clock_ghz = ghz; d.vram_total = gib << 30; return d; } } // namespace int main() { // 1. the ranking rule: speed (SMs x clock) first, memory on a near tie, the lower index after that { DeviceSet s = make_synthetic(Backend::Cuda, {card("slow", 48, 1.5, 16), card("fast", 84, 2.6, 16)}); check(s.primary() == 1, "the faster card is primary"); DeviceSet t = make_synthetic(Backend::Cuda, {card("a", 48, 1.5, 16), card("b", 48, 1.5, 24)}); check(t.primary() == 1, "two equally fast cards: the larger memory is primary"); DeviceSet u = make_synthetic(Backend::Cuda, {card("a", 48, 1.5, 16), card("b", 48, 1.5, 16)}); check(u.primary() == 0, "identical cards: the lower index"); DeviceSet w = u; // two identical cards report sizes a few MiB apart (z440: 16748576768 and 16750149632 bytes): still the lower index w.dev_mut(1).vram_total += 1572864; check(w.primary() == 0, "identical cards whose memory differs by MiB: the lower index"); DeviceSet v = make_synthetic(Backend::Cuda, {card("a", 48, 1.50, 16), card("b", 48, 1.52, 16), card("c", 48, 1.49, 24)}); check(v.primary() == 2, "within two percent counts as equal speed, so the 24 GB card wins"); check(DeviceSet().primary() == -1, "an empty set has no primary"); } // 2. synthetic builder: staged figures follow each end's host link { SyntheticDevice a = card("a", 48, 1.5, 16), b = card("b", 48, 1.5, 16); a.d2h_gbps = 24; a.h2d_gbps = 22; a.d2h_us = 10; a.h2d_us = 11; b.d2h_gbps = 12; b.h2d_gbps = 14; b.d2h_us = 20; b.h2d_us = 21; DeviceSet s = make_synthetic(Backend::Cuda, {a, b}); check(near(s.link(0, 1).gbps_staged, 14.0), "staged 0->1 = min(d2h of 0, h2d of 1)"); check(near(s.link(1, 0).gbps_staged, 12.0), "staged 1->0 = min(d2h of 1, h2d of 0)"); check(near(s.link(0, 1).lat_staged_us, 10 + 21), "staged latency 0->1 = the two hops"); check(s.link(0, 1).path == Path::HostStaged && s.link(1, 0).path == Path::HostStaged, "synthetic pairs start host-staged"); check(near(s.link(0, 1).gbps(), 14.0) && near(s.link(0, 1).latency_us(), 31.0), "a host-staged link answers with the staged figures"); set_p2p(s, 0, 1, 50.0, 6.0, true); check(s.link(0, 1).path == Path::P2PNvlink && s.link(1, 0).atomics && s.link(1, 0).data_ok, "set_p2p marks both directions"); check(near(s.link(0, 1).gbps(), 50.0) && near(s.link(1, 0).latency_us(), 6.0), "a P2P link answers with the peer-copy figures"); set_host_link(s, 1, 4, 2, 3.5, 3.4, 30.0); check(s.dev(1).pcie_width == 2 && near(s.dev(1).h2d_gbps, 3.5), "set_host_link updates the card"); check(near(s.link(0, 1).gbps_staged, 3.5) && near(s.link(1, 0).gbps_staged, 3.4), "and every pair through it"); check(s.link(0, 1).path == Path::P2PNvlink, "the P2P path is not clobbered by a host-link change"); check(s.link(0, 0).path == Path::None && s.link(0, 7).path == Path::None, "a link to itself or to nothing is empty"); } // 3. JSON round trip, pretty and compact { DeviceSet s = make_synthetic(Backend::Hip, {card("R9700 \"a\"", 64, 2.4, 32), card("R9700 b", 64, 2.4, 32), card("R9700 c", 64, 2.4, 32), card("R9700 d", 64, 2.4, 32)}); set_p2p(s, 0, 1, 40.0, 7.5, false); s.link_mut(2, 3).why = "multi\nline\t\"quoted\" \\ reason"; s.driver = "HIP runtime 70000"; s.warnings = {"one", "two \"2\""}; s.dev_mut(2).pci_bdf = "0000:65:00.0"; s.dev_mut(2).uma = true; for (bool pretty : {true, false}) { const std::string j = s.to_json(pretty); DeviceSet r; std::string err; check(DeviceSet::from_json(j, r, err), "from_json accepts to_json output: " + err); check(r.to_json(pretty) == j, std::string("a round trip changes nothing (") + (pretty ? "pretty" : "compact") + ")"); check(r.size() == 4 && r.backend() == Backend::Hip && r.driver == s.driver && r.warnings == s.warnings, "scalar fields survive"); check(r.dev(0).name == "R9700 \"a\"" && r.dev(2).pci_bdf == "0000:65:00.0" && r.dev(2).uma, "device fields survive"); check(r.link(0, 1).path == Path::P2PDirect && near(r.link(1, 0).gbps_p2p, 40.0), "link fields survive"); check(r.link(2, 3).why == "multi\nline\t\"quoted\" \\ reason", "escapes survive"); check(r.primary() == s.primary(), "the primary is recomputed the same"); } check(s.to_json(true).find("\"schema_version\": 1") != std::string::npos, "the schema version is written"); } // 4. JSON rejects what it should { DeviceSet r; std::string err; check(!DeviceSet::from_json("", r, err) && !err.empty(), "empty text is refused"); check(!DeviceSet::from_json("{\"schema_version\": 2, \"devices\": []}", r, err), "an unknown schema version is refused"); check(!DeviceSet::from_json("{\"schema_version\": 1}", r, err), "a missing devices array is refused"); check(!DeviceSet::from_json("{\"schema_version\": 1, \"devices\": [], \"links\": [{\"from\": 0, \"to\": 1}]}", r, err), "a link to a device that is not listed is refused"); check(!DeviceSet::from_json("{\"schema_version\": 1, \"devices\": [} ", r, err), "broken JSON is refused"); check(!DeviceSet::from_json("{\"schema_version\": 1, \"devices\": []} trailing", r, err), "trailing text is refused"); check(DeviceSet::from_json("{\"schema_version\": 1, \"devices\": []}", r, err) && r.size() == 0, "an empty device list is a valid empty set"); check(DeviceSet::from_json("[]", r, err) == false, "an array at the top is refused"); } // 5. text table { DeviceSet s = make_synthetic(Backend::Cuda, {card("fast", 84, 2.6, 16), card("slow", 48, 1.5, 16)}); const std::string t = s.to_text(); check(t.find("<- primary") != std::string::npos && t.find("0 -> 1") != std::string::npos && t.find("1 -> 0") != std::string::npos, "the table names the primary and both directions"); } // 6. names check(backend_from_name(backend_name(Backend::Hip)) == Backend::Hip && backend_from_name("x") == Backend::None, "backend names round trip"); check(path_from_name(path_name(Path::P2PNvlink)) == Path::P2PNvlink && path_from_name("x") == Path::None, "path names round trip"); if (g_fail != 0) { std::fprintf(stderr, "%d check(s) failed\n", g_fail); return 1; } std::puts("mgpu_device_set_test: all checks passed"); return 0; }