SimiLie
Loading...
Searching...
No Matches
owning_tensor.hpp
1// SPDX-FileCopyrightText: 2026 Baptiste Legouix
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4#pragma once
5
6#include <utility>
7
8#include <similie/tensor/tensor_impl.hpp>
9
10namespace sil::tensor {
11
12template <class TensorType, class Storage>
14
15template <
16 class ElementType,
17 class DomainType,
18 class LayoutStridedPolicy,
19 class MemorySpace,
20 class Storage>
21class OwningTensor<Tensor<ElementType, DomainType, LayoutStridedPolicy, MemorySpace>, Storage>
22 : public Tensor<ElementType, DomainType, LayoutStridedPolicy, MemorySpace>
23{
25
26 Storage m_storage;
27
28 KOKKOS_FUNCTION void rebind_to_owned_storage(DomainType const& domain)
29 {
30 static_cast<Base&>(*this)
31 = Base(ddc::ChunkSpan<
32 ElementType,
33 DomainType,
34 LayoutStridedPolicy,
35 MemorySpace>(m_storage.data(), domain));
36 }
37
38public:
40
41 KOKKOS_FUNCTION explicit OwningTensor(Base const& tensor, Storage&& storage)
42 : Base(tensor)
43 , m_storage(std::move(storage))
44 {
45 rebind_to_owned_storage(tensor.domain());
46 }
47
48 KOKKOS_FUNCTION OwningTensor(OwningTensor const& other)
49 : Base(static_cast<Base const&>(other))
50 , m_storage(other.m_storage)
51 {
52 rebind_to_owned_storage(other.domain());
53 }
54
55 KOKKOS_FUNCTION OwningTensor(OwningTensor&& other)
56 : Base(static_cast<Base&&>(other))
57 , m_storage(std::move(other.m_storage))
58 {
59 rebind_to_owned_storage(other.domain());
60 }
61
62 KOKKOS_FUNCTION OwningTensor& operator=(OwningTensor const& other)
63 {
64 m_storage = other.m_storage;
65 rebind_to_owned_storage(other.domain());
66 return *this;
67 }
68
69 KOKKOS_FUNCTION OwningTensor& operator=(OwningTensor&& other)
70 {
71 m_storage = std::move(other.m_storage);
72 rebind_to_owned_storage(other.domain());
73 return *this;
74 }
75
76 KOKKOS_FUNCTION Storage& storage() noexcept
77 {
78 return m_storage;
79 }
80
81 KOKKOS_FUNCTION Storage const& storage() const noexcept
82 {
83 return m_storage;
84 }
85};
86
87template <class TensorType, class Storage>
89
90} // namespace sil::tensor
OwningTensor(TensorType, Storage &&) -> OwningTensor< TensorType, Storage >