Skip to content

Commit 1a5aff7

Browse files
committed
Add MGTwoLevelTransferCopyToHost, a two level transfer class which permits transfer on device vectors via use of an internal MGTwoLevelTransfer class defined on the host.
1 parent d14fbf0 commit 1a5aff7

12 files changed

+1821
-74
lines changed

include/deal.II/multigrid/mg_transfer_matrix_free.h

Lines changed: 320 additions & 27 deletions
Large diffs are not rendered by default.

include/deal.II/multigrid/mg_transfer_matrix_free.templates.h

Lines changed: 71 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2746,15 +2746,15 @@ namespace internal
27462746

27472747

27482748
template <typename VectorType>
2749-
MGTwoLevelTransferBase<VectorType>::MGTwoLevelTransferBase()
2749+
MGTwoLevelTransferCore<VectorType>::MGTwoLevelTransferCore()
27502750
: vec_fine_needs_ghost_update(true)
27512751
{}
27522752

27532753

27542754

27552755
template <typename VectorType>
27562756
void
2757-
MGTwoLevelTransferBase<VectorType>::prolongate_and_add(
2757+
MGTwoLevelTransferCore<VectorType>::prolongate_and_add(
27582758
VectorType &dst,
27592759
const VectorType &src) const
27602760
{
@@ -2793,7 +2793,6 @@ MGTwoLevelTransferBase<VectorType>::prolongate_and_add(
27932793
}
27942794

27952795

2796-
27972796
namespace internal
27982797
{
27992798
// Helper class to compute correct weights, which works by simply using
@@ -3038,7 +3037,7 @@ MGTwoLevelTransfer<dim, VectorType>::prolongate_and_add_internal(
30383037

30393038
template <typename VectorType>
30403039
void
3041-
MGTwoLevelTransferBase<VectorType>::restrict_and_add(
3040+
MGTwoLevelTransferCore<VectorType>::restrict_and_add(
30423041
VectorType &dst,
30433042
const VectorType &src) const
30443043
{
@@ -3524,7 +3523,7 @@ namespace internal
35243523
template <typename VectorType>
35253524
template <int dim, std::size_t width, typename IndexType>
35263525
std::pair<bool, bool>
3527-
MGTwoLevelTransferBase<VectorType>::
3526+
MGTwoLevelTransferCore<VectorType>::
35283527
internal_enable_inplace_operations_if_possible(
35293528
const std::shared_ptr<const Utilities::MPI::Partitioner>
35303529
&external_partitioner_coarse,
@@ -3949,7 +3948,7 @@ MGTwoLevelTransfer<dim, VectorType>::memory_consumption() const
39493948

39503949
template <typename VectorType>
39513950
void
3952-
MGTwoLevelTransferBase<VectorType>::update_ghost_values(
3951+
MGTwoLevelTransferCore<VectorType>::update_ghost_values(
39533952
const VectorType &vec) const
39543953
{
39553954
if ((vec.get_partitioner().get() == this->partitioner_coarse.get()) &&
@@ -3970,7 +3969,7 @@ MGTwoLevelTransferBase<VectorType>::update_ghost_values(
39703969

39713970
template <typename VectorType>
39723971
void
3973-
MGTwoLevelTransferBase<VectorType>::compress(
3972+
MGTwoLevelTransferCore<VectorType>::compress(
39743973
VectorType &vec,
39753974
const VectorOperation::values op) const
39763975
{
@@ -3994,7 +3993,7 @@ MGTwoLevelTransferBase<VectorType>::compress(
39943993

39953994
template <typename VectorType>
39963995
void
3997-
MGTwoLevelTransferBase<VectorType>::zero_out_ghost_values(
3996+
MGTwoLevelTransferCore<VectorType>::zero_out_ghost_values(
39983997
const VectorType &vec) const
39993998
{
40003999
if ((vec.get_partitioner().get() == this->partitioner_coarse.get()) &&
@@ -4091,6 +4090,7 @@ MGTransferMatrixFree<dim, Number, MemorySpace>::get_dof_handler_fine() const
40914090
return {t->dof_handler_fine, t->mg_level_fine};
40924091
}
40934092

4093+
40944094
if constexpr (std::is_same_v<MemorySpace, ::dealii::MemorySpace::Host>)
40954095
{
40964096
// MGTwoLevelTransferNonNested transfer is only instantiated for Host
@@ -4104,6 +4104,19 @@ MGTransferMatrixFree<dim, Number, MemorySpace>::get_dof_handler_fine() const
41044104
}
41054105
}
41064106

4107+
if constexpr (std::is_same_v<MemorySpace, ::dealii::MemorySpace::Default>)
4108+
{
4109+
// MGTwoLevelTransferCopyToHost should only be instantiated on device
4110+
// memory:
4111+
if (const auto t = dynamic_cast<const MGTwoLevelTransferCopyToHost<
4112+
dim,
4113+
LinearAlgebra::distributed::Vector<Number, MemorySpace>> *>(
4114+
this->transfer[this->transfer.max_level()].get()))
4115+
{
4116+
return {(t->host_transfer).dof_handler_fine,
4117+
(t->host_transfer).mg_level_fine};
4118+
}
4119+
}
41074120
{
41084121
DEAL_II_NOT_IMPLEMENTED();
41094122
return {nullptr, numbers::invalid_unsigned_int};
@@ -4297,22 +4310,35 @@ MGTransferMatrixFree<dim, Number, MemorySpace>::build(
42974310
const std::vector<std::shared_ptr<const Utilities::MPI::Partitioner>>
42984311
&external_partitioners)
42994312
{
4300-
const bool use_local_smoothing =
4301-
this->transfer.n_levels() == 0 || this->internal_transfer.n_levels() > 0;
4302-
4303-
if (use_local_smoothing)
4313+
// Local smoothing is not supported on the device
4314+
if constexpr (std::is_same_v<MemorySpace, dealii::MemorySpace::Host>)
43044315
{
4305-
this->initialize_internal_transfer(dof_handler,
4306-
this->mg_constrained_dofs);
4307-
this->initialize_transfer_references(internal_transfer);
4308-
}
4316+
bool use_local_smoothing = this->transfer.n_levels() == 0 ||
4317+
this->internal_transfer.n_levels() > 0;
43094318

4310-
this->build(external_partitioners);
4319+
if (use_local_smoothing)
4320+
{
4321+
this->initialize_internal_transfer(dof_handler,
4322+
this->mg_constrained_dofs);
4323+
this->initialize_transfer_references(internal_transfer);
4324+
}
43114325

4312-
if (use_local_smoothing)
4313-
this->fill_and_communicate_copy_indices(dof_handler);
4326+
this->build(external_partitioners);
4327+
4328+
{
4329+
if (use_local_smoothing)
4330+
this->fill_and_communicate_copy_indices(dof_handler);
4331+
else
4332+
this->fill_and_communicate_copy_indices_global_coarsening(
4333+
dof_handler);
4334+
}
4335+
}
43144336
else
4315-
this->fill_and_communicate_copy_indices_global_coarsening(dof_handler);
4337+
{
4338+
this->build(external_partitioners);
4339+
4340+
this->fill_and_communicate_copy_indices_global_coarsening(dof_handler);
4341+
}
43164342
}
43174343

43184344

@@ -4324,22 +4350,35 @@ MGTransferMatrixFree<dim, Number, MemorySpace>::build(
43244350
const std::function<void(const unsigned int, VectorType &)>
43254351
&initialize_dof_vector)
43264352
{
4327-
const bool use_local_smoothing =
4328-
this->transfer.n_levels() == 0 || this->internal_transfer.n_levels() > 0;
4329-
4330-
if (use_local_smoothing)
4353+
// Local smoothing is not supported on the device
4354+
if constexpr (std::is_same_v<MemorySpace, dealii::MemorySpace::Host>)
43314355
{
4332-
this->initialize_internal_transfer(dof_handler,
4333-
this->mg_constrained_dofs);
4334-
this->initialize_transfer_references(internal_transfer);
4335-
}
4356+
bool use_local_smoothing = this->transfer.n_levels() == 0 ||
4357+
this->internal_transfer.n_levels() > 0;
4358+
4359+
if (use_local_smoothing)
4360+
{
4361+
this->initialize_internal_transfer(dof_handler,
4362+
this->mg_constrained_dofs);
4363+
this->initialize_transfer_references(internal_transfer);
4364+
}
43364365

4337-
this->build(initialize_dof_vector);
4366+
this->build(initialize_dof_vector);
43384367

4339-
if (use_local_smoothing)
4340-
this->fill_and_communicate_copy_indices(dof_handler);
4368+
{
4369+
if (use_local_smoothing)
4370+
this->fill_and_communicate_copy_indices(dof_handler);
4371+
else
4372+
this->fill_and_communicate_copy_indices_global_coarsening(
4373+
dof_handler);
4374+
}
4375+
}
43414376
else
4342-
this->fill_and_communicate_copy_indices_global_coarsening(dof_handler);
4377+
{
4378+
this->build(initialize_dof_vector);
4379+
4380+
this->fill_and_communicate_copy_indices_global_coarsening(dof_handler);
4381+
}
43434382
}
43444383

43454384

source/multigrid/mg_base.inst.in

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,9 @@ for (VEC : VECTOR_TYPES)
2020
template class MGSmootherBase<VEC>;
2121
template class MGCoarseGridBase<VEC>;
2222
}
23+
24+
for (S1 : REAL_SCALARS)
25+
{
26+
template class MGTransferBase<
27+
LinearAlgebra::distributed::Vector<S1, MemorySpace::Default>>;
28+
}

source/multigrid/mg_level_global_transfer.cc

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ MGLevelGlobalTransfer<VectorType>::memory_consumption() const
157157

158158
namespace
159159
{
160-
template <int dim, int spacedim, typename Number>
160+
template <int dim, int spacedim, typename Number, typename MemorySpace>
161161
void
162162
fill_internal(
163163
const DoFHandler<dim, spacedim> &mg_dof,
@@ -167,8 +167,9 @@ namespace
167167
std::vector<Table<2, unsigned int>> &copy_indices,
168168
std::vector<Table<2, unsigned int>> &copy_indices_global_mine,
169169
std::vector<Table<2, unsigned int>> &copy_indices_level_mine,
170-
LinearAlgebra::distributed::Vector<Number> &ghosted_global_vector,
171-
MGLevelObject<LinearAlgebra::distributed::Vector<Number>>
170+
LinearAlgebra::distributed::Vector<Number, MemorySpace>
171+
&ghosted_global_vector,
172+
MGLevelObject<LinearAlgebra::distributed::Vector<Number, MemorySpace>>
172173
&ghosted_level_vector)
173174
{
174175
// first go to the usual routine...
@@ -312,7 +313,7 @@ MGLevelGlobalTransfer<LinearAlgebra::distributed::Vector<Number, MemorySpace>>::
312313
{
313314
// note: variables not needed
314315
std::vector<Table<2, unsigned int>> solution_copy_indices_global_mine;
315-
MGLevelObject<LinearAlgebra::distributed::Vector<Number>>
316+
MGLevelObject<LinearAlgebra::distributed::Vector<Number, MemorySpace>>
316317
solution_ghosted_level_vector;
317318

318319
fill_internal(mg_dof,

source/multigrid/mg_level_global_transfer.inst.in

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ for (S1 : REAL_SCALARS)
2323
{
2424
template class MGLevelGlobalTransfer<
2525
LinearAlgebra::distributed::Vector<S1, MemorySpace::Host>>;
26+
27+
template class MGLevelGlobalTransfer<
28+
LinearAlgebra::distributed::Vector<S1, MemorySpace::Default>>;
2629
}
2730

2831
for (deal_II_dimension : DIMENSIONS; V1 : VECTORS_WITHOUT_LAVEC)
@@ -55,6 +58,11 @@ for (deal_II_dimension : DIMENSIONS; S1 : REAL_SCALARS)
5558
LinearAlgebra::distributed::Vector<S1, MemorySpace::Host>>::
5659
fill_and_communicate_copy_indices<deal_II_dimension, deal_II_dimension>(
5760
const DoFHandler<deal_II_dimension, deal_II_dimension> &mg_dof);
61+
62+
template void MGLevelGlobalTransfer<
63+
LinearAlgebra::distributed::Vector<S1, MemorySpace::Default>>::
64+
fill_and_communicate_copy_indices<deal_II_dimension, deal_II_dimension>(
65+
const DoFHandler<deal_II_dimension, deal_II_dimension> &mg_dof);
5866
}
5967

6068
for (deal_II_dimension : DIMENSIONS; S1, S2 : REAL_SCALARS)
@@ -67,16 +75,6 @@ for (deal_II_dimension : DIMENSIONS; S1, S2 : REAL_SCALARS)
6775
&,
6876
const LinearAlgebra::distributed::Vector<S2, MemorySpace::Host> &,
6977
const bool) const;
70-
71-
// template void
72-
// MGLevelGlobalTransfer<LinearAlgebra::distributed::Vector<S1,
73-
// MemorySpace::Default>>::copy_to_mg( const
74-
// DoFHandler<deal_II_dimension> &,
75-
// MGLevelObject<LinearAlgebra::distributed::Vector<S1,
76-
// MemorySpace::Host>>
77-
// &,
78-
// const LinearAlgebra::distributed::Vector<S2, MemorySpace::Host> &,
79-
// const bool) const;
8078
}
8179

8280
for (deal_II_dimension : DIMENSIONS; S1, S2 : REAL_SCALARS)

source/multigrid/mg_transfer_matrix_free.inst.in

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
for (S1 : REAL_SCALARS)
1717
{
18-
template class MGTwoLevelTransferBase<
18+
template class MGTwoLevelTransferCore<
1919
LinearAlgebra::distributed::Vector<S1, ::dealii::MemorySpace::Host>>;
2020
}
2121

@@ -27,6 +27,10 @@ for (deal_II_dimension : DIMENSIONS; S1 : REAL_SCALARS)
2727
deal_II_dimension,
2828
LinearAlgebra::distributed::Vector<S1>>;
2929

30+
template class MGTwoLevelTransferCopyToHost<
31+
deal_II_dimension,
32+
LinearAlgebra::distributed::Vector<S1, ::dealii::MemorySpace::Default>>;
33+
3034
template class MGTransferBlockMatrixFreeBase<
3135
deal_II_dimension,
3236
S1,
@@ -36,4 +40,8 @@ for (deal_II_dimension : DIMENSIONS; S1 : REAL_SCALARS)
3640
template class MGTransferMatrixFree<deal_II_dimension,
3741
S1,
3842
::dealii::MemorySpace::Host>;
43+
44+
template class MGTransferMatrixFree<deal_II_dimension,
45+
S1,
46+
::dealii::MemorySpace::Default>;
3947
}

0 commit comments

Comments
 (0)