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