Skip to content

Commit bede31c

Browse files
Merge pull request #70 from gridap/adding_weights_to_partition
Adding the possibility of prescribing weights before redistributing
2 parents 743bd10 + 0dc4ddb commit bede31c

File tree

3 files changed

+81
-11
lines changed

3 files changed

+81
-11
lines changed

src/OctreeDistributedDiscreteModels.jl

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1599,9 +1599,23 @@ end
15991599
# Assumptions. Either:
16001600
# A) model.parts MPI tasks are included in parts_redistributed_model MPI tasks; or
16011601
# B) model.parts MPI tasks include parts_redistributed_model MPI tasks
1602+
const WeightsArrayType=Union{Nothing,MPIArray{<:Vector{<:Integer}}}
16021603
function GridapDistributed.redistribute(model::OctreeDistributedDiscreteModel{Dc,Dp},
1603-
parts_redistributed_model=model.parts) where {Dc,Dp}
1604+
parts_redistributed_model=model.parts;
1605+
weights::WeightsArrayType=nothing) where {Dc,Dp}
16041606
parts = (parts_redistributed_model === model.parts) ? model.parts : parts_redistributed_model
1607+
_weights=nothing
1608+
if (weights !== nothing)
1609+
Gridap.Helpers.@notimplementedif parts!==model.parts
1610+
_weights=map(model.dmodel.models,weights) do lmodel,weights
1611+
# The length of the local weights array has to match the number of
1612+
# cells in the model. This includes both owned and ghost cells.
1613+
# Only the flags for owned cells are actually taken into account.
1614+
@assert num_cells(lmodel)==length(weights)
1615+
convert(Vector{Cint},weights)
1616+
end
1617+
end
1618+
16051619
comm = parts.comm
16061620
if (GridapDistributed.i_am_in(model.parts.comm) || GridapDistributed.i_am_in(parts.comm))
16071621
if (parts_redistributed_model !== model.parts)
@@ -1610,7 +1624,7 @@ function GridapDistributed.redistribute(model::OctreeDistributedDiscreteModel{Dc
16101624
@assert A || B
16111625
end
16121626
if (parts_redistributed_model===model.parts || A)
1613-
_redistribute_parts_subseteq_parts_redistributed(model,parts_redistributed_model)
1627+
_redistribute_parts_subseteq_parts_redistributed(model,parts_redistributed_model,_weights)
16141628
else
16151629
_redistribute_parts_supset_parts_redistributed(model, parts_redistributed_model)
16161630
end
@@ -1619,7 +1633,9 @@ function GridapDistributed.redistribute(model::OctreeDistributedDiscreteModel{Dc
16191633
end
16201634
end
16211635

1622-
function _redistribute_parts_subseteq_parts_redistributed(model::OctreeDistributedDiscreteModel{Dc,Dp}, parts_redistributed_model) where {Dc,Dp}
1636+
function _redistribute_parts_subseteq_parts_redistributed(model::OctreeDistributedDiscreteModel{Dc,Dp},
1637+
parts_redistributed_model,
1638+
_weights::WeightsArrayType) where {Dc,Dp}
16231639
parts = (parts_redistributed_model === model.parts) ? model.parts : parts_redistributed_model
16241640
if (parts_redistributed_model === model.parts)
16251641
ptr_pXest_old = model.ptr_pXest
@@ -1631,7 +1647,15 @@ function _redistribute_parts_subseteq_parts_redistributed(model::OctreeDistribut
16311647
parts.comm)
16321648
end
16331649
ptr_pXest_new = pXest_copy(model.pXest_type, ptr_pXest_old)
1634-
pXest_partition!(model.pXest_type, ptr_pXest_new)
1650+
if (_weights !== nothing)
1651+
init_fn_callback_c = pXest_reset_callbacks(model.pXest_type)
1652+
map(_weights) do _weights
1653+
pXest_reset_data!(model.pXest_type, ptr_pXest_new, Cint(sizeof(Cint)), init_fn_callback_c, pointer(_weights))
1654+
end
1655+
pXest_partition!(model.pXest_type, ptr_pXest_new; weights_set=true)
1656+
else
1657+
pXest_partition!(model.pXest_type, ptr_pXest_new; weights_set=false)
1658+
end
16351659

16361660
# Compute RedistributeGlue
16371661
parts_snd, lids_snd, old2new = pXest_compute_migration_control_data(model.pXest_type,ptr_pXest_old,ptr_pXest_new)

src/PXestTypeMethods.jl

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -309,16 +309,31 @@ function pXest_balance!(::P8estType, ptr_pXest; k_2_1_balance=0)
309309
end
310310
end
311311

312-
function pXest_partition!(::P4estType, ptr_pXest)
313-
p4est_partition(ptr_pXest, 0, C_NULL)
312+
function pXest_partition!(pXest_type::P4estType, ptr_pXest; weights_set=false)
313+
if (!weights_set)
314+
p4est_partition(ptr_pXest, 0, C_NULL)
315+
else
316+
wcallback=pXest_weight_callback(pXest_type)
317+
p4est_partition(ptr_pXest, 0, wcallback)
318+
end
314319
end
315320

316-
function pXest_partition!(::P6estType, ptr_pXest)
317-
p6est_partition(ptr_pXest, C_NULL)
321+
function pXest_partition!(pXest_type::P6estType, ptr_pXest; weights_set=false)
322+
if (!weights_set)
323+
p6est_partition(ptr_pXest, C_NULL)
324+
else
325+
wcallback=pXest_weight_callback(pXest_type)
326+
p6est_partition(ptr_pXest, wcallback)
327+
end
318328
end
319329

320-
function pXest_partition!(::P8estType, ptr_pXest)
321-
p8est_partition(ptr_pXest, 0, C_NULL)
330+
function pXest_partition!(pXest_type::P8estType, ptr_pXest; weights_set=false)
331+
if (!weights_set)
332+
p8est_partition(ptr_pXest, 0, C_NULL)
333+
else
334+
wcallback=pXest_weight_callback(pXest_type)
335+
p8est_partition(ptr_pXest, 0, wcallback)
336+
end
322337
end
323338

324339

@@ -805,6 +820,30 @@ function pXest_refine_callbacks(::P8estType)
805820
refine_callback_c, refine_replace_callback_c
806821
end
807822

823+
function pXest_weight_callback(::P4estType)
824+
function weight_callback(::Ptr{p4est_t},
825+
which_tree::p4est_topidx_t,
826+
quadrant_ptr::Ptr{p4est_quadrant_t})
827+
quadrant = quadrant_ptr[]
828+
return unsafe_wrap(Array, Ptr{Cint}(quadrant.p.user_data), 1)[]
829+
end
830+
@cfunction($weight_callback, Cint, (Ptr{p4est_t}, p4est_topidx_t, Ptr{p4est_quadrant_t}))
831+
end
832+
833+
function pXest_weight_callback(::P6estType)
834+
Gridap.Helpers.@notimplemented
835+
end
836+
837+
function pXest_weight_callback(::P8estType)
838+
function weight_callback(::Ptr{p8est_t},
839+
which_tree::p4est_topidx_t,
840+
quadrant_ptr::Ptr{p8est_quadrant_t})
841+
quadrant = quadrant_ptr[]
842+
return unsafe_wrap(Array, Ptr{Cint}(quadrant.p.user_data), 1)[]
843+
end
844+
@cfunction($weight_callback, Cint, (Ptr{p8est_t}, p4est_topidx_t, Ptr{p8est_quadrant_t}))
845+
end
846+
808847
function _unwrap_ghost_quadrants(::P4estType, pXest_ghost)
809848
Ptr{p4est_quadrant_t}(pXest_ghost.ghosts.array)
810849
end

test/PoissonNonConformingOctreeModelsTests.jl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,14 @@ module PoissonNonConformingOctreeModelsTests
139139
e = uH - uhH
140140
el2 = sqrt(sum( ( ee )*dΩH ))
141141

142-
fmodel_red, red_glue=GridapDistributed.redistribute(fmodel);
142+
weights=map(ranks,fmodel.dmodel.models) do rank,lmodel
143+
if (rank%2==0)
144+
zeros(Cint,num_cells(lmodel))
145+
else
146+
ones(Cint,num_cells(lmodel))
147+
end
148+
end
149+
fmodel_red, red_glue=GridapDistributed.redistribute(fmodel,weights=weights);
143150
Vhred=FESpace(fmodel_red,reffe,conformity=:H1;dirichlet_tags="boundary")
144151
Uhred=TrialFESpace(Vhred,u)
145152

0 commit comments

Comments
 (0)