IgANet
IGAnets - Isogeometric Analysis Networks
Loading...
Searching...
No Matches
tensorarray.hpp
Go to the documentation of this file.
1
15#pragma once
16
17#include <array>
18#include <initializer_list>
19
20#include <iganet/core/core.hpp>
23
24namespace iganet::utils {
25
26template <std::size_t N> using TensorArray = std::array<torch::Tensor, N>;
27
33
39template <typename... Ts>
40inline constexpr TensorArray<sizeof...(Ts)>
41to_tensorArray(std::initializer_list<Ts> &&...lists) {
42 return {to_tensor(std::forward<std::initializer_list<Ts>>(lists),
43 torch::IntArrayRef{-1}, Options<Ts>{})...};
44}
45
49template <typename... Ts>
50inline constexpr TensorArray<sizeof...(Ts)>
51to_tensorArray(torch::IntArrayRef sizes, std::initializer_list<Ts> &&...lists) {
52 return {to_tensor(std::forward<std::initializer_list<Ts>>(lists), sizes,
53 Options<Ts>{})...};
54}
55
60template <typename... Ts, typename T>
61inline constexpr TensorArray<sizeof...(Ts)>
63 std::initializer_list<Ts> &&...lists) {
64 static_assert(
65 (std::is_same_v<T, Ts> && ...),
66 "Type mismatch between Options<T> and std::initializer_list<Ts>");
67 return {to_tensor(std::forward<std::initializer_list<Ts>>(lists),
68 torch::IntArrayRef{-1}, options)...};
69}
70
75template <typename... Ts, typename T>
76inline constexpr TensorArray<sizeof...(Ts)>
77to_tensorArray(torch::IntArrayRef sizes, const iganet::Options<T> &options,
78 std::initializer_list<Ts> &&...lists) {
79 static_assert(
80 (std::is_same_v<T, Ts> && ...),
81 "Type mismatch between Options<T> and std::initializer_list<Ts>");
82 return {to_tensor(std::forward<std::initializer_list<Ts>>(lists), sizes,
83 options)...};
84}
86
94template <typename T, std::size_t N>
95auto to_tensorAccessor(const torch::Tensor &tensor) {
96 return tensor.accessor<T, N>();
97}
98
107template <typename T, std::size_t N>
108auto to_tensorAccessor(const torch::Tensor &tensor,
109 c10::DeviceType deviceType) {
110
111 if (deviceType != tensor.device().type()) {
112 auto tensor_device = tensor.to(deviceType);
113 auto accessor = tensor_device.accessor<T, N>();
114 return std::tuple(tensor_device, accessor);
115 } else {
116 auto accessor = tensor.accessor<T, N>();
117 return std::tuple(tensor, accessor);
118 }
119}
121
122namespace detail {
131template <typename T, std::size_t N, std::size_t... Is>
132auto to_tensorAccessor(const TensorArray<sizeof...(Is)> &tensorArray,
133 std::index_sequence<Is...>) {
134 return std::array<torch::TensorAccessor<T, N>, sizeof...(Is)>{
135 tensorArray[Is].template accessor<T, N>()...};
136}
137
145template <typename T, std::size_t N, std::size_t... Is>
146auto to_tensorAccessor(const TensorArray<sizeof...(Is)> &tensorArray,
147 c10::DeviceType deviceType, std::index_sequence<Is...>) {
148 std::array<torch::Tensor, sizeof...(Is)> tensorArray_device{
149 tensorArray[Is].to(deviceType)...};
150 std::array<torch::TensorAccessor<T, N>, sizeof...(Is)> accessors{
151 tensorArray_device[Is].template accessor<T, N>()...};
152 return std::tuple(tensorArray_device, accessors);
153}
154
163template <typename T, std::size_t N, size_t... Dims, std::size_t... Is>
165 c10::DeviceType deviceType, std::index_sequence<Is...>) {
166 std::array<torch::Tensor, sizeof...(Is)> tensorArray_device{
167 blocktensor[Is]->to(deviceType)...};
168 std::array<torch::TensorAccessor<T, N>, sizeof...(Is)> accessors{
169 tensorArray_device[Is].template accessor<T, N>()...};
170 return std::tuple(tensorArray_device, accessors);
171}
173} // namespace detail
174
183template <typename T, std::size_t N, std::size_t M>
184auto to_tensorAccessor(const TensorArray<M> &tensorArray) {
185 return detail::to_tensorAccessor<T, N>(tensorArray,
186 std::make_index_sequence<M>());
187}
188
196template <typename T, std::size_t N, std::size_t M>
197auto to_tensorAccessor(const TensorArray<M> &tensorArray,
198 c10::DeviceType deviceType) {
199 return detail::to_tensorAccessor<T, N>(tensorArray, deviceType,
200 std::make_index_sequence<M>());
201}
202
210template <typename T, std::size_t N, std::size_t... Dims>
212 c10::DeviceType deviceType) {
213 return detail::to_tensorAccessor<T, N, Dims...>(
214 blocktensor, deviceType, std::make_index_sequence<(Dims * ...)>());
215}
217
218} // namespace iganet::utils
219
220#define TENSORARRAY_FORALL(obj, func, ...) \
221 []<std::size_t N>(const ::iganet::utils::TensorArray<N> &tensorArray) { \
222 ::iganet::utils::TensorArray<N> result; \
223 for (std::size_t i = 0; i < N; ++i) \
224 result[i] = tensorArray[i].func(__VA_ARGS__); \
225 return result; \
226 }(obj)
227
228namespace std {
229
235template <std::size_t N>
236inline std::ostream &operator<<(std::ostream &os,
237 const std::array<torch::Tensor, N> &obj) {
238 at::optional<std::string> name_ = c10::demangle(typeid(obj).name());
239
240#if defined(_WIN32)
241 // Windows adds "struct" or "class" as a prefix.
242 if (name_->find("struct ") == 0) {
243 name_->erase(name_->begin(), name_->begin() + 7);
244 } else if (name_->find("class ") == 0) {
245 name_->erase(name_->begin(), name_->begin() + 6);
246 }
247#endif // defined(_WIN32)
248
249 os << *name_ << "(\n";
250 for (std::size_t i = 0; i < N; ++i) {
251 os << obj[i] << "\n";
252
253 if (iganet::is_verbose(os))
254 os << "[ " << obj[i].options() << " ]\n";
255 }
256
257 os << ")";
258
259 return os;
260}
261
262} // namespace std
The Options class handles the automated determination of dtype from the template argument and the sel...
Definition options.hpp:47
Container utility functions.
Core components.
auto to_tensorAccessor(const TensorArray< sizeof...(Is)> &tensorArray, std::index_sequence< Is... >)
Converts a std::array of torch::Tensor objects to an array of torch::TensorAccessor objects.
Definition tensorarray.hpp:132
Definition blocktensor.hpp:24
TensorArray< 4 > TensorArray4
Definition tensorarray.hpp:32
std::array< torch::Tensor, N > TensorArray
Definition tensorarray.hpp:26
constexpr TensorArray< sizeof...(Ts)> to_tensorArray(std::initializer_list< Ts > &&...lists)
Converts a set of std::initializer_list objects to a TensorArray object.
Definition tensorarray.hpp:41
auto to_tensor(const std::array< T, N > &array, torch::IntArrayRef sizes=torch::IntArrayRef{-1}, const iganet::Options< T > &options=iganet::Options< T >{})
Converts a std::array to torch::Tensor.
Definition container.hpp:79
TensorArray< 3 > TensorArray3
Definition tensorarray.hpp:31
TensorArray< 0 > TensorArray0
Definition tensorarray.hpp:28
TensorArray< 1 > TensorArray1
Definition tensorarray.hpp:29
auto to_tensorAccessor(const torch::Tensor &tensor)
Converts a torch::Tensor object to a torch::TensorAccessor object.
Definition tensorarray.hpp:95
TensorArray< 2 > TensorArray2
Definition tensorarray.hpp:30
Forward declaration of BlockTensor.
Definition blocktensor.hpp:47
bool is_verbose(std::ostream &os)
Tests whether verbose output is enabled on a stream.
Definition core.hpp:871
STL namespace.
std::ostream & operator<<(std::ostream &os, const std::array< T, N > &obj)
Prints a std::array of generic objects.
Definition core.hpp:887
Options.