1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
//! M1-PP2 increment-2 TRANSPORT SMOKE (single-device-runnable).
//!
//! What it proves on one GPU:
//! 1. the cuDeviceCanAccessPeer guard path runs (matrix printed for every device pair);
//! 2. the EXACT peer-copy FFI the cross-device TX uses (`cuMemcpyPeerAsync` with
//! explicit src/dst contexts) moves correct bytes — same-context both sides is a
//! legal degenerate case of the same driver call, so the plumbing (contexts, raw
//! pointers, byte counts, stream handle) is exercised without a second device;
//! 3. the Pp2Rt boundary choreography (per-stage streams, ev_tx/ev_rx, persistent
//! slots, overlap double-buffering) round-trips patterned buffers bit-exactly.
//!
//! With `MEMRA_PP_DEVICES=0,1`, the boundary roundtrip also proves an actual cross-device
//! copy. Each stage scope binds its CUDA context just like the production PP walkers; pushing
//! an ambient stream alone is insufficient when the stages live on different devices.
//!
//! usage: pp-transport-smoke [--runtime-probe-cycle] (exit 0 = all sub-smokes pass)
use cudarc::driver::{DevicePtr, DevicePtrMut};
use memra_engine::Engine;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let runtime_probe_cycle = match std::env::args().nth(1).as_deref() {
None => false,
Some("--runtime-probe-cycle") => true,
Some(arg) => return Err(format!("unknown argument {arg:?}").into()),
};
let mut fails = 0usize;
// Match the serving worker: under an explicit placement the primary Engine follows
// the last/head stage. With no placement this remains the original device-0 smoke.
let primary = std::env::var("MEMRA_PP_DEVICES")
.ok()
.and_then(|value| value.split(',').next_back()?.trim().parse::<usize>().ok())
.unwrap_or(0);
// Engine initialization also initializes the driver (raw result:: calls below need cuInit).
let e = Engine::new(primary)?;
println!("primary device: {primary}");
// ---- 1. device census + CanAccessPeer matrix ----
let ndev = cudarc::driver::result::device::get_count()? as usize;
println!("devices: {ndev}");
for a in 0..ndev {
for b in 0..ndev {
if a == b {
continue;
}
let da = cudarc::driver::result::device::get(a as i32)?;
let db = cudarc::driver::result::device::get(b as i32)?;
let mut can: i32 = 0;
unsafe {
cudarc::driver::sys::cuDeviceCanAccessPeer(&mut can, da, db).result()?;
}
println!("cuDeviceCanAccessPeer({a} -> {b}) = {can}");
}
}
if ndev < 2 {
println!(
"(single device: no peer pairs — matrix section is census-only here; \
the cross-device arm gates on the 8x box)"
);
}
// ---- 2. forced peer-arm copy, same context both sides ----
// A host-bounce run is specifically for a machine whose peer-copy path may corrupt
// bytes. Do not poison that process before exercising the fallback.
if !memra_engine::pp::pp_host_bounce_on() {
let ctx = e.ctx();
let s = ctx.new_stream()?;
let n = 4096usize;
let pat: Vec<f32> = (0..n).map(|i| (i as f32) * 0.5 - 7.0).collect();
let src = s.clone_htod(&pat)?;
let mut dst = s.alloc_zeros::<f32>(n)?;
{
let (sp, _g0) = src.device_ptr(&s);
let (dp, _g1) = dst.device_ptr_mut(&s);
unsafe {
cudarc::driver::result::memcpy_peer_async(
ctx.cu_ctx(),
dp,
ctx.cu_ctx(),
sp,
n * 4,
s.cu_stream(),
)?;
}
}
s.synchronize()?;
let back = s.clone_dtoh(&dst)?;
s.synchronize()?;
let diff = back
.iter()
.zip(&pat)
.filter(|(a, b)| a.to_bits() != b.to_bits())
.count();
println!(
"peer-arm copy (cuMemcpyPeerAsync, same-ctx degenerate): bytediff={diff} {}",
if diff == 0 {
"OK"
} else {
fails += 1;
"FAIL"
}
);
} else {
println!("peer-arm copy skipped: MEMRA_PP_HOST_BOUNCE=1");
}
// ---- 3. Pp2Rt boundary choreography: tx/rx roundtrip, then overlap slots ----
unsafe {
std::env::set_var("MEMRA_PP_OVERLAP", "1");
} // exercise slot alternation
let rt = memra_engine::pp::Pp2Rt::get(&e)?;
rt.init_boundary_transport(&e, 4096)?;
println!("Pp2Rt built: cross_device={}", rt.cross_device());
let n = 5120usize;
let mut round_fail = 0usize;
for step in 0..4 {
let pat: Vec<f32> = (0..n).map(|i| (i as f32) + 1000.0 * step as f32).collect();
// TX inside the stage-0 scope (ambient stream = stage-0's)
let slot = {
rt.bind_stage(0)?;
let _s0 = rt.enter(0);
let e0 = rt.engine(0, &e);
let x = e0.htod(&pat)?;
rt.tx(0, &x, n)?
};
// RX inside the stage-1 scope; dtoh through the ambient (stage-1) stream
rt.bind_stage(1)?;
let _s1 = rt.enter(1);
let e1 = rt.engine(1, &e);
let work = rt.rx(0, slot, n)?;
let back = e1.dtoh(&work)?;
let diff = back
.iter()
.zip(&pat)
.filter(|(a, b)| a.to_bits() != b.to_bits())
.count();
if diff != 0 {
round_fail += 1;
}
println!(
"boundary roundtrip step {step} slot {slot}: bytediff={diff} {}",
if diff == 0 { "OK" } else { "FAIL" }
);
}
// overlap=1 must alternate slots 0,1,0,1 — assert we actually exercised both
if round_fail > 0 {
fails += 1;
}
// Optional model-free runtime cadence receipt. Keep all CUDA work on this owner thread and
// service between completed boundary ticks, matching the server call site. The four regular
// smoke copies above count toward the fixed cadence, so continue through one exact cycle.
if runtime_probe_cycle {
if !rt.cross_device() {
return Err("--runtime-probe-cycle requires a cross-device PP placement".into());
}
let cycle_x = {
rt.bind_stage(0)?;
let _s0 = rt.enter(0);
rt.engine(0, &e).htod(&[1.0f32])?
};
let mut serviced = 0u64;
for copy_index in 4..memra_engine::pp::PEER_RUNTIME_PROBE_CYCLE_COPIES {
let slot = {
rt.bind_stage(0)?;
let _s0 = rt.enter(0);
rt.tx(0, &cycle_x, 1)?
};
{
rt.bind_stage(1)?;
let _s1 = rt.enter(1);
let _work = rt.rx(0, slot, 1)?;
}
if memra_engine::pp::service_runtime_peer_probe(&e, true, true)?.ran() {
serviced += 1;
println!(
"runtime probe serviced after boundary copy {}",
copy_index + 1
);
}
}
let metrics = memra_engine::pp::peer_probe_metrics();
let expected = memra_engine::pp::PEER_RUNTIME_PROBE_CYCLE_COPIES
/ memra_engine::pp::PEER_RUNTIME_PROBE_INTERVAL_COPIES;
let ok = serviced == expected
&& metrics.boundary_copies == memra_engine::pp::PEER_RUNTIME_PROBE_CYCLE_COPIES
&& metrics.runtime_probes == expected
&& metrics.runtime_failures == 0;
println!(
"runtime probe cycle: serviced={serviced}/{expected} boundary_copies={} \
runtime_failures={} {}",
metrics.boundary_copies,
metrics.runtime_failures,
if ok {
"OK"
} else {
fails += 1;
"FAIL"
},
);
}
if fails == 0 {
println!("pp-transport-smoke PASS");
Ok(())
} else {
println!("pp-transport-smoke FAIL ({fails} sub-smokes)");
std::process::exit(1);
}
}