SimiLie
Loading...
Searching...
No Matches
identity_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 "tensor_impl.hpp"
9
10namespace sil {
11
12namespace tensor {
13
14// struct representing an identity tensor (no storage).
15template <TensorNatIndex... TensorIndex>
17{
18 static constexpr bool is_tensor_index = true;
19 static constexpr bool is_explicitely_stored_tensor = true;
20
21 using subindices_domain_t = ddc::DiscreteDomain<TensorIndex...>;
22
23 KOKKOS_FUNCTION static constexpr subindices_domain_t subindices_domain()
24 {
25 return ddc::DiscreteDomain<TensorIndex...>(
26 ddc::DiscreteElement<TensorIndex...>(ddc::DiscreteElement<TensorIndex>(0)...),
27 ddc::DiscreteVector<TensorIndex...>(
28 ddc::DiscreteVector<TensorIndex>(TensorIndex::size())...));
29 }
30
31 KOKKOS_FUNCTION static constexpr std::size_t rank()
32 {
33 return (TensorIndex::rank() + ...);
34 }
35
36 KOKKOS_FUNCTION static constexpr std::size_t size()
37 {
38 return (TensorIndex::size() * ...);
39 }
40
41 KOKKOS_FUNCTION static constexpr std::size_t mem_size()
42 {
43 return 0;
44 }
45
46 KOKKOS_FUNCTION static constexpr std::size_t access_size()
47 {
48 return 2;
49 }
50
51 KOKKOS_FUNCTION static constexpr std::size_t mem_id(
52 std::array<std::size_t, sizeof...(TensorIndex)> const natural_ids)
53 {
54 return std::numeric_limits<std::size_t>::max();
55 }
56
57 KOKKOS_FUNCTION static constexpr std::size_t access_id(
58 std::array<std::size_t, sizeof...(TensorIndex)> const natural_ids)
59 {
60 if (!std::all_of(natural_ids.begin(), natural_ids.end(), [&](const std::size_t id) {
61 return id == *natural_ids.begin();
62 })) {
63 return 0;
64 } else {
65 return 1;
66 }
67 }
68
69 KOKKOS_FUNCTION static constexpr std::size_t access_id_to_mem_id(
70 [[maybe_unused]] std::size_t access_id)
71 {
72 return std::numeric_limits<std::size_t>::max();
73 }
74
75 template <class Tensor, class Elem, class Id, class FunctorType>
76 KOKKOS_FUNCTION static constexpr Tensor::element_type process_access(
77 const FunctorType& access,
78 Tensor tensor,
79 Elem elem)
80 {
81 if (elem.template uid<Id>() == 0) {
82 return 0.;
83 } else {
84 return access(tensor, elem);
85 }
86 }
87
88 KOKKOS_FUNCTION static constexpr std::array<std::size_t, rank()>
90 {
91 assert(false);
92 std::array<std::size_t, rank()> ids;
93 for (auto i = ids.begin(); i < ids.end(); ++i) {
94 *i = 0;
95 }
96 return ids;
97 }
98};
99
100} // namespace tensor
101
102} // 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 size()
static KOKKOS_FUNCTION constexpr Tensor::element_type process_access(const FunctorType &access, Tensor tensor, Elem elem)
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 rank()
static constexpr bool is_tensor_index
static KOKKOS_FUNCTION constexpr std::size_t access_id(std::array< std::size_t, sizeof...(TensorIndex)> const natural_ids)
ddc::DiscreteDomain< TensorIndex... > 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 KOKKOS_FUNCTION constexpr std::size_t mem_id(std::array< std::size_t, sizeof...(TensorIndex)> const natural_ids)
static KOKKOS_FUNCTION constexpr std::size_t mem_size()
static constexpr bool is_explicitely_stored_tensor
static KOKKOS_FUNCTION constexpr std::size_t access_size()