diff --git a/helios/native/roce_proxy.c b/helios/native/roce_proxy.c index be4a21e8d..a6380eff7 100644 --- a/helios/native/roce_proxy.c +++ b/helios/native/roce_proxy.c @@ -82,6 +82,16 @@ // On the ring transports a credit to a rank that is not a neighbor travels as an empty mailbox // write, whose flag lands on every rail; only rail 0's credit word is read. // +// On closed rings of three or more ranks, pipelined reduce-scatter (ctrl kind[slot] = 7), +// shard gather (ctrl kind[slot] = 8), and fused reduce-scatter plus all-gather (ctrl kind[slot] +// = 9) also run as bidirectional 1-hop ring collectives over pipe_send, pipe_recv, and +// pipe_flag. Each direction (clockwise and counter-clockwise) owns half of the payload across +// W shards; every RDMA write targets a direct neighbor striped across all rails and rotated +// across n_lane QPs with no CPU mailbox relay. pipe_recv holds 4 * (W - 1) half-shard slots +// of shard_cap / 2 (2 * (W - 1) for RS, 2 * (W - 1) for AG), and pipe_flag uses rows 0..1 +// (RS) and W..W + 1 (AG). Cross-family transitions between two-shot/pipe and ring_bidir ops +// exchange start-of-op credits because their pipe_recv layouts overlap. +// // A cabled fabric can relay the all-gather chunks (phase 1 of a two-shot op or a shard gather) // when its ranks form pairs joined by direct cables (roce_set_relay). A rank then sends its // reduced shard to its partner and to one rank in every other pair, which forwards each chunk to @@ -172,6 +182,11 @@ _Static_assert(CTRL_CREDIT >= CTRL_READY_VL + ROCE_VARLEN_MAX_CHUNKS && #define KIND_GATHER_SHARDS 4 #define KIND_RING2_STEP1 5u #define KIND_RING2_STEP2 6u +// Opcodes 5..6 are the 2-step direct-neighbor ring allreduce (KIND_RING2_STEP1/2); +// 7..9 add 1-hop bidirectional prefill reduce-scatter/all-gather (run_ring_bidir_op). +#define KIND_RING_RS 7 +#define KIND_RING_AG 8 +#define KIND_RING_RSAG 9 // Two-step ring direct writes signal 1 in RING2_SIGNAL_STRIDE ops per (HCA, peer) // QP. Capping signaled outstanding ops at RING2_MAX_OUTSTANDING keeps at most // RING2_MAX_OUTSTANDING * RING2_SIGNAL_STRIDE * 2 + (RING2_SIGNAL_STRIDE - 1) * 2 @@ -1448,13 +1463,30 @@ static int forward_pipe_chunk(roce_ctx_t* c, uint32_t seq, int s, uint32_t chunk } // The phases of an op of `kind` following an op of `prev` that must wait for peers' credits (see -// the header): phase 0 after a reduce-scatter, phase 1 of a shard gather after an op that gathers. +// the header): phase 0 after a reduce-scatter, phase 1 of a shard gather after an op that gathers, +// and the first phase of any cross-family transition between pipe and ring_bidir (which partition +// pipe_recv with different phase layouts). static uint32_t credit_phases(uint32_t prev, uint32_t kind) { + int prev_pipe = (prev == KIND_TWOSHOT || prev == KIND_REDUCE_SCATTER || + prev == KIND_GATHER_SHARDS); + int prev_ring = (prev == KIND_RING_RSAG || prev == KIND_RING_RS || prev == KIND_RING_AG); + int cur_pipe = (kind == KIND_TWOSHOT || kind == KIND_REDUCE_SCATTER || + kind == KIND_GATHER_SHARDS); + int cur_ring = (kind == KIND_RING_RSAG || kind == KIND_RING_RS || kind == KIND_RING_AG); + int cur_ag_only = (kind == KIND_GATHER_SHARDS || kind == KIND_RING_AG); + if ((prev_pipe && cur_ring) || (prev_ring && cur_pipe)) { + return cur_ag_only ? CREDIT_AG : CREDIT_RS; + } uint32_t need = 0; - if (prev == KIND_REDUCE_SCATTER && (kind == KIND_REDUCE_SCATTER || kind == KIND_TWOSHOT)) { + int prev_rs_only = (prev == KIND_REDUCE_SCATTER || prev == KIND_RING_RS); + int cur_uses_rs = (kind == KIND_REDUCE_SCATTER || kind == KIND_TWOSHOT || + kind == KIND_RING_RSAG || kind == KIND_RING_RS); + int prev_has_ag = (prev == KIND_TWOSHOT || prev == KIND_GATHER_SHARDS || + prev == KIND_RING_RSAG || prev == KIND_RING_AG); + if (prev_rs_only && cur_uses_rs) { need |= CREDIT_RS; } - if (kind == KIND_GATHER_SHARDS && (prev == KIND_TWOSHOT || prev == KIND_GATHER_SHARDS)) { + if (prev_has_ag && cur_ag_only) { need |= CREDIT_AG; } return need; @@ -1623,6 +1655,242 @@ static int run_pipe_op(roce_ctx_t* c, uint32_t seq, uint32_t nbytes, uint32_t ch return 0; } +static inline void ring_dir_shard_bounds(const roce_ctx_t* c, int dir, int shard, uint32_t total, + uint32_t* first_out, uint32_t* end_out) { + uint32_t half_p = total / 2u; + uint32_t base = dir == 0 ? 0u : half_p; + uint32_t len = dir == 0 ? half_p : (total - half_p); + *first_out = base + (uint32_t)(((uint64_t)shard * len) / (uint64_t)c->world); + *end_out = base + (uint32_t)(((uint64_t)(shard + 1) * len) / (uint64_t)c->world); +} + +static inline int ring_ag_arrived(const roce_ctx_t* c, uint32_t seq, int dir, uint32_t slot_idx) { + uint64_t base = + c->pipe_flag_off + + ((((uint64_t)c->world + (uint64_t)dir) * ROCE_MAX_CHUNKS + (uint64_t)slot_idx) * + (uint64_t)c->n_rail) * + ROCE_FLAG_STRIDE; + for (int k = 0; k < c->n_rail; k++) { + const uint32_t* flag = (const uint32_t*)(c->region + base + (uint64_t)k * ROCE_FLAG_STRIDE); + if (__atomic_load_n(flag, __ATOMIC_ACQUIRE) != seq) { + return 0; + } + } + return 1; +} + +// Concurrent multi-rail, bidirectional 1-hop closed-ring collective supporting: +// PIPE_PHASE_RS (KIND_RING_RS = 7): standalone 1-hop Reduce-Scatter +// PIPE_PHASE_AG (KIND_RING_AG = 8): standalone 1-hop All-Gather +// PIPE_PHASE_RS | PIPE_PHASE_AG (KIND_RING_RSAG = 9): fused Reduce-Scatter + All-Gather +// Every RDMA write crosses strictly 1 hop to CW/CCW direct neighbors, striped across all +// n_rail links and rotated across n_lane QPs per rail (zero 2-hop CPU mailbox relay). +static int run_ring_bidir_op(roce_ctx_t* c, uint32_t seq, uint32_t nbytes, uint32_t chunk_packs, + uint32_t chunks, uint32_t phases, uint32_t credit) { + volatile uint32_t* ctrl = (volatile uint32_t*)(c->region + c->ctrl_off); + int W = c->world; + int R = c->rank; + int do_rs = (phases & PIPE_PHASE_RS) != 0; + int do_ag = (phases & PIPE_PHASE_AG) != 0; + if (c->ring == NULL || c->ring_open || W < 3 || nbytes == 0 || nbytes % 16 != 0 || + nbytes > c->pipe_bytes || chunks == 0 || (uint64_t)(W - 1) * chunks > ROCE_MAX_CHUNKS || + chunk_packs == 0 || (!do_rs && !do_ag)) { + snprintf(c->err, sizeof(c->err), + "invalid ring_bidir op %u: world=%d nbytes=%u chunks=%u chunk_packs=%u phases=%u", + seq, W, nbytes, chunks, chunk_packs, phases); + return -1; + } + uint32_t total = nbytes / 16u; + uint32_t half_max = (total + 1u) / 2u; + uint32_t widest = (half_max + (uint32_t)W - 1u) / (uint32_t)W; + uint64_t half_cap = c->shard_cap / 2u; + if ((uint64_t)chunks * chunk_packs < widest || (uint64_t)widest * 16u > half_cap) { + snprintf(c->err, sizeof(c->err), + "ring_bidir op %u chunk coverage %u*%u < widest %u (half_cap %llu)", seq, chunks, + chunk_packs, widest, (unsigned long long)half_cap); + return -1; + } + int cw_peer = (R + 1) % W; + int ccw_peer = (R + W - 1) % W; + int peer_of_dir[2] = {cw_peer, ccw_peer}; + const uint8_t* pipe_send = c->region + c->pipe_send_off; + + uint32_t rs_sent[ROCE_MAX_PEERS]; + uint32_t ag_fwd[2][ROCE_MAX_PEERS]; + for (int s = 0; s < W - 1; s++) { + rs_sent[s] = do_rs ? 0u : chunks; + ag_fwd[0][s] = (do_ag && s >= 1) ? 0u : chunks; + ag_fwd[1][s] = (do_ag && s >= 1) ? 0u : chunks; + } + uint32_t ag0_sent = do_ag ? 0u : chunks; + uint64_t idle = 0; + uint32_t post_batch = 0; + + for (;;) { + int done = (ag0_sent == chunks); + for (int s = 0; s < W - 1; s++) { + if (rs_sent[s] < chunks) done = 0; + if (s >= 1 && (ag_fwd[0][s] < chunks || ag_fwd[1][s] < chunks)) done = 0; + } + if (done) break; + + if (credit != 0 && credits_arrived(c, seq)) { + credit = 0; + } + + int posted = 0; + + // 1. Prioritize zero-copy 1-hop AG forwarding so incoming shards keep the ring pipeline full. + if (do_ag && !(credit & CREDIT_AG)) { + for (int s = 1; s < W - 1; s++) { + for (int dir = 0; dir < 2; dir++) { + uint32_t c_idx = ag_fwd[dir][s]; + if (c_idx < chunks) { + uint32_t prev_flag_idx = (uint32_t)(s - 1) * chunks + c_idx; + if (ring_ag_arrived(c, seq, dir, prev_flag_idx)) { + int sh = dir == 0 ? ((R - s + 2 * W) % W) : ((R + s) % W); + uint32_t first, end; + ring_dir_shard_bounds(c, dir, sh, total, &first, &end); + uint64_t lo = (uint64_t)first + (uint64_t)c_idx * chunk_packs; + uint64_t hi = lo + chunk_packs; + if (lo > end) lo = end; + if (hi > end) hi = end; + uint64_t src_slot = (uint64_t)(2 * (W - 1) + dir * (W - 1) + (s - 1)); + uint64_t dst_slot = (uint64_t)(2 * (W - 1) + dir * (W - 1) + s); + uint64_t local_off = c->pipe_recv_off + src_slot * half_cap + (lo - first) * 16u; + uint64_t remote_off = c->pipe_recv_off + dst_slot * half_cap + (lo - first) * 16u; + uint32_t dst_flag_idx = (uint32_t)s * chunks + c_idx; + uint64_t flag_off = + c->pipe_flag_off + + ((((uint64_t)W + (uint64_t)dir) * ROCE_MAX_CHUNKS + (uint64_t)dst_flag_idx) * + (uint64_t)c->n_rail) * + ROCE_FLAG_STRIDE; + if (post_striped(c, peer_of_dir[dir], c->region + local_off, remote_off, + (uint32_t)(hi - lo), flag_off, seq, + (int)(dst_flag_idx % (uint32_t)c->n_lane)) != 0) { + return -1; + } + ag_fwd[dir][s]++; + posted = 1; + } + } + } + } + } + + // 2. RS steps 0 .. W-2: post CW and CCW 1-hop stripes concurrently across all rails. + if (do_rs && !(credit & CREDIT_RS)) { + for (int s = 0; s < W - 1; s++) { + uint32_t c_idx = rs_sent[s]; + if (c_idx < chunks) { + uint32_t r_word = (uint32_t)s * chunks + c_idx; + if (__atomic_load_n(&ctrl[CTRL_READY_RS + r_word], __ATOMIC_ACQUIRE) == seq) { + int cw_sh = (R - 1 - s + 2 * W) % W; + int ccw_sh = (R + 1 + s) % W; + int sh_dir[2] = {cw_sh, ccw_sh}; + int lane = (int)(r_word % (uint32_t)c->n_lane); + for (int dir = 0; dir < 2; dir++) { + uint32_t first, end; + ring_dir_shard_bounds(c, dir, sh_dir[dir], total, &first, &end); + uint64_t lo = (uint64_t)first + (uint64_t)c_idx * chunk_packs; + uint64_t hi = lo + chunk_packs; + if (lo > end) lo = end; + if (hi > end) hi = end; + uint64_t recv_slot = (uint64_t)(dir * (W - 1) + s); + uint64_t remote_off = c->pipe_recv_off + recv_slot * half_cap + (lo - first) * 16u; + uint64_t flag_off = + c->pipe_flag_off + + (((uint64_t)dir * ROCE_MAX_CHUNKS + (uint64_t)r_word) * (uint64_t)c->n_rail) * + ROCE_FLAG_STRIDE; + if (post_striped(c, peer_of_dir[dir], pipe_send + lo * 16u, remote_off, + (uint32_t)(hi - lo), flag_off, seq, lane) != 0) { + return -1; + } + } + rs_sent[s]++; + posted = 1; + } + } + } + } + + // 3. AG step 0: send own shard R directly 1-hop CW and CCW across all rails. + if (do_ag && !(credit & CREDIT_AG) && ag0_sent < chunks && + __atomic_load_n(&ctrl[CTRL_READY_AG + ag0_sent], __ATOMIC_ACQUIRE) == seq) { + uint32_t c_idx = ag0_sent; + int lane = (int)(c_idx % (uint32_t)c->n_lane); + for (int dir = 0; dir < 2; dir++) { + uint32_t first, end; + ring_dir_shard_bounds(c, dir, R, total, &first, &end); + uint64_t lo = (uint64_t)first + (uint64_t)c_idx * chunk_packs; + uint64_t hi = lo + chunk_packs; + if (lo > end) lo = end; + if (hi > end) hi = end; + uint64_t recv_slot = (uint64_t)(2 * (W - 1) + dir * (W - 1) + 0); + uint64_t remote_off = c->pipe_recv_off + recv_slot * half_cap + (lo - first) * 16u; + uint64_t flag_off = c->pipe_flag_off + + ((((uint64_t)W + (uint64_t)dir) * ROCE_MAX_CHUNKS + (uint64_t)c_idx) * + (uint64_t)c->n_rail) * + ROCE_FLAG_STRIDE; + if (post_striped(c, peer_of_dir[dir], pipe_send + lo * 16u, remote_off, + (uint32_t)(hi - lo), flag_off, seq, lane) != 0) { + return -1; + } + } + ag0_sent++; + posted = 1; + } + + if (posted) { + if ((++post_batch & 3u) == 0u) { + for (int h = 0; h < c->n_hca; h++) { + if (drain_cq(c, h) != 0) return -1; + } + } + idle = 0; + continue; + } + if (++idle % 64 == 0) { + if (drain_all(c) != 0) return -1; + uint32_t err_seq = __atomic_load_n(&ctrl[CTRL_ERROR], __ATOMIC_ACQUIRE); + if (err_seq != 0) { + snprintf(c->err, sizeof(c->err), "ring_bidir op %u abandoned: kernel timed out at seq %u", + seq, err_seq); + return -1; + } + if (!atomic_load_explicit(&c->running, memory_order_relaxed)) { + snprintf(c->err, sizeof(c->err), "RoCE proxy stopped inside ring_bidir op %u", seq); + return -1; + } + } + } + c->ops_posted += 1; + for (;;) { + if (drain_all(c) != 0) return -1; + uint32_t pending = 0; + for (int h = 0; h < c->n_hca; h++) { + for (int p = 0; p < c->world; p++) { + for (int l = 0; l < c->n_lane; l++) { + pending += c->hca[h].outstanding[p][l]; + } + } + } + if (pending == 0) break; + uint32_t err_seq = __atomic_load_n(&ctrl[CTRL_ERROR], __ATOMIC_ACQUIRE); + if (err_seq != 0) { + snprintf(c->err, sizeof(c->err), "ring_bidir op %u abandoned: kernel timed out at seq %u", + seq, err_seq); + return -1; + } + if (!atomic_load_explicit(&c->running, memory_order_relaxed)) { + snprintf(c->err, sizeof(c->err), "RoCE proxy stopped releasing ring_bidir op %u", seq); + return -1; + } + } + __atomic_store_n(&ctrl[CTRL_PIPE_DONE], seq, __ATOMIC_RELEASE); + return 0; +} + // Run variable-length all-gather op seq: post each chunk of our contribution, from our row of // the op's slot to the same row of every peer, once the kernel staged it. static int run_varlen_op(roce_ctx_t* c, uint32_t seq, uint32_t nbytes, uint32_t chunk_packs, @@ -1853,6 +2121,16 @@ static void* proxy_main(void* arg) { } else if (kind == KIND_VARLEN) { rc = run_varlen_op(c, s, nbytes, ctrl[CTRL_SLOT_CHUNK_PACKS + slot], ctrl[CTRL_SLOT_CHUNKS + slot]); + } else if (kind == KIND_RING_RS || kind == KIND_RING_AG || kind == KIND_RING_RSAG) { + uint32_t phases = kind == KIND_RING_RSAG ? (PIPE_PHASE_RS | PIPE_PHASE_AG) + : kind == KIND_RING_RS ? PIPE_PHASE_RS + : PIPE_PHASE_AG; + uint32_t credit = credit_phases(c->last_kind, kind); + rc = credit != 0 ? post_credits(c, s) : 0; + if (rc == 0) { + rc = run_ring_bidir_op(c, s, nbytes, ctrl[CTRL_SLOT_CHUNK_PACKS + slot], + ctrl[CTRL_SLOT_CHUNKS + slot], phases, credit); + } } else { snprintf(c->err, sizeof(c->err), "unknown op kind %u at seq %u", kind, s); rc = -1;