Skip to content

Commit 20e01d9

Browse files
author
Johannes Markert
committed
Applying review comments and adding a test.
1 parent 445f1a2 commit 20e01d9

File tree

3 files changed

+38
-10
lines changed

3 files changed

+38
-10
lines changed

src/T8code.jl

+8-10
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,13 @@ if !@isdefined(T8CODE_OBJECT_TRACKER)
167167
end
168168

169169
"""
170-
T8codeForestWrapper
170+
ForestWrapper
171171
172172
Lightweight `t8_forest_t` pointer wrapper which helps to free
173173
resources allocated by t8code in an orderly fashion.
174174
175175
When initialized with a t8code forest pointer the wrapper
176-
registers itself with a t8code object tracker called `__T8CODE_OBJECT_TRACKER`.
176+
registers itself with a t8code object tracker called `T8CODE_OBJECT_TRACKER`.
177177
178178
In serial mode the wrapper and in consequence the t8code forest
179179
can be finalized immediately whenever Julia's garbage collector sees fit.
@@ -182,22 +182,20 @@ In (MPI) parallel mode the wrapper (and the t8code forest) is kept till the end
182182
of the session or when finalized explicitly. At the end of the session (resp.
183183
when the program shuts down) the object tracker finalizes all registered t8code
184184
objects in sync with all MPI ranks. This is necessary since t8code internally
185-
allocates MPI shared arrays. See `src/auxiliary/t8code.jl` for the finalization
186-
code when Trixi is shutting down.
185+
allocates MPI shared arrays.
187186
"""
188187
mutable struct ForestWrapper
189188
pointer::Ptr{t8_forest} # cpointer to t8code forest
190-
unique_id::UInt
191189

192190
function ForestWrapper(pointer::Ptr{t8_forest})
193191
wrapper = new(pointer)
194192

195-
# Compute the unique id from the T8codeForestWrapper object.
196-
wrapper.unique_id = pointer_from_objref(wrapper)
193+
# Compute a unique id from the ForestWrapper object.
194+
unique_id = UInt64(pointer_from_objref(wrapper))
197195

198196
if MPI.Comm_size(MPI.Comm(t8_forest_get_mpicomm(pointer))) > 1
199197
# Make sure the unique id is identical for each MPI rank.
200-
wrapper.unique_id = MPI.bcast(wrapper.unique_id, MPI.Comm(t8_forest_get_mpicomm(pointer)))
198+
unique_id = MPI.bcast(unique_id, MPI.Comm(t8_forest_get_mpicomm(pointer)))
201199
end
202200

203201
finalizer(wrapper) do wrapper
@@ -211,11 +209,11 @@ mutable struct ForestWrapper
211209
t8_forest_unref(Ref(wrapper.pointer))
212210

213211
# Deregister from the object tracker.
214-
delete!(T8CODE_OBJECT_TRACKER, wrapper.unique_id)
212+
delete!(T8CODE_OBJECT_TRACKER, unique_id)
215213
end
216214

217215
# Register the T8codeForestWrapper with the object tracker.
218-
T8CODE_OBJECT_TRACKER[wrapper.unique_id] = wrapper
216+
T8CODE_OBJECT_TRACKER[unique_id] = wrapper
219217
end
220218
end
221219

test/test_all.jl

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ end
2323
end
2424
end
2525

26+
@testset "forestwrapper" begin
27+
include("test_forestwrapper.jl")
28+
end
29+
2630
@testset "cmesh" begin
2731
include("cmesh/test_readmshfile.jl")
2832
end

test/test_forestwrapper.jl

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
@testset "test forestwrapper" begin
2+
3+
# Clean up t8code before MPI shuts down.
4+
MPI.add_finalize_hook!() do
5+
T8code.clean_up()
6+
T8code.Libt8.sc_finalize()
7+
end
8+
9+
# Create a forest and wrap by `ForestWrapper`
10+
scheme = t8_scheme_new_default_cxx()
11+
cmesh = t8_cmesh_new_hypercube(T8_ECLASS_QUAD, comm, 0, 0, 0)
12+
forest = t8_forest_new_uniform(cmesh, scheme, 0, 0, comm)
13+
wrapper_A = T8code.ForestWrapper(forest)
14+
15+
# Create another forest and wrap by `ForestWrapper`
16+
scheme = t8_scheme_new_default_cxx()
17+
cmesh = t8_cmesh_new_hypercube(T8_ECLASS_TRIANGLE, comm, 0, 0, 0)
18+
forest = t8_forest_new_uniform(cmesh, scheme, 0, 0, comm)
19+
wrapper_B = T8code.ForestWrapper(forest)
20+
21+
# Finalize the first wrapper.
22+
finalize(wrapper_A)
23+
24+
# The second wrapper should be finalized automatically when Julia shuts down.
25+
# ... finalize(wrapper_B) ...
26+
end

0 commit comments

Comments
 (0)