Uni20
Asynchronous tensor and runtime library.
Loading...
Searching...
No Matches
tensor/copy.hpp
Go to the documentation of this file.
1#pragma once
2
13
14#include <concepts>
15#include <cstddef>
16#include <type_traits>
17#include <utility>
18
19namespace uni20
20{
28template <class RequestedLayout = void, class BackendSelector, MdspanLike InputMdspan>
29[[nodiscard]] auto make_tensor(BackendSelector&& selector, InputMdspan&& input)
30{
31 using input_type = std::remove_cvref_t<InputMdspan>;
32 using layout_type = detail::materialized_layout_t<RequestedLayout, input_type>;
33 using result_type =
35
36 result_type result(uninitialized, convert_tensor_extents<typename result_type::extents_type>(input.extents()));
37 auto output_span = result.mdspan();
38 copy(std::forward<BackendSelector>(selector), output_span, std::forward<InputMdspan>(input));
39 return result;
40}
41
47template <class RequestedLayout = void, TensorView InputTensor> [[nodiscard]] auto make_tensor(InputTensor const& input)
48{
49 using input_mdspan = tensor_mdspec_t<InputTensor>;
50 using layout_type = detail::materialized_layout_t<RequestedLayout, input_mdspan>;
51 using result_type = Tensor<tensor_element_t<InputTensor>, input_mdspan::rank(), HostStorage, layout_type>;
52
53 return result_type(input);
54}
55
56#if UNI20_BACKEND_CUDA
61template <TensorView InputTensor>
62 requires(std::same_as<tensor_storage_policy_t<InputTensor>, CudaStorage> &&
63 detail::CanonicalReshapeLayout<typename tensor_mdspec_t<InputTensor>::layout_type>)
64[[nodiscard]] auto to_host(InputTensor const& input)
65{
66 using input_mdspan = tensor_mdspec_t<InputTensor>;
67 using layout_type = typename input_mdspan::layout_type;
68 using result_type = Tensor<tensor_element_t<InputTensor>, input_mdspan::rank(), HostStorage, layout_type>;
69
70 result_type result(uninitialized, convert_tensor_extents<typename result_type::extents_type>(input.extents()));
71 copy(result, input);
72 return result;
73}
74
79template <TensorView InputTensor>
80 requires detail::CanonicalReshapeLayout<typename tensor_mdspec_t<InputTensor>::layout_type>
81[[nodiscard]] auto to_device(InputTensor const& input, cuda::DeviceResources& resources)
82{
83 using input_mdspan = tensor_mdspec_t<InputTensor>;
84 using layout_type = typename input_mdspan::layout_type;
85 using result_type = Tensor<tensor_element_t<InputTensor>, input_mdspan::rank(), CudaStorage, layout_type>;
86
87 result_type result(uninitialized, resources,
88 convert_tensor_extents<typename result_type::extents_type>(input.extents()));
89 copy(result, input);
90 return result;
91}
92
94template <TensorView InputTensor>
95 requires detail::CanonicalReshapeLayout<typename tensor_mdspec_t<InputTensor>::layout_type>
96[[nodiscard]] auto to_device(InputTensor const& input, int device)
97{
98 return to_device(input, cuda::device_resources(device));
99}
100
102template <TensorView InputTensor>
103 requires detail::CanonicalReshapeLayout<typename tensor_mdspec_t<InputTensor>::layout_type>
104[[nodiscard]] auto to_device(InputTensor const& input, cuda::Device device)
105{
106 return to_device(input, device.ordinal());
107}
108#endif
109
117template <class RequestedLayout = void, TensorView InputTensor, std::integral... Extents>
118 requires(!OwningTensor<InputTensor> &&
119 (std::is_void_v<RequestedLayout> || detail::CanonicalReshapeLayout<RequestedLayout>) &&
120 (!std::is_void_v<RequestedLayout> || !StridedTensorView<InputTensor> ||
121 detail::CanonicalReshapeLayout<typename tensor_mdspec_t<InputTensor>::layout_type>))
122[[nodiscard]] auto reshape(InputTensor const& input, Extents... requested_extents)
123{
124 using input_layout = typename tensor_mdspec_t<InputTensor>::layout_type;
126 detail::CanonicalReshapeLayout<input_layout>)
127 {
128 auto view = reshape_view(input, requested_extents...);
129 return make_tensor<RequestedLayout>(view);
130 }
131 else
132 {
133 auto materialized = make_tensor<RequestedLayout>(input);
134 return reshape(std::move(materialized), requested_extents...);
135 }
136}
137
138} // namespace uni20
General-purpose owning tensor with immediate or deferred mdspan metadata.
Definition basic_tensor.hpp:109
TensorView whose mdspec is already an mdspan.
Definition tensor/concepts.hpp:100
TensorView whose readable mdspec is strided.
Definition tensor/concepts.hpp:153
Backend-dispatched copies into existing mdspan and tensor outputs.
Mdspan concept and accessor extensions for Uni20.
DeviceResources & device_resources()
Return canonical resources for the installed runtime's default device.
Definition runtime.cpp:820
Definition async/async.hpp:26
constexpr uninitialized_t uninitialized
Explicit allocation tag for overwrite outputs and scratch space.
Definition common/initialization.hpp:22
auto reshape_view(Span &&source, Extents... requested_extents)
Return a no-copy mdspan reshape preserving a canonical source layout.
Definition reshape.hpp:175
auto make_tensor(async::Async< InputTensor > const &input)
Materialize an async Tensor view as an independent owning host Tensor.
Definition linalg/async/copy.hpp:199
void copy(BackendSelector selector, async::Async< OutputTensor > &output, async::Async< InputTensor > const &input)
Schedule a Tensor copy through an explicit backend selector.
Definition linalg/async/copy.hpp:174
auto reshape(async::Async< InputTensor > const &input, Extents... requested_extents)
Schedule a preserving owning reshape of an async Tensor.
Definition tensor/async.hpp:156
std::remove_cvref_t< detail::normalized_const_mdspec_t< T > > tensor_mdspec_t
Normalized readable MdspecLike type exposed by a tensor-level object.
Definition tensor/concepts.hpp:123
Default pageable-host tensor storage policy.
Definition host_storage.hpp:316
Shape preparation contracts for tensor operation outputs.
General-purpose owning tensors with runtime extents.