Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
108 changes: 55 additions & 53 deletions source/sparse_matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace ryujin
template <typename Number,
int n_comp,
int simd_length,
typename MemorySpace = dealii::MemorySpace::Host::kokkos_space,
typename MemorySpace = dealii::MemorySpace::Host,
bool writable = true>
class SparseMatrixView;

Expand All @@ -54,7 +54,7 @@ namespace ryujin
/**
* Default constructor.
*/
SparseMatrix();
SparseMatrix() = default;

/**
* Constructor taking a SIMD sparsity pattern as an argument.
Expand Down Expand Up @@ -82,14 +82,14 @@ namespace ryujin
* Return a writable view on the sparse matrix for the selected memory
* space.
*/
template <typename MemorySpace = dealii::MemorySpace::Host::kokkos_space>
template <typename MemorySpace = dealii::MemorySpace::Host>
SparseMatrixView<Number, n_comp, simd_length, MemorySpace, true> get_view();

/**
* Return a read-only view on the sparse matrix for the selected memory
* space.
*/
template <typename MemorySpace = dealii::MemorySpace::Host::kokkos_space>
template <typename MemorySpace = dealii::MemorySpace::Host>
SparseMatrixView<Number, n_comp, simd_length, MemorySpace, false>
get_view() const;

Expand Down Expand Up @@ -144,15 +144,15 @@ namespace ryujin
const SparsityPattern<simd_length> *sparsity_pattern_ =
nullptr; // FIXME shared_ptr

using HostSpace = dealii::MemorySpace::Host::kokkos_space;
Kokkos::View<Number *, HostSpace> data_host_;
Kokkos::View<Number *, HostSpace> exchange_buffer_host_;
using KokkosHost = dealii::MemorySpace::Host::kokkos_space;
Kokkos::View<Number *, KokkosHost> data_host_;
Kokkos::View<Number *, KokkosHost> exchange_buffer_host_;

using DefaultSpace = dealii::MemorySpace::Default::kokkos_space;
Kokkos::View<Number *, DefaultSpace> data_default_;
Kokkos::View<Number *, DefaultSpace> exchange_buffer_default_;
using KokkosDefault = dealii::MemorySpace::Default::kokkos_space;
Kokkos::View<Number *, KokkosDefault> data_default_;
Kokkos::View<Number *, KokkosDefault> exchange_buffer_default_;

bool host_space_active_;
bool host_space_active_ = true;

std::vector<MPI_Request> requests_;

Expand Down Expand Up @@ -359,8 +359,11 @@ namespace ryujin
private:
using SM = SparseMatrix<Number, n_comp, simd_length>;
std::conditional_t<writable, SM *, const SM *> sparse_matrix_;

SparsityPatternView<simd_length, MemorySpace> sparsity_pattern_;
Kokkos::View<Number *, MemorySpace> data_;

using KokkosSpace = typename MemorySpace::kokkos_space;
Kokkos::View<Number *, KokkosSpace> data_;
};


Expand Down Expand Up @@ -412,14 +415,6 @@ namespace ryujin
*/


template <typename Number, int n_components, int simd_length>
SparseMatrix<Number, n_components, simd_length>::SparseMatrix()
: sparsity_pattern_(nullptr)
, host_space_active_(true)
{
}


template <typename Number, int n_components, int simd_length>
SparseMatrix<Number, n_components, simd_length>::SparseMatrix(
const SparsityPattern<simd_length> &sparsity)
Expand All @@ -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<Kokkos::Aligned>;

data_host_ = Kokkos::View<Number *, HostSpace, Aligned>(
data_host_ = Kokkos::View<Number *, KokkosHost, Aligned>(
"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<Number *, HostSpace, Aligned>(
exchange_buffer_host_ = Kokkos::View<Number *, KokkosHost, Aligned>(
"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<Number, n_components, simd_length>::reinit(*this);
Expand Down Expand Up @@ -489,11 +484,11 @@ namespace ryujin
bool SparseMatrix<Number, n_components, simd_length>::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<MemorySpace, HostSpace> ||
std::is_same_v<MemorySpace, DefaultSpace>,
"Unexpected Kokkos memory space");
"Unexpected memory space");

return host_space_active_ == std::is_same_v<MemorySpace, HostSpace>;
}
Expand All @@ -503,26 +498,33 @@ namespace ryujin
template <typename MemorySpace>
void SparseMatrix<Number, n_components, simd_length>::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<MemorySpace, HostSpace> ||
std::is_same_v<MemorySpace, DefaultSpace>,
"Unexpected Kokkos memory space");
"Unexpected memory space");

if (is_active_memory_space<MemorySpace>())
return;

/* No copy required if default and host are the same memory spaces: */
constexpr bool move_required =
!std::is_same_v<HostSpace::kokkos_space, DefaultSpace::kokkos_space>;

if constexpr (std::is_same_v<MemorySpace, HostSpace>) {
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<MemorySpace, DefaultSpace>) {
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_);
}
}
}

