diff --git a/examples/ksp/ex50.jl b/examples/ksp/ex50.jl index d7d3e413..d7b39337 100644 --- a/examples/ksp/ex50.jl +++ b/examples/ksp/ex50.jl @@ -13,10 +13,10 @@ function rhs!( ksp::PETSc.AbstractKSP{PetscScalar}, b_vec::PETSc.AbstractVec{PetscScalar}, ) where {PetscScalar} - dm = PETSc.KSPGetDM(ksp) + dm = PETSc.DMDA(ksp) comm = PETSc.getcomm(ksp) - corners = PETSc.DMDAGetCorners(dm) - global_size = PETSc.DMDAGetInfo(dm).global_size[1:2] + corners = PETSc.getcorners(dm) + global_size = PETSc.getinfo(dm).global_size[1:2] # Grid spacing in each direction h = PetscScalar(1) ./ global_size @@ -54,11 +54,11 @@ function jacobian!( J::PETSc.AbstractMat{PetscScalar}, jac::PETSc.AbstractMat{PetscScalar}, ) where {PetscScalar} - dm = PETSc.KSPGetDM(ksp) - corners = PETSc.DMDAGetCorners(dm) + dm = PETSc.DMDA(ksp) + corners = PETSc.getcorners(dm) PetscInt = eltype(corners.size) - global_size = PETSc.DMDAGetInfo(dm).global_size[1:2] + global_size = PETSc.getinfo(dm).global_size[1:2] # Grid spacing in each direction h = PetscScalar(1) ./ global_size diff --git a/src/dm.jl b/src/dm.jl index 5e20d63a..a886bd80 100644 --- a/src/dm.jl +++ b/src/dm.jl @@ -1,37 +1,67 @@ const CDM = Ptr{Cvoid} - abstract type AbstractDM{PetscLib} end +""" + DMLocalVec(v::CVec, dm::AbstractDM) + +Container for an PETSc vector we know is "local" + +# External Links +$(_doc_external("Vec/Vec")) +""" +mutable struct DMLocalVec{PetscLib, T, T_DM} <: AbstractVec{T} + ptr::CVec + dm::T_DM + function DMLocalVec(ptr, dm::AbstractDM{PetscLib}) where {PetscLib} + new{PetscLib, scalartype(PetscLib), typeof(dm)}(ptr, dm) + end +end + +""" + DMGlobalVec(v::CVec, dm::AbstractDM) + +Container for an PETSc vector we know is "global" + +# External Links +$(_doc_external("Vec/Vec")) +""" +mutable struct DMGlobalVec{PetscLib, T, T_DM} <: AbstractVec{T} + ptr::CVec + dm::T_DM + function DMGlobalVec(ptr, dm::AbstractDM{PetscLib}) where {PetscLib} + new{PetscLib, scalartype(PetscLib), typeof(dm)}(ptr, dm) + end +end + # Mainly for DM we do not know the type of, namely ones returned by PETSc # functions such as `KSPGetDM` mutable struct PetscDM{PetscLib} <: AbstractDM{PetscLib} ptr::CDM end - """ - DMSetUp!(da::DM, opts=da.opts) + setup!(da::DM, opts=da.opts) # External Links $(_doc_external("DM/DMSetUp")) """ -function DMSetUp! end +function setup! end -@for_petsc function DMSetUp!(da::AbstractDM{$PetscLib}, opts::Options = da.opts) +@for_petsc function setup!(da::AbstractDM{$PetscLib}, opts::Options = da.opts) with(opts) do @chk ccall((:DMSetUp, $petsc_library), PetscErrorCode, (CDM,), da) end end """ - DMSetFromOptions!(da::DM, opts=da.opts) + setfromoptions!(da::DM, opts=da.opts) # External Links $(_doc_external("DM/DMSetFromOptions")) """ -function DMSetFromOptions! end +function setfromoptions! end -@for_petsc function DMSetFromOptions!( +@for_petsc function setfromoptions!( da::AbstractDM{$PetscLib}, opts::Options = da.opts, ) @@ -59,16 +89,16 @@ end end """ - DMGetDimension(dm::AbstractDM) + getdimension(dm::AbstractDM) Return the topological dimension of the `dm` # External Links $(_doc_external("DM/DMGetDimension")) """ -function DMGetDimension end +function getdimension(::AbstractDM) end -@for_petsc function DMGetDimension(dm::AbstractDM{$PetscLib}) +@for_petsc function getdimension(dm::AbstractDM{$PetscLib}) dim = Ref{$PetscInt}() @chk ccall( @@ -129,16 +159,16 @@ function view(::AbstractDM) end end """ - DMCreateMatrix(dm::AbstractDM) + creatematrix(dm::AbstractDM) Generates a matrix from the `dm` object. # External Links $(_doc_external("DM/DMCreateMatrix")) """ -function DMCreateMatrix end +function creatematrix end -@for_petsc function DMCreateMatrix(dm::AbstractDM{$PetscLib}) +@for_petsc function creatematrix(dm::AbstractDM{$PetscLib}) mat = Mat{$PetscScalar}(C_NULL) @chk ccall( @@ -153,17 +183,17 @@ function DMCreateMatrix end end """ - DMCreateLocalVector(dm::AbstractDM) + createlocalvector(dm::AbstractDM) returns a local vector from the `dm` object. # External Links $(_doc_external("DM/DMCreateLocalVector")) """ -function DMCreateLocalVector end +function createlocalvector end -@for_petsc function DMCreateLocalVector(dm::AbstractDM{$PetscLib}) - vec = Vec{$PetscScalar}(C_NULL) +@for_petsc function createlocalvector(dm::AbstractDM{$PetscLib}) + vec = DMLocalVec(C_NULL, dm) @chk ccall( (:DMCreateLocalVector, $petsc_library), @@ -177,17 +207,17 @@ function DMCreateLocalVector end end """ - DMCreateGlobalVector(dm::DM; write::Bool = true, read::Bool = true) + createglobalvector(dm::DM; write::Bool = true, read::Bool = true) returns a global vector from the `dm` object. # External Links $(_doc_external("DM/DMCreateGlobalVector")) """ -function DMCreateGlobalVector end +function createglobalvector end -@for_petsc function DMCreateGlobalVector(dm::AbstractDM{$PetscLib}) - vec = Vec{$PetscScalar}(C_NULL) +@for_petsc function createglobalvector(dm::AbstractDM{$PetscLib}) + vec = DMGlobalVec(C_NULL, dm) @chk ccall( (:DMCreateGlobalVector, $petsc_library), @@ -201,31 +231,30 @@ function DMCreateGlobalVector end end """ - DMLocalToGlobal!( - dm::AbstractDM - local_vec::AbstractVec, + update!( + global_vec::DMGlobalVec, + local_vec::DMLocalVec, mode::InsertMode, - global_vec::AbstractVec, ) -Updates `global_vec` from `local_vec` using the `dm` with insert `mode` +Updates `global_vec` from `local_vec` with insert `mode` # External Links $(_doc_external("DM/DMLocalToGlobal")) """ -function DMLocalToGlobal! end +update!(::DMGlobalVec, ::DMLocalVec, ::InsertMode) -@for_petsc function DMLocalToGlobal!( - dm::AbstractDM{$PetscLib}, - local_vec::AbstractVec, +@for_petsc function update!( + global_vec::DMGlobalVec{$PetscLib}, + local_vec::DMLocalVec{$PetscLib}, mode::InsertMode, - global_vec::AbstractVec, ) + @assert local_vec.dm === global_vec.dm @chk ccall( (:DMLocalToGlobal, $petsc_library), PetscErrorCode, (CDM, CVec, InsertMode, CVec), - dm, + local_vec.dm, local_vec, mode, global_vec, @@ -235,31 +264,30 @@ function DMLocalToGlobal! end end """ - DMGlobalToLocal!( - dm::DM - global_vec::AbstractVec, + update!( + local_vec::DMLocalVec, + global_vec::DMGlobalVec, mode::InsertMode, - local_vec::AbstractVec, ) -Updates `local_vec` from `global_vec` using the `dm` with insert `mode` +Updates `local_vec` from `global_vec` with insert `mode` # External Links $(_doc_external("DM/DMGlobalToLocal")) """ -function DMGlobalToLocal! end +update!(::DMLocalVec, ::DMGlobalVec, ::InsertMode) -@for_petsc function DMGlobalToLocal!( - dm::AbstractDM{$PetscLib}, - global_vec::AbstractVec, +@for_petsc function update!( + local_vec::DMLocalVec{$PetscLib}, + global_vec::DMGlobalVec{$PetscLib}, mode::InsertMode, - local_vec::AbstractVec, ) + @assert local_vec.dm === global_vec.dm @chk ccall( (:DMGlobalToLocal, $petsc_library), PetscErrorCode, (CDM, CVec, InsertMode, CVec), - dm, + global_vec.dm, global_vec, mode, local_vec, @@ -269,18 +297,16 @@ function DMGlobalToLocal! end end """ - DMGetCoordinateDM( - dm::AbstractDM - ) + getcoordinateDM(dm::AbstractDM) Create a `coord_dm` for the coordinates of `dm`. # External Links $(_doc_external("DM/DMGetCoordinateDM")) """ -function DMGetCoordinateDM end +function getcoordinateDM end -@for_petsc function DMGetCoordinateDM(dm::AbstractDM{$PetscLib}) +@for_petsc function getcoordinateDM(dm::AbstractDM{$PetscLib}) coord_dm = empty(dm) @chk ccall( (:DMGetCoordinateDM, $petsc_library), @@ -296,19 +322,17 @@ function DMGetCoordinateDM end end """ - DMGetCoordinatesLocal( - dm::AbstractDM - ) + getcoordinateslocal(dm::AbstractDM) Gets a local vector with the coordinates associated with `dm`. # External Links $(_doc_external("DM/DMGetCoordinatesLocal")) """ -function DMGetCoordinatesLocal end +function getcoordinateslocal end -@for_petsc function DMGetCoordinatesLocal(dm::AbstractDM{$PetscLib}) - coord_vec = Vec{$PetscScalar}(C_NULL) +@for_petsc function getcoordinateslocal(dm::AbstractDM{$PetscLib}) + coord_vec = DMLocalVec(C_NULL, dm) @chk ccall( (:DMGetCoordinatesLocal, $petsc_library), PetscErrorCode, diff --git a/src/dmda.jl b/src/dmda.jl index 4a3b29f5..f25332c3 100644 --- a/src/dmda.jl +++ b/src/dmda.jl @@ -25,7 +25,7 @@ end return an uninitialized `DMDA` struct. """ -Base.empty(::DMDA{PetscLib}) where PetscLib = DMDA{PetscLib}(C_NULL) +Base.empty(::DMDA{PetscLib}) where {PetscLib} = DMDA{PetscLib}(C_NULL) """ DMDACreate1d( @@ -44,8 +44,8 @@ Base.empty(::DMDA{PetscLib}) where PetscLib = DMDA{PetscLib}(C_NULL) Creates a 1-D distributed array with the options specified using keyword arguments. -If keyword argument `dmsetfromoptions == true` then `DMSetFromOptions!` called. -If keyword argument `dmsetup == true` then `DMSetUp!` is called. +If keyword argument `dmsetfromoptions == true` then `setfromoptions!` called. +If keyword argument `dmsetup == true` then `setup!` is called. # External Links $(_doc_external("DMDA/DMDACreate1d")) @@ -60,8 +60,8 @@ function DMDACreate1d end dof_per_node, stencil_width, points_per_proc::Union{Nothing, Vector{$PetscInt}}; - dmsetfromoptions=true, - dmsetup=true, + dmsetfromoptions = true, + dmsetup = true, options..., ) opts = Options($petsclib, options...) @@ -94,8 +94,8 @@ function DMDACreate1d end da, ) end - dmsetfromoptions && DMSetFromOptions!(da) - dmsetup && DMSetUp!(da) + dmsetfromoptions && setfromoptions!(da) + dmsetup && setup!(da) # We can only let the garbage collect finalize when we do not need to # worry about MPI (since garbage collection is asyncronous) if comm == MPI.COMM_SELF @@ -127,8 +127,8 @@ end Creates a 2-D distributed array with the options specified using keyword arguments. -If keyword argument `dmsetfromoptions == true` then `DMSetFromOptions!` called. -If keyword argument `dmsetup == true` then `DMSetUp!` is called. +If keyword argument `dmsetfromoptions == true` then `setfromoptions!` called. +If keyword argument `dmsetup == true` then `setup!` is called. # External Links $(_doc_external("DMDA/DMDACreate2d")) @@ -149,8 +149,8 @@ function DMDACreate2d end stencil_width, points_per_proc_x::Union{Nothing, Vector{$PetscInt}}, points_per_proc_y::Union{Nothing, Vector{$PetscInt}}; - dmsetfromoptions=true, - dmsetup=true, + dmsetfromoptions = true, + dmsetup = true, options..., ) opts = Options($petsclib, options...) @@ -201,8 +201,8 @@ function DMDACreate2d end da, ) end - dmsetfromoptions && DMSetFromOptions!(da) - dmsetup && DMSetUp!(da) + dmsetfromoptions && setfromoptions!(da) + dmsetup && setup!(da) # We can only let the garbage collect finalize when we do not need to # worry about MPI (since garbage collection is asyncronous) if comm == MPI.COMM_SELF @@ -212,7 +212,7 @@ function DMDACreate2d end end """ - DMDACreate2d( + DMDACreate3d( ::PetscLib comm::MPI.Comm, boundary_type_x::DMBoundaryType, @@ -239,8 +239,8 @@ end Creates a 3-D distributed array with the options specified using keyword arguments. -If keyword argument `dmsetfromoptions == true` then `DMSetFromOptions!` called. -If keyword argument `dmsetup == true` then `DMSetUp!` is called. +If keyword argument `dmsetfromoptions == true` then `setfromoptions!` called. +If keyword argument `dmsetup == true` then `setup!` is called. # External Links $(_doc_external("DMDA/DMDACreate3d")) @@ -265,8 +265,8 @@ function DMDACreate3d end points_per_proc_x::Union{Nothing, Vector{$PetscInt}}, points_per_proc_y::Union{Nothing, Vector{$PetscInt}}, points_per_proc_z::Union{Nothing, Vector{$PetscInt}}; - dmsetfromoptions=true, - dmsetup=true, + dmsetfromoptions = true, + dmsetup = true, options..., ) opts = Options($petsclib, options...) @@ -331,8 +331,8 @@ function DMDACreate3d end da, ) end - dmsetfromoptions && DMSetFromOptions!(da) - dmsetup && DMSetUp!(da) + dmsetfromoptions && setfromoptions!(da) + dmsetup && setup!(da) # We can only let the garbage collect finalize when we do not need to # worry about MPI (since garbage collection is asyncronous) if comm == MPI.COMM_SELF @@ -342,16 +342,16 @@ function DMDACreate3d end end """ - DMDAGetInfo(da::AbstractDM) + getinfo(da::DMDA) Get the info associated with the distributed array `da`. # External Links $(_doc_external("DMDA/DMDAGetInfo")) """ -function DMDAGetInfo end +getinfo(::DMDA) -@for_petsc function DMDAGetInfo(da::AbstractDM{$PetscLib}) +@for_petsc function getinfo(da::DMDA{$PetscLib}) dim = [$PetscInt(0)] glo_size = [$PetscInt(0), $PetscInt(0), $PetscInt(0)] procs_per_dim = [$PetscInt(0), $PetscInt(0), $PetscInt(0)] @@ -405,7 +405,7 @@ function DMDAGetInfo end end """ - DMDAGetCorners(da::AbstractDM) + getcorners(da::DMDA) Returns a `NamedTuple` with the global indices (excluding ghost points) of the `lower` and `upper` corners as well as the `size`. @@ -413,9 +413,9 @@ Returns a `NamedTuple` with the global indices (excluding ghost points) of the # External Links $(_doc_external("DMDA/DMDAGetCorners")) """ -function DMDAGetCorners end +function getcorners end -@for_petsc function DMDAGetCorners(da::AbstractDM{$PetscLib}) +@for_petsc function getcorners(da::DMDA{$PetscLib}) info = DMDALocalInfo{$PetscInt}() corners = [$PetscInt(0), $PetscInt(0), $PetscInt(0)] local_size = [$PetscInt(0), $PetscInt(0), $PetscInt(0)] @@ -448,7 +448,7 @@ function DMDAGetCorners end end """ - DMDAGetGhostCorners(da::AbstractDM) + getghostcorners(da::DMDA) Returns a `NamedTuple` with the global indices (including ghost points) of the `lower` and `upper` corners as well as the `size`. @@ -456,9 +456,9 @@ Returns a `NamedTuple` with the global indices (including ghost points) of the # External Links $(_doc_external("DMDA/DMDAGetGhostCorners")) """ -function DMDAGetGhostCorners end +function getghostcorners end -@for_petsc function DMDAGetGhostCorners(da::AbstractDM{$PetscLib}) +@for_petsc function getghostcorners(da::DMDA{$PetscLib}) info = DMDALocalInfo{$PetscInt}() corners = [$PetscInt(0), $PetscInt(0), $PetscInt(0)] local_size = [$PetscInt(0), $PetscInt(0), $PetscInt(0)] @@ -491,8 +491,8 @@ function DMDAGetGhostCorners end end """ - DMDASetUniformCoordinates!( - da::AbstractDM + setuniformcoordinates!( + da::DMDA xyzmin::NTuple{N, Real}, xyzmax::NTuple{N, Real}, ) where {N} @@ -504,10 +504,10 @@ by the `NTuple`s `xyzmin` and `xyzmax`. If `N` is less than the dimension of the # External Links $(_doc_external("DMDA/DMDASetUniformCoordinates")) """ -function DMDASetUniformCoordinates! end +function setuniformcoordinates! end -@for_petsc function DMDASetUniformCoordinates!( - da::AbstractDM{$PetscLib}, +@for_petsc function setuniformcoordinates!( + da::DMDA{$PetscLib}, xyzmin::NTuple{N, Real}, xyzmax::NTuple{N, Real}, ) where {N} diff --git a/src/ksp.jl b/src/ksp.jl index 1798df14..f2a5ba13 100644 --- a/src/ksp.jl +++ b/src/ksp.jl @@ -164,7 +164,7 @@ struct Fn_KSPComputeOperators{T} end return nothing end - function KSPGetDM(ksp::AbstractKSP{$PetscScalar}) + function DMDA(ksp::AbstractKSP{$PetscScalar}) t_dm = Ref{CDM}() @chk ccall( (:KSPGetDM, $libpetsc), @@ -173,7 +173,7 @@ struct Fn_KSPComputeOperators{T} end ksp, t_dm, ) - dm = PetscDM{$PetscLib}(t_dm[]) + dm = DMDA{$PetscLib}(t_dm[]) return dm end diff --git a/test/dmda.jl b/test/dmda.jl index e77d2081..ec610218 100644 --- a/test/dmda.jl +++ b/test/dmda.jl @@ -43,11 +43,11 @@ PETSc.initialize() ) @test PETSc.gettype(da) == "da" - @test PETSc.DMGetDimension(da) == 1 + @test PETSc.getdimension(da) == 1 - da_info = PETSc.DMDAGetInfo(da) - corners = PETSc.DMDAGetCorners(da) - ghost_corners = PETSc.DMDAGetGhostCorners(da) + da_info = PETSc.getinfo(da) + corners = PETSc.getcorners(da) + ghost_corners = PETSc.getghostcorners(da) @test da_info.dim == 1 @test da_info.global_size == [global_size, 1, 1] @@ -83,9 +83,9 @@ PETSc.initialize() da_refine = da_refine, ) @test PETSc.gettype(da) == "da" - @test PETSc.DMGetDimension(da) == 1 + @test PETSc.getdimension(da) == 1 - da_info = PETSc.DMDAGetInfo(da) + da_info = PETSc.getinfo(da) @test da_info.dim == 1 if boundary_type == PETSc.DM_BOUNDARY_PERIODIC @@ -156,9 +156,9 @@ end nothing, ) @test PETSc.gettype(da) == "da" - @test PETSc.DMGetDimension(da) == 2 + @test PETSc.getdimension(da) == 2 - da_info = PETSc.DMDAGetInfo(da) + da_info = PETSc.getinfo(da) @test da_info.global_size == [global_size_x, global_size_y, 1] @test da_info.dim == 2 @@ -187,9 +187,9 @@ end da_refine = da_refine, ) @test PETSc.gettype(da) == "da" - @test PETSc.DMGetDimension(da) == 2 + @test PETSc.getdimension(da) == 2 - da_info = PETSc.DMDAGetInfo(da) + da_info = PETSc.getinfo(da) # Compute refined global size ref_global_size_x = @@ -268,9 +268,9 @@ end nothing, ) @test PETSc.gettype(da) == "da" - @test PETSc.DMGetDimension(da) == 3 + @test PETSc.getdimension(da) == 3 - da_info = PETSc.DMDAGetInfo(da) + da_info = PETSc.getinfo(da) @test da_info.global_size == [global_size_x, global_size_y, global_size_z] @@ -304,9 +304,9 @@ end da_refine = da_refine, ) @test PETSc.gettype(da) == "da" - @test PETSc.DMGetDimension(da) == 3 + @test PETSc.getdimension(da) == 3 - da_info = PETSc.DMDAGetInfo(da) + da_info = PETSc.getinfo(da) # Compute refined global size ref_global_size_x = @@ -340,7 +340,7 @@ end end end -@testset "DMCreateMatrix" begin +@testset "creatematrix" begin comm = MPI.COMM_WORLD mpirank = MPI.Comm_rank(comm) mpisize = MPI.Comm_size(comm) @@ -363,14 +363,14 @@ end stencil_width, points_per_proc, ) - mat = PETSc.DMCreateMatrix(da) + mat = PETSc.creatematrix(da) # Build the 1-D Laplacian FD matrix Sten = PETSc.MatStencil{PetscInt} col = Vector{Sten}(undef, 2) row = Vector{Sten}(undef, 2) val = Vector{PetscScalar}(undef, 4) - corners = PETSc.DMDAGetCorners(da) + corners = PETSc.getcorners(da) for i in corners.lower[1]:min(corners.upper[1], global_size - 1) row[1] = Sten(i = i) @@ -420,18 +420,18 @@ end points_per_proc, ) - corners = PETSc.DMDAGetCorners(da) + corners = PETSc.getcorners(da) # Create the local and global vectors - local_vec = PETSc.DMCreateLocalVector(da) - global_vec = PETSc.DMCreateGlobalVector(da) + local_vec = PETSc.createlocalvector(da) + global_vec = PETSc.createglobalvector(da) # Fill everything with some data fill!(local_vec, mpirank) fill!(global_vec, mpisize) # Add the local values to the global values - PETSc.DMLocalToGlobal!(da, local_vec, PETSc.ADD_VALUES, global_vec) + PETSc.update!(global_vec, local_vec, PETSc.ADD_VALUES) # end points added with neighbor due to ghost of size 1 bot_val = mpisize + mpirank + (mpirank == 0 ? 0 : mpirank - 1) @@ -445,7 +445,7 @@ end end # reset the local values with the global values - PETSc.DMGlobalToLocal!(da, global_vec, PETSc.INSERT_VALUES, local_vec) + PETSc.update!(local_vec, global_vec, PETSc.INSERT_VALUES) # My first value and my ghost should be the bot/top values @test local_vec[1] == bot_val @@ -459,10 +459,11 @@ end end # Test DM Coordinates - coord_da = PETSc.DMGetCoordinateDM(da) + coord_da = PETSc.getcoordinateDM(da) + # Crank it up to 11! xmin, xmax = 0, 11 - PETSc.DMDASetUniformCoordinates!(coord_da, (xmin,), (xmax,)) - coord_vec = PETSc.DMGetCoordinatesLocal(coord_da) + PETSc.setuniformcoordinates!(coord_da, (xmin,), (xmax,)) + coord_vec = PETSc.getcoordinateslocal(coord_da) Δx = (xmax - xmin) / (global_size - 1) # Figure out the values we should have in the coordinate vector