diff --git a/lib/JLArrays/src/JLArrays.jl b/lib/JLArrays/src/JLArrays.jl index 2b5281c6..9784ac37 100644 --- a/lib/JLArrays/src/JLArrays.jl +++ b/lib/JLArrays/src/JLArrays.jl @@ -349,6 +349,31 @@ function Base.unsafe_convert(::Type{Ptr{T}}, x::JLArray{T}) where {T} error("Illegal conversion of a JLArray to a Ptr") end +""" + lu(A::JLArray, args...; kwargs...) + +Compute an LU factorization using the host representation of `A`. +""" +function LinearAlgebra.lu(A::JLArray{T, 2}, args...; kwargs...) where {T} + return LinearAlgebra.lu(Array(A), args...; kwargs...) +end + +function LinearAlgebra.ldiv!( + F::LinearAlgebra.LU{T, <:StridedMatrix{T}}, B::JLArray{T, N} + ) where {T <: LinearAlgebra.BlasFloat, N} + B_cpu = Array(B) + LinearAlgebra.ldiv!(F, B_cpu) + copyto!(B, B_cpu) + return B +end + +function LinearAlgebra.ldiv!(Y::JLArray, F::LinearAlgebra.LU, B::JLArray) + B_cpu = Array(B) + LinearAlgebra.ldiv!(F, B_cpu) + copyto!(Y, B_cpu) + return Y +end + ## interop with Julia arrays diff --git a/test/jlarrays_lu.jl b/test/jlarrays_lu.jl new file mode 100644 index 00000000..d8814349 --- /dev/null +++ b/test/jlarrays_lu.jl @@ -0,0 +1,16 @@ +using Test, JLArrays, LinearAlgebra + +@testset "JLArray LU" begin + A = jl([2.0 1.0; 1.0 3.0]) + F = lu(A; check = false) + + @test F isa LU{Float64, Matrix{Float64}, Vector{Int}} + + b = jl([1.0, 2.0]) + ldiv!(F, b) + @test Array(b) ≈ [0.2, 0.6] + + y = similar(b) + ldiv!(y, F, jl([1.0, 2.0])) + @test Array(y) ≈ [0.2, 0.6] +end diff --git a/test/runtests.jl b/test/runtests.jl index a91715b9..fb26b4c1 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,6 +1,8 @@ using ParallelTestRunner: runtests, parse_args import GPUArrays +include("jlarrays_lu.jl") + include("testsuite.jl") const init_worker_code = quote