Skip to content

Commit

Permalink
Enabled the use with both explicit and implicit executor #12
Browse files Browse the repository at this point in the history
  • Loading branch information
youwuyou committed Dec 12, 2023
1 parent 738b2af commit fd05c27
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 41 deletions.
32 changes: 32 additions & 0 deletions examples/simple-solver/simple-solver-explicit-executor.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Ginkgo

# Type alias
const (Tv, Ti) = (Float32, Int32)

# Print ginkgo library version
version()

# Obtain executor with a specific backend
const exec = create(:omp)

# Read matrix and vector from mtk files
A = GkoCsr{Tv, Ti}("data/A.mtx", exec)
b = GkoDense{Tv}("data/b.mtx", exec)
x = GkoDense{Tv}("data/x0.mtx", exec)

cg!(x, A, b, exec; maxiter = 20, reduction = 1.0e-7)

@info "Solution (x):"
display(x)

one = number(Tv(1.0), exec)
neg_one = number(Tv(-1.0), exec)
res = number(Tv(0.0), exec)

# x = one*A*b + neg_one*x
spmm!(A, one, x, neg_one, b)


norm2!(b, res)
@info "Residual norm sqrt(r^T r):"
display(res)
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ version()

# Obtain executor with a specific backend
const exec = create(:omp)
@info "The executor we just created was $(exec.ptr)"

