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 "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{
24 using Base = Tensor<ElementType, DomainType, LayoutStridedPolicy, MemorySpace>;
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:
39 KOKKOS_FUNCTION explicit OwningTensor(Base tensor, Storage&& storage)
40 : Base(tensor)
41 , m_storage(std::move(storage))
42 {
43 rebind_to_owned_storage(tensor.domain());
44 }
45
46 KOKKOS_FUNCTION OwningTensor(OwningTensor const& other)
47 : Base(static_cast<Base const&>(other))
48 , m_storage(other.m_storage)
49 {
50 rebind_to_owned_storage(other.domain());
51 }
52
53 KOKKOS_FUNCTION OwningTensor(OwningTensor&& other)
54 : Base(static_cast<Base&&>(other))
55 , m_storage(std::move(other.m_storage))
56 {
57 rebind_to_owned_storage(other.domain());
58 }
59
60 KOKKOS_FUNCTION OwningTensor& operator=(OwningTensor const& other)
61 {
62 m_storage = other.m_storage;
63 rebind_to_owned_storage(other.domain());
64 return *this;
65 }
66
67 KOKKOS_FUNCTION OwningTensor& operator=(OwningTensor&& other)
68 {
69 m_storage = std::move(other.m_storage);
70 rebind_to_owned_storage(other.domain());
71 return *this;
72 }
73
74 KOKKOS_FUNCTION Storage& storage() noexcept
75 {
76 return m_storage;
77 }
78
79 KOKKOS_FUNCTION Storage const& storage() const noexcept
80 {
81 return m_storage;
82 }
83};
84
85template <class TensorType, class Storage>
87
88} // namespace sil::tensor
OwningTensor(TensorType, Storage &&) -> OwningTensor< TensorType, Storage >