Browse Source

copy! -> copyto! (??)

fe/copyto
Fredrik Ekre 3 years ago
parent
commit
2a0ed3dfa2
  1. 7
      src/HYPRE.jl
  2. 8
      src/solvers.jl
  3. 36
      test/runtests.jl

7
src/HYPRE.jl

@ -269,11 +269,12 @@ HYPREVector(x::Vector, ilower=1, iupper=length(x)) =
HYPREVector(MPI.COMM_WORLD, x, ilower, iupper) HYPREVector(MPI.COMM_WORLD, x, ilower, iupper)
# TODO: Other eltypes could be support by using a intermediate buffer # TODO: Other eltypes could be support by using a intermediate buffer
function Base.copy!(x::Vector{HYPRE_Complex}, h::HYPREVector) function Base.copyto!(x::Vector{HYPRE_Complex}, h::HYPREVector)
ilower, iupper = Internals.get_proc_rows(h) ilower, iupper = Internals.get_proc_rows(h)
nvalues = iupper - ilower + 1 nvalues = iupper - ilower + 1
if length(x) != nvalues if length(x) != nvalues
throw(ArgumentError("different lengths")) # TODO: This isn't required by the copyto! interface, only that there is enough room
throw(ArgumentError("length(dst) != length(src)"))
end end
indices = collect(HYPRE_BigInt, ilower:iupper) indices = collect(HYPRE_BigInt, ilower:iupper)
@check HYPRE_IJVectorGetValues(h.IJVector, nvalues, indices, x) @check HYPRE_IJVectorGetValues(h.IJVector, nvalues, indices, x)
@ -455,7 +456,7 @@ function HYPREVector(v::PVector)
end end
# TODO: Other eltypes could be support by using a intermediate buffer # TODO: Other eltypes could be support by using a intermediate buffer
function Base.copy!(v::PVector{HYPRE_Complex}, h::HYPREVector) function Base.copyto!(v::PVector{HYPRE_Complex}, h::HYPREVector)
ilower_v, iupper_v = Internals.get_proc_rows(v) ilower_v, iupper_v = Internals.get_proc_rows(v)
ilower_h, iupper_h = Internals.get_proc_rows(h) ilower_h, iupper_h = Internals.get_proc_rows(h)
if ilower_v != ilower_h && iupper_v != iupper_h if ilower_v != ilower_h && iupper_v != iupper_h

8
src/solvers.jl

@ -40,13 +40,13 @@ solve!(pcg::HYPRESolver, x::HYPREVector, A::HYPREMatrix, ::HYPREVector)
function solve(solver::HYPRESolver, A::PSparseMatrix, b::PVector) function solve(solver::HYPRESolver, A::PSparseMatrix, b::PVector)
hypre_x = solve(solver, HYPREMatrix(A), HYPREVector(b)) hypre_x = solve(solver, HYPREMatrix(A), HYPREVector(b))
x = copy!(similar(b, HYPRE_Complex), hypre_x) x = copyto!(similar(b, HYPRE_Complex), hypre_x)
return x return x
end end
function solve!(solver::HYPRESolver, x::PVector, A::PSparseMatrix, b::PVector) function solve!(solver::HYPRESolver, x::PVector, A::PSparseMatrix, b::PVector)
hypre_x = HYPREVector(x) hypre_x = HYPREVector(x)
solve!(solver, hypre_x, HYPREMatrix(A), HYPREVector(b)) solve!(solver, hypre_x, HYPREMatrix(A), HYPREVector(b))
copy!(x, hypre_x) copyto!(x, hypre_x)
return x return x
end end
@ -58,13 +58,13 @@ end
function solve(solver::HYPRESolver, A::Union{SparseMatrixCSC,SparseMatrixCSR}, b::Vector) function solve(solver::HYPRESolver, A::Union{SparseMatrixCSC,SparseMatrixCSR}, b::Vector)
hypre_x = solve(solver, HYPREMatrix(A), HYPREVector(b)) hypre_x = solve(solver, HYPREMatrix(A), HYPREVector(b))
x = copy!(similar(b, HYPRE_Complex), hypre_x) x = copyto!(similar(b, HYPRE_Complex), hypre_x)
return x return x
end end
function solve!(solver::HYPRESolver, x::Vector, A::Union{SparseMatrixCSC,SparseMatrixCSR}, b::Vector) function solve!(solver::HYPRESolver, x::Vector, A::Union{SparseMatrixCSC,SparseMatrixCSR}, b::Vector)
hypre_x = HYPREVector(x) hypre_x = HYPREVector(x)
solve!(solver, hypre_x, HYPREMatrix(A), HYPREVector(b)) solve!(solver, hypre_x, HYPREMatrix(A), HYPREVector(b))
copy!(x, hypre_x) copyto!(x, hypre_x)
return x return x
end end

