Skip to content

Commit 7046832

Browse files
Merge pull request #642 from ChrisRackauckas/fix-formatting
Apply JuliaFormatter to fix formatting CI
2 parents f28ca7e + 32da876 commit 7046832

27 files changed

+224
-181
lines changed

docs/src/tutorials/gpu.md

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@
22

33
LinearSolve.jl provides two ways to GPU accelerate linear solves:
44

5-
* Offloading: offloading takes a CPU-based problem and automatically transforms it into a
6-
GPU-based problem in the background, and returns the solution on CPU. Thus using
7-
offloading requires no change on the part of the user other than to choose an offloading
8-
solver.
9-
* Array type interface: the array type interface requires that the user defines the
10-
`LinearProblem` using an `AbstractGPUArray` type and chooses an appropriate solver
11-
(or uses the default solver). The solution will then be returned as a GPU array type.
5+
- Offloading: offloading takes a CPU-based problem and automatically transforms it into a
6+
GPU-based problem in the background, and returns the solution on CPU. Thus using
7+
offloading requires no change on the part of the user other than to choose an offloading
8+
solver.
9+
- Array type interface: the array type interface requires that the user defines the
10+
`LinearProblem` using an `AbstractGPUArray` type and chooses an appropriate solver
11+
(or uses the default solver). The solution will then be returned as a GPU array type.
1212

1313
The offloading approach has the advantage of being simpler and requiring no change to
1414
existing CPU code, while having the disadvantage of having more overhead. In the following
1515
sections we will demonstrate how to use each of the approaches.
1616

1717
!!! warn
18-
18+
1919
GPUs are not always faster! Your matrices need to be sufficiently large in order for
2020
GPU accelerations to actually be faster. For offloading it's around 1,000 x 1,000 matrices
2121
and for Array type interface it's around 100 x 100. For sparse matrices, it is highly
@@ -74,7 +74,7 @@ to return it to the CPU. This setup does no automated memory transfers and will
7474
move things to CPU on command.
7575

