PERF-2 P3: the batched device V-cycle microbenchmark — mg_vcycle.cu (masked variable-coefficient five-point red-black half-sweeps, residual, CSR restriction in a fixed order, prolongation, coarsest sweeps; [K][n] layout, blockIdx.y = march), LevelExport/export_hierarchy and vcycle_f32_reference on the CPU side, and the ignored cuda-feature test that checks the K = 1 device V-cycle against the CPU f32 reference and times K = 1 / 8 / 16 per march
Performance Benchmarks / Run Benchmarks (push) Failing after 6s
CI / Build (ubuntu-latest) (push) Failing after 5s
Documentation / Build API Documentation (push) Failing after 6s
Documentation / Build User Guide (push) Successful in 7s
CI / Format Check (push) Failing after 12s
CI / Clippy Check (push) Failing after 36s
CI / Build CPU-Only (Explicit) (push) Failing after 1m37s
CI / Build (macos-latest) (push) Canceled after 0s
CI / Test (macos-latest) (push) Canceled after 0s
CI / Test (ubuntu-latest) (push) Canceled after 0s
CI / Python Bindings (maturin) (macos-latest) (push) Canceled after 0s
CI / Python Bindings (maturin) (ubuntu-latest) (push) Canceled after 0s
CI / WASM Build + Size Check (push) Canceled after 0s
CI / Distributed Training Tests (push) Canceled after 0s
CI / CI Success (push) Canceled after 0s

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_01YJPeT6WA2e7YvAnS875AHL
This commit is contained in:
Omar Sobh
2026-09-16 01:03:02 -05:00
co-authored by Claude Fable 5.1
parent 53078c2aa7
commit 0dc95a8de5
4 changed files with 571 additions and 1 deletions
@@ -0,0 +1,107 @@
/**
* PERF-2 P3 (`docs/perf2_campaign.md`): the multigrid V-cycle's maps for a
* MASKED, VARIABLE-COEFFICIENT five-point operator, batched over K marches.
* Layout: every per-cell array is [K][n] (march-major, n = nx*ny of the
* level); the index lists (cells, colours, children) are shared across the
* batch in this benchmark. blockIdx.y = the march.
*/
extern "C" __global__ void mg_rb_half(
int n_col, const unsigned int* __restrict__ col, int n,
const float* __restrict__ ae, const float* __restrict__ aw,
const float* __restrict__ an, const float* __restrict__ as_,
const float* __restrict__ ap, const float* __restrict__ b,
float* __restrict__ x, int nx)
{
int t = blockIdx.x * blockDim.x + threadIdx.x;
if (t >= n_col) return;
int base = blockIdx.y * n;
int idx = col[t];
int g = base + idx;
float s = 0.0f;
float e = ae[g]; if (e != 0.0f) s += e * x[g + 1];
float w = aw[g]; if (w != 0.0f) s += w * x[g - 1];
float nn = an[g]; if (nn != 0.0f) s += nn * x[g + nx];
float ss = as_[g]; if (ss != 0.0f) s += ss * x[g - nx];
x[g] = (b[g] + s) / ap[g];
}
extern "C" __global__ void mg_residual(
int n_cells, const unsigned int* __restrict__ cells, int n,
const float* __restrict__ ae, const float* __restrict__ aw,
const float* __restrict__ an, const float* __restrict__ as_,
const float* __restrict__ ap, const float* __restrict__ b,
const float* __restrict__ x, float* __restrict__ r, int nx)
{
int t = blockIdx.x * blockDim.x + threadIdx.x;
if (t >= n_cells) return;
int base = blockIdx.y * n;
int g = base + cells[t];
float s = 0.0f;
float e = ae[g]; if (e != 0.0f) s += e * x[g + 1];
float w = aw[g]; if (w != 0.0f) s += w * x[g - 1];
float nn = an[g]; if (nn != 0.0f) s += nn * x[g + nx];
float ss = as_[g]; if (ss != 0.0f) s += ss * x[g - nx];
r[g] = b[g] - (ap[g] * x[g] - s);
}
/* b_c[c] = sum of r_f over the children of coarse cell c (fixed order). */
extern "C" __global__ void mg_restrict(
int n_coarse, const unsigned int* __restrict__ coarse_cells,
const unsigned int* __restrict__ children_ptr, const unsigned int* __restrict__ children_idx,
int n_f, int n_c, const float* __restrict__ r_f, float* __restrict__ b_c)
{
int t = blockIdx.x * blockDim.x + threadIdx.x;
if (t >= n_coarse) return;
int k = blockIdx.y;
const float* rf = r_f + (size_t)k * n_f;
float s = 0.0f;
for (unsigned int p = children_ptr[t]; p < children_ptr[t + 1]; ++p) s += rf[children_idx[p]];
b_c[(size_t)k * n_c + coarse_cells[t]] = s;
}
/* x_f += 2 x_c[coarse_of[idx]] */
extern "C" __global__ void mg_prolong(
int n_cells, const unsigned int* __restrict__ cells, const unsigned int* __restrict__ coarse_of,
int n_f, int n_c, float* __restrict__ x_f, const float* __restrict__ x_c)
{
int t = blockIdx.x * blockDim.x + threadIdx.x;
if (t >= n_cells) return;
int k = blockIdx.y;
int idx = cells[t];
x_f[(size_t)k * n_f + idx] += 2.0f * x_c[(size_t)k * n_c + coarse_of[idx]];
}
extern "C" __global__ void mg_zero(int n_cells, const unsigned int* __restrict__ cells, int n, float* __restrict__ x)
{
int t = blockIdx.x * blockDim.x + threadIdx.x;
if (t >= n_cells) return;
x[(size_t)blockIdx.y * n + cells[t]] = 0.0f;
}
/* The coarsest level: one thread per march, `sweeps` symmetric lexicographic sweeps over <= a few dozen cells. */
extern "C" __global__ void mg_coarsest(
int K, int n_cells, const unsigned int* __restrict__ cells, int n,
const float* __restrict__ ae, const float* __restrict__ aw,
const float* __restrict__ an, const float* __restrict__ as_,
const float* __restrict__ ap, const float* __restrict__ b,
float* __restrict__ x, int nx, int sweeps)
{
int k = blockIdx.x * blockDim.x + threadIdx.x;
if (k >= K) return;
int base = k * n;
for (int t = 0; t < n_cells; ++t) x[base + cells[t]] = 0.0f;
for (int sw = 0; sw < sweeps; ++sw) {
for (int pass = 0; pass < 2; ++pass) {
for (int q = 0; q < n_cells; ++q) {
int t = pass == 0 ? q : n_cells - 1 - q;
int g = base + cells[t];
float s = 0.0f;
float e = ae[g]; if (e != 0.0f) s += e * x[g + 1];
float w = aw[g]; if (w != 0.0f) s += w * x[g - 1];
float nn = an[g]; if (nn != 0.0f) s += nn * x[g + nx];
float ss = as_[g]; if (ss != 0.0f) s += ss * x[g - nx];
x[g] = (b[g] + s) / ap[g];
}
}
}
}