36
test/runtests.jl

@ -191,11 +191,11 @@ end
@test h.IJVector != HYPRE_IJVector(C_NULL) @test h.IJVector != HYPRE_IJVector(C_NULL)
@test h.ParVector != HYPRE_ParVector(C_NULL) @test h.ParVector != HYPRE_ParVector(C_NULL)
# Base.zero(::HYPREVector) and Base.copy!(::Vector, HYPREVector) # Base.zero(::HYPREVector) and Base.copyto!(::Vector, HYPREVector)
b = rand(10) b = rand(10)
h = HYPREVector(b, 1, 10) h = HYPREVector(b, 1, 10)
z = zero(h) z = zero(h)
b′ = copy!(b, z) b′ = copyto!(b, z)
@test b === b′ @test b === b′
@test iszero(b) @test iszero(b)
end end
@ -254,7 +254,7 @@ end
@test H.IJVector != HYPRE_IJVector(C_NULL) @test H.IJVector != HYPRE_IJVector(C_NULL)
@test H.ParVector != HYPRE_ParVector(C_NULL) @test H.ParVector != HYPRE_ParVector(C_NULL)
pbc = fill!(copy(pb), 0) pbc = fill!(copy(pb), 0)
copy!(pbc, H) copyto!(pbc, H)
@test tomain(pbc) == tomain(pb) @test tomain(pbc) == tomain(pb)
# MPI backend # MPI backend
backend = MPIBackend() backend = MPIBackend()
@ -271,7 +271,7 @@ end
@test H.IJVector != HYPRE_IJVector(C_NULL) @test H.IJVector != HYPRE_IJVector(C_NULL)
@test H.ParVector != HYPRE_ParVector(C_NULL) @test H.ParVector != HYPRE_ParVector(C_NULL)
pbc = fill!(copy(pb), 0) pbc = fill!(copy(pb), 0)
copy!(pbc, H) copyto!(pbc, H)
@test tomain(pbc) == tomain(pb) @test tomain(pbc) == tomain(pb)
end end
@ -292,12 +292,12 @@ end
tol = 1e-9 tol = 1e-9
bicg = HYPRE.BiCGSTAB(; Tol = tol) bicg = HYPRE.BiCGSTAB(; Tol = tol)
HYPRE.solve!(bicg, x_h, A_h, b_h) HYPRE.solve!(bicg, x_h, A_h, b_h)
copy!(x, x_h) copyto!(x, x_h)
# Test result with direct solver # Test result with direct solver
@test x A \ b atol=tol @test x A \ b atol=tol
# Test without passing initial guess # Test without passing initial guess
x_h = HYPRE.solve(bicg, A_h, b_h) x_h = HYPRE.solve(bicg, A_h, b_h)
copy!(x, x_h) copyto!(x, x_h)
@test x A \ b atol=tol @test x A \ b atol=tol
# Solve with preconditioner # Solve with preconditioner
@ -305,12 +305,12 @@ end
bicg = HYPRE.BiCGSTAB(; Tol = tol, Precond = precond) bicg = HYPRE.BiCGSTAB(; Tol = tol, Precond = precond)
x_h = HYPREVector(zeros(100)) x_h = HYPREVector(zeros(100))
HYPRE.solve!(bicg, x_h, A_h, b_h) HYPRE.solve!(bicg, x_h, A_h, b_h)
copy!(x, x_h) copyto!(x, x_h)
# Test result with direct solver # Test result with direct solver
@test x A \ b atol=tol @test x A \ b atol=tol
# Test without passing initial guess # Test without passing initial guess
x_h = HYPRE.solve(bicg, A_h, b_h) x_h = HYPRE.solve(bicg, A_h, b_h)
copy!(x, x_h) copyto!(x, x_h)
@test x A \ b atol=tol @test x A \ b atol=tol
end end
@ -332,12 +332,12 @@ end
tol = 1e-9 tol = 1e-9
amg = HYPRE.BoomerAMG(; Tol = tol) amg = HYPRE.BoomerAMG(; Tol = tol)
HYPRE.solve!(amg, x_h, A_h, b_h) HYPRE.solve!(amg, x_h, A_h, b_h)
copy!(x, x_h) copyto!(x, x_h)
# Test result with direct solver # Test result with direct solver
@test x A \ b atol=tol @test x A \ b atol=tol
# Test without passing initial guess # Test without passing initial guess
x_h = HYPRE.solve(amg, A_h, b_h) x_h = HYPRE.solve(amg, A_h, b_h)
copy!(x, x_h) copyto!(x, x_h)
@test x A \ b atol=tol @test x A \ b atol=tol
end end
@ -358,12 +358,12 @@ end
tol = 1e-9 tol = 1e-9
gmres = HYPRE.GMRES(; Tol = tol) gmres = HYPRE.GMRES(; Tol = tol)
HYPRE.solve!(gmres, x_h, A_h, b_h) HYPRE.solve!(gmres, x_h, A_h, b_h)
copy!(x, x_h) copyto!(x, x_h)
# Test result with direct solver # Test result with direct solver
@test x A \ b atol=tol @test x A \ b atol=tol
# Test without passing initial guess # Test without passing initial guess
x_h = HYPRE.solve(gmres, A_h, b_h) x_h = HYPRE.solve(gmres, A_h, b_h)
copy!(x, x_h) copyto!(x, x_h)
@test x A \ b atol=tol @test x A \ b atol=tol
# Solve with preconditioner # Solve with preconditioner
@ -371,12 +371,12 @@ end
gmres = HYPRE.GMRES(; Tol = tol, Precond = precond) gmres = HYPRE.GMRES(; Tol = tol, Precond = precond)
x_h = HYPREVector(zeros(100)) x_h = HYPREVector(zeros(100))
HYPRE.solve!(gmres, x_h, A_h, b_h) HYPRE.solve!(gmres, x_h, A_h, b_h)
copy!(x, x_h) copyto!(x, x_h)
# Test result with direct solver # Test result with direct solver
@test x A \ b atol=tol @test x A \ b atol=tol
# Test without passing initial guess # Test without passing initial guess
x_h = HYPRE.solve(gmres, A_h, b_h) x_h = HYPRE.solve(gmres, A_h, b_h)
copy!(x, x_h) copyto!(x, x_h)
@test x A \ b atol=tol @test x A \ b atol=tol
end end
@ -398,24 +398,24 @@ end
tol = 1e-9 tol = 1e-9
pcg = HYPRE.PCG(; Tol = tol) pcg = HYPRE.PCG(; Tol = tol)
HYPRE.solve!(pcg, x_h, A_h, b_h) HYPRE.solve!(pcg, x_h, A_h, b_h)
copy!(x, x_h) copyto!(x, x_h)
# Test result with direct solver # Test result with direct solver
@test x A \ b atol=tol @test x A \ b atol=tol
# Test without passing initial guess # Test without passing initial guess
x_h = HYPRE.solve(pcg, A_h, b_h) x_h = HYPRE.solve(pcg, A_h, b_h)
copy!(x, x_h) copyto!(x, x_h)
@test x A \ b atol=tol @test x A \ b atol=tol
# Solve with AMG preconditioner # Solve with AMG preconditioner
precond = HYPRE.BoomerAMG(; MaxIter = 1, Tol = 0.0) precond = HYPRE.BoomerAMG(; MaxIter = 1, Tol = 0.0)
pcg = HYPRE.PCG(; Tol = tol, Precond = precond) pcg = HYPRE.PCG(; Tol = tol, Precond = precond)
x_h = HYPREVector(zeros(100)) x_h = HYPREVector(zeros(100))
HYPRE.solve!(pcg, x_h, A_h, b_h) HYPRE.solve!(pcg, x_h, A_h, b_h)
copy!(x, x_h) copyto!(x, x_h)
# Test result with direct solver # Test result with direct solver
@test x A \ b atol=tol @test x A \ b atol=tol
# Test without passing initial guess # Test without passing initial guess
x_h = HYPRE.solve(pcg, A_h, b_h) x_h = HYPRE.solve(pcg, A_h, b_h)
copy!(x, x_h) copyto!(x, x_h)
@test x A \ b atol=tol @test x A \ b atol=tol
end end

Loading…
Cancel
Save