7676
!!! warn
77-
77+
7878
Many GPU functionalities, such as `CUDA.cu`, have a built-in preference for `Float32`.
7979
Generally it is much faster to use 32-bit floating point operations on GPU than 64-bit
8080
operations, and thus this is generally the right choice if going to such platforms.
@@ -118,7 +118,7 @@ sol = LS.solve(prob, LS.LUFactorization())
118118
```
119119

120120
!!! note
121-
121+
122122
For now, CUDSS only supports CuSparseMatrixCSR type matrices.
123123

124124
Note that `KrylovJL` methods also work with sparse GPU arrays:
@@ -130,7 +130,8 @@ sol = LS.solve(prob, LS.KrylovJL_GMRES())
130130
Note that CUSPARSE also has some GPU-based preconditioners, such as a built-in `ilu`. However:
131131

132132
```julia
133-
sol = LS.solve(prob, LS.KrylovJL_GMRES(precs = (A, p) -> (CUDA.CUSPARSE.ilu02!(A, 'O'), LA.I)))
133+
sol = LS.solve(
134+
prob, LS.KrylovJL_GMRES(precs = (A, p) -> (CUDA.CUSPARSE.ilu02!(A, 'O'), LA.I)))
134135
```
135136

136137
However, right now CUSPARSE is missing the right `ldiv!` implementation for this to work

docs/src/tutorials/linear.md

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,11 @@ The Matrix-Free operators are provided by the [SciMLOperators.jl interface](http
103103
For example, for the matrix `A` defined via:
104104

105105
```@example linsys1
106-
A = [-2.0 1 0 0 0
107-
1 -2 1 0 0
108-
0 1 -2 1 0
109-
0 0 1 -2 1
110-
0 0 0 1 -2]
106+
A = [-2.0 1 0 0 0
107+
1 -2 1 0 0
108+
0 1 -2 1 0
109+
0 0 1 -2 1
110+
0 0 0 1 -2]
111111
```
112112

113113
We can define the `FunctionOperator` that does the `A*v` operations, without using the matrix `A`. This is done by defining
@@ -116,18 +116,18 @@ operator. See the [SciMLOperators.jl documentation](https://docs.sciml.ai/SciMLO
116116
non-constant operators, operator algebras, and many more features). This is done by:
117117

118118
```@example linsys1
119-
function Afunc!(w,v,u,p,t)
119+
function Afunc!(w, v, u, p, t)
120120
w[1] = -2v[1] + v[2]
121121
for i in 2:4
122-
w[i] = v[i-1] - 2v[i] + v[i+1]
122+
w[i] = v[i - 1] - 2v[i] + v[i + 1]
123123
end
124124
w[5] = v[4] - 2v[5]
125125
nothing
126126
end
127127
128-
function Afunc!(v,u,p,t)
128+
function Afunc!(v, u, p, t)
129129
w = zeros(5)
130-
Afunc!(w,v,u,p,t)
130+
Afunc!(w, v, u, p, t)
131131
w
132132
end
133133
@@ -159,6 +159,8 @@ mfopA * sol.u - b
159159
```
160160

161161
!!! note
162-
Note that not all methods can use a matrix-free operator. For example, `LS.LUFactorization()` requires a matrix. If you use an
163-
invalid method, you will get an error. The methods particularly from KrylovJL are the ones preferred for these cases
164-
(and are defaulted to).
162+
163+
164+
Note that not all methods can use a matrix-free operator. For example, `LS.LUFactorization()` requires a matrix. If you use an
165+
invalid method, you will get an error. The methods particularly from KrylovJL are the ones preferred for these cases
166+
(and are defaulted to).

ext/LinearSolveCUDAExt.jl

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
module LinearSolveCUDAExt
22

33
using CUDA
4-
using LinearSolve: LinearSolve, is_cusparse, defaultalg, cudss_loaded, DefaultLinearSolver,
5-
DefaultAlgorithmChoice, ALREADY_WARNED_CUDSS, LinearCache, needs_concrete_A,
6-
error_no_cudss_lu, init_cacheval, OperatorAssumptions, CudaOffloadFactorization,
4+
using LinearSolve: LinearSolve, is_cusparse, defaultalg, cudss_loaded, DefaultLinearSolver,
5+
DefaultAlgorithmChoice, ALREADY_WARNED_CUDSS, LinearCache,
6+
needs_concrete_A,
7+
error_no_cudss_lu, init_cacheval, OperatorAssumptions,
8+
CudaOffloadFactorization,
79
SparspakFactorization, KLUFactorization, UMFPACKFactorization
810
using LinearSolve.LinearAlgebra, LinearSolve.SciMLBase, LinearSolve.ArrayInterface
911
using SciMLBase: AbstractSciMLOperator
1012

11-
function LinearSolve.is_cusparse(A::Union{CUDA.CUSPARSE.CuSparseMatrixCSR, CUDA.CUSPARSE.CuSparseMatrixCSC})
13+
function LinearSolve.is_cusparse(A::Union{
14+
CUDA.CUSPARSE.CuSparseMatrixCSR, CUDA.CUSPARSE.CuSparseMatrixCSC})
1215
true
1316
end
1417

ext/LinearSolveEnzymeExt.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module LinearSolveEnzymeExt
22

3-
using LinearSolve: LinearSolve, SciMLLinearSolveAlgorithm, init, solve!, LinearProblem,
4-
LinearCache, AbstractKrylovSubspaceMethod, DefaultLinearSolver,
3+
using LinearSolve: LinearSolve, SciMLLinearSolveAlgorithm, init, solve!, LinearProblem,
4+
LinearCache, AbstractKrylovSubspaceMethod, DefaultLinearSolver,
55
defaultalg_adjoint_eval, solve
66
using LinearSolve.LinearAlgebra
77
using EnzymeCore
@@ -205,9 +205,9 @@ function EnzymeRules.augmented_primal(
205205

206206
cache = (copy(res.u), resvals, cachesolve, dAs, dbs)
207207

208-
_res = EnzymeRules.needs_primal(config) ? res : nothing
208+
_res = EnzymeRules.needs_primal(config) ? res : nothing
209209
_dres = EnzymeRules.needs_shadow(config) ? dres : nothing
210-
210+
211211
return EnzymeRules.AugmentedReturn(_res, _dres, cache)
212212
end
213213

ext/LinearSolveForwardDiffExt.jl

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -150,26 +150,29 @@ function SciMLBase.init(
150150
dual_type = get_dual_type(prob.b)
151151
end
152152

153-
alg isa LinearSolve.DefaultLinearSolver ? real_alg = LinearSolve.defaultalg(primal_prob.A, primal_prob.b) : real_alg = alg
153+
alg isa LinearSolve.DefaultLinearSolver ?
154+
real_alg = LinearSolve.defaultalg(primal_prob.A, primal_prob.b) : real_alg = alg
154155

155156
non_partial_cache = init(
156157
primal_prob, real_alg, assumptions, args...;
157158
alias = alias, abstol = abstol, reltol = reltol,
158159
maxiters = maxiters, verbose = verbose, Pl = Pl, Pr = Pr, assumptions = assumptions,
159160
sensealg = sensealg, u0 = new_u0, kwargs...)
160-
return DualLinearCache(non_partial_cache, dual_type, ∂_A, ∂_b, !isnothing(∂_b) ? zero.(∂_b) : ∂_b, A, b, zeros(dual_type, length(b)))
161+
return DualLinearCache(non_partial_cache, dual_type, ∂_A, ∂_b,
162+
!isnothing(∂_b) ? zero.(∂_b) : ∂_b, A, b, zeros(dual_type, length(b)))
161163
end
162164

163165
function SciMLBase.solve!(cache::DualLinearCache, args...; kwargs...)
164-
solve!(cache, cache.alg, args...; kwargs...)
166+
solve!(cache, cache.alg, args...; kwargs...)
165167
end
166168

167-
function SciMLBase.solve!(cache::DualLinearCache, alg::SciMLLinearSolveAlgorithm, args...; kwargs...)
169+
function SciMLBase.solve!(
170+
cache::DualLinearCache, alg::SciMLLinearSolveAlgorithm, args...; kwargs...)
168171
sol,
169172
partials = linearsolve_forwarddiff_solve(
170173
cache::DualLinearCache, cache.alg, args...; kwargs...)
171174
dual_sol = linearsolve_dual_solution(sol.u, partials, cache.dual_type)
172-
175+
173176
if cache.dual_u isa AbstractArray
174177
cache.dual_u[:] = dual_sol
175178
else
@@ -192,7 +195,6 @@ function Base.setproperty!(dc::DualLinearCache, sym::Symbol, val)
192195
setproperty!(dc.linear_cache, sym, val)
193196
end
194197

195-
196198
# Update the partials if setting A or b
197199
if sym === :A
198200
setfield!(dc, :dual_A, val)
@@ -221,8 +223,6 @@ function Base.getproperty(dc::DualLinearCache, sym::Symbol)
221223
end
222224
end
223225

224-
225-
226226
# Enhanced helper functions for Dual numbers to handle recursion
227227
get_dual_type(x::Dual{T, V, P}) where {T, V <: AbstractFloat, P} = typeof(x)
228228
get_dual_type(x::Dual{T, V, P}) where {T, V <: Dual, P} = typeof(x)
@@ -241,7 +241,6 @@ nodual_value(x::Dual{T, V, P}) where {T, V <: AbstractFloat, P} = ForwardDiff.va
241241
nodual_value(x::Dual{T, V, P}) where {T, V <: Dual, P} = x.value # Keep the inner dual intact
242242
nodual_value(x::AbstractArray{<:Dual}) = map(nodual_value, x)
243243

244-
245244
function partials_to_list(partial_matrix::AbstractVector{T}) where {T}
246245
p = eachindex(first(partial_matrix))
247246
[[partial[i] for partial in partial_matrix] for i in p]
@@ -263,6 +262,4 @@ function partials_to_list(partial_matrix)
263262
return res_list
264263
end
265264

266-
267265
end
268-

ext/LinearSolveRecursiveFactorizationExt.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
module LinearSolveRecursiveFactorizationExt
22

3-
using LinearSolve: LinearSolve, userecursivefactorization, LinearCache, @get_cacheval, RFLUFactorization
3+
using LinearSolve: LinearSolve, userecursivefactorization, LinearCache, @get_cacheval,
4+
RFLUFactorization
45
using LinearSolve.LinearAlgebra, LinearSolve.ArrayInterface, RecursiveFactorization
56
using SciMLBase: SciMLBase, ReturnCode
67

ext/LinearSolveSparseArraysExt.jl

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
module LinearSolveSparseArraysExt
22

33
using LinearSolve: LinearSolve, BLASELTYPES, pattern_changed, ArrayInterface,
4-
@get_cacheval, CHOLMODFactorization, GenericFactorization, GenericLUFactorization,
5-
KLUFactorization, LUFactorization, NormalCholeskyFactorization, OperatorAssumptions,
6-
QRFactorization, RFLUFactorization, UMFPACKFactorization, solve
4+
@get_cacheval, CHOLMODFactorization, GenericFactorization,
5+
GenericLUFactorization,
6+
KLUFactorization, LUFactorization, NormalCholeskyFactorization,
7+
OperatorAssumptions,
8+
QRFactorization, RFLUFactorization, UMFPACKFactorization, solve
79
using ArrayInterface: ArrayInterface
810
using LinearAlgebra: LinearAlgebra, I, Hermitian, Symmetric, cholesky, ldiv!, lu, lu!, QR
9-
using SparseArrays: SparseArrays, AbstractSparseArray, AbstractSparseMatrixCSC, SparseMatrixCSC,
11+
using SparseArrays: SparseArrays, AbstractSparseArray, AbstractSparseMatrixCSC,
12+
SparseMatrixCSC,
1013
nonzeros, rowvals, getcolptr, sparse, sprand
1114
using SparseArrays.UMFPACK: UMFPACK_OK
1215
using Base: /, \, convert
@@ -107,23 +110,25 @@ function LinearSolve.init_cacheval(
107110
alg::LUFactorization, A::AbstractSparseArray{T, Int64}, b, u,
108111
Pl, Pr,
109112
maxiters::Int, abstol, reltol,
110-
verbose::Bool, assumptions::OperatorAssumptions) where {T<:BLASELTYPES}
113+
verbose::Bool, assumptions::OperatorAssumptions) where {T <: BLASELTYPES}
111114
if LinearSolve.is_cusparse(A)
112115
ArrayInterface.lu_instance(A)
113116
else
114-
SparseArrays.UMFPACK.UmfpackLU(SparseMatrixCSC{T, Int64}(zero(Int64), zero(Int64), [Int64(1)], Int64[], T[]))
117+
SparseArrays.UMFPACK.UmfpackLU(SparseMatrixCSC{T, Int64}(
118+
zero(Int64), zero(Int64), [Int64(1)], Int64[], T[]))
115119
end
116120
end
117121

118122
function LinearSolve.init_cacheval(
119123
alg::LUFactorization, A::AbstractSparseArray{T, Int32}, b, u,
120124
Pl, Pr,
121125
maxiters::Int, abstol, reltol,
122-
verbose::Bool, assumptions::OperatorAssumptions) where {T<:BLASELTYPES}
126+
verbose::Bool, assumptions::OperatorAssumptions) where {T <: BLASELTYPES}
123127
if LinearSolve.is_cusparse(A)
124128
ArrayInterface.lu_instance(A)
125129
else
126-
SparseArrays.UMFPACK.UmfpackLU(SparseMatrixCSC{T, Int32}(zero(Int32), zero(Int32), [Int32(1)], Int32[], T[]))
130+
SparseArrays.UMFPACK.UmfpackLU(SparseMatrixCSC{T, Int32}(
131+
zero(Int32), zero(Int32), [Int32(1)], Int32[], T[]))
127132
end
128133
end
129134

@@ -140,7 +145,6 @@ function LinearSolve.init_cacheval(
140145
maxiters::Int, abstol,
141146
reltol,
142147
verbose::Bool, assumptions::OperatorAssumptions)
143-
144148
PREALLOCATED_UMFPACK
145149
end
146150

@@ -156,16 +160,18 @@ function LinearSolve.init_cacheval(
156160
alg::UMFPACKFactorization, A::AbstractSparseArray{T, Int64}, b, u,
157161
Pl, Pr,
158162
maxiters::Int, abstol, reltol,
159-
verbose::Bool, assumptions::OperatorAssumptions) where {T<:BLASELTYPES}
160-
SparseArrays.UMFPACK.UmfpackLU(SparseMatrixCSC{T, Int64}(zero(Int64), zero(Int64), [Int64(1)], Int64[], T[]))
163+
verbose::Bool, assumptions::OperatorAssumptions) where {T <: BLASELTYPES}
164+
SparseArrays.UMFPACK.UmfpackLU(SparseMatrixCSC{T, Int64}(
165+
zero(Int64), zero(Int64), [Int64(1)], Int64[], T[]))
161166
end
162167

163168
function LinearSolve.init_cacheval(
164169
alg::UMFPACKFactorization, A::AbstractSparseArray{T, Int32}, b, u,
165170
Pl, Pr,
166171
maxiters::Int, abstol, reltol,
167-
verbose::Bool, assumptions::OperatorAssumptions) where {T<:BLASELTYPES}
168-
SparseArrays.UMFPACK.UmfpackLU(SparseMatrixCSC{T, Int32}(zero(Int32), zero(Int32), [Int32(1)], Int32[], T[]))
172+
verbose::Bool, assumptions::OperatorAssumptions) where {T <: BLASELTYPES}
173+
SparseArrays.UMFPACK.UmfpackLU(SparseMatrixCSC{T, Int32}(
174+
zero(Int32), zero(Int32), [Int32(1)], Int32[], T[]))
169175
end
170176

171177
function SciMLBase.solve!(
@@ -197,7 +203,8 @@ function SciMLBase.solve!(
197203
F = LinearSolve.@get_cacheval(cache, :UMFPACKFactorization)
198204
if F.status == UMFPACK_OK
199205
y = ldiv!(cache.u, F, cache.b)
200-
SciMLBase.build_linear_solution(alg, y, nothing, cache; retcode = ReturnCode.Success)
206+
SciMLBase.build_linear_solution(
207+
alg, y, nothing, cache; retcode = ReturnCode.Success)
201208
else
202209
SciMLBase.build_linear_solution(
203210
alg, cache.u, nothing, cache; retcode = ReturnCode.Infeasible)
@@ -236,7 +243,8 @@ function LinearSolve.init_cacheval(
236243
maxiters::Int, abstol,
237244
reltol,
238245
verbose::Bool, assumptions::OperatorAssumptions)
239-
KLU.KLUFactorization(SparseMatrixCSC{Float64, Int32}(0, 0, [Int32(1)], Int32[], Float64[]))
246+
KLU.KLUFactorization(SparseMatrixCSC{Float64, Int32}(
247+
0, 0, [Int32(1)], Int32[], Float64[]))
240248
end
241249

242250
# TODO: guard this against errors
@@ -266,14 +274,15 @@ function SciMLBase.solve!(cache::LinearSolve.LinearCache, alg::KLUFactorization;
266274
F = LinearSolve.@get_cacheval(cache, :KLUFactorization)
267275
if F.common.status == KLU.KLU_OK
268276
y = ldiv!(cache.u, F, cache.b)
269-
SciMLBase.build_linear_solution(alg, y, nothing, cache; retcode = ReturnCode.Success)
277+
SciMLBase.build_linear_solution(
278+
alg, y, nothing, cache; retcode = ReturnCode.Success)
270279
else
271280
SciMLBase.build_linear_solution(
272281
alg, cache.u, nothing, cache; retcode = ReturnCode.Infeasible)
273282
end
274283
end
275284

276-
const PREALLOCATED_CHOLMOD = cholesky(sparse(reshape([1.0],1,1)))
285+
const PREALLOCATED_CHOLMOD = cholesky(sparse(reshape([1.0], 1, 1)))
277286

278287
function LinearSolve.init_cacheval(alg::CHOLMODFactorization,
279288
A::Union{SparseMatrixCSC{T, Int}, Symmetric{T, SparseMatrixCSC{T, Int}}}, b, u,
@@ -290,7 +299,7 @@ function LinearSolve.init_cacheval(alg::CHOLMODFactorization,
290299
maxiters::Int, abstol, reltol,
291300
verbose::Bool, assumptions::OperatorAssumptions) where {T <:
292301
BLASELTYPES}
293-
cholesky(sparse(reshape([one(T)],1,1)))
302+
cholesky(sparse(reshape([one(T)], 1, 1)))
294303
end
295304

296305
function LinearSolve.init_cacheval(alg::CHOLMODFactorization,
@@ -368,7 +377,8 @@ function LinearSolve.init_cacheval(
368377
nothing
369378
end
370379

371-
function LinearSolve.init_cacheval(alg::QRFactorization, A::SparseMatrixCSC{Float64, <:Integer}, b, u, Pl, Pr,
380+
function LinearSolve.init_cacheval(
381+
alg::QRFactorization, A::SparseMatrixCSC{Float64, <:Integer}, b, u, Pl, Pr,
372382
maxiters::Int, abstol, reltol, verbose::Bool,
373383
assumptions::OperatorAssumptions)
374384
ArrayInterface.qr_instance(convert(AbstractMatrix, A), alg.pivot)

ext/LinearSolveSparspakExt.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ function LinearSolve.init_cacheval(
2020
::SparspakFactorization, A::AbstractSparseMatrixCSC{Tv, Ti}, b, u, Pl, Pr, maxiters::Int, abstol,
2121
reltol,
2222
verbose::Bool, assumptions::OperatorAssumptions) where {Tv, Ti}
23-
24-
if size(A,1) == size(A,2)
23+
if size(A, 1) == size(A, 2)
2524
A = convert(AbstractMatrix, A)
2625
if A isa SparseArrays.AbstractSparseArray
2726
return sparspaklu(
2827
SparseMatrixCSC{Tv, Ti}(size(A)..., getcolptr(A), rowvals(A),
2928
nonzeros(A)),
3029
factorize = false)
3130
else
32-
return sparspaklu(SparseMatrixCSC{Tv, Ti}(zero(Ti), zero(Ti), [one(Ti)], Ti[], eltype(A)[]),
31+
return sparspaklu(
32+
SparseMatrixCSC{Tv, Ti}(zero(Ti), zero(Ti), [one(Ti)], Ti[], eltype(A)[]),
3333
factorize = false)
3434
end
3535
else

0 commit comments

Comments
 (0)