SimiLie
Loading...
Searching...
No Matches
diagonal_tensor.hpp
1// SPDX-FileCopyrightText: 2024 Baptiste Legouix
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4#pragma once
5
6#include <ddc/ddc.hpp>
7
8#include <similie/misc/filled_struct.hpp>
9#include <similie/tensor/tensor_impl.hpp>
10
11namespace sil {
12
13namespace tensor {
14
15// struct representing and index for a diagonal tensor (only diagonal is stored).
16template <TensorNatIndex... TensorIndex>
18{
19 static constexpr bool is_tensor_index = true;
20 static constexpr bool is_explicitely_stored_tensor = true;
21
22 using subindices_domain_t = ddc::DiscreteDomain<TensorIndex...>;
23
24 KOKKOS_FUNCTION static constexpr subindices_domain_t subindices_domain()
25 {
26 return ddc::DiscreteDomain<TensorIndex...>(
27 ddc::DiscreteElement<TensorIndex...>(ddc::DiscreteElement<TensorIndex>(0)...),
28 ddc::DiscreteVector<TensorIndex...>(
29 ddc::DiscreteVector<TensorIndex>(TensorIndex::size())...));
30 }
31
32 static constexpr std::size_t rank()
33 {
34 return (TensorIndex::rank() + ...);
35 }
36
37 static constexpr std::size_t size()
38 {
39 return (TensorIndex::size() * ...);
40 }
41
42 static constexpr std::size_t mem_size()
43 {
44 return ddc::type_seq_element_t<0, ddc::TypeSeq<TensorIndex...>>::mem_size();
45 }
46
47 static constexpr std::size_t access_size()
48 {
49 return 1 + mem_size();
50 }
51
52 KOKKOS_FUNCTION static constexpr std::size_t mem_id(
53 std::array<std::size_t, sizeof...(TensorIndex)> const& natural_ids)
54 {
55 assert(std::all_of(natural_ids.begin(), natural_ids.end(), [&](const std::size_t id) {
56 return id == *natural_ids.begin();
57 }));
58 return natural_ids[0];
59 }
60
61 KOKKOS_FUNCTION static constexpr std::size_t access_id(
62 std::array<std::size_t, sizeof...(TensorIndex)> const& natural_ids)
63 {
64 if (!std::all_of(natural_ids.begin(), natural_ids.end(), [&](const std::size_t id) {
65 return id == *natural_ids.begin();
66 })) {
67 return 0;
68 } else {
69 return 1 + mem_id(natural_ids);
70 }
71 }
72
73 KOKKOS_FUNCTION static constexpr std::size_t access_id_to_mem_id(std::size_t access_id)
74 {
75 assert(access_id != 0 && "There is no mem_id associated to access_id=0");
76 return access_id - 1;
77 }
78
79 template <class Tensor, class Elem, class Id, class FunctorType>
80 KOKKOS_FUNCTION static constexpr Tensor::element_type process_access(
81 const FunctorType& access,
82 Tensor const& tensor,
83 Elem const& elem)
84 {
85 if (elem.template uid<Id>() == 0) {
86 return 0.;
87 } else {
88 return access(tensor, elem);
89 }
90 }
91
92 KOKKOS_FUNCTION static constexpr std::array<std::size_t, rank()>
94 {
95 assert(mem_id < mem_size());
96 std::array<std::size_t, rank()> ids;
97 std::fill(ids.begin(), ids.end(), mem_id);
98 return ids;
99 }
100};
101
102static_assert(
103 TensorIndexConcept<TensorDiagonalIndex<TensorNaturalIndex<detail::DimensionConcept>>>);
104
105} // namespace tensor
106
107} // namespace sil
The top-level namespace of SimiLie.
Definition csr.hpp:15
static KOKKOS_FUNCTION constexpr Tensor::element_type process_access(const FunctorType &access, Tensor const &tensor, Elem const &elem)
static KOKKOS_FUNCTION constexpr std::size_t access_id_to_mem_id(std::size_t access_id)
static constexpr std::size_t access_size()
static KOKKOS_FUNCTION constexpr std::array< std::size_t, rank()> mem_id_to_canonical_natural_ids(std::size_t mem_id)
static constexpr std::size_t mem_size()
static KOKKOS_FUNCTION constexpr subindices_domain_t subindices_domain()
ddc::DiscreteDomain< TensorIndex... > subindices_domain_t
static constexpr std::size_t size()
static KOKKOS_FUNCTION constexpr std::size_t access_id(std::array< std::size_t, sizeof...(TensorIndex)> const &natural_ids)
static KOKKOS_FUNCTION constexpr std::size_t mem_id(std::array< std::size_t, sizeof...(TensorIndex)> const &natural_ids)
static constexpr bool is_explicitely_stored_tensor
static constexpr bool is_tensor_index
static constexpr std::size_t rank()