Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/src/manual/developer_interfaces.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ DiffEqGPU.lufact!
DiffEqGPU.LinSolveGPUSplitFactorize
```

`lufact!` provides a KernelAbstractions fallback for batched LU without pivoting, including
AMDGPU and JLArrays. CPU and CUDA have specialized implementations. Each matrix must admit
factorization without zero pivots; the factors are stored in place for
`LinSolveGPUSplitFactorize`.

## Problem Conversion

`make_prob_compatible` is the generic conversion hook used before passing a batch of
Expand Down
6 changes: 0 additions & 6 deletions ext/AMDGPUExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,4 @@ end
DiffEqGPU.maxthreads(::ROCBackend) = 256
DiffEqGPU.maybe_prefer_blocks(::ROCBackend) = ROCBackend()

# Not yet implemented in AMDGPU
# function DiffEqGPU.lufact!(::ROCBackend, W)
# AMDGPU.rocBLAS.getrf_strided_batched!(W, false)
# return nothing
# end

end
26 changes: 21 additions & 5 deletions src/ensemblegpuarray/kernels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -395,12 +395,12 @@ end
"""
lufact!(backend, W)

Factorize each square matrix in a batched matrix array in place.
Factorize each square matrix in a batched matrix array in place without pivoting.

This is a developer interface implemented by backend extensions. The factorization is
consumed by `LinSolveGPUSplitFactorize`; each slice `W[:, :, i]` must be a square matrix,
and the backend implementation must provide the factorization operation callable from the
selected execution environment.
The generic KernelAbstractions implementation factorizes one matrix per work item.
Backend extensions may provide specialized implementations. The factorization is consumed
by `LinSolveGPUSplitFactorize`; each slice `W[:, :, i]` must be square and admit LU
factorization without zero pivots.

# Arguments

Expand All @@ -419,6 +419,22 @@ W = reshape([2.0f0, 0.0f0, 0.0f0, 3.0f0], 2, 2, 1)
lufact!(CPU(), W)
```
"""
function lufact!(backend::KernelAbstractions.Backend, W)
nfacts = size(W, 3)
iszero(nfacts) && return nothing
lufact!_kernel(backend)(
W, size(W, 1); ndrange = nfacts,
workgroupsize = workgroupsize(backend, nfacts)
)
return nothing
end

@kernel function lufact!_kernel(W, @Const(len))
i = @index(Global, Linear)
_W = @inbounds @view(W[:, :, i])
generic_lufact!(_W, len)
end

function lufact!(::CPU, W)
len = size(W, 1)
for i in 1:size(W, 3)
Expand Down
37 changes: 37 additions & 0 deletions test/ensemblegpuarray_lufact.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using DiffEqGPU, Test
using KernelAbstractions: Backend, allocate
using LinearAlgebra: UnitLowerTriangular, UpperTriangular

include("utils.jl")

function portable_lufact!(backend, W)
return invoke(DiffEqGPU.lufact!, Tuple{Backend, Any}, backend, W)
end

@testset "Batched LU $T, size $n, batches $nbatch" for T in (Float32, Float64),
n in (1, 3, 5), nbatch in (1, 257)
A = [T(i == j ? 3n + i + k / 257 : (i - 2j) / (n + k)) for i in 1:n, j in 1:n, k in 1:nbatch]
for factorize! in (DiffEqGPU.lufact!, portable_lufact!)
W = allocate(backend, T, n, n, nbatch)
copyto!(W, A)
b = reshape(T.(1:(n * nbatch)), n, nbatch)
x = allocate(backend, T, n, nbatch)
rhs = allocate(backend, T, n, nbatch)
copyto!(rhs, b)
@test factorize!(backend, W) === nothing
DiffEqGPU.LinSolveGPUSplitFactorize(n, nbatch)(x, W, rhs)
factors = Array(W)
for k in 1:nbatch
F = factors[:, :, k]
@test UnitLowerTriangular(F) * UpperTriangular(F) ≈ A[:, :, k]
end
solutions = Array(x)
@test all(k -> isapprox(A[:, :, k] * solutions[:, k], b[:, k]), 1:nbatch)
end
end

@testset "Empty batched LU" begin
W = allocate(backend, Float32, 3, 3, 0)
@test portable_lufact!(backend, W) === nothing
@test size(W) == (3, 3, 0)
end
18 changes: 12 additions & 6 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ if GROUP == "QA"
exit()
end

if GROUP in ("CPU", "JLArrays", "CUDA", "AMDGPU")
@time @safetestset "EnsembleGPUArray batched LU" begin
include("ensemblegpuarray_lufact.jl")
end
end

if GROUP == "CPU" || GROUP in SUPPORTS_LUFACT
@time @safetestset "EnsembleGPUArray implicit methods" begin
include("ensemblegpuarray_implicit_jac.jl")
end
end

@time @safetestset "GPU Kernelized Stiff ODE Mass Matrix" begin
include("gpu_kernel_de/stiff_ode/gpu_ode_mass_matrix.jl")
end
Expand Down Expand Up @@ -87,9 +99,6 @@ if GROUP in SUPPORTS_LUFACT
@time @safetestset "EnsembleGPUArray OOP" begin
include("ensemblegpuarray_oop.jl")
end
@time @safetestset "EnsembleGPUArray implicit methods" begin
include("ensemblegpuarray_implicit_jac.jl")
end
end

# EnsembleGPUArray kernels has Int64 arguments, causing them to fail with Metal and oneAPI
Expand Down Expand Up @@ -133,9 +142,6 @@ if GROUP == "CPU"
@time @safetestset "EnsembleGPUArray scalar batch parameters" begin
include("ensemblegpuarray_scalar_batch.jl")
end
@time @safetestset "EnsembleGPUArray implicit methods" begin
include("ensemblegpuarray_implicit_jac.jl")
end
@time @safetestset "Kernel ensemble host storage" begin
include("gpu_kernel_de/host_fastpath.jl")
end
Expand Down
Loading