SimiLie
Loading...
Searching...
No Matches
diagonal_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/misc/filled_struct.hpp>
9
10#include "tensor_impl.hpp"
11
12namespace sil {
13
14namespace tensor {
15
16// struct representing and index for a diagonal tensor (only diagonal is stored).
17template <TensorNatIndex... TensorIndex>
19{
20 static constexpr bool is_tensor_index = true;
21 static constexpr bool is_explicitely_stored_tensor = true;
22
23 using subindices_domain_t = ddc::DiscreteDomain<TensorIndex...>;
24
25 KOKKOS_FUNCTION static constexpr subindices_domain_t subindices_domain()
26 {
27 return ddc::DiscreteDomain<TensorIndex...>(
28 ddc::DiscreteElement<TensorIndex...>(ddc::DiscreteElement<TensorIndex>(0)...),
29 ddc::DiscreteVector<TensorIndex...>(
30 ddc::DiscreteVector<TensorIndex>(TensorIndex::size())...));
31 }
32
33 KOKKOS_FUNCTION static constexpr std::size_t rank()
34 {
35 return (TensorIndex::rank() + ...);
36 }
37
38 KOKKOS_FUNCTION static constexpr std::size_t size()
39 {
40 return (TensorIndex::size() * ...);
41 }
42
43 KOKKOS_FUNCTION static constexpr std::size_t mem_size()
44 {
45 return ddc::type_seq_element_t<0, ddc::detail::TypeSeq<TensorIndex...>>::mem_size();
46 }
47
48 KOKKOS_FUNCTION static constexpr std::size_t access_size()
49 {
50 return 1 + mem_size();
51 }
52
53 KOKKOS_FUNCTION static constexpr std::size_t mem_id(
54 std::array<std::size_t, sizeof...(TensorIndex)> const natural_ids)
55 {
56 assert(std::all_of(natural_ids.begin(), natural_ids.end(), [&](const std::size_t id) {
57 return id == *natural_ids.begin();
58 }));
59 return natural_ids[0];
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 if (!std::all_of(natural_ids.begin(), natural_ids.end(), [&](const std::size_t id) {
66 return id == *natural_ids.begin();
67 })) {
68 return 0;
69 } else {
70 return 1 + mem_id(natural_ids);
71 }
72 }
73
74 KOKKOS_FUNCTION static constexpr std::size_t access_id_to_mem_id(std::size_t access_id)
75 {
76 assert(access_id != 0 && "There is no mem_id associated to access_id=0");
77 return access_id - 1;
78 }
79
80 template <class Tensor, class Elem, class Id, class FunctorType>
81 KOKKOS_FUNCTION static constexpr Tensor::element_type process_access(
82 const FunctorType& access,
83 Tensor tensor,
84 Elem elem)
85 {
86 if (elem.template uid<Id>() == 0) {
87 return 0.;
88 } else {
89 return access(tensor, elem);
90 }
91 }
92
93 KOKKOS_FUNCTION static constexpr std::array<std::size_t, rank()>
95 {
96 assert(mem_id < mem_size());
97 std::array<std::size_t, rank()> ids;
98 std::fill(ids.begin(), ids.end(), mem_id);
99 return ids;
100 }
101};
102
103} // namespace tensor
104
105} // namespace sil
Tensor(ddc::Chunk< ElementType, SupportType, Allocator >) -> Tensor< ElementType, SupportType, Kokkos::layout_right, typename Allocator::memory_space >
The top-level namespace of SimiLie.
Definition csr.hpp:14
static KOKKOS_FUNCTION constexpr std::size_t access_id_to_mem_id(std::size_t access_id)
static KOKKOS_FUNCTION constexpr std::size_t size()
static KOKKOS_FUNCTION constexpr std::array< std::size_t, rank()> mem_id_to_canonical_natural_ids(std::size_t mem_id)
static KOKKOS_FUNCTION constexpr std::size_t access_size()
static KOKKOS_FUNCTION constexpr std::size_t rank()
static KOKKOS_FUNCTION constexpr std::size_t mem_id(std::array< std::size_t, sizeof...(TensorIndex)> const natural_ids)
static KOKKOS_FUNCTION constexpr subindices_domain_t subindices_domain()
static KOKKOS_FUNCTION constexpr std::size_t mem_size()
ddc::DiscreteDomain< TensorIndex... > subindices_domain_t
static KOKKOS_FUNCTION constexpr std::size_t access_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 KOKKOS_FUNCTION constexpr Tensor::element_type process_access(const FunctorType &access, Tensor tensor, Elem elem)