SimiLie
Loading...
Searching...
No Matches
tensor_impl.hpp
1// SPDX-FileCopyrightText: 2024 Baptiste Legouix
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4#pragma once
5
6#include <iostream>
7
8#include <ddc/ddc.hpp>
9
10#include <similie/misc/portable_stl.hpp>
11#include <similie/misc/specialization.hpp>
12#include <similie/misc/type_seq_ext.hpp>
13
14#include "concepts.hpp"
15
16namespace sil {
17
18namespace tensor {
19
20// struct representing an index mu or nu in a tensor Tmunu.
21template <class... CDim>
23{
24 static constexpr bool is_tensor_index = true;
25 static constexpr bool is_tensor_natural_index = true;
26 static constexpr bool is_explicitely_stored_tensor = true;
27
28 using type_seq_dimensions = ddc::TypeSeq<CDim...>;
29
30 using subindices_domain_t = ddc::DiscreteDomain<>;
31
32 KOKKOS_FUNCTION static constexpr subindices_domain_t subindices_domain()
33 {
34 return ddc::DiscreteDomain<>();
35 }
36
37 static constexpr std::size_t rank()
38 {
39 return sizeof...(CDim) != 0;
40 }
41
42 static constexpr std::size_t size()
43 {
44 if constexpr (rank() == 0) {
45 return 1;
46 } else {
47 return sizeof...(CDim);
48 }
49 }
50
51 static constexpr std::size_t mem_size()
52 {
53 return size();
54 }
55
56 static constexpr std::size_t access_size()
57 {
58 return size();
59 }
60
61 template <class ODim>
62 KOKKOS_FUNCTION static constexpr std::size_t mem_id()
63 {
64 if constexpr (rank() == 0) {
65 return 0;
66 } else {
67 return ddc::type_seq_rank_v<ODim, type_seq_dimensions>;
68 }
69 }
70
71 KOKKOS_FUNCTION static constexpr std::size_t mem_id(std::size_t const natural_id)
72 {
73 return natural_id;
74 }
75
76 KOKKOS_FUNCTION static constexpr std::size_t access_id(std::size_t const natural_id)
77 {
78 return natural_id;
79 }
80
81 KOKKOS_FUNCTION static constexpr std::size_t access_id_to_mem_id(std::size_t access_id)
82 {
83 return access_id;
84 }
85
86 template <class Tensor, class Elem, class Id, class FunctorType>
87 KOKKOS_FUNCTION static constexpr Tensor::element_type process_access(
88 const FunctorType& access,
89 Tensor const& tensor,
90 Elem const& elem)
91 {
92 return access(tensor, elem);
93 }
94
95 KOKKOS_FUNCTION static constexpr std::array<std::size_t, rank()>
97 {
98 assert(mem_id < mem_size());
99 if constexpr (rank() == 0) {
100 return std::array<std::size_t, rank()> {};
101 } else {
102 return std::array<std::size_t, rank()> {mem_id};
103 }
104 }
105};
106
107static_assert(NaturalTensorIndexConcept<
108 TensorNaturalIndex<detail::DimensionConcept>,
109 detail::DimensionConcept>);
110
111template <class DDim>
112concept TensorIndex = requires {
113 { DDim::is_tensor_index } -> std::convertible_to<bool>;
114} && DDim::is_tensor_index;
115
116template <class DDim>
117concept TensorNatIndex = requires {
118 { DDim::is_tensor_natural_index } -> std::convertible_to<bool>;
119} && DDim::is_tensor_natural_index;
120
121// ScalarIndex, a generic rank-0 index
123{
124};
125
127
128// natural_domain_t is obtained using concept and specialization
129namespace detail {
130
131template <class Index>
132struct NaturalDomainType;
133
134template <class Index>
136struct NaturalDomainType<Index>
137{
138 using type = typename Index::subindices_domain_t;
139};
140
141template <TensorNatIndex Index>
142struct NaturalDomainType<Index>
143{
144 using type = ddc::DiscreteDomain<Index>;
145};
146
147} // namespace detail
148
149template <TensorIndex Index>
150using natural_domain_t = typename detail::NaturalDomainType<Index>::type;
151
152// Helpers to build the access_id() function which computes the ids of subindices of an index. This cumbersome logic is necessary because subindices do not necessarily have the same rank.
153namespace detail {
154// For Tmunu and index=nu, returns 1
155template <class Index, class...>
156struct NbDimsBeforeIndex;
157
158template <class Index, class IndexHead, class... IndexTail>
159struct NbDimsBeforeIndex<Index, ddc::TypeSeq<IndexHead, IndexTail...>>
160{
161 static constexpr std::size_t operator()(std::size_t nb_dims_before_index)
162 {
163 if constexpr (std::is_same_v<IndexHead, Index>) {
164 return nb_dims_before_index;
165 } else {
166 return NbDimsBeforeIndex<Index, ddc::TypeSeq<IndexTail...>>::operator()(
167 nb_dims_before_index + IndexHead::rank());
168 }
169 }
170};
171
172// Offset and index sequence
173template <std::size_t Offset, class IndexSeq>
174struct OffsetIndexSeq;
175
176template <std::size_t Offset, std::size_t... Is>
177struct OffsetIndexSeq<Offset, std::integer_sequence<std::size_t, Is...>>
178{
179 using type = std::integer_sequence<std::size_t, Offset + Is...>;
180};
181
182template <std::size_t Offset, class IndexSeq>
183using offset_index_seq_t = OffsetIndexSeq<Offset, IndexSeq>::type;
184
185// Returns dimensions from integers (ie. for Tmunu, <1> gives nu)
186template <class CDimTypeSeq, class IndexSeq>
187struct TypeSeqDimsAtInts;
188
189template <class CDimTypeSeq, std::size_t... Is>
190struct TypeSeqDimsAtInts<CDimTypeSeq, std::integer_sequence<std::size_t, Is...>>
191{
192 using type = ddc::TypeSeq<ddc::type_seq_element_t<Is, CDimTypeSeq>...>;
193};
194
195template <class CDimTypeSeq, class IndexSeq>
196using type_seq_dims_at_ints_t = TypeSeqDimsAtInts<CDimTypeSeq, IndexSeq>::type;
197
198// Returns Index::access_id but from a type seq (in place of a variadic template CDim...)
199template <class Index, class SubindicesDomain, class TypeSeqDims>
200struct IdFromTypeSeqDims;
201
202template <class Index, class... Subindex, class... CDim>
203struct IdFromTypeSeqDims<Index, ddc::DiscreteDomain<Subindex...>, ddc::TypeSeq<CDim...>>
204{
205 KOKKOS_FUNCTION static constexpr std::size_t operator()()
206 {
207 static_assert(sizeof...(Subindex) == sizeof...(CDim));
208 if constexpr (TensorNatIndex<Index>) {
209 return Index::access_id(
210 ddc::type_seq_rank_v<CDim, typename Index::type_seq_dimensions>...);
211 } else {
212 return Index::access_id(
213 std::array<std::size_t, sizeof...(Subindex)> {ddc::type_seq_rank_v<
214 typename ddc::type_seq_element_t<
215 ddc::type_seq_rank_v<Subindex, ddc::TypeSeq<Subindex...>>,
216 ddc::TypeSeq<CDim...>>,
217 typename Subindex::type_seq_dimensions>...});
218 }
219 }
220};
221
222// Returns Index::access_id for the subindex Index of the IndicesTypeSeq
223template <class Index, class IndicesTypeSeq, class... CDim>
224KOKKOS_FUNCTION static constexpr std::size_t access_id()
225{
226 if constexpr (TensorNatIndex<Index>) {
227 return IdFromTypeSeqDims<
228 Index,
229 ddc::DiscreteDomain<Index>,
230 type_seq_dims_at_ints_t<
231 ddc::TypeSeq<CDim...>,
232 offset_index_seq_t<
233 NbDimsBeforeIndex<Index, IndicesTypeSeq>::operator()(0),
234 std::make_integer_sequence<std::size_t, Index::rank()>>>>::
235 operator()();
236 } else {
237 return IdFromTypeSeqDims<
238 Index,
239 typename Index::subindices_domain_t,
240 type_seq_dims_at_ints_t<
241 ddc::TypeSeq<CDim...>,
242 offset_index_seq_t<
243 NbDimsBeforeIndex<Index, IndicesTypeSeq>::operator()(0),
244 std::make_integer_sequence<std::size_t, Index::rank()>>>>::
245 operator()();
246 }
247}
248
249template <class Index, class SubindicesDomain>
250struct IdFromElem;
251
252template <class Index, class... Subindex>
253struct IdFromElem<Index, ddc::DiscreteDomain<Subindex...>>
254{
255 template <class Elem>
256 KOKKOS_FUNCTION static constexpr std::size_t operator()(Elem natural_elem)
257 {
258 if constexpr (TensorNatIndex<Index>) {
259 return Index::access_id(natural_elem.template uid<Index>());
260 } else {
261 return Index::access_id(
262 std::array<std::size_t, sizeof...(Subindex)> {
263 natural_elem.template uid<Subindex>()...});
264 }
265 }
266};
267
268template <class Index, class IndicesTypeSeq, class... NaturalIndex>
269KOKKOS_FUNCTION static constexpr std::size_t access_id(
270 ddc::DiscreteElement<NaturalIndex...> natural_elem)
271{
272 if constexpr (TensorNatIndex<Index>) {
273 return IdFromElem<Index, ddc::DiscreteDomain<Index>>::operator()(natural_elem);
274 } else {
275 return IdFromElem<Index, typename Index::subindices_domain_t>::operator()(natural_elem);
276 }
277}
278
279} // namespace detail
280
281// TensorAccessor class, allows to build a domain which represents the tensor and access elements.
282template <TensorIndex... Index>
284{
285public:
286 KOKKOS_FUNCTION explicit constexpr TensorAccessor();
287
288 using discrete_domain_type = ddc::DiscreteDomain<Index...>;
289
290 using discrete_element_type = ddc::DiscreteElement<Index...>;
291
292 using natural_domain_t = sil::misc::cartesian_prod_t<std::conditional_t< // TODO natural_domain_type
294 ddc::DiscreteDomain<Index>,
295 typename Index::subindices_domain_t>...>;
296
297 KOKKOS_FUNCTION static constexpr natural_domain_t natural_domain();
298
299 KOKKOS_FUNCTION static constexpr discrete_domain_type domain();
300
301 KOKKOS_FUNCTION static constexpr discrete_domain_type access_domain();
302
303 template <class... CDim>
304 KOKKOS_FUNCTION static constexpr discrete_element_type access_element();
305
306 template <class... NaturalIndex>
307 KOKKOS_FUNCTION static constexpr discrete_element_type access_element(
308 ddc::DiscreteElement<NaturalIndex...> natural_elem);
309
310 template <class... MemIndex>
311 KOKKOS_FUNCTION static constexpr natural_domain_t::discrete_element_type
312 canonical_natural_element(ddc::DiscreteElement<MemIndex...> mem_elem);
313};
314
315namespace detail {
316
317template <class Seq>
318struct TensorAccessorForTypeSeq;
319
320template <TensorIndex... Index>
321struct TensorAccessorForTypeSeq<ddc::TypeSeq<Index...>>
322{
323 using type = TensorAccessor<Index...>;
324};
325
326template <class Dom>
327struct TensorAccessorForDomain;
328
329template <class... DDim>
330struct TensorAccessorForDomain<ddc::DiscreteDomain<DDim...>>
331{
332 using type = typename TensorAccessorForTypeSeq<
333 ddc::to_type_seq_t<sil::misc::cartesian_prod_t<std::conditional_t<
334 TensorIndex<DDim>,
335 ddc::DiscreteDomain<DDim>,
336 ddc::DiscreteDomain<>>...>>>::type;
337};
338
339} // namespace detail
340
341template <misc::Specialization<ddc::DiscreteDomain> Dom>
342using tensor_accessor_for_domain_t = detail::TensorAccessorForDomain<Dom>::type;
343
344template <TensorIndex... Index>
346{
347}
348
349namespace detail {
350template <class Index>
351KOKKOS_FUNCTION constexpr auto natural_domain()
352{
353 if constexpr (TensorNatIndex<Index>) {
354 return typename ddc::DiscreteDomain<
355 Index>(ddc::DiscreteElement<Index>(0), ddc::DiscreteVector<Index>(Index::size()));
356 } else {
357 return Index::subindices_domain();
358 }
359}
360} // namespace detail
361
362template <TensorIndex... Index>
363KOKKOS_FUNCTION constexpr TensorAccessor<Index...>::natural_domain_t TensorAccessor<
364 Index...>::natural_domain()
365{
366 return natural_domain_t(detail::natural_domain<Index>()...);
367}
368
369template <TensorIndex... Index>
370KOKKOS_FUNCTION constexpr TensorAccessor<Index...>::discrete_domain_type TensorAccessor<
371 Index...>::domain()
372{
373 return ddc::DiscreteDomain<Index...>(
374 ddc::DiscreteElement<Index...>(ddc::DiscreteElement<Index>(0)...),
375 ddc::DiscreteVector<Index...>(ddc::DiscreteVector<Index>(Index::mem_size())...));
376}
377
378template <TensorIndex... Index>
379KOKKOS_FUNCTION constexpr TensorAccessor<Index...>::discrete_domain_type TensorAccessor<
380 Index...>::access_domain()
381{
382 return ddc::DiscreteDomain<Index...>(
383 ddc::DiscreteElement<Index...>(ddc::DiscreteElement<Index>(0)...),
384 ddc::DiscreteVector<Index...>(ddc::DiscreteVector<Index>(Index::access_size())...));
385}
386
387template <TensorIndex... Index>
388template <class... CDim>
389KOKKOS_FUNCTION constexpr TensorAccessor<Index...>::discrete_element_type TensorAccessor<
390 Index...>::access_element()
391{
392 return ddc::DiscreteElement<Index...>(ddc::DiscreteElement<Index>(
393 detail::access_id<Index, ddc::TypeSeq<Index...>, CDim...>())...);
394}
395
396template <TensorIndex... Index>
397template <class... NaturalIndex>
398KOKKOS_FUNCTION constexpr TensorAccessor<Index...>::discrete_element_type TensorAccessor<Index...>::
399 access_element([[maybe_unused]] ddc::DiscreteElement<NaturalIndex...> natural_elem)
400{
401 return ddc::DiscreteElement<Index...>(
402 ddc::DiscreteElement<Index>(detail::access_id<Index, ddc::TypeSeq<Index...>>(
403 typename natural_domain_t::discrete_element_type(natural_elem)))...);
404}
405
406template <TensorIndex... Index>
407template <class... MemIndex>
408KOKKOS_FUNCTION constexpr TensorAccessor<Index...>::natural_domain_t::discrete_element_type
409TensorAccessor<Index...>::canonical_natural_element(ddc::DiscreteElement<MemIndex...> mem_elem)
410{
411 std::array<std::size_t, natural_domain_t::rank()> ids {};
412 auto it = ids.begin();
413 (
414 [&]() {
415 auto const i = MemIndex::mem_id_to_canonical_natural_ids(
416 mem_elem.template uid<MemIndex>());
417 misc::detail::copy(i.begin(), i.end(), it);
418 it += i.size();
419 }(),
420 ...);
421 typename natural_domain_t::discrete_element_type natural_elem;
422 ddc::detail::array(natural_elem) = std::array<std::size_t, natural_domain_t::rank()>(ids);
423 return natural_elem;
424}
425
426namespace detail {
427
428// Helpers to handle memory access and processing for particular tensor structures (ie. eventual multiplication with -1 for antisymmetry or non-stored zeros)
429template <
430 class TensorField,
431 class Element,
432 class IndexHeadsTypeSeq,
433 class IndexInterest,
434 class... IndexTail>
435struct Access;
436
437template <
438 class TensorField,
439 class Element,
440 class... IndexHead,
441 class IndexInterest,
442 class... IndexTail>
443struct Access<TensorField, Element, ddc::TypeSeq<IndexHead...>, IndexInterest, IndexTail...>
444{
445 template <class Elem>
446 KOKKOS_FUNCTION static TensorField::element_type operator()(
447 TensorField const& tensor_field,
448 Elem const& elem)
449 {
450 /*
451 ----- Important warning -----
452 The general case is not correctly handled here. It would be difficult to do so.
453 It means you can get silent bug (with wrong result) if you try to use exotic ordering
454 of dimensions/indices. Ie., a TensorYoungTableauIndex has to be the last of the list.
455 */
456 if constexpr (sizeof...(IndexTail) > 0) {
457 if constexpr (TensorIndex<IndexInterest>) {
458 return IndexInterest::template process_access<TensorField, Elem, IndexInterest>(
459 KOKKOS_LAMBDA(TensorField const& tensor_field_, Elem const& elem_)
460 ->TensorField::element_type {
461 return Access<
462 TensorField,
463 Element,
464 ddc::TypeSeq<IndexHead..., IndexInterest>,
465 IndexTail...>::operator()(tensor_field_, elem_);
466 },
467 tensor_field,
468 elem);
469 } else {
470 return Access<
471 TensorField,
472 Element,
473 ddc::TypeSeq<IndexHead..., IndexInterest>,
474 IndexTail...>::operator()(tensor_field, elem);
475 }
476 } else {
477 if constexpr (TensorIndex<IndexInterest>) {
478 return IndexInterest::template process_access<TensorField, Elem, IndexInterest>(
479 KOKKOS_LAMBDA(TensorField const& tensor_field_, Elem const& elem_)
480 ->TensorField::element_type {
481 double tensor_field_value = 0;
482 if constexpr (IndexInterest::is_explicitely_stored_tensor) {
483 std::size_t const mem_id
484 = IndexInterest::access_id_to_mem_id(
485 elem_.template uid<IndexInterest>());
486 if (mem_id != std::numeric_limits<std::size_t>::max()) {
487 tensor_field_value
488 = tensor_field_
489 .mem(ddc::DiscreteElement<
490 IndexHead...>(elem_),
491 ddc::DiscreteElement<
492 IndexInterest>(mem_id));
493 } else {
494 tensor_field_value = 1.;
495 }
496 } else {
497 std::pair<
498 std::vector<double>,
499 std::vector<std::size_t>> const mem_lin_comb
500 = IndexInterest::access_id_to_mem_lin_comb(
501 elem_.template uid<IndexInterest>());
502
503 if (std::get<0>(mem_lin_comb).size() > 0) {
504 for (std::size_t i = 0;
505 i < std::get<0>(mem_lin_comb).size();
506 ++i) {
507 tensor_field_value
508 += std::get<0>(mem_lin_comb)[i]
509 * tensor_field_.mem(
510 ddc::DiscreteElement<
511 IndexHead...>(elem_),
512 ddc::DiscreteElement<
513 IndexInterest>(std::get<
514 1>(
515 mem_lin_comb)[i]));
516 }
517 } else {
518 tensor_field_value = 1.;
519 }
520 }
521
522 return tensor_field_value;
523 },
524 tensor_field,
525 elem);
526 } else {
527 return tensor_field(elem);
528 }
529 }
530 }
531};
532
533// Functor for memory element access (if defined)
534template <class InterestDim>
535struct LambdaMemElem
536{
537 template <class Elem>
538 KOKKOS_FUNCTION static ddc::DiscreteElement<InterestDim> operator()(Elem elem)
539 {
540 return ddc::DiscreteElement<InterestDim>(elem);
541 }
542};
543
544template <TensorIndex InterestDim>
545struct LambdaMemElem<InterestDim>
546{
547 template <class Elem>
548 KOKKOS_FUNCTION static ddc::DiscreteElement<InterestDim> operator()(Elem elem)
549 {
550 if constexpr (InterestDim::is_explicitely_stored_tensor) {
551 std::size_t const mem_id
552 = InterestDim::access_id_to_mem_id(elem.template uid<InterestDim>());
553 assert(mem_id != std::numeric_limits<std::size_t>::max()
554 && "mem_elem is not defined because mem_id() returned a max integer. Maybe you "
555 "used Tensor::operator() in place of Tensor::get ?");
556 return ddc::DiscreteElement<InterestDim>(mem_id);
557 } else {
558 std::pair<std::vector<double>, std::vector<std::size_t>> const mem_lin_comb
559 = InterestDim::access_id_to_mem_lin_comb(elem.template uid<InterestDim>());
560 assert(std::get<0>(mem_lin_comb).size() > 0
561 && "mem_elem is not defined because mem_lin_comb contains no id. Maybe you used "
562 "Tensor::operator() in place of Tensor::get ?");
563 assert(std::get<0>(mem_lin_comb).size() == 1
564 && "mem_elem is not defined because mem_lin_comb contains several ids. Maybe "
565 "you used Tensor::operator() in place of Tensor::get ?");
566 return ddc::DiscreteElement<InterestDim>(std::get<1>(mem_lin_comb)[0]);
567 }
568 }
569};
570
571} // namespace detail
572
573// @cond
574
575template <class ElementType, class SupportType, class LayoutStridedPolicy, class MemorySpace>
576class Tensor;
577
578} // namespace tensor
579
580} // namespace sil
581
582namespace ddc {
583
584template <class ElementType, class SupportType, class LayoutStridedPolicy, class MemorySpace>
585inline constexpr bool enable_chunk<
587 = true;
588
589template <class ElementType, class SupportType, class LayoutStridedPolicy, class MemorySpace>
590inline constexpr bool enable_borrowed_chunk<
592 = true;
593
594} // namespace ddc
595
596namespace sil {
597
598namespace tensor {
599
600// @endcond
601
603template <class ElementType, class... DDim, class LayoutStridedPolicy, class MemorySpace>
604class Tensor<ElementType, ddc::DiscreteDomain<DDim...>, LayoutStridedPolicy, MemorySpace>
605 : public ddc::
606 ChunkSpan<ElementType, ddc::DiscreteDomain<DDim...>, LayoutStridedPolicy, MemorySpace>
607{
608protected:
609 using base_type = ddc::
610 ChunkSpan<ElementType, ddc::DiscreteDomain<DDim...>, LayoutStridedPolicy, MemorySpace>;
611
612public:
613 using base_type::base_type;
614 using reference = base_type::reference;
615 using discrete_domain_type = base_type::discrete_domain_type;
616 using discrete_element_type = base_type::discrete_element_type;
617
618 using base_type::domain;
619
620 KOKKOS_FUNCTION constexpr explicit Tensor(
621 ddc::ChunkSpan<
622 ElementType,
623 ddc::DiscreteDomain<DDim...>,
624 LayoutStridedPolicy,
625 MemorySpace> other) noexcept
626 : base_type(other)
627 {
628 }
629
632 ddc::DiscreteDomain<DDim>,
633 ddc::DiscreteDomain<>>...>>;
634
635 KOKKOS_FUNCTION static constexpr accessor_t accessor()
636 {
637 return accessor_t();
638 }
639
640 using indices_domain_t = accessor_t::discrete_domain_type;
641
643 = ddc::detail::convert_type_seq_to_discrete_domain_t<ddc::type_seq_remove_t<
644 ddc::to_type_seq_t<discrete_domain_type>,
645 ddc::to_type_seq_t<indices_domain_t>>>;
647 KOKKOS_FUNCTION constexpr indices_domain_t indices_domain() const noexcept
648 {
649 return indices_domain_t(domain());
650 }
652 KOKKOS_FUNCTION constexpr non_indices_domain_t non_indices_domain() const noexcept
653 {
654 return non_indices_domain_t(domain());
655 }
657 using natural_domain_t = sil::misc::
658 cartesian_prod_t<non_indices_domain_t, typename accessor_t::natural_domain_t>;
660 KOKKOS_FUNCTION constexpr natural_domain_t natural_domain() const noexcept
661 {
662 return natural_domain_t(non_indices_domain(), accessor_t::natural_domain());
663 }
665 KOKKOS_FUNCTION constexpr discrete_domain_type access_domain() const noexcept
666 {
667 return discrete_domain_type(non_indices_domain(), accessor_t::access_domain());
668 }
669
670 template <class... CDim>
671 KOKKOS_FUNCTION constexpr discrete_element_type access_element()
672 const noexcept // TODO merge this with the one below
673 {
674 return discrete_element_type(accessor_t::template access_element<CDim...>());
675 }
676
677 template <class... Elem>
678 KOKKOS_FUNCTION constexpr discrete_element_type access_element(Elem... elem) const noexcept
679 {
681 accessor_t::access_element(
682 typename accessor_t::natural_domain_t::discrete_element_type(elem...)),
683 typename non_indices_domain_t::discrete_element_type(elem...));
684 }
685
686 template <class... Elem>
687 KOKKOS_FUNCTION constexpr natural_domain_t::discrete_element_type canonical_natural_element(
688 Elem... mem_elem) const noexcept
689 {
690 return typename natural_domain_t::discrete_element_type(
691 accessor_t::canonical_natural_element(
692 typename accessor_t::discrete_element_type(mem_elem...)),
693 typename non_indices_domain_t::discrete_element_type(mem_elem...));
694 }
695
696 template <class... DElems>
697 KOKKOS_FUNCTION constexpr reference mem(DElems const&... delems) const noexcept
698 {
699 return ddc::ChunkSpan<
700 ElementType,
701 ddc::DiscreteDomain<DDim...>,
702 LayoutStridedPolicy,
703 MemorySpace>::operator()(delems...);
704 }
705
706 template <class... DElems>
707 KOKKOS_FUNCTION constexpr reference operator()(DElems const&... delems) const noexcept
708 {
709 return ddc::ChunkSpan<
710 ElementType,
711 ddc::DiscreteDomain<DDim...>,
712 LayoutStridedPolicy,
713 MemorySpace>::
714 operator()(ddc::DiscreteElement<DDim...>(
715 detail::LambdaMemElem<DDim>::operator()(ddc::DiscreteElement<DDim>(delems...))...));
716 }
717
718 template <class... ODDim>
719 KOKKOS_FUNCTION constexpr auto operator[](
720 ddc::DiscreteElement<ODDim...> const& slice_spec) const noexcept
721 {
722 ddc::ChunkSpan chunkspan = ddc::ChunkSpan<
723 ElementType,
724 ddc::DiscreteDomain<DDim...>,
725 LayoutStridedPolicy,
726 MemorySpace>::
727 operator[](ddc::DiscreteElement<ODDim...>(
728 detail::LambdaMemElem<ODDim>::operator()(slice_spec)...));
729 return Tensor<
730 ElementType,
731 ddc::detail::convert_type_seq_to_discrete_domain_t<
732 ddc::type_seq_remove_t<ddc::TypeSeq<DDim...>, ddc::TypeSeq<ODDim...>>>,
733 typename decltype(chunkspan)::layout_type,
734 MemorySpace>(chunkspan);
735 }
736
737 template <class... DElems>
738 KOKKOS_FUNCTION ElementType get(DElems const&... delems) const noexcept
739 {
740 if constexpr (sizeof...(DDim) == 0) {
741 return operator()(delems...);
742 } else {
743 return detail::Access<
744 Tensor<ElementType,
745 ddc::DiscreteDomain<DDim...>,
746 LayoutStridedPolicy,
747 MemorySpace>,
748 ddc::DiscreteElement<DDim...>,
749 ddc::TypeSeq<>,
750 DDim...>::operator()(*this, ddc::DiscreteElement<DDim...>(delems...));
751 }
752 }
753
754 KOKKOS_FUNCTION Tensor<
755 ElementType,
756 ddc::DiscreteDomain<DDim...>,
757 LayoutStridedPolicy,
758 MemorySpace>&
759 operator+=(const Tensor<
760 ElementType,
761 ddc::DiscreteDomain<DDim...>,
762 LayoutStridedPolicy,
763 MemorySpace>& tensor)
764 {
765 ddc::device_for_each(this->domain(), [&](ddc::DiscreteElement<DDim...> elem) {
766 this->mem(elem) += tensor.mem(elem);
767 });
768 return *this;
769 }
770
771 KOKKOS_FUNCTION Tensor<
772 ElementType,
773 ddc::DiscreteDomain<DDim...>,
774 LayoutStridedPolicy,
775 MemorySpace>&
776 operator*=(const ElementType scalar)
777 {
778 ddc::device_for_each(this->domain(), [&](ddc::DiscreteElement<DDim...> elem) {
779 this->mem(elem) *= scalar;
780 });
781 return *this;
782 }
783};
784
785template <class ElementType, class SupportType, class Allocator>
786Tensor(ddc::Chunk<ElementType, SupportType, Allocator>)
788
789template <class ElementType, class SupportType, class LayoutStridedPolicy, class MemorySpace>
790Tensor(ddc::ChunkSpan<ElementType, SupportType, LayoutStridedPolicy, MemorySpace>)
792
793template <class ElementType, class SupportType>
794Tensor(ElementType*, SupportType) -> Tensor<
795 ElementType,
796 SupportType,
797 Kokkos::layout_right,
798 Kokkos::DefaultHostExecutionSpace::memory_space>;
799
800namespace detail {
801
802// Domain of a tensor result of product between two tensors
803template <class Dom1, class Dom2>
804struct NaturalTensorProdDomain;
805
806template <class... DDim1, class... DDim2>
807struct NaturalTensorProdDomain<ddc::DiscreteDomain<DDim1...>, ddc::DiscreteDomain<DDim2...>>
808{
809 using type = ddc::detail::convert_type_seq_to_discrete_domain_t<ddc::type_seq_merge_t<
810 ddc::type_seq_remove_t<ddc::TypeSeq<DDim1...>, ddc::TypeSeq<DDim2...>>,
811 ddc::type_seq_remove_t<ddc::TypeSeq<DDim2...>, ddc::TypeSeq<DDim1...>>>>;
812};
813
814} // namespace detail
815
816template <
819using natural_tensor_prod_domain_t = detail::NaturalTensorProdDomain<Dom1, Dom2>::type;
820
821template <
828
829namespace detail {
830
831// Product between two tensors naturally indexed.
832template <class HeadDDim1TypeSeq, class ContractDDimTypeSeq, class TailDDim2TypeSeq>
833struct NaturalTensorProd;
834
835template <class... HeadDDim1, class... ContractDDim, class... TailDDim2>
836struct NaturalTensorProd<
837 ddc::TypeSeq<HeadDDim1...>,
838 ddc::TypeSeq<ContractDDim...>,
839 ddc::TypeSeq<TailDDim2...>>
840{
841 template <class ElementType, class LayoutStridedPolicy, class MemorySpace>
842 KOKKOS_FUNCTION static Tensor<
843 ElementType,
844 ddc::DiscreteDomain<HeadDDim1..., TailDDim2...>,
845 LayoutStridedPolicy,
846 MemorySpace>
847 operator()(
848 Tensor<ElementType,
849 ddc::DiscreteDomain<HeadDDim1..., TailDDim2...>,
850 LayoutStridedPolicy,
851 MemorySpace> const& prod_tensor,
852 Tensor<ElementType,
853 ddc::DiscreteDomain<HeadDDim1..., ContractDDim...>,
854 LayoutStridedPolicy,
855 MemorySpace> const& tensor1,
856 Tensor<ElementType,
857 ddc::DiscreteDomain<ContractDDim..., TailDDim2...>,
858 LayoutStridedPolicy,
859 MemorySpace> const& tensor2)
860 {
861 ddc::device_for_each(
862 prod_tensor.domain(),
863 [&](ddc::DiscreteElement<HeadDDim1..., TailDDim2...> elem) {
864 prod_tensor(elem) = ddc::device_transform_reduce(
865 tensor1.template domain<ContractDDim...>(),
866 0.,
867 ddc::reducer::sum<ElementType>(),
868 [&](ddc::DiscreteElement<ContractDDim...> contract_elem) {
869 return tensor1(ddc::select<HeadDDim1...>(elem), contract_elem)
870 * tensor2(ddc::select<TailDDim2...>(elem), contract_elem);
871 });
872 });
873 return prod_tensor;
874 }
875};
876
877} // namespace detail
878
879template <
880 TensorNatIndex... ProdDDim,
881 TensorNatIndex... DDim1,
882 TensorNatIndex... DDim2,
883 class ElementType,
884 class LayoutStridedPolicy,
885 class MemorySpace>
886Tensor<ElementType, ddc::DiscreteDomain<ProdDDim...>, LayoutStridedPolicy, MemorySpace> tensor_prod(
887 Tensor<ElementType,
888 ddc::DiscreteDomain<ProdDDim...>,
889 LayoutStridedPolicy,
890 MemorySpace> const& prod_tensor,
891 Tensor<ElementType, ddc::DiscreteDomain<DDim1...>, LayoutStridedPolicy, MemorySpace> const&
892 tensor1,
893 Tensor<ElementType, ddc::DiscreteDomain<DDim2...>, LayoutStridedPolicy, MemorySpace> const&
894 tensor2)
895{
896 static_assert(std::is_same_v<
897 ddc::type_seq_remove_t<ddc::TypeSeq<DDim1...>, ddc::TypeSeq<ProdDDim...>>,
898 ddc::type_seq_remove_t<ddc::TypeSeq<DDim2...>, ddc::TypeSeq<ProdDDim...>>>);
899 return detail::NaturalTensorProd<
900 ddc::type_seq_remove_t<ddc::TypeSeq<ProdDDim...>, ddc::TypeSeq<DDim2...>>,
901 ddc::type_seq_remove_t<ddc::TypeSeq<DDim1...>, ddc::TypeSeq<ProdDDim...>>,
902 ddc::type_seq_remove_t<ddc::TypeSeq<ProdDDim...>, ddc::TypeSeq<DDim1...>>>::
903 operator()(prod_tensor, tensor1, tensor2);
904}
905
906namespace detail {
907
908template <class HeadDom, class InterestDom, class TailDom>
909struct PrintTensor;
910
911template <class... HeadDDim, class InterestDDim>
912struct PrintTensor<
913 ddc::DiscreteDomain<HeadDDim...>,
914 ddc::DiscreteDomain<InterestDDim>,
915 ddc::DiscreteDomain<>>
916{
917 template <class TensorType>
918 static std::string operator()(
919 std::string& str,
920 TensorType const& tensor,
921 ddc::DiscreteElement<HeadDDim...> i)
922 {
923 for (ddc::DiscreteElement<InterestDDim> elem :
924 ddc::DiscreteDomain<InterestDDim>(tensor.natural_domain())) {
925 str = str + " "
926 + std::to_string(tensor.get(tensor.access_element(
927 ddc::DiscreteElement<HeadDDim..., InterestDDim>(i, elem))));
928 }
929 str += "\n";
930 return str;
931 }
932};
933
934template <class... HeadDDim, class InterestDDim, class HeadOfTailDDim, class... TailOfTailDDim>
935struct PrintTensor<
936 ddc::DiscreteDomain<HeadDDim...>,
937 ddc::DiscreteDomain<InterestDDim>,
938 ddc::DiscreteDomain<HeadOfTailDDim, TailOfTailDDim...>>
939{
940 template <class TensorType>
941 static std::string operator()(
942 std::string& str,
943 TensorType const& tensor,
944 ddc::DiscreteElement<HeadDDim...> i)
945 {
946 str += "[";
947 for (ddc::DiscreteElement<InterestDDim> elem :
948 ddc::DiscreteDomain<InterestDDim>(tensor.natural_domain())) {
949 str = PrintTensor<
950 ddc::DiscreteDomain<HeadDDim..., InterestDDim>,
951 ddc::DiscreteDomain<HeadOfTailDDim>,
952 ddc::DiscreteDomain<TailOfTailDDim...>>::
953 operator()(str, tensor, ddc::DiscreteElement<HeadDDim..., InterestDDim>(i, elem));
954 }
955 str += "]\n";
956 return str;
957 }
958};
959
960} // namespace detail
961
962template <misc::Specialization<Tensor> TensorType>
963std::ostream& operator<<(std::ostream& os, TensorType const& tensor)
964{
965 std::string str = "";
966 os << detail::PrintTensor<
967 ddc::DiscreteDomain<>,
968 ddc::DiscreteDomain<ddc::type_seq_element_t<
969 0,
970 ddc::to_type_seq_t<typename TensorType::natural_domain_t>>>,
971 ddc::detail::convert_type_seq_to_discrete_domain_t<ddc::type_seq_remove_t<
972 ddc::to_type_seq_t<typename TensorType::natural_domain_t>,
973 ddc::TypeSeq<ddc::type_seq_element_t<
974 0,
975 ddc::to_type_seq_t<typename TensorType::natural_domain_t>>>>>>::
976 operator()(str, tensor, ddc::DiscreteElement<>());
977 return os;
978}
979
980} // namespace tensor
981
982} // namespace sil
KOKKOS_FUNCTION constexpr TensorAccessor()
static KOKKOS_FUNCTION constexpr discrete_element_type access_element()
static KOKKOS_FUNCTION constexpr natural_domain_t natural_domain()
static KOKKOS_FUNCTION constexpr discrete_domain_type access_domain()
sil::misc::cartesian_prod_t< std::conditional_t< TensorNatIndex< Index >, ddc::DiscreteDomain< Index >, typename Index::subindices_domain_t >... > natural_domain_t
ddc::DiscreteDomain< Index... > discrete_domain_type
ddc::DiscreteElement< Index... > discrete_element_type
static KOKKOS_FUNCTION constexpr natural_domain_t::discrete_element_type canonical_natural_element(ddc::DiscreteElement< MemIndex... > mem_elem)
static KOKKOS_FUNCTION constexpr discrete_domain_type domain()
ddc:: ChunkSpan< ElementType, ddc::DiscreteDomain< DDim... >, LayoutStridedPolicy, MemorySpace > base_type
sil::misc:: cartesian_prod_t< non_indices_domain_t, typename accessor_t::natural_domain_t > natural_domain_t
tensor_accessor_for_domain_t< sil::misc::cartesian_prod_t< std::conditional_t< TensorIndex< DDim >, ddc::DiscreteDomain< DDim >, ddc::DiscreteDomain<> >... > > accessor_t
ddc::detail::convert_type_seq_to_discrete_domain_t< ddc::type_seq_remove_t< ddc::to_type_seq_t< discrete_domain_type >, ddc::to_type_seq_t< indices_domain_t > > > non_indices_domain_t
KOKKOS_FUNCTION constexpr Tensor(ddc::ChunkSpan< ElementType, ddc::DiscreteDomain< DDim... >, LayoutStridedPolicy, MemorySpace > other) noexcept
convert_type_seq_to_t< ddc::DiscreteDomain, typename detail::CartesianProd< Domains... >::type > cartesian_prod_t
Tensor(ddc::Chunk< ElementType, SupportType, Allocator >) -> Tensor< ElementType, SupportType, Kokkos::layout_right, typename Allocator::memory_space >
detail::NaturalTensorProdDomain< Dom1, Dom2 >::type natural_tensor_prod_domain_t
natural_tensor_prod_domain_t< Dom1, Dom2 > natural_tensor_prod_domain(Dom1 dom1, Dom2 dom2)
typename detail::NaturalDomainType< Index >::type natural_domain_t
detail::TensorAccessorForDomain< Dom >::type tensor_accessor_for_domain_t
The top-level namespace of SimiLie.
Definition csr.hpp:15
static KOKKOS_FUNCTION constexpr std::size_t mem_id(std::size_t const natural_id)
static KOKKOS_FUNCTION constexpr std::size_t mem_id()
ddc::DiscreteDomain<> subindices_domain_t
static KOKKOS_FUNCTION constexpr subindices_domain_t subindices_domain()
static KOKKOS_FUNCTION constexpr std::size_t access_id_to_mem_id(std::size_t access_id)
static constexpr std::size_t mem_size()
static constexpr bool is_explicitely_stored_tensor
ddc::TypeSeq< CDim... > type_seq_dimensions
static KOKKOS_FUNCTION constexpr std::size_t access_id(std::size_t const natural_id)
static constexpr std::size_t rank()
static constexpr std::size_t size()
static KOKKOS_FUNCTION constexpr Tensor::element_type process_access(const FunctorType &access, Tensor const &tensor, Elem const &elem)
static KOKKOS_FUNCTION constexpr std::array< std::size_t, rank()> mem_id_to_canonical_natural_ids(std::size_t mem_id)
static constexpr bool is_tensor_natural_index
static constexpr std::size_t access_size()
static constexpr bool is_tensor_index