mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-05 07:48:45 +00:00
237 lines
12 KiB
C++
237 lines
12 KiB
C++
// tests/mgpu/transport_test.cpp - the host-staged transport (always) and the peer-copy transport (where the probe proves P2P) on two real cards:
|
|
// data both ways at several sizes, the epoch ring, the slot-busy rule, wait timeouts, abort, a two-thread stream of epochs, and the fallback of
|
|
// make_transport. Needs two GPUs; exit 77 = a stated skip.
|
|
#include "strata/mgpu/probe.hpp"
|
|
#include "strata/mgpu/transport.hpp"
|
|
|
|
#include <cuda_runtime.h>
|
|
|
|
#include <atomic>
|
|
#include <chrono>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <memory>
|
|
#include <string>
|
|
#include <thread>
|
|
#include <vector>
|
|
|
|
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;
|
|
}
|
|
|
|
std::vector<uint8_t> pattern(uint64_t epoch, size_t n, int salt) {
|
|
std::vector<uint8_t> v(n);
|
|
uint64_t x = epoch * 0x9E3779B97F4A7C15ull + (uint64_t) salt * 7919u + 1;
|
|
for (size_t i = 0; i < n; ++i) { x ^= x << 13; x ^= x >> 7; x ^= x << 17; v[i] = (uint8_t) x; }
|
|
return v;
|
|
}
|
|
|
|
struct Side {
|
|
int dev = 0;
|
|
cudaStream_t stream = nullptr;
|
|
void* buf = nullptr;
|
|
size_t cap = 0;
|
|
bool init(int d, size_t c) {
|
|
dev = d;
|
|
cap = c;
|
|
return cudaSetDevice(d) == cudaSuccess && cudaStreamCreateWithFlags(&stream, cudaStreamNonBlocking) == cudaSuccess && cudaMalloc(&buf, c) == cudaSuccess;
|
|
}
|
|
~Side() {
|
|
(void) cudaSetDevice(dev);
|
|
if (stream) (void) cudaStreamDestroy(stream);
|
|
if (buf) (void) cudaFree(buf);
|
|
}
|
|
};
|
|
|
|
// upload `data` to the sender's buffer and post it
|
|
bool send(Transport& t, Side& from, const std::vector<uint8_t>& data, uint64_t epoch) {
|
|
(void) cudaSetDevice(from.dev);
|
|
if (cudaMemcpyAsync(from.buf, data.data(), data.size(), cudaMemcpyHostToDevice, from.stream) != cudaSuccess) return false;
|
|
if (cudaStreamSynchronize(from.stream) != cudaSuccess) return false;
|
|
return t.post(from.dev, from.buf, data.size(), epoch, from.stream);
|
|
}
|
|
|
|
// wait on the receiver's stream, copy the payload to the host, release
|
|
bool recv(Transport& t, Side& to, size_t n, uint64_t epoch, std::vector<uint8_t>& out) {
|
|
out.assign(n, 0);
|
|
if (!t.wait_device(to.dev, epoch, to.stream)) return false;
|
|
(void) cudaSetDevice(to.dev);
|
|
if (cudaMemcpyAsync(out.data(), t.recv_buffer(to.dev, epoch), n, cudaMemcpyDeviceToHost, to.stream) != cudaSuccess) return false;
|
|
if (cudaStreamSynchronize(to.stream) != cudaSuccess) return false;
|
|
return t.release(to.dev, epoch, to.stream);
|
|
}
|
|
|
|
void exercise(Transport& t, Side& a, Side& b, const std::string& name) {
|
|
check(t.capacity() >= a.cap, name + ": capacity covers the test buffers");
|
|
// sizes, both directions
|
|
uint64_t epoch = 0;
|
|
for (size_t n : {size_t(4), size_t(1001), size_t(65536), a.cap}) {
|
|
for (int dir = 0; dir < 2; ++dir) {
|
|
Side& from = dir == 0 ? a : b;
|
|
Side& to = dir == 0 ? b : a;
|
|
++epoch;
|
|
const auto data = pattern(epoch, n, dir);
|
|
std::vector<uint8_t> got;
|
|
check(send(t, from, data, epoch) && recv(t, to, n, epoch, got) && got == data, name + ": " + std::to_string(n) + " bytes, " + (dir == 0 ? "a->b" : "b->a") + " arrive exact: " + t.last_error());
|
|
}
|
|
}
|
|
// the slot ring: two epochs in flight; a third needs the first released
|
|
{
|
|
const size_t n = 4096;
|
|
const uint64_t e1 = ++epoch, e2 = ++epoch, e3 = ++epoch;
|
|
// slots are (e - 1) % 2: e1 and e3 share one. The previous owner of e1's slot (epoch e1 - 2) was released by recv() above.
|
|
const auto d1 = pattern(e1, n, 5), d2 = pattern(e2, n, 5), d3 = pattern(e3, n, 5);
|
|
check(send(t, a, d1, e1) && send(t, a, d2, e2), name + ": two epochs can be in flight");
|
|
check(!send(t, a, d3, e3), name + ": the third is refused while the first holds its slot");
|
|
check(t.last_error().find("busy") != std::string::npos, name + ": and the reason says the slot is busy: " + t.last_error());
|
|
std::vector<uint8_t> got;
|
|
check(recv(t, b, n, e1, got) && got == d1, name + ": the first epoch is intact after the refusal");
|
|
check(send(t, a, d3, e3), name + ": once the first is released the third goes: " + t.last_error());
|
|
check(recv(t, b, n, e2, got) && got == d2, name + ": the second epoch is intact");
|
|
check(recv(t, b, n, e3, got) && got == d3, name + ": the third epoch is intact");
|
|
check(!send(t, a, d1, e1), name + ": a stale epoch (older than the slot's) is refused");
|
|
}
|
|
// errors are answers, not silence
|
|
{
|
|
std::vector<uint8_t> got;
|
|
check(!t.wait_device(b.dev, 9999, b.stream) && !t.last_error().empty(), name + ": waiting for an epoch nobody posted is refused: " + t.last_error());
|
|
check(!t.post(a.dev, a.buf, a.cap + 1, ++epoch, a.stream), name + ": a payload over the capacity is refused");
|
|
check(!t.post(a.dev, a.buf, 8, 0, a.stream), name + ": epoch 0 is refused");
|
|
check(!t.post(77, a.buf, 8, ++epoch, a.stream), name + ": a device that is not an endpoint is refused");
|
|
const auto t0 = std::chrono::steady_clock::now();
|
|
check(!t.wait_host(b.dev, 424242, std::chrono::milliseconds(30)), name + ": wait_host times out when nothing is posted");
|
|
const double ms = std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - t0).count();
|
|
check(ms >= 25.0 && ms < 2000.0, name + ": and the wait took about its timeout (" + std::to_string(ms) + " ms)");
|
|
}
|
|
// wait_host: the payload has landed when it returns; a host view for the staged transport
|
|
{
|
|
const size_t n = 8192;
|
|
const uint64_t e = ++epoch;
|
|
const auto data = pattern(e, n, 9);
|
|
check(send(t, a, data, e) && t.wait_host(b.dev, e, std::chrono::milliseconds(5000)), name + ": wait_host sees the post: " + t.last_error());
|
|
if (t.mode() == Mode::HostStaged) {
|
|
const void* v = t.host_view(b.dev, e);
|
|
check(v != nullptr && std::memcmp(v, data.data(), n) == 0, name + ": the host view holds the payload");
|
|
} else {
|
|
check(t.host_view(b.dev, e) == nullptr, name + ": no host view on a device-to-device transport");
|
|
}
|
|
std::vector<uint8_t> got;
|
|
check(recv(t, b, n, e, got) && got == data, name + ": and the device path agrees");
|
|
}
|
|
// two threads: a stream of epochs with the receiver releasing as it goes
|
|
{
|
|
constexpr uint64_t kN = 150;
|
|
const size_t n = 32768;
|
|
const uint64_t base = epoch;
|
|
std::atomic<int> sender_fail{0}, recv_fail{0};
|
|
std::thread tx([&] {
|
|
for (uint64_t k = 1; k <= kN; ++k) {
|
|
const auto data = pattern(base + k, n, 11);
|
|
bool ok = false;
|
|
for (int tries = 0; tries < 200000 && !ok; ++tries) {
|
|
ok = send(t, a, data, base + k);
|
|
if (!ok) std::this_thread::yield(); // slot busy: the receiver has not released yet
|
|
}
|
|
if (!ok) { ++sender_fail; return; }
|
|
}
|
|
});
|
|
std::thread rx([&] {
|
|
std::vector<uint8_t> got;
|
|
for (uint64_t k = 1; k <= kN; ++k) {
|
|
if (!t.wait_host(b.dev, base + k, std::chrono::milliseconds(20000))) { ++recv_fail; return; }
|
|
if (!recv(t, b, n, base + k, got) || got != pattern(base + k, n, 11)) { ++recv_fail; return; }
|
|
}
|
|
});
|
|
tx.join();
|
|
rx.join();
|
|
epoch = base + kN;
|
|
check(sender_fail == 0 && recv_fail == 0, name + ": 150 epochs between two threads arrive intact (" + std::to_string((int) sender_fail) + " sender, " + std::to_string((int) recv_fail) + " receiver failures)");
|
|
}
|
|
// abort: later calls fail, a waiting thread returns at once
|
|
{
|
|
std::atomic<bool> returned{false};
|
|
std::thread w([&] { (void) t.wait_host(b.dev, 777777, std::chrono::milliseconds(30000)); returned = true; });
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(50));
|
|
t.abort();
|
|
const auto t0 = std::chrono::steady_clock::now();
|
|
w.join();
|
|
const double ms = std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - t0).count();
|
|
check(returned && ms < 1000.0 && t.aborted(), name + ": abort frees a waiting host thread at once (" + std::to_string(ms) + " ms)");
|
|
check(!t.post(a.dev, a.buf, 8, ++epoch, a.stream) && t.last_error().find("abort") != std::string::npos, name + ": post after abort fails and says so");
|
|
check(!t.wait_device(b.dev, 1, b.stream), name + ": wait after abort fails");
|
|
}
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main() {
|
|
ProbeOptions po;
|
|
po.bw_bytes = 16u << 20;
|
|
po.reps = 2;
|
|
po.lat_samples = 30;
|
|
po.doorbell = false;
|
|
if (const char* f = std::getenv("MGPU_TEST_FORCE_P2P"); f != nullptr && f[0] == '1') po.force_p2p = true; // a host-bridge box whose driver says P2P is OK (a rented machine you can lose)
|
|
const DeviceSet s = probe(po);
|
|
if (s.size() < 2) { std::puts("SKIPPED: mgpu_transport_test needs two GPUs"); return 77; }
|
|
std::fputs(s.to_text().c_str(), stdout);
|
|
const size_t ia = 0, ib = 1;
|
|
const int da = s.dev(ia).ordinal, db = s.dev(ib).ordinal;
|
|
constexpr size_t kCap = 1u << 20;
|
|
TransportOptions to;
|
|
to.capacity_bytes = kCap;
|
|
to.slots = 2;
|
|
|
|
Side a, b;
|
|
check(a.init(da, kCap) && b.init(db, kCap), "buffers on both cards");
|
|
|
|
// ---- host-staged, always
|
|
{
|
|
std::string why;
|
|
auto t = make_transport(s, ia, ib, Mode::HostStaged, to, &why);
|
|
check(t != nullptr && t->mode() == Mode::HostStaged, "make_transport(HostStaged) gives a host-staged transport: " + why);
|
|
if (t) exercise(*t, a, b, "host_staged");
|
|
}
|
|
// ---- peer copy, where the probe proved it; otherwise make_transport must fall back, with a reason
|
|
{
|
|
std::string why;
|
|
auto t = make_transport(s, ia, ib, Mode::PeerCopy, to, &why);
|
|
check(t != nullptr, "make_transport(PeerCopy) always yields a transport (it falls back): " + why);
|
|
const bool p2p = s.link(ia, ib).path == Path::P2PNvlink || s.link(ia, ib).path == Path::P2PDirect;
|
|
if (t) {
|
|
std::printf("peer-copy request on this pair gave %s: %s\n", mode_name(t->mode()), why.c_str());
|
|
if (p2p && s.link(ia, ib).data_ok && s.link(ib, ia).data_ok) {
|
|
check(t->mode() == Mode::PeerCopy, "peer copy asked for by name on a proven, exact P2P pair gets peer copy: " + why);
|
|
std::string why_auto;
|
|
auto ta = make_transport(s, ia, ib, Mode::Auto, to, &why_auto);
|
|
std::printf("auto on this pair gave %s: %s\n", ta ? mode_name(ta->mode()) : "(none)", why_auto.c_str());
|
|
} else {
|
|
check(t->mode() == Mode::HostStaged && !why.empty(), "a pair without proven P2P falls back to host-staged with a reason");
|
|
}
|
|
exercise(*t, a, b, std::string(mode_name(t->mode())) + " (peer request)");
|
|
}
|
|
}
|
|
// ---- auto, and the doorbell request (a stub in P0: the fallback says so)
|
|
{
|
|
std::string why;
|
|
auto t = make_transport(s, ia, ib, Mode::Auto, to, &why);
|
|
check(t != nullptr, "make_transport(Auto) yields a transport: " + why);
|
|
std::string why2;
|
|
auto d = make_transport(s, ia, ib, Mode::Doorbell, to, &why2);
|
|
check(d != nullptr && d->mode() != Mode::Doorbell && why2.find("not implemented") != std::string::npos, "make_transport(Doorbell) falls back and names the reason: " + why2);
|
|
check(make_transport(s, ia, ia, Mode::Auto, to, &why) == nullptr && !why.empty(), "the same device twice is refused");
|
|
TransportOptions bad = to;
|
|
bad.capacity_bytes = 0;
|
|
check(make_transport(s, ia, ib, Mode::HostStaged, bad, &why) == nullptr && !why.empty(), "zero capacity is refused with a reason");
|
|
}
|
|
if (g_fail != 0) { std::fprintf(stderr, "%d check(s) failed\n", g_fail); return 1; }
|
|
std::puts("mgpu_transport_test: all checks passed");
|
|
return 0;
|
|
}
|