|
| 1 | +# Algorithm Selection Guide |
| 2 | + |
| 3 | +LinearSolve.jl automatically selects appropriate algorithms based on your problem characteristics, but understanding how this works can help you make better choices for your specific use case. |
| 4 | + |
| 5 | +## Automatic Algorithm Selection |
| 6 | + |
| 7 | +When you call `solve(prob)` without specifying an algorithm, LinearSolve.jl uses intelligent heuristics to choose the best solver: |
| 8 | + |
| 9 | +```julia |
| 10 | +using LinearSolve |
| 11 | + |
| 12 | +# LinearSolve.jl automatically chooses the best algorithm |
| 13 | +A = rand(100, 100) |
| 14 | +b = rand(100) |
| 15 | +prob = LinearProblem(A, b) |
| 16 | +sol = solve(prob) # Automatic algorithm selection |
| 17 | +``` |
| 18 | + |
| 19 | +The selection process considers: |
| 20 | + |
| 21 | +- **Matrix type**: Dense vs. sparse vs. structured matrices |
| 22 | +- **Matrix properties**: Square vs. rectangular, symmetric, positive definite |
| 23 | +- **Size**: Small vs. large matrices for performance optimization |
| 24 | +- **Hardware**: CPU vs. GPU arrays |
| 25 | +- **Conditioning**: Well-conditioned vs. ill-conditioned systems |
| 26 | + |
| 27 | +## Algorithm Categories |
| 28 | + |
| 29 | +LinearSolve.jl organizes algorithms into several categories: |
| 30 | + |
| 31 | +### Factorization Methods |
| 32 | + |
| 33 | +These algorithms decompose your matrix into simpler components: |
| 34 | + |
| 35 | +- **Dense factorizations**: Best for matrices without special sparsity structure |
| 36 | + - `LUFactorization()`: General-purpose, good balance of speed and stability |
| 37 | + - `QRFactorization()`: More stable for ill-conditioned problems |
| 38 | + - `CholeskyFactorization()`: Fastest for symmetric positive definite matrices |
| 39 | + |
| 40 | +- **Sparse factorizations**: Optimized for matrices with many zeros |
| 41 | + - `UMFPACKFactorization()`: General sparse LU with good fill-in control |
| 42 | + - `KLUFactorization()`: Optimized for circuit simulation problems |
| 43 | + |
| 44 | +### Iterative Methods |
| 45 | + |
| 46 | +These solve the system iteratively without explicit factorization: |
| 47 | + |
| 48 | +- **Krylov methods**: Memory-efficient for large sparse systems |
| 49 | + - `KrylovJL_GMRES()`: General-purpose iterative solver |
| 50 | + - `KrylovJL_CG()`: For symmetric positive definite systems |
| 51 | + |
| 52 | +### Direct Methods |
| 53 | + |
| 54 | +Simple direct approaches: |
| 55 | + |
| 56 | +- `DirectLdiv!()`: Uses Julia's built-in `\` operator |
| 57 | +- `DiagonalFactorization()`: Optimized for diagonal matrices |
| 58 | + |
| 59 | +## Performance Characteristics |
| 60 | + |
| 61 | +### Dense Matrices |
| 62 | + |
| 63 | +For dense matrices, algorithm choice depends on size and conditioning: |
| 64 | + |
| 65 | +```julia |
| 66 | +# Small matrices (< 100×100): SimpleLUFactorization often fastest |
| 67 | +A_small = rand(50, 50) |
| 68 | +sol = solve(LinearProblem(A_small, rand(50)), SimpleLUFactorization()) |
| 69 | + |
| 70 | +# Medium matrices (100×500): RFLUFactorization often optimal |
| 71 | +A_medium = rand(200, 200) |
| 72 | +sol = solve(LinearProblem(A_medium, rand(200)), RFLUFactorization()) |
| 73 | + |
| 74 | +# Large matrices (> 500×500): MKLLUFactorization or AppleAccelerate |
| 75 | +A_large = rand(1000, 1000) |
| 76 | +sol = solve(LinearProblem(A_large, rand(1000)), MKLLUFactorization()) |
| 77 | +``` |
| 78 | + |
| 79 | +### Sparse Matrices |
| 80 | + |
| 81 | +For sparse matrices, structure matters: |
| 82 | + |
| 83 | +```julia |
| 84 | +using SparseArrays |
| 85 | + |
| 86 | +# General sparse matrices |
| 87 | +A_sparse = sprand(1000, 1000, 0.01) |
| 88 | +sol = solve(LinearProblem(A_sparse, rand(1000)), UMFPACKFactorization()) |
| 89 | + |
| 90 | +# Structured sparse (e.g., from discretized PDEs) |
| 91 | +# KLUFactorization often better for circuit-like problems |
| 92 | +``` |
| 93 | + |
| 94 | +### GPU Acceleration |
| 95 | + |
| 96 | +For very large problems, GPU offloading can be beneficial: |
| 97 | + |
| 98 | +```julia |
| 99 | +# Requires CUDA.jl |
| 100 | +# A_gpu = CuArray(rand(Float32, 2000, 2000)) |
| 101 | +# sol = solve(LinearProblem(A_gpu, CuArray(rand(Float32, 2000))), |
| 102 | +# CudaOffloadLUFactorization()) |
| 103 | +``` |
| 104 | + |
| 105 | +## When to Override Automatic Selection |
| 106 | + |
| 107 | +You might want to manually specify an algorithm when: |
| 108 | + |
| 109 | +1. **You know your problem structure**: E.g., you know your matrix is positive definite |
| 110 | + ```julia |
| 111 | + sol = solve(prob, CholeskyFactorization()) # Faster for SPD matrices |
| 112 | + ``` |
| 113 | + |
| 114 | +2. **You need maximum stability**: For ill-conditioned problems |
| 115 | + ```julia |
| 116 | + sol = solve(prob, QRFactorization()) # More numerically stable |
| 117 | + ``` |
| 118 | + |
| 119 | +3. **You're doing many solves**: Factorization methods amortize cost over multiple solves |
| 120 | + ```julia |
| 121 | + cache = init(prob, LUFactorization()) |
| 122 | + for i in 1:1000 |
| 123 | + cache.b = new_rhs[i] |
| 124 | + sol = solve!(cache) |
| 125 | + end |
| 126 | + ``` |
| 127 | + |
| 128 | +4. **Memory constraints**: Iterative methods use less memory |
| 129 | + ```julia |
| 130 | + sol = solve(prob, KrylovJL_GMRES()) # Lower memory usage |
| 131 | + ``` |
| 132 | + |
| 133 | +## Algorithm Selection Flowchart |
| 134 | + |
| 135 | +The automatic selection roughly follows this logic: |
| 136 | + |
| 137 | +``` |
| 138 | +Is A diagonal? → DiagonalFactorization |
| 139 | +Is A tridiagonal/bidiagonal? → DirectLdiv! (Julia 1.11+) or LUFactorization |
| 140 | +Is A symmetric positive definite? → CholeskyFactorization |
| 141 | +Is A symmetric indefinite? → BunchKaufmanFactorization |
| 142 | +Is A sparse? → UMFPACKFactorization or KLUFactorization |
| 143 | +Is A small dense? → RFLUFactorization or SimpleLUFactorization |
| 144 | +Is A large dense? → MKLLUFactorization or AppleAccelerateLUFactorization |
| 145 | +Is A GPU array? → QRFactorization or LUFactorization |
| 146 | +Is A an operator/function? → KrylovJL_GMRES |
| 147 | +Is the system overdetermined? → QRFactorization or KrylovJL_LSMR |
| 148 | +``` |
| 149 | + |
| 150 | +## Custom Functions |
| 151 | + |
| 152 | +For specialized algorithms not covered by the built-in solvers: |
| 153 | + |
| 154 | +```julia |
| 155 | +function my_custom_solver(A, b, u, p, isfresh, Pl, Pr, cacheval; kwargs...) |
| 156 | + # Your custom solving logic here |
| 157 | + return A \ b # Simple example |
| 158 | +end |
| 159 | + |
| 160 | +sol = solve(prob, LinearSolveFunction(my_custom_solver)) |
| 161 | +``` |
| 162 | + |
| 163 | +See the [Custom Linear Solvers](@ref custom) section for more details. |
0 commit comments