Expand All @@ -532,8 +534,8 @@ namespace ryujin
void SparseMatrix<Number, n_components, simd_length>::
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<MemorySpace>(),
dealii::ExcMessage("The chosen memory space is not active."));
Expand All @@ -556,8 +558,8 @@ namespace ryujin
void SparseMatrix<Number, n_components, simd_length>::
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<MemorySpace, HostSpace>),
dealii::ExcNotImplemented());
Expand Down Expand Up @@ -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<MemorySpace, HostSpace>),
dealii::ExcNotImplemented());
Expand Down Expand Up @@ -809,8 +811,8 @@ namespace ryujin
SparseMatrix &sparse_matrix)
requires(writable != std::is_const_v<SparseMatrix>)
{
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<MemorySpace, HostSpace> ||
std::is_same_v<MemorySpace, DefaultSpace>,
Expand Down Expand Up @@ -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<MemorySpace, HostSpace> ||
std::is_same_v<MemorySpace, DefaultSpace>,
Expand All @@ -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<MemorySpace, HostSpace> ||
std::is_same_v<MemorySpace, DefaultSpace>,
Expand All @@ -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<MemorySpace, HostSpace> ||
std::is_same_v<MemorySpace, DefaultSpace>,
Expand Down
32 changes: 16 additions & 16 deletions source/sparsity_pattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@

namespace ryujin
{
template <int simd_length,
typename MemorySpace = dealii::MemorySpace::Host::kokkos_space>
template <int simd_length, typename MemorySpace = dealii::MemorySpace::Host>
class SparsityPatternView;


Expand Down Expand Up @@ -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<unsigned int *, HostSpace> row_starts_host_;
Kokkos::View<unsigned int *, HostSpace> column_indices_host_;
Kokkos::View<unsigned int *, HostSpace> indices_transposed_host_;
using KokkosHost = dealii::MemorySpace::Host::kokkos_space;
Kokkos::View<unsigned int *, KokkosHost> row_starts_host_;
Kokkos::View<unsigned int *, KokkosHost> column_indices_host_;
Kokkos::View<unsigned int *, KokkosHost> indices_transposed_host_;

using DefaultSpace = dealii::MemorySpace::Default::kokkos_space;
Kokkos::View<unsigned int *, DefaultSpace> row_starts_default_;
Kokkos::View<unsigned int *, DefaultSpace> column_indices_default_;
Kokkos::View<unsigned int *, DefaultSpace> indices_transposed_default_;
using KokkosDefault = dealii::MemorySpace::Default::kokkos_space;
Kokkos::View<unsigned int *, KokkosDefault> row_starts_default_;
Kokkos::View<unsigned int *, KokkosDefault> column_indices_default_;
Kokkos::View<unsigned int *, KokkosDefault> indices_transposed_default_;

std::vector<std::pair<unsigned int, unsigned int>> entries_to_be_sent_;
std::vector<std::pair<unsigned int, unsigned int>> send_targets_;
Expand Down Expand Up @@ -327,9 +326,10 @@ namespace ryujin
unsigned int n_internal_dofs_;
unsigned int n_locally_owned_dofs_;

Kokkos::View<const unsigned int *, MemorySpace> row_starts_;
Kokkos::View<const unsigned int *, MemorySpace> column_indices_;
Kokkos::View<const unsigned int *, MemorySpace> indices_transposed_;
using KokkosSpace = typename MemorySpace::kokkos_space;
Kokkos::View<const unsigned int *, KokkosSpace> row_starts_;
Kokkos::View<const unsigned int *, KokkosSpace> column_indices_;
Kokkos::View<const unsigned int *, KokkosSpace> indices_transposed_;
//@}
};

Expand Down Expand Up @@ -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<MemorySpace, HostSpace> ||
std::is_same_v<MemorySpace, DefaultSpace>,
"Unexpected Kokkos memory space");
"Unexpected memory space");

if constexpr (std::is_same_v<MemorySpace, HostSpace>) {
row_starts_ = sparsity_pattern.row_starts_host_;
Expand Down
20 changes: 10 additions & 10 deletions source/sparsity_pattern.template.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Kokkos::Aligned>;

row_starts_host_ = Kokkos::View<unsigned int *, HostSpace, Aligned>(
row_starts_host_ = Kokkos::View<unsigned int *, KokkosHost, Aligned>(
"sparsity_pattern_row_starts", sparsity.n_rows() + 1);

column_indices_host_ = Kokkos::View<unsigned int *, HostSpace, Aligned>(
"sparsity_pattern_column_indices", sparsity.n_nonzero_elements());

indices_transposed_host_ = Kokkos::View<unsigned int *, HostSpace, Aligned>(
column_indices_host_ = Kokkos::View<unsigned int *, KokkosHost, Aligned>(
"sparsity_pattern_column_indices", sparsity.n_nonzero_elements());

indices_transposed_host_ =
Kokkos::View<unsigned int *, KokkosHost, Aligned>(
"sparsity_pattern_column_indices", sparsity.n_nonzero_elements());

/* Vectorized part: */

Expand Down Expand Up @@ -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<simd_length>::reinit(*this);
}
Expand Down
6 changes: 3 additions & 3 deletions tests/common/sparse_matrix_02.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ int main(int argc, char *argv[])
ryujin::SparseMatrix<double, 1, simd_width> 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 == "
Expand Down Expand Up @@ -67,7 +67,7 @@ int main(int argc, char *argv[])
print_status();

const auto &view = sparse_matrix.template get_view<DefaultSpace>();
using ExecutionSpace = DefaultSpace::execution_space;
using ExecutionSpace = DefaultSpace::kokkos_space::execution_space;
const auto exec = ExecutionSpace{};
Kokkos::parallel_for("test",
Kokkos::RangePolicy<ExecutionSpace>(exec, 0, 3),
Expand Down
8 changes: 4 additions & 4 deletions tests/common/sparse_matrix_02.threads=2.output
Original file line number Diff line number Diff line change
@@ -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
Loading
Loading