# Specify executor to be passed for matrix creation including CSR, Dense and "number" and cg solver
@with EXECUTOR => exec begin
@info "The executor we are using is $(EXECUTOR[].ptr)"
# Read matrix and vector from mtk files, now omit the passing of exec
A = GkoCsr{Tv, Ti}("data/A.mtx");
b = GkoDense{Tv}("data/b.mtx");
Expand All @@ -31,4 +33,4 @@ const exec = create(:omp)
norm2!(b, res)
@info "Residual norm sqrt(r^T r):"
display(res)
end
end
15 changes: 8 additions & 7 deletions src/matrix/Csr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,19 @@ $(_doc_external("gko::matrix::Csr<ValueType, IndexType>", "classgko_1_1matrix_1_
"""
mutable struct GkoCsr{Tv,Ti} <: AbstractSparseMatrix{Tv,Ti}
ptr::Ptr{Cvoid}
executor::ScopedValue{GkoExecutor} # Pointer to the struct wrapping the executor shared ptr

executor::GkoExecutor

############################# CONSTRUCTOR ####################################
# Constructors for matrix with initialized values
function GkoCsr{Tv,Ti}(filename::String) where {Tv,Ti}
function GkoCsr{Tv,Ti}(filename::String, executor::GkoExecutor = EXECUTOR[]) where {Tv,Ti}
!isfile(filename) && error("File not found: $filename")
function_name = Symbol("ginkgo_matrix_csr_", gko_type(Tv), "_", gko_type(Ti), "_read")
# Pass the void pointer to executor_st to C API
ptr = eval(:($API.$function_name($filename, $(EXECUTOR[].ptr))))
finalizer(delete_csr_matrix, new{Tv,Ti}(ptr, EXECUTOR));
ptr = eval(:($API.$function_name($filename, $(executor.ptr))))
finalizer(delete_csr_matrix, new{Tv,Ti}(ptr, executor));
end
# Destructor

############################# DESTRUCTOR ####################################
function delete_csr_matrix(mat::GkoCsr{Tv,Ti}) where {Tv, Ti}
@warn "Calling the destructor for GkoCsr{$Tv,$Ti}!"
function_name = Symbol("ginkgo_matrix_csr_", gko_type(Tv), "_", gko_type(Ti), "_delete")
Expand Down
46 changes: 24 additions & 22 deletions src/matrix/Dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,54 +32,55 @@ $(_doc_external("gko::matrix::Dense<T>", "classgko_1_1matrix_1_1Dense"))
"""
mutable struct GkoDense{T} <: AbstractMatrix{T}
ptr::Ptr{Cvoid}
executor::ScopedValue{GkoExecutor} # Pointer to the struct wrapping the executor shared ptr

executor::GkoExecutor

############################# CONSTRUCTOR ####################################
# Constructors for matrix with uninitialized values
function GkoDense{T}(m::Integer, n::Integer) where T
function GkoDense{T}(m::Integer, n::Integer, executor::GkoExecutor = EXECUTOR[]) where T
# @warn "Constructing $(m) x $(n) matrix with uninitialized values. Displayed values may not be meaningful. It's advisable to initialize the matrix before usage."
function_name = Symbol("ginkgo_matrix_dense_", gko_type(T), "_create")
ptr = eval(:($API.$function_name($(EXECUTOR[].ptr), $GkoDim{2}($m,$n))))
finalizer(delete_dense_matrix, new{T}(ptr, EXECUTOR))
ptr = eval(:($API.$function_name($(executor.ptr), $GkoDim{2}($m,$n))))
finalizer(delete_dense_matrix, new{T}(ptr, executor))
end

function GkoDense{T}(size::Integer) where T
function GkoDense{T}(size::Integer, executor::GkoExecutor = EXECUTOR[]) where T
# @warn "Constructing $(size) x $(size) square matrix with uninitialized values. Displayed values may not be meaningful. It's advisable to initialize the matrix before usage."
function_name = Symbol("ginkgo_matrix_dense_", gko_type(T), "_create")
ptr = eval(:($API.$function_name($(EXECUTOR[].ptr), $GkoDim{2}($size,$size))))
return finalizer(delete_dense_matrix, new{T}(ptr, EXECUTOR))
ptr = eval(:($API.$function_name($(executor.ptr), $GkoDim{2}($size,$size))))
return finalizer(delete_dense_matrix, new{T}(ptr, executor))
end

function GkoDense{T}(m::Tuple{Integer, Integer}) where T
function GkoDense{T}(m::Tuple{Integer, Integer}, executor::GkoExecutor = EXECUTOR[]) where T
# @warn "Constructing $(m[1]) x $(m[2]) matrix with uninitialized values. Displayed values may not be meaningful. It's advisable to initialize the matrix before usage."
function_name = Symbol("ginkgo_matrix_dense_", gko_type(T), "_create")
ptr = eval(:($API.$function_name($(EXECUTOR[].ptr), $m)))
finalizer(delete_dense_matrix, new{T}(ptr, EXECUTOR))
ptr = eval(:($API.$function_name($(executor.ptr), $m)))
finalizer(delete_dense_matrix, new{T}(ptr, executor))
end

function GkoDense{T}(size::GkoDim2) where T
function GkoDense{T}(size::GkoDim2, executor::GkoExecutor = EXECUTOR[]) where T
# @warn "Constructing $(size[1]) x $(size[2]) matrix with uninitialized values. Displayed values may not be meaningful. It's advisable to initialize the matrix before usage."
function_name = Symbol("ginkgo_matrix_dense_", gko_type(T), "_create")
ptr = eval(:($API.$function_name($(EXECUTOR[].ptr), $size)))
finalizer(delete_dense_matrix, new{T}(ptr, EXECUTOR))
ptr = eval(:($API.$function_name($(executor.ptr), $size)))
finalizer(delete_dense_matrix, new{T}(ptr, executor))
end

# Constructors for matrix with initialized values
function GkoDense{T}(filename::String) where T
function GkoDense{T}(filename::String, executor::GkoExecutor = EXECUTOR[]) where T
!isfile(filename) && error("File not found: $filename")
function_name = Symbol("ginkgo_matrix_dense_", gko_type(T), "_read")
ptr = eval(:($API.$function_name($filename, $(EXECUTOR[].ptr))))
finalizer(delete_dense_matrix, new{T}(ptr, EXECUTOR))
ptr = eval(:($API.$function_name($filename, $(executor.ptr))))
finalizer(delete_dense_matrix, new{T}(ptr, executor))
end

# Destructor

############################# DESTRUCTOR ####################################
function delete_dense_matrix(mat::GkoDense{T}) where T
@warn "Calling the destructor for GkoDense{$T}!"
function_name = Symbol("ginkgo_matrix_dense_", gko_type(T), "_delete")
eval(:($API.$function_name($mat.ptr)))
end
end


# Conversion
# Base.cconvert(::Type{API.gko_matrix_dense_f32_st}, obj::Dense) = API.gko_matrix_dense_f32_st()

Expand All @@ -98,14 +99,15 @@ function Base.getindex(mat::GkoDense{T}, m::Integer, n::Integer) where T
return (eval(:($API.$function_name($mat.ptr, $(m-1), $(n-1)))))
end


"""
number(val::Number)
number(val::Number, exec::GkoExecutor = EXECUTOR[])
Initialize a 1x1 matrix representing a number with the provided value `val`
"""
function number(val::Number)
function number(val::Number, exec::GkoExecutor = EXECUTOR[])
T = typeof(val)
mat = GkoDense{T}(1)
mat = GkoDense{T}(1, exec)
fill!(mat, val)
return mat
end
Expand Down
4 changes: 2 additions & 2 deletions src/solver/CG.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# gko::solver::Cg<T>
function cg!(x::Ginkgo.GkoDense{Tv}, A::Ginkgo.GkoCsr{Tv, Ti}, b::Ginkgo.GkoDense{Tv};
function cg!(x::Ginkgo.GkoDense{Tv}, A::Ginkgo.GkoCsr{Tv, Ti}, b::Ginkgo.GkoDense{Tv}, executor::GkoExecutor = EXECUTOR[];
abstol::Real = zero(real(eltype(b))),
reltol::Real = sqrt(eps(real(eltype(b)))),
reduction::Real = 1e-3,
Expand All @@ -11,5 +11,5 @@ function cg!(x::Ginkgo.GkoDense{Tv}, A::Ginkgo.GkoCsr{Tv, Ti}, b::Ginkgo.GkoDens
kwargs...) where {Tv, Ti}

# Store the result in the x
API.ginkgo_solver_cg_solve(EXECUTOR[].ptr, A.ptr, b.ptr, x.ptr, maxiter, reduction)
API.ginkgo_solver_cg_solve(executor.ptr, A.ptr, b.ptr, x.ptr, maxiter, reduction)
end
29 changes: 21 additions & 8 deletions test/matrix/test-csr.jl
Original file line number Diff line number Diff line change
@@ -1,29 +1,42 @@
# gko::matrix::Csr<T>
for (Tv, Ti) in Iterators.product(SUPPORTED_CSR_ELTYPE, SUPPORTED_CSR_INDEXTYPE)
@testset "Integration test: gko::matrix::Csr<$Tv, $Ti> read from file and retrieve information " begin

@testset "Integration test: gko::matrix::Csr<$Tv, $Ti> read from file and retrieve information [implicit executor] " begin
exec = create(:omp)
with(EXECUTOR => exec) do
A = Ginkgo.GkoCsr{Tv, Ti}("matrix/data/A.mtx")

# Retrive dimensions
@test Ginkgo.size(A) == (19, 19)

# Retrive nnz in the sparse matrix, number of elements stored
@test Ginkgo.nnz(A) == 147

# Detail
@test Ginkgo.srows(A) == 0
end


end
end


for (Tv, Ti) in Iterators.product(SUPPORTED_CSR_ELTYPE, SUPPORTED_CSR_INDEXTYPE)
@testset "Integration test: gko::matrix::Csr<$Tv, $Ti> read from file and retrieve information [explicit executor]" begin
exec = create(:omp)
A = Ginkgo.GkoCsr{Tv, Ti}("matrix/data/A.mtx", exec)
# Retrive dimensions
@test Ginkgo.size(A) == (19, 19)

# Retrive nnz in the sparse matrix, number of elements stored
@test Ginkgo.nnz(A) == 147

# Detail
@test Ginkgo.srows(A) == 0
end
end


# @testset "Integration test: gko::matrix::Csr<$Tv, $Ti> read from file and retrieve information " begin

# @testset "Integration test: gko::matrix::Csr<$Tv, $Ti> read from file and retrieve information " begin

# exec = create(:omp)
# A = Ginkgo.GkoCsr{Tv, Ti}("matrix/data/A.mtx", exec)
Expand Down
2 changes: 1 addition & 1 deletion test/matrix/test-dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ exec = create(:omp)

for T in SUPPORTED_DENSE_ELTYPE


# Using executor implicitly
with(EXECUTOR => exec) do
# Create unitialized matrix, will trigger warnings
M = Ginkgo.GkoDense{T}(Ginkgo.GkoDim{2}(2, 3))
Expand Down

0 comments on commit fd05c27

Please sign in to comment.