numr 0.16.0

Numerical computing and tensor library for Rust, in the role of NumPy: n-dimensional arrays, linear algebra, FFT, statistics and autograd on CPU, CUDA and WebGPU
Documentation
// Find diagonal indices in CSR matrix

struct DiagParams {
    n: u32,
    _padding0: u32,
    _padding1: u32,
    _padding2: u32,
}

@group(0) @binding(0) var<storage, read> row_ptrs: array<i32>;
@group(0) @binding(1) var<storage, read> col_indices: array<i32>;
@group(0) @binding(2) var<storage, read_write> diag_indices: array<i32>;
@group(0) @binding(3) var<uniform> params: DiagParams;

@compute @workgroup_size(256)
fn find_diag_indices(
    @builtin(global_invocation_id) gid_grid: vec3<u32>,
    @builtin(num_workgroups) nwg: vec3<u32>,
) {
    // Linear thread index over the 2-D dispatch grid.
    let gid = vec3<u32>(gid_grid.x + gid_grid.y * nwg.x * 256u, 0u, 0u);
    let row = i32(gid.x);
    if (u32(row) >= params.n) {
        return;
    }

    let start = row_ptrs[row];
    let end = row_ptrs[row + 1];

    diag_indices[row] = -1;  // Default: no diagonal found

    for (var idx = start; idx < end; idx = idx + 1) {
        if (col_indices[idx] == row) {
            diag_indices[row] = idx;
            break;
        }
    }
}