Skip to content

Commit 0ea250a

Browse files
authored
Merge pull request #162 from JuliaGPU/vc/chol
add ishermitian
2 parents a305f78 + 521175e commit 0ea250a

File tree

3 files changed

+32
-3
lines changed

3 files changed

+32
-3
lines changed

src/linalg.jl

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,19 @@ function LinearAlgebra.permutedims!(dest::GPUArray, src::GPUArray, perm::NTuple{
8989
return dest
9090
end
9191

92-
93-
function Base.copyto!(A::AbstractArray, B::Adjoint{T, <: GPUArray}) where T
94-
copyto!(A, Adjoint(Array(B.parent)))
92+
function Base.copyto!(A::AbstractArray, B::Adjoint{<:Any, <:GPUArray})
93+
copyto!(A, Adjoint(Array(parent(B))))
94+
end
95+
function Base.copyto!(A::AbstractArray, B::Transpose{<:Any, <:GPUArray})
96+
copyto!(A, Transpose(Array(parent(B))))
97+
end
98+
function Base.copyto!(A::AbstractArray, B::UpperTriangular{<:Any, <:GPUArray})
99+
copyto!(A, UpperTriangular(Array(parent(B))))
95100
end
101+
function Base.copyto!(A::AbstractArray, B::LowerTriangular{<:Any, <:GPUArray})
102+
copyto!(A, LowerTriangular(Array(parent(B))))
103+
end
104+
96105
function Base.copyto!(A::GPUArray, B::Adjoint{T, <: GPUArray}) where T
97106
transpose!(A, B.parent)
98107
end

src/mapreduce.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ Base.count(pred::Function, A::GPUArray) = Int(mapreduce(pred, +, A; init = 0))
1212

1313
Base.:(==)(A::GPUArray, B::GPUArray) = Bool(mapreduce(==, &, A, B; init = true))
1414

15+
LinearAlgebra.ishermitian(A::GPUMatrix) = acc_mapreduce(==, &, true, A, (adjoint(A),))
16+
1517
# hack to get around of fetching the first element of the GPUArray
1618
# as a startvalue, which is a bit complicated with the current reduce implementation
1719
function startvalue(f, T)
@@ -159,6 +161,9 @@ end
159161

160162
to_cpu(x) = x
161163
to_cpu(x::GPUArray) = Array(x)
164+
to_cpu(x::LinearAlgebra.Transpose) = LinearAlgebra.Transpose(to_cpu(parent(x)))
165+
to_cpu(x::LinearAlgebra.Adjoint) = LinearAlgebra.Adjoint(to_cpu(parent(x)))
166+
to_cpu(x::SubArray) = SubArray(to_cpu(parent(x)), parentindices(x))
162167

163168
function acc_mapreduce(
164169
f, op, v0::OT, A::GPUArray{T, N}, rest::Tuple

src/testsuite/linalg.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,27 @@ function test_linalg(AT)
22
@testset "linear algebra" begin
33
@testset "transpose" begin
44
@test compare(adjoint, AT, rand(Float32, 32, 32))
5+
@test compare(transpose, AT, rand(Float32, 32, 32))
56
end
67

78
@testset "permutedims" begin
89
@test compare(x -> permutedims(x, (2, 1)), AT, rand(Float32, 2, 3))
910
@test compare(x -> permutedims(x, (2, 1, 3)), AT, rand(Float32, 4, 5, 6))
1011
@test compare(x -> permutedims(x, (3, 1, 2)), AT, rand(Float32, 4, 5, 6))
1112
end
13+
14+
@testset "issymmetric/ishermitian" begin
15+
n = 128
16+
areal = randn(n,n)/2
17+
aimg = randn(n,n)/2
18+
19+
@testset for eltya in (Float32, Float64, ComplexF32, ComplexF64)
20+
a = convert(Matrix{eltya}, eltya <: Complex ? complex.(areal, aimg) : areal)
21+
asym = transpose(a) + a # symmetric indefinite
22+
aherm = a' + a # Hermitian indefinite
23+
@test issymmetric(asym)
24+
@test ishermitian(aherm)
25+
end
26+
end
1227
end
1328
end

0 commit comments

Comments
 (0)