diff --git a/source/sparse_matrix.h b/source/sparse_matrix.h index 63365826..33bdf843 100644 --- a/source/sparse_matrix.h +++ b/source/sparse_matrix.h @@ -27,7 +27,7 @@ namespace ryujin template class SparseMatrixView; @@ -54,7 +54,7 @@ namespace ryujin /** * Default constructor. */ - SparseMatrix(); + SparseMatrix() = default; /** * Constructor taking a SIMD sparsity pattern as an argument. @@ -82,14 +82,14 @@ namespace ryujin * Return a writable view on the sparse matrix for the selected memory * space. */ - template + template SparseMatrixView get_view(); /** * Return a read-only view on the sparse matrix for the selected memory * space. */ - template + template SparseMatrixView get_view() const; @@ -144,15 +144,15 @@ namespace ryujin const SparsityPattern *sparsity_pattern_ = nullptr; // FIXME shared_ptr - using HostSpace = dealii::MemorySpace::Host::kokkos_space; - Kokkos::View data_host_; - Kokkos::View exchange_buffer_host_; + using KokkosHost = dealii::MemorySpace::Host::kokkos_space; + Kokkos::View data_host_; + Kokkos::View exchange_buffer_host_; - using DefaultSpace = dealii::MemorySpace::Default::kokkos_space; - Kokkos::View data_default_; - Kokkos::View exchange_buffer_default_; + using KokkosDefault = dealii::MemorySpace::Default::kokkos_space; + Kokkos::View data_default_; + Kokkos::View exchange_buffer_default_; - bool host_space_active_; + bool host_space_active_ = true; std::vector requests_; @@ -359,8 +359,11 @@ namespace ryujin private: using SM = SparseMatrix; std::conditional_t sparse_matrix_; + SparsityPatternView sparsity_pattern_; - Kokkos::View data_; + + using KokkosSpace = typename MemorySpace::kokkos_space; + Kokkos::View data_; }; @@ -412,14 +415,6 @@ namespace ryujin */ - template - SparseMatrix::SparseMatrix() - : sparsity_pattern_(nullptr) - , host_space_active_(true) - { - } - - template SparseMatrix::SparseMatrix( const SparsityPattern &sparsity) @@ -435,23 +430,23 @@ namespace ryujin this->sparsity_pattern_ = &sparsity; this->host_space_active_ = true; - using HostSpace = dealii::MemorySpace::Host::kokkos_space; - using DefaultSpace = dealii::MemorySpace::Default::kokkos_space; + using KokkosHost = dealii::MemorySpace::Host::kokkos_space; + using KokkosDefault = dealii::MemorySpace::Default::kokkos_space; using Aligned = Kokkos::MemoryTraits; - data_host_ = Kokkos::View( + data_host_ = Kokkos::View( "sparse_matrix_data", sparsity.n_nonzero_elements() * n_components); data_default_ = Kokkos::create_mirror_view( - typename DefaultSpace::execution_space(), data_host_); + typename KokkosDefault::execution_space(), data_host_); const std::size_t n_indices = sparsity.entries_to_be_sent().size(); - exchange_buffer_host_ = Kokkos::View( + exchange_buffer_host_ = Kokkos::View( "sparse_matrix_exchange_buffer", n_components * n_indices); exchange_buffer_default_ = Kokkos::create_mirror_view( - typename DefaultSpace::execution_space(), exchange_buffer_host_); + typename KokkosDefault::execution_space(), exchange_buffer_host_); /* reinitialize the view: */ SparseMatrixView::reinit(*this); @@ -489,11 +484,11 @@ namespace ryujin bool SparseMatrix::is_active_memory_space() const { - using HostSpace = dealii::MemorySpace::Host::kokkos_space; - using DefaultSpace = dealii::MemorySpace::Default::kokkos_space; + using HostSpace = dealii::MemorySpace::Host; + using DefaultSpace = dealii::MemorySpace::Default; static_assert(std::is_same_v || std::is_same_v, - "Unexpected Kokkos memory space"); + "Unexpected memory space"); return host_space_active_ == std::is_same_v; } @@ -503,26 +498,33 @@ namespace ryujin template void SparseMatrix::move_to_memory_space() { - using HostSpace = dealii::MemorySpace::Host::kokkos_space; - using DefaultSpace = dealii::MemorySpace::Default::kokkos_space; + using HostSpace = dealii::MemorySpace::Host; + using DefaultSpace = dealii::MemorySpace::Default; static_assert(std::is_same_v || std::is_same_v, - "Unexpected Kokkos memory space"); + "Unexpected memory space"); if (is_active_memory_space()) return; + /* No copy required if default and host are the same memory spaces: */ + constexpr bool move_required = + !std::is_same_v; + if constexpr (std::is_same_v) { host_space_active_ = true; - Kokkos::deep_copy(/*dst*/ data_host_, /*src*/ data_default_); - Kokkos::deep_copy(/*dst*/ exchange_buffer_host_, - /*src*/ exchange_buffer_default_); - + if constexpr (move_required) { + Kokkos::deep_copy(/*dst*/ data_host_, /*src*/ data_default_); + Kokkos::deep_copy(/*dst*/ exchange_buffer_host_, + /*src*/ exchange_buffer_default_); + } } else if constexpr (std::is_same_v) { host_space_active_ = false; - Kokkos::deep_copy(/*dst*/ data_default_, /*src*/ data_host_); - Kokkos::deep_copy(/*dst*/ exchange_buffer_default_, - /*src*/ exchange_buffer_host_); + if constexpr (move_required) { + Kokkos::deep_copy(/*dst*/ data_default_, /*src*/ data_host_); + Kokkos::deep_copy(/*dst*/ exchange_buffer_default_, + /*src*/ exchange_buffer_host_); + } } } @@ -532,8 +534,8 @@ namespace ryujin void SparseMatrix:: zero_out_ghost_rows_on_memory_space() { - using HostSpace = dealii::MemorySpace::Host::kokkos_space; - using DefaultSpace = dealii::MemorySpace::Default::kokkos_space; + using HostSpace = dealii::MemorySpace::Host; + using DefaultSpace = dealii::MemorySpace::Default; Assert(is_active_memory_space(), dealii::ExcMessage("The chosen memory space is not active.")); @@ -556,8 +558,8 @@ namespace ryujin void SparseMatrix:: update_ghost_rows_on_memory_space() { - using HostSpace = dealii::MemorySpace::Host::kokkos_space; - using DefaultSpace = dealii::MemorySpace::Default::kokkos_space; + using HostSpace = dealii::MemorySpace::Host; + using DefaultSpace = dealii::MemorySpace::Default; AssertThrow((std::is_same_v), dealii::ExcNotImplemented()); @@ -661,8 +663,8 @@ namespace ryujin AssertThrow(operation == dealii::VectorOperation::add, dealii::ExcNotImplemented()); - using HostSpace = dealii::MemorySpace::Host::kokkos_space; - using DefaultSpace = dealii::MemorySpace::Default::kokkos_space; + using HostSpace = dealii::MemorySpace::Host; + using DefaultSpace = dealii::MemorySpace::Default; AssertThrow((std::is_same_v), dealii::ExcNotImplemented()); @@ -809,8 +811,8 @@ namespace ryujin SparseMatrix &sparse_matrix) requires(writable != std::is_const_v) { - using HostSpace = dealii::MemorySpace::Host::kokkos_space; - using DefaultSpace = dealii::MemorySpace::Default::kokkos_space; + using HostSpace = dealii::MemorySpace::Host; + using DefaultSpace = dealii::MemorySpace::Default; static_assert(std::is_same_v || std::is_same_v, @@ -1162,8 +1164,8 @@ namespace ryujin zero_out_ghost_rows() const requires(writable) { - using HostSpace = dealii::MemorySpace::Host::kokkos_space; - using DefaultSpace = dealii::MemorySpace::Default::kokkos_space; + using HostSpace = dealii::MemorySpace::Host; + using DefaultSpace = dealii::MemorySpace::Default; static_assert(std::is_same_v || std::is_same_v, @@ -1185,8 +1187,8 @@ namespace ryujin update_ghost_rows() const requires(writable) { - using HostSpace = dealii::MemorySpace::Host::kokkos_space; - using DefaultSpace = dealii::MemorySpace::Default::kokkos_space; + using HostSpace = dealii::MemorySpace::Host; + using DefaultSpace = dealii::MemorySpace::Default; static_assert(std::is_same_v || std::is_same_v, @@ -1208,8 +1210,8 @@ namespace ryujin compress(dealii::VectorOperation::values operation) const requires(writable) { - using HostSpace = dealii::MemorySpace::Host::kokkos_space; - using DefaultSpace = dealii::MemorySpace::Default::kokkos_space; + using HostSpace = dealii::MemorySpace::Host; + using DefaultSpace = dealii::MemorySpace::Default; static_assert(std::is_same_v || std::is_same_v, diff --git a/source/sparsity_pattern.h b/source/sparsity_pattern.h index 89b9d1c1..ddae576f 100644 --- a/source/sparsity_pattern.h +++ b/source/sparsity_pattern.h @@ -15,8 +15,7 @@ namespace ryujin { - template + template class SparsityPatternView; @@ -132,15 +131,15 @@ namespace ryujin unsigned int n_internal_dofs_; unsigned int n_locally_owned_dofs_; - using HostSpace = dealii::MemorySpace::Host::kokkos_space; - Kokkos::View row_starts_host_; - Kokkos::View column_indices_host_; - Kokkos::View indices_transposed_host_; + using KokkosHost = dealii::MemorySpace::Host::kokkos_space; + Kokkos::View row_starts_host_; + Kokkos::View column_indices_host_; + Kokkos::View indices_transposed_host_; - using DefaultSpace = dealii::MemorySpace::Default::kokkos_space; - Kokkos::View row_starts_default_; - Kokkos::View column_indices_default_; - Kokkos::View indices_transposed_default_; + using KokkosDefault = dealii::MemorySpace::Default::kokkos_space; + Kokkos::View row_starts_default_; + Kokkos::View column_indices_default_; + Kokkos::View indices_transposed_default_; std::vector> entries_to_be_sent_; std::vector> send_targets_; @@ -327,9 +326,10 @@ namespace ryujin unsigned int n_internal_dofs_; unsigned int n_locally_owned_dofs_; - Kokkos::View row_starts_; - Kokkos::View column_indices_; - Kokkos::View indices_transposed_; + using KokkosSpace = typename MemorySpace::kokkos_space; + Kokkos::View row_starts_; + Kokkos::View column_indices_; + Kokkos::View indices_transposed_; //@} }; @@ -366,12 +366,12 @@ namespace ryujin n_internal_dofs_ = sparsity_pattern.n_internal_dofs_; n_locally_owned_dofs_ = sparsity_pattern.n_locally_owned_dofs_; - using HostSpace = dealii::MemorySpace::Host::kokkos_space; - using DefaultSpace = dealii::MemorySpace::Default::kokkos_space; + using HostSpace = dealii::MemorySpace::Host; + using DefaultSpace = dealii::MemorySpace::Default; static_assert(std::is_same_v || std::is_same_v, - "Unexpected Kokkos memory space"); + "Unexpected memory space"); if constexpr (std::is_same_v) { row_starts_ = sparsity_pattern.row_starts_host_; diff --git a/source/sparsity_pattern.template.h b/source/sparsity_pattern.template.h index 14726e35..983526bd 100644 --- a/source/sparsity_pattern.template.h +++ b/source/sparsity_pattern.template.h @@ -86,19 +86,19 @@ namespace ryujin /* Allocate memory: */ - using HostSpace = dealii::MemorySpace::Host::kokkos_space; - using DefaultSpace = dealii::MemorySpace::Default::kokkos_space; + using KokkosHost = dealii::MemorySpace::Host::kokkos_space; + using KokkosDefault = dealii::MemorySpace::Default::kokkos_space; using Aligned = Kokkos::MemoryTraits; - row_starts_host_ = Kokkos::View( + row_starts_host_ = Kokkos::View( "sparsity_pattern_row_starts", sparsity.n_rows() + 1); - column_indices_host_ = Kokkos::View( - "sparsity_pattern_column_indices", sparsity.n_nonzero_elements()); - - indices_transposed_host_ = Kokkos::View( + column_indices_host_ = Kokkos::View( "sparsity_pattern_column_indices", sparsity.n_nonzero_elements()); + indices_transposed_host_ = + Kokkos::View( + "sparsity_pattern_column_indices", sparsity.n_nonzero_elements()); /* Vectorized part: */ @@ -390,13 +390,13 @@ namespace ryujin */ row_starts_default_ = Kokkos::create_mirror_view_and_copy( - typename DefaultSpace::execution_space(), row_starts_host_); + typename KokkosDefault::execution_space(), row_starts_host_); column_indices_default_ = Kokkos::create_mirror_view_and_copy( - typename DefaultSpace::execution_space(), column_indices_host_); + typename KokkosDefault::execution_space(), column_indices_host_); indices_transposed_default_ = Kokkos::create_mirror_view_and_copy( - typename DefaultSpace::execution_space(), indices_transposed_host_); + typename KokkosDefault::execution_space(), indices_transposed_host_); SparsityPatternView::reinit(*this); } diff --git a/tests/common/sparse_matrix_02.cc b/tests/common/sparse_matrix_02.cc index bad7f515..75bb60e1 100644 --- a/tests/common/sparse_matrix_02.cc +++ b/tests/common/sparse_matrix_02.cc @@ -36,8 +36,8 @@ int main(int argc, char *argv[]) ryujin::SparseMatrix sparse_matrix; sparse_matrix.reinit(sparsity_pattern); - using HostSpace = dealii::MemorySpace::Host::kokkos_space; - using DefaultSpace = dealii::MemorySpace::Default::kokkos_space; + using HostSpace = dealii::MemorySpace::Host; + using DefaultSpace = dealii::MemorySpace::Default; const auto print_status = [&]() { std::cout << "HostSpace active == " @@ -67,7 +67,7 @@ int main(int argc, char *argv[]) print_status(); const auto &view = sparse_matrix.template get_view(); - using ExecutionSpace = DefaultSpace::execution_space; + using ExecutionSpace = DefaultSpace::kokkos_space::execution_space; const auto exec = ExecutionSpace{}; Kokkos::parallel_for("test", Kokkos::RangePolicy(exec, 0, 3), diff --git a/tests/common/sparse_matrix_02.threads=2.output b/tests/common/sparse_matrix_02.threads=2.output index 106cb45b..6392a299 100644 --- a/tests/common/sparse_matrix_02.threads=2.output +++ b/tests/common/sparse_matrix_02.threads=2.output @@ -1,14 +1,14 @@ HostSpace active == 1 -DefaultSpace active == 1 +DefaultSpace active == 0 After move to DefaultSpace: -HostSpace active == 1 +HostSpace active == 0 DefaultSpace active == 1 After repeated move to DefaultSpace: -HostSpace active == 1 +HostSpace active == 0 DefaultSpace active == 1 After move to HostSpace: HostSpace active == 1 -DefaultSpace active == 1 +DefaultSpace active == 0 Entry (0, 0): 42 Entry (1, 1): 420 Entry (2, 2): 4200 diff --git a/tests/common/sparse_matrix_02.threads=2.output.cuda b/tests/common/sparse_matrix_02.threads=2.output.cuda deleted file mode 100644 index 6392a299..00000000 --- a/tests/common/sparse_matrix_02.threads=2.output.cuda +++ /dev/null @@ -1,14 +0,0 @@ -HostSpace active == 1 -DefaultSpace active == 0 -After move to DefaultSpace: -HostSpace active == 0 -DefaultSpace active == 1 -After repeated move to DefaultSpace: -HostSpace active == 0 -DefaultSpace active == 1 -After move to HostSpace: -HostSpace active == 1 -DefaultSpace active == 0 -Entry (0, 0): 42 -Entry (1, 1): 420 -Entry (2, 2): 4200