inv#
Matrix inverse#
Compute the inverse of a square matrix.
Note
This function is currently not supported with host-based executors (CPU)
Note
CUDA JIT kernel fusion is supported by cuSolverDx if -DMATX_EN_MATHDX=ON is enabled. The current JIT path
supports rank 2 through 4 square matrices with float, double, complex<float>, and
complex<double> values. Unsupported ranks, shapes, or data types should use a normal executor and will be
rejected by CUDAJITExecutor.
Note
The default inverse uses LU/GETRF-style factorization for general square matrices. For Hermitian
positive-definite inputs, inv<MAT_INVERSE_ALGO_POSV>(A) enables a cuSolverDx POSV JIT path that solves
A * X = I directly and can fuse with other compatible JIT operators such as cuBLASDx matmul. POSV is
currently a MathDx/CUDAJITExecutor-only inverse algorithm, and callers must ensure the input satisfies the
positive-definite contract.
Added in version 0.6.0.
-
template<MatInverseAlgo_t ALGO = MAT_INVERSE_ALGO_LU, typename OpA>
__MATX_INLINE__ auto matx::inv(const OpA &a)# Performs a matrix inverse on a square matrix. The inverse API currently uses cuBLAS as a backend with the
cublas<t>matinvBatched()family of functions forN <= 32andgetri/getrffunctions otherwise.If rank > 2, operations are batched.
The CUDAJITExecutor MathDx path also supports
MAT_INVERSE_ALGO_POSV, which uses cuSolverDx POSV to invert Hermitian positive-definite matrices by solvingA * X = I. The caller is responsible for only using POSV with positive-definite inputs; normal CUDA executor paths currently support the LU algorithm only.- Template Parameters:
ALGO – Algorithm to use for matrix inversion.
MAT_INVERSE_ALGO_LUsupports normal CUDA execution and MathDx JIT.MAT_INVERSE_ALGO_POSVsupports MathDx JIT for Hermitian positive-definite inputs.OpA – Data type of input a tensor or operator
- Parameters:
a – Input tensor or operator of shape
... x n x n- Returns:
Operator that produces the inverse tensor of shape
... x n x n.
-
template<typename OpA>
__MATX_INLINE__ auto matx::inv(const OpA &a, const SolverStatus &status)# Performs a matrix inverse on a square matrix, reporting per-matrix errors through a solver status.
Same as inv(a) with the LU algorithm, except each matrix’s factorization
infovalue is written to stage 0 ofstatusand its inversioninfovalue to stage 1 (zero when cuBLAS matinvBatched does both), instead of the call synchronizing the stream and throwing for a singular matrix. A positive stage 0 value i means U(i,i) is exactly zero, so that matrix is singular and its output is not valid. The call does not synchronize the stream (see the Solver Status docs for when it can). Check the result after synchronizing, as for qr_solver(a, status). Not supported with CUDAJITExecutor, soMAT_INVERSE_ALGO_POSVis not available.- Template Parameters:
OpA – Data type of input a tensor or operator
- Parameters:
a – Input tensor or operator of shape
... x n x nstatus – Solver status with at least two stages, created with make_solver_status() for an input shaped like
a
- Returns:
Operator that produces the inverse tensor of shape
... x n x n.
Note
The overload that takes a solver status writes each matrix’s factorization info
value into stage 0 and its inversion info value into stage 1, instead of synchronizing the stream and throwing
for a singular matrix. The status needs at least two stages. It supports the LU algorithm only and is not
supported with CUDAJITExecutor.
Examples#
// Perform an inverse on matrix "A" and store the output in "Ainv"
(Ainv = inv(A)).run(this->exec);