diff --git a/docs/src/manual/developer_interfaces.md b/docs/src/manual/developer_interfaces.md index 4e341227..a91c6b79 100644 --- a/docs/src/manual/developer_interfaces.md +++ b/docs/src/manual/developer_interfaces.md @@ -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 diff --git a/ext/AMDGPUExt.jl b/ext/AMDGPUExt.jl index 3cff2db5..29d5752d 100644 --- a/ext/AMDGPUExt.jl +++ b/ext/AMDGPUExt.jl @@ -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 diff --git a/src/ensemblegpuarray/kernels.jl b/src/ensemblegpuarray/kernels.jl index 70af51cc..832092f7 100644 --- a/src/ensemblegpuarray/kernels.jl +++ b/src/ensemblegpuarray/kernels.jl @@ -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 @@ -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) diff --git a/test/ensemblegpuarray_lufact.jl b/test/ensemblegpuarray_lufact.jl new file mode 100644 index 00000000..81af84a3 --- /dev/null +++ b/test/ensemblegpuarray_lufact.jl @@ -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 diff --git a/test/runtests.jl b/test/runtests.jl index a0a4a6b1..b87c756d 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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 @@ -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 @@ -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