Skip to content

Solvers in pure C++/CUDA: relaxation, CG, and full multigrid (FMG) #34

Description

@balbasty

Umbrella for implementing the linear-system solvers (relaxation, conjugate gradient, and full multigrid) for spatially-regularised fields in pure C++/CUDA, launching CPU/CUDA kernels directly from the algorithm — rather than orchestrating them from Python.

Why now (the nitorch → jitfields → fastfields arc)

  • nitorch implemented the full multigrid + relaxation solvers in pure C++/CUDA (nitorch/_C/src/: fmg.cpp/fmg_common.cpp, multires.cpp/multires_common.cpp, relax.cpp/relax_grid.cpp + *_common.cpp, precond.cpp/precond_grid.cpp, resize.cpp/resize_common.cpp; exposed relax, relax_grid, precond, precond_grid, resize, prolongation, restriction, fmg).
  • jitfields couldn't do this: cupy-JIT only lets you write kernels, not host code, so the algorithm had to move to Python, built from the jitfields ingredients — see nitorch/nitorch-solvers (solve_field_fmg / solve_grid_fmg, CG + Jacobi preconditioning or checkerboard relaxation, in field and grid=flow flavours).
  • fastfields launches its own kernels (host launchers in cpu-impl / cuda-impl), so we can move the algorithm back into pure C++/CUDA and drive it from the hub, matching nitorch's design.

Ingredients already present in the stack

  • Relaxation (Gauss-Seidel): flow_relax landed; field relaxation exists in fastfields-cpu-impl/reg_field.h (relax_*) and just needs wiring (tracked in Field regulariser: full jitfields parity (kernel / precond / forward / relax / accumulate) #33).
  • Prolong / restrict: resample / restriction (spline prolongation + its adjoint).
  • Diagonal / preconditioner: flow_diag/flow_precond, field_diag/field_precond.
  • Regulariser matvec / forward: *_matvec, *_forward, plus the compact-symmetric sym_matvec/sym_solve for the data-term Hessian.

So the missing piece is the solver driver itself (V-cycle / FMG, CG/PCG), which can now be genuine C++/CUDA.

Proposed breakdown (field + flow/"grid" flavours)

  • prolong/restrict for solvers — thin C++ helpers over resample/restriction with the grid bookkeeping (make_grid, per-level shapes) nitorch's _FMG does in Python
  • per-level operator apply + relaxation(M + R) x and a Gauss-Seidel/Jacobi sweep at each level (reuse *_forward / *_relax)
  • conjugate gradient (CG/PCG) — Jacobi-preconditioned CG solver in C++ (the jitfields default inner solver)
  • V-cycle + FMG driver — the multigrid recursion in C++, mirroring fmg_common.cpp / multires_common.cpp
  • expose through the stacksolve_field / solve_flow (relax / cg / fmg) in cpu-lib → cuda-lib → lib → bind-py → numpy/torch/cupy → fastfields.any
  • CPU test gate — validate each solver reduces the residual (M+R)x − g to tolerance vs a dense reference (mirrors the existing relax residual tests)

References

Depends on the field_relax wiring from #33; otherwise the ingredients are in place.

Workstream: claude-jitfields-to-fastfields

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions