Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format #111

Merged
merged 5 commits into from
Jan 31, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
really add .JuliaFormatter.toml
JoshuaLampert committed Jan 26, 2024
commit 5ff7bec39f0671dfb8fa15b2bc3113a9be8d043c
9 changes: 8 additions & 1 deletion .JuliaFormatter.toml
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@

# Use SciML style: https://github.com/SciML/SciMLStyle
style = "sciml"

# Python style alignment. See https://github.com/domluna/JuliaFormatter.jl/pull/732.
yas_style_nesting = true

# Align struct fields for better readability of large struct definitions
align_struct_field = true
8 changes: 2 additions & 6 deletions src/P4est.jl
Original file line number Diff line number Diff line change
@@ -11,7 +11,6 @@ using UUIDs: UUID
const _PREFERENCE_LIBP4EST = @load_preference("libp4est", "P4est_jll")
const _PREFERENCE_LIBSC = @load_preference("libsc", _PREFERENCE_LIBP4EST)


# Include p4est bindings
include("LibP4est.jl")
@reexport using .LibP4est
@@ -20,7 +19,6 @@ include("LibP4est.jl")
include("pointerwrappers.jl")
@reexport using .PointerWrappers: PointerWrapper


# Higher-level API defined in P4est.jl
"""
P4est.uses_mpi()
@@ -109,8 +107,8 @@ path_sc_library() = _PREFERENCE_LIBSC
Returns `false` if a system-provided MPI installation is set via the MPIPreferences, but
not a system-provided `p4est` installation. In this case, P4est.jl is not usable.
"""
preferences_set_correctly() =
!(_PREFERENCE_LIBP4EST == "P4est_jll" && MPIPreferences.binary == "system")
preferences_set_correctly() = !(_PREFERENCE_LIBP4EST == "P4est_jll" &&
MPIPreferences.binary == "system")

"""
P4est.init(log_handler, log_threshold)
@@ -135,7 +133,6 @@ function init(log_handler, log_threshold)
return nothing
end


function __init__()
# If a system-provided MPI installation with default p4est version is used, we cannot execute `P4est.version()`
# because the p4est functions are not available
@@ -152,5 +149,4 @@ function __init__()
return nothing
end


end
30 changes: 16 additions & 14 deletions src/pointerwrappers.jl
Original file line number Diff line number Diff line change
@@ -44,14 +44,16 @@ end
PointerWrapper(::Type{T}, pointer) where {T} = PointerWrapper{T}(pointer)

# Pointer-type fields get dereferenced such that PointerWrapper wraps the pointer to the field type
PointerWrapper(::Type{Ptr{T}}, pointer) where {T} =
function PointerWrapper(::Type{Ptr{T}}, pointer) where {T}
PointerWrapper{T}(unsafe_load(Ptr{Ptr{T}}(pointer)))
end

# Cannot use `pw.pointer` since we implement `getproperty` to return the fields of `T` itself
Base.pointer(pw::PointerWrapper{T}) where {T} = getfield(pw, :pointer)
# Allow passing a `PointerWrapper` to wrapped C functions
Base.unsafe_convert(::Type{Ptr{T}}, pw::PointerWrapper{T}) where {T} =
function Base.unsafe_convert(::Type{Ptr{T}}, pw::PointerWrapper{T}) where {T}
Base.unsafe_convert(Ptr{T}, pointer(pw))
end

# Syntactic sugar
Base.propertynames(::PointerWrapper{T}) where {T} = fieldnames(T)
@@ -76,10 +78,8 @@ function Base.setproperty!(pw::PointerWrapper{T}, name::Symbol, v) where {T}
# see https://github.com/trixi-framework/P4est.jl/issues/72 and https://github.com/trixi-framework/P4est.jl/issues/79
return Base.setproperty!(pointer(pw), name, v)
end
return unsafe_store!(
reinterpret(Ptr{fieldtype(T, i)}, pointer(pw) + fieldoffset(T, i)),
v,
)
return unsafe_store!(reinterpret(Ptr{fieldtype(T, i)}, pointer(pw) + fieldoffset(T, i)),
v)
end

# `[]` allows one to access the actual underlying data and
@@ -91,19 +91,21 @@ Base.setindex!(pw::PointerWrapper, value, i::Integer = 1) = unsafe_store!(pw, va
Base.unsafe_load(pw::PointerWrapper, i::Integer = 1) = unsafe_load(pointer(pw), i)

# When `unsafe_wrap`ping a PointerWrapper object, we really want to wrap the underlying array
Base.unsafe_wrap(
AType::Union{Type{Array},Type{Array{T}},Type{Array{T,N}}},
pw::PointerWrapper,
dims::Union{NTuple{N,Int},Integer};
own::Bool = false,
) where {T,N} = unsafe_wrap(AType, pointer(pw), dims; own)
function Base.unsafe_wrap(AType::Union{Type{Array}, Type{Array{T}}, Type{Array{T, N}}},
pw::PointerWrapper,
dims::Union{NTuple{N, Int}, Integer};
own::Bool = false,) where {T, N}
unsafe_wrap(AType, pointer(pw), dims; own)
end

# If value is of the wrong type, try to convert it
Base.unsafe_store!(pw::PointerWrapper{T}, value, i::Integer = 1) where {T} =
function Base.unsafe_store!(pw::PointerWrapper{T}, value, i::Integer = 1) where {T}
unsafe_store!(pw, convert(T, value), i)
end

# Store value to wrapped location
Base.unsafe_store!(pw::PointerWrapper{T}, value::T, i::Integer = 1) where {T} =
function Base.unsafe_store!(pw::PointerWrapper{T}, value::T, i::Integer = 1) where {T}
unsafe_store!(pointer(pw), value, i)
end

end
16 changes: 6 additions & 10 deletions test/configure_packages.jl
Original file line number Diff line number Diff line change
@@ -26,16 +26,12 @@ end
# Finally, we configure P4est.jl as desired.
@static if JULIA_P4EST_TEST == "P4EST_CUSTOM_MPI_CUSTOM"
import UUIDs, Preferences
Preferences.set_preferences!(
UUIDs.UUID("7d669430-f675-4ae7-b43e-fab78ec5a902"), # UUID of P4est.jl
"libp4est" => JULIA_P4EST_TEST_LIBP4EST,
force = true,
)
Preferences.set_preferences!(
UUIDs.UUID("7d669430-f675-4ae7-b43e-fab78ec5a902"), # UUID of P4est.jl
"libsc" => JULIA_P4EST_TEST_LIBSC,
force = true,
)
Preferences.set_preferences!(UUIDs.UUID("7d669430-f675-4ae7-b43e-fab78ec5a902"), # UUID of P4est.jl
"libp4est" => JULIA_P4EST_TEST_LIBP4EST,
force = true)
Preferences.set_preferences!(UUIDs.UUID("7d669430-f675-4ae7-b43e-fab78ec5a902"), # UUID of P4est.jl
"libsc" => JULIA_P4EST_TEST_LIBSC,
force = true)
end

@info "P4est.jl tests configured" JULIA_P4EST_TEST JULIA_P4EST_TEST_LIBP4EST JULIA_P4EST_TEST_LIBSC
5 changes: 1 addition & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -10,7 +10,6 @@ using P4est
import MPIPreferences
@info "Testing P4est.jl with" MPIPreferences.binary MPIPreferences.abi


@time @testset "P4est.jl tests" begin
# For some weird reason, the MPI tests must come first since they fail
# otherwise with a custom MPI installation.
@@ -23,9 +22,7 @@ import MPIPreferences
@info "Starting parallel tests"

mpiexec() do cmd
run(
`$cmd -n 2 $(Base.julia_cmd()) --threads=1 --check-bounds=yes --project=$(dirname(@__DIR__)) $(abspath("tests_basic.jl"))`,
)
run(`$cmd -n 2 $(Base.julia_cmd()) --threads=1 --check-bounds=yes --project=$(dirname(@__DIR__)) $(abspath("tests_basic.jl"))`)
end

@info "Finished parallel tests"
37 changes: 15 additions & 22 deletions test/test_nested_attributes_2d.jl
Original file line number Diff line number Diff line change
@@ -15,7 +15,6 @@ function unsafe_load_side(info::Ptr{p4est_iter_face_info_t}, i = 1)
return unsafe_load_sc(p4est_iter_face_side_t, unsafe_load(info).sides, i)
end


function refine_fn(p4est, which_tree, quadrant)
quadrant_obj = unsafe_load(quadrant)
if quadrant_obj.x == 0 && quadrant_obj.y == 0 && quadrant_obj.level < 4
@@ -46,11 +45,9 @@ function iter_face(info::Ptr{p4est_iter_face_info_t}, user_data)
# test nested attributes
@test sides[local_side].treeid isa Integer
@test sides[local_side].is.full.quadid isa Integer
@test unsafe_wrap(
Array,
unsafe_load(unsafe_load(info).ghost_layer).proc_offsets,
MPI.Comm_size(MPI.COMM_WORLD) + 1,
) isa Vector{Int32}
@test unsafe_wrap(Array,
unsafe_load(unsafe_load(info).ghost_layer).proc_offsets,
MPI.Comm_size(MPI.COMM_WORLD) + 1) isa Vector{Int32}
@test sides[remote_side].is.full.quadid isa Integer
if local_side == 2
@test unsafe_load(sides[2].is.full.quad.p.piggy3.local_num) isa Integer
@@ -66,7 +63,7 @@ function iter_face(info::Ptr{p4est_iter_face_info_t}, user_data)
@test sides[hanging_side].is_hanging == true &&
sides[full_side].is_hanging == false
@test sides[full_side].is.full.is_ghost isa Integer
@test sides[hanging_side].is.hanging.is_ghost isa Tuple{Int8,Int8}
@test sides[hanging_side].is.hanging.is_ghost isa Tuple{Int8, Int8}
if sides[full_side].is.full.is_ghost == false &&
all(sides[hanging_side].is.hanging.is_ghost .== false)
return nothing
@@ -80,21 +77,17 @@ end
# See https://github.com/trixi-framework/Trixi.jl/blob/main/src/solvers/dgsem_p4est/dg_parallel.jl
@testset "nested attributes" begin
connectivity = @test_nowarn p4est_connectivity_new_brick(2, 2, 0, 0)
p4est = @test_nowarn p4est_new_ext(
MPI.COMM_WORLD,
connectivity,
0,
0,
true,
0,
C_NULL,
C_NULL,
)
refine_fn_c = @cfunction(
refine_fn,
Cint,
(Ptr{p4est_t}, Ptr{p4est_topidx_t}, Ptr{p4est_quadrant_t})
)
p4est = @test_nowarn p4est_new_ext(MPI.COMM_WORLD,
connectivity,
0,
0,
true,
0,
C_NULL,
C_NULL)
refine_fn_c = @cfunction(refine_fn,
Cint,
(Ptr{p4est_t}, Ptr{p4est_topidx_t}, Ptr{p4est_quadrant_t}))
p4est_refine(p4est, true, refine_fn_c, C_NULL)
p4est_balance(p4est, P4EST_CONNECT_FACE, C_NULL)

56 changes: 24 additions & 32 deletions test/test_nested_attributes_3d.jl
Original file line number Diff line number Diff line change
@@ -45,11 +45,9 @@ function iter_face(info::Ptr{p8est_iter_face_info_t}, user_data)
# test nested attributes
@test sides[local_side].treeid isa Integer
@test sides[local_side].is.full.quadid isa Integer
@test unsafe_wrap(
Array,
unsafe_load(unsafe_load(info).ghost_layer).proc_offsets,
MPI.Comm_size(MPI.COMM_WORLD) + 1,
) isa Vector{Int32}
@test unsafe_wrap(Array,
unsafe_load(unsafe_load(info).ghost_layer).proc_offsets,
MPI.Comm_size(MPI.COMM_WORLD) + 1) isa Vector{Int32}
@test sides[remote_side].is.full.quadid isa Integer
if local_side == 2
@test unsafe_load(sides[2].is.full.quad.p.piggy3.local_num) isa Integer
@@ -65,7 +63,7 @@ function iter_face(info::Ptr{p8est_iter_face_info_t}, user_data)
@test sides[hanging_side].is_hanging == true &&
sides[full_side].is_hanging == false
@test sides[full_side].is.full.is_ghost isa Integer
@test sides[hanging_side].is.hanging.is_ghost isa NTuple{4,Int8}
@test sides[hanging_side].is.hanging.is_ghost isa NTuple{4, Int8}
if sides[full_side].is.full.is_ghost == false &&
all(sides[hanging_side].is.hanging.is_ghost .== false)
return nothing
@@ -79,36 +77,30 @@ end
# See https://github.com/trixi-framework/Trixi.jl/blob/main/src/solvers/dgsem_p4est/dg_parallel.jl
@testset "nested attributes" begin
connectivity = @test_nowarn p8est_connectivity_new_brick(2, 2, 2, 0, 0, 0)
p4est = @test_nowarn p8est_new_ext(
MPI.COMM_WORLD,
connectivity,
0,
0,
true,
0,
C_NULL,
C_NULL,
)
p4est = @test_nowarn p8est_new_ext(MPI.COMM_WORLD,
connectivity,
0,
0,
true,
0,
C_NULL,
C_NULL)

refine_fn_c = @cfunction(
refine_fn,
Cint,
(Ptr{p8est_t}, Ptr{p4est_topidx_t}, Ptr{p8est_quadrant_t})
)
refine_fn_c = @cfunction(refine_fn,
Cint,
(Ptr{p8est_t}, Ptr{p4est_topidx_t}, Ptr{p8est_quadrant_t}))
p8est_refine(p4est, true, refine_fn_c, C_NULL)
p8est_balance(p4est, P8EST_CONNECT_FACE, C_NULL)

iter_face_nested_attributes_c =
@cfunction(iter_face, Cvoid, (Ptr{p8est_iter_face_info_t}, Ptr{Cvoid}))
p8est_iterate(
p4est,
C_NULL,
C_NULL,
C_NULL,
iter_face_nested_attributes_c,
C_NULL,
C_NULL,
)
iter_face_nested_attributes_c = @cfunction(iter_face, Cvoid,
(Ptr{p8est_iter_face_info_t}, Ptr{Cvoid}))
p8est_iterate(p4est,
C_NULL,
C_NULL,
C_NULL,
iter_face_nested_attributes_c,
C_NULL,
C_NULL)
@test_nowarn p8est_destroy(p4est)
@test_nowarn p8est_connectivity_destroy(connectivity)
end
33 changes: 14 additions & 19 deletions test/test_p4est_balance.jl
Original file line number Diff line number Diff line change
@@ -10,39 +10,34 @@ end
# This test is inspired by test/test_balance_type2.c from `p4est` for 2D
@testset "p4est_balance" begin
connectivity = @test_nowarn p4est_connectivity_new_star()
p4est =
@test_nowarn p4est_new_ext(MPI.COMM_WORLD, connectivity, 0, 0, 0, 0, C_NULL, C_NULL)
p4est = @test_nowarn p4est_new_ext(MPI.COMM_WORLD, connectivity, 0, 0, 0, 0, C_NULL,
C_NULL)

refine_fn_balance_c = @cfunction(
refine_fn_balance,
Cint,
(Ptr{p4est_t}, Ptr{p4est_topidx_t}, Ptr{p4est_quadrant_t})
)
refine_fn_balance_c = @cfunction(refine_fn_balance,
Cint,
(Ptr{p4est_t}, Ptr{p4est_topidx_t},
Ptr{p4est_quadrant_t}))
@test_nowarn p4est_refine(p4est, 1, refine_fn_balance_c, C_NULL)
# face balance
p4estF = @test_nowarn p4est_copy(p4est, 0)
@test_nowarn p4est_balance(p4estF, P4EST_CONNECT_FACE, C_NULL)
crcF = @test_nowarn p4est_checksum(p4estF)
@test unsafe_load(p4estF).global_num_quadrants == 6
println(
"Face balance with ",
unsafe_load(p4estF).global_num_quadrants,
" quadrants and crc ",
crcF,
)
println("Face balance with ",
unsafe_load(p4estF).global_num_quadrants,
" quadrants and crc ",
crcF)
# corner balance
p4estC = @test_nowarn p4est_copy(p4est, 1)
@test_nowarn p4est_balance(p4estF, P4EST_CONNECT_CORNER, C_NULL)
@test_nowarn p4est_balance(p4estC, P4EST_CONNECT_CORNER, C_NULL)
crcC = @test_nowarn p4est_checksum(p4estC)
@test crcC == p4est_checksum(p4estF)
@test unsafe_load(p4estC).global_num_quadrants == 6
println(
"Corner balance with ",
unsafe_load(p4estC).global_num_quadrants,
" quadrants and crc ",
crcC,
)
println("Corner balance with ",
unsafe_load(p4estC).global_num_quadrants,
" quadrants and crc ",
crcC)

@test_nowarn p4est_destroy(p4est)
@test_nowarn p4est_destroy(p4estF)
Loading
Loading