SimiLie
Loading...
Searching...
No Matches
young_tableau_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/csr/csr.hpp>
9#include <similie/misc/stride.hpp>
10#include <similie/tensor/tensor_impl.hpp>
11#include <similie/young_tableau/young_tableau.hpp>
12
13namespace sil {
14
15namespace tensor {
16
17// struct representing an abstract unique index sweeping on all possible combination of natural indices, for a summetric tensor.
18template <class YoungTableau, TensorNatIndex... TensorIndex>
20{
21 static constexpr bool is_tensor_index = true;
22 static constexpr bool is_explicitely_stored_tensor = false;
23
24 using young_tableau = YoungTableau;
25
26 using subindices_domain_t = ddc::DiscreteDomain<TensorIndex...>;
27
28 KOKKOS_FUNCTION static constexpr subindices_domain_t subindices_domain()
29 {
30 return ddc::DiscreteDomain<TensorIndex...>(
31 ddc::DiscreteElement<TensorIndex...>(ddc::DiscreteElement<TensorIndex>(0)...),
32 ddc::DiscreteVector<TensorIndex...>(
33 ddc::DiscreteVector<TensorIndex>(TensorIndex::size())...));
34 }
35
36 static constexpr std::size_t rank()
37 {
38 return (TensorIndex::rank() + ...);
39 }
40
41 static constexpr std::size_t size()
42 {
43 return (TensorIndex::size() * ...);
44 }
45
46 static constexpr std::size_t mem_size()
47 {
48 return YoungTableau::irrep_dim();
49 }
50
51 static constexpr std::size_t access_size()
52 {
53 return size();
54 }
55
56 static constexpr std::pair<std::vector<double>, std::vector<std::size_t>> mem_lin_comb(
57 std::array<std::size_t, sizeof...(TensorIndex)> const& natural_ids)
58 {
59 return access_id_to_mem_lin_comb(access_id(natural_ids));
60 }
61
62 KOKKOS_FUNCTION static constexpr std::size_t access_id(
63 std::array<std::size_t, sizeof...(TensorIndex)> const& natural_ids)
64 {
65 return ((misc::detail::stride<TensorIndex, TensorIndex...>()
66 * natural_ids[ddc::type_seq_rank_v<TensorIndex, ddc::TypeSeq<TensorIndex...>>])
67 + ...);
68 }
69
70 static constexpr std::pair<std::vector<double>, std::vector<std::size_t>>
72 {
73 std::pair<std::vector<double>, std::vector<std::size_t>> result {};
74 constexpr csr::Csr v = young_tableau::template v<
75 TensorYoungTableauIndex<YoungTableau, TensorIndex...>,
76 TensorIndex...>(ddc::DiscreteDomain<TensorIndex...>(
77 ddc::DiscreteElement<TensorIndex...>(ddc::DiscreteElement<TensorIndex>(0)...),
78 ddc::DiscreteVector<TensorIndex...>(
79 ddc::DiscreteVector<TensorIndex>(TensorIndex::size())...)));
80 for (std::size_t j = 0; j < v.values().size(); ++j) {
81 if (((v.idx()[ddc::type_seq_rank_v<TensorIndex, ddc::TypeSeq<TensorIndex...>>][j]
82 == ((access_id % misc::detail::next_stride<TensorIndex, TensorIndex...>())
83 / misc::detail::stride<TensorIndex, TensorIndex...>()))
84 && ...)) {
85 std::get<0>(result).push_back(v.values()[j]);
86 std::size_t k = 0;
87 while (k < v.coalesc_idx().size() - 1 && v.coalesc_idx()[k + 1] <= j) {
88 k++;
89 }
90 std::get<1>(result).push_back(k);
91 }
92 }
93 return result;
94 }
95
96 template <class Tensor, class Elem, class Id, class FunctorType>
97 KOKKOS_FUNCTION static constexpr Tensor::element_type process_access(
98 const FunctorType& access,
99 Tensor const& tensor,
100 Elem const& elem)
101 {
102 return access(tensor, elem);
103 }
104};
105
106static_assert(YoungTableauTensorIndexConcept<TensorYoungTableauIndex<
107 sil::young_tableau::YoungTableau<
108 1,
109 sil::young_tableau::YoungTableauSeq<std::index_sequence<1>>>,
110 TensorNaturalIndex<detail::DimensionConcept>>>);
111
112// Compress & uncompress (multiply by young_tableau.u or young_tableau.v
113template <class YoungTableauIndex, class... Id>
115 double,
116 ddc::DiscreteDomain<YoungTableauIndex>,
117 Kokkos::layout_right,
118 Kokkos::DefaultHostExecutionSpace::memory_space>
121 double,
122 ddc::DiscreteDomain<YoungTableauIndex>,
123 Kokkos::layout_right,
124 Kokkos::DefaultHostExecutionSpace::memory_space> const& compressed,
126 double,
127 ddc::DiscreteDomain<Id...>,
128 Kokkos::layout_right,
129 Kokkos::DefaultHostExecutionSpace::memory_space> const& tensor)
130{
131 typename YoungTableauIndex::young_tableau young_tableau;
132 csr::Csr u = young_tableau.template u<YoungTableauIndex, Id...>(tensor.domain());
133
134 return csr::tensor_prod(compressed, u, tensor);
135}
136
137template <class YoungTableauIndex, class... Id>
139 double,
140 ddc::DiscreteDomain<Id...>,
141 Kokkos::layout_right,
142 Kokkos::DefaultHostExecutionSpace::memory_space>
145 double,
146 ddc::DiscreteDomain<Id...>,
147 Kokkos::layout_right,
148 Kokkos::DefaultHostExecutionSpace::memory_space> const& uncompressed,
150 double,
151 ddc::DiscreteDomain<YoungTableauIndex>,
152 Kokkos::layout_right,
153 Kokkos::DefaultHostExecutionSpace::memory_space> const& tensor)
154{
155 typename YoungTableauIndex::young_tableau young_tableau;
156 csr::Csr v = young_tableau.template v<YoungTableauIndex, Id...>(uncompressed.domain());
157
158 return csr::tensor_prod(uncompressed, tensor, v);
159}
160
161} // namespace tensor
162
163} // namespace sil
KOKKOS_FUNCTION constexpr std::array< std::size_t, HeadTensorIndex::mem_size()+1 > const & coalesc_idx() const
Definition csr.hpp:82
KOKKOS_FUNCTION constexpr std::array< std::array< std::size_t, N >, sizeof...(TailTensorIndex)> const & idx() const
Definition csr.hpp:89
KOKKOS_FUNCTION constexpr std::array< double, N > const & values() const
Definition csr.hpp:94
sil::tensor::Tensor< double, ddc::DiscreteDomain< TailTensorIndex... >, Kokkos::layout_right, Kokkos::DefaultHostExecutionSpace::memory_space > tensor_prod(sil::tensor::Tensor< double, ddc::DiscreteDomain< TailTensorIndex... >, Kokkos::layout_right, Kokkos::DefaultHostExecutionSpace::memory_space > const &prod, sil::tensor::Tensor< double, ddc::DiscreteDomain< HeadTensorIndex >, Kokkos::layout_right, Kokkos::DefaultHostExecutionSpace::memory_space > const &dense, Csr< N, HeadTensorIndex, TailTensorIndex... > const &csr)
Definition csr.hpp:112
tensor::Tensor< double, ddc::DiscreteDomain< YoungTableauIndex >, Kokkos::layout_right, Kokkos::DefaultHostExecutionSpace::memory_space > compress(tensor::Tensor< double, ddc::DiscreteDomain< YoungTableauIndex >, Kokkos::layout_right, Kokkos::DefaultHostExecutionSpace::memory_space > const &compressed, tensor::Tensor< double, ddc::DiscreteDomain< Id... >, Kokkos::layout_right, Kokkos::DefaultHostExecutionSpace::memory_space > const &tensor)
tensor::Tensor< double, ddc::DiscreteDomain< Id... >, Kokkos::layout_right, Kokkos::DefaultHostExecutionSpace::memory_space > uncompress(tensor::Tensor< double, ddc::DiscreteDomain< Id... >, Kokkos::layout_right, Kokkos::DefaultHostExecutionSpace::memory_space > const &uncompressed, tensor::Tensor< double, ddc::DiscreteDomain< YoungTableauIndex >, Kokkos::layout_right, Kokkos::DefaultHostExecutionSpace::memory_space > const &tensor)
The top-level namespace of SimiLie.
Definition csr.hpp:15
static KOKKOS_FUNCTION constexpr subindices_domain_t subindices_domain()
static constexpr std::size_t mem_size()
static constexpr std::size_t size()
static constexpr std::size_t rank()
static KOKKOS_FUNCTION constexpr std::size_t access_id(std::array< std::size_t, sizeof...(TensorIndex)> const &natural_ids)
static constexpr std::pair< std::vector< double >, std::vector< std::size_t > > mem_lin_comb(std::array< std::size_t, sizeof...(TensorIndex)> const &natural_ids)
ddc::DiscreteDomain< TensorIndex... > subindices_domain_t
static constexpr std::size_t access_size()
static constexpr std::pair< std::vector< double >, std::vector< std::size_t > > access_id_to_mem_lin_comb(std::size_t access_id)
static KOKKOS_FUNCTION constexpr Tensor::element_type process_access(const FunctorType &access, Tensor const &tensor, Elem const &elem)