Skip to content

Explicit: CUDA options ​

Select implementation=:kernel with HighOrderExplicitOp.

Use this constructor for explicit CUDA encoding. Legacy mode partitions voxels and keeps solver vectors on the host. Resident modes partition sample rows and keep solver vectors on the primary GPU. Read the operator guide for the choice of implementation.

Backends and storage ​

  • :legacy preserves the original fused kernels and CPU solver storage. Multi-GPU execution partitions masked voxels.
  • :direct retains the direct CUDA encoding kernels with resident workspaces and GPU solver storage.
  • :blocked generates a complex phase block, applies all coils, and reuses that block for both products of the weighted normal.
  • :cached retains the full complex phase matrix and applies it in sample blocks.
  • :cached_full retains the same full matrix and applies one complete GEMM per forward or adjoint; it also needs a full sample-by-coil workspace.

Both full-cache modes initialize the matrix in blocks. For ComplexF32, the full matrix alone requires 8 * nSam * count(mask) bytes per single-GPU problem. Include phase scratch, CSM, coil images, solver vectors, data, and library workspaces. A two-dimensional dataset does not necessarily fit.

For resident multi-GPU backends, each device stores all masked voxels and coils but only its contiguous sample range. Phase-cache feasibility must hold on each device. The resident implementation types are internal; use HighOrderExplicitOp(...; implementation=:kernel, gpus=...) for every backend.

Partial phase caching ​

With backend=:blocked, phase_cache_bytes retains a prefix of complex phase blocks and regenerates the rest. The budget counts cached complex entries only, rounds down to complete blocks, and applies to each GPU independently. It excludes the other workspaces and the allocator/library reserve. A positive budget therefore does not guarantee that construction fits available memory.

The budget is explicit; the constructor does not automatically choose a cache size from free memory. For full caching or a nonempty prefix cache, its allocation estimate includes model, scratch, and solver allowances and must be below 0.8 * CUDA.free_memory() on that worker. This leaves a 20% margin for libraries and allocator overhead, rather than an unconditional fit guarantee.

The default zero recomputes every block. Cache initialization belongs in one-shot reconstruction time. Partial caching preserves all samples, coils, mask voxels, and phase terms. It is separate from the complete-matrix :cached and :cached_full modes.

Matrix execution ​

The resident matrix backends store Pb as (block_samples,nVox), with entries exp⁡(+i2πϕjv). With phase_backend=:gemm, a real GEMM evaluates the prepared dynamic coefficient/basis product; the phase kernel adds times * fieldmap and evaluates the complex exponential. With :kernel, that kernel also evaluates the dynamic phase sum.

For a normal application, Z contains the masked image multiplied by every CSM. explicit_gpu_normal! computes Y = P_b * Z, applies the already squared diagonal weights to Y, and accumulates Q += P_b' * Y. The same P_b is reused for both products. Native cuBLAS uses transpose flags N,N and C,N, so it does not allocate a conjugate-transposed copy of P_b. After all blocks, the coil-reduction kernel multiplies by conjugated CSM, sums coils, and applies 1/nVox; scattering restores the full image shape. Standalone forward and adjoint each apply 1/sqrt(nVox).

The :direct backend instead calls the resident direct forward and adjoint kernels. It does not construct a phase matrix or use the matrix-block reuse described above.

Returns and lifecycle ​

A linear operator with nSam * nCha rows and prod(grid.matrixSize) columns. Resident input/output vectors must be CuArrays on first(gpus); supply host model arrays at construction. Pass GPU data and weights to recon_HOOp to keep CG vectors on that device. The returned image is a CPU array.

Operators and derived normals share mutable workspaces and are not reentrant. Call close(op) after their final use. The reconstruction wrapper does not automatically close resident explicit operators.

Weighted normal ​

julia
using LinearOperatorCollection

N = explicit_gpu_normal(op; weights=weights_gpu)  # raw square-root weights
D = abs2.(weights_gpu)
N_same = normalOperator(op, D)                   # already squared WᴴW

explicit_gpu_normal accepts one raw weight per sample or per sample and coil, and squares it once; nothing means unit weights. normalOperator(op, D) expects an already squared real nonnegative diagonal. normalOperator(W ∘ op) supplies that diagonal automatically. Regularization remains in the solver, outside these data normal operators.

Complete resident example ​

This synthetic example requires one CUDA GPU. It computes a weighted normal product, not an image reconstruction or a performance measurement.

julia
using HighOrderMRI, CUDA

CUDA.functional() || error("CUDA is not functional")
CUDA.device!(0)
grid = Grid(8, 8, 1, 1f-3, 1f-3, 5f-3)
kspha = zeros(Float32, 9, 32)
kspha[2, :] .= range(-200f0, 200f0; length=32)
times = collect(range(0f0, 3f-3; length=32))
op = HighOrderExplicitOp(grid, kspha, times; implementation=:kernel,
    gpus=[0], backend=:blocked, block_size=16, phase_cache_bytes=0)
image = try
    x = CUDA.ones(ComplexF32, prod(grid.matrixSize))
    weights = CUDA.ones(ComplexF32, size(op, 1))
    normal = explicit_gpu_normal(op; weights)
    Array(reshape(normal * x, grid.matrixSize))
finally
    close(op)
end

For reconstruction, pass GPU data and weights to recon_HOOp. The default legacy Float64 adjoint can exceed its fixed CUDA launch's register budget. The resident direct Float64 cap does not apply to that legacy path. Derivative mode remains forward-only in the sense explained in the generated constructor reference.

Keyword defaults ​

These keywords are forwarded by HighOrderExplicitOp for this implementation.

KeywordDefault
fieldmapzeros(T, grid.matrixSize...)
csmones(Complex{T}, grid.matrixSize..., 1)
masktrues(grid.matrixSize...)
concomitantnothing
recon_termsnothing
k_nominalkspha[2:4, :]
kspha_dtnothing
nBlock50
arrayTypeCuArray
gpus[0]
backend:legacy
block_size320
phase_backend:kernel
gemm_backend:cublas
phase_cache_bytes0
forward_threads256
adjoint_threads512
verbosefalse

Weighted normal API ​

Resident public wrapper ​

julia
explicit_gpu_normal(op::CUDAExplicitOp{T, F1, F2, S, O};
    weights = nothing) where {T, F1, F2, S, O <: HOOp}

Construct the resident wrapped operator's data normal Aᴴ D A, where D = abs2.(weights) and nothing means unit weights. The wrapper must own a resident backend. Raw weights may be sample-wise or sample-and-coil-wise. The result shares its parent's workspace and lifetime; regularization remains the solver's responsibility. normalOperator(op, D) instead accepts the already squared real nonnegative diagonal.

Source: src/EncodingOperator/Explicit/explicit_gpu_operator.jl

Internal single GPU ​

julia
explicit_gpu_normal(op::ExplicitGPUOp;
    weights = nothing)

Create the data normal operator Aᴴ * D * A for a resident CUDAExplicitOp (backend=:direct, :blocked, :cached, or :cached_full), with D = abs2.(weights). Raw sample weights may have one entry per sample or per sample and coil; nothing means unit weights.

In contrast, normalOperator(op, D) accepts an already squared, real nonnegative diagonal. normalOperator(W ∘ op) also supplies that squared diagonal automatically. Regularization is applied separately by the solver. The result shares the operator's mutable workspaces and lifetime.

Source: src/EncodingOperator/Explicit/explicit_gpu_operator.jl

Internal multiple GPU ​

julia
explicit_gpu_normal(op::ExplicitMultiGPUOp;
    weights = nothing)

Construct the sample-distributed explicit GPU data normal Aᴴ D A, with D = abs2.(weights). nothing selects unit weights. Raw weights have one entry per sample or per sample and coil. Workers operate on disjoint sample rows and their image contributions are summed. The result shares the parent operator's workers and workspaces; finish using it before close(op). Regularization is applied separately by the solver.

Source: src/EncodingOperator/Explicit/multi_gpu_operator.jl

HighOrderMRI.jl documentation · stable · v0.1.2 · code 9af532a · docs 9af532a