18#include <initializer_list>
26template <std::
size_t N>
using TensorArray = std::array<torch::Tensor, N>;
37template <
typename... Ts>
40 return {
to_tensor(std::forward<std::initializer_list<Ts>>(lists),
44template <
typename... Ts>
46to_tensorArray(torch::IntArrayRef sizes, std::initializer_list<Ts> &&...lists) {
47 return {
to_tensor(std::forward<std::initializer_list<Ts>>(lists), sizes,
51template <
typename... Ts,
typename T>
54 std::initializer_list<Ts> &&...lists) {
56 (std::is_same_v<T, Ts> && ...),
57 "Type mismatch between Options<T> and std::initializer_list<Ts>");
58 return {
to_tensor(std::forward<std::initializer_list<Ts>>(lists),
59 torch::IntArrayRef{-1}, options)...};
62template <
typename... Ts,
typename T>
65 std::initializer_list<Ts> &&...lists) {
67 (std::is_same_v<T, Ts> && ...),
68 "Type mismatch between Options<T> and std::initializer_list<Ts>");
69 return {
to_tensor(std::forward<std::initializer_list<Ts>>(lists), sizes,
77template <
typename T, std::
size_t N>
79 return tensor.accessor<T, N>();
82template <
typename T, std::
size_t N>
84 c10::DeviceType deviceType) {
86 if (deviceType != tensor.device().type()) {
87 auto tensor_device = tensor.to(deviceType);
88 auto accessor = tensor_device.accessor<T, N>();
89 return std::tuple(tensor_device, accessor);
91 auto accessor = tensor.accessor<T, N>();
92 return std::tuple(tensor, accessor);
101template <
typename T, std::size_t N, std::size_t... Is>
103 std::index_sequence<Is...>) {
104 return std::array<torch::TensorAccessor<T, N>,
sizeof...(Is)>{
105 tensorArray[Is].template accessor<T, N>()...};
108template <
typename T, std::size_t N, std::size_t... Is>
110 c10::DeviceType deviceType, std::index_sequence<Is...>) {
111 std::array<torch::TensorBase,
sizeof...(Is)> tensorArray_device{
112 tensorArray[Is].to(deviceType)...};
113 std::array<torch::TensorAccessor<T, N>,
sizeof...(Is)> accessors{
114 tensorArray_device[Is].template accessor<T, N>()...};
115 return std::tuple(tensorArray_device, accessors);
118template <
typename T, std::size_t N,
size_t... Dims, std::size_t... Is>
120 c10::DeviceType deviceType, std::index_sequence<Is...>) {
121 std::array<torch::TensorBase,
sizeof...(Is)> tensorArray_device{
122 blocktensor[Is]->to(deviceType)...};
123 std::array<torch::TensorAccessor<T, N>,
sizeof...(Is)> accessors{
124 tensorArray_device[Is].template accessor<T, N>()...};
125 return std::tuple(tensorArray_device, accessors);
133template <
typename T, std::
size_t N, std::
size_t M>
135 return detail::to_tensorAccessor<T, N>(tensorArray,
136 std::make_index_sequence<M>());
139template <
typename T, std::
size_t N, std::
size_t M>
141 c10::DeviceType deviceType) {
142 return detail::to_tensorAccessor<T, N>(tensorArray, deviceType,
143 std::make_index_sequence<M>());
146template <
typename T, std::size_t N, std::size_t... Dims>
148 c10::DeviceType deviceType) {
150 blocktensor, deviceType, std::make_index_sequence<(Dims * ...)>());
156#define TENSORARRAY_FORALL(obj, func, ...) \
157 []<std::size_t N>(const ::iganet::utils::TensorArray<N> &tensorArray) { \
158 ::iganet::utils::TensorArray<N> result; \
159 for (std::size_t i = 0; i < N; ++i) \
160 result[i] = tensorArray[i].func(__VA_ARGS__); \
167template <std::
size_t N>
169 const std::array<torch::Tensor, N> &obj) {
170 at::optional<std::string> name_ = c10::demangle(
typeid(obj).name());
174 if (name_->find(
"struct ") == 0) {
175 name_->erase(name_->begin(), name_->begin() + 7);
176 }
else if (name_->find(
"class ") == 0) {
177 name_->erase(name_->begin(), name_->begin() + 6);
181 os << *name_ <<
"(\n";
182 for (std::size_t i = 0; i < N; ++i) {
183 os << obj[i] <<
"\n";
186 os <<
"[ " << obj[i].options() <<
" ]\n";
The Options class handles the automated determination of dtype from the template argument and the sel...
Definition options.hpp:104
Container utility functions.
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:102
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:39
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:56
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:78
TensorArray< 2 > TensorArray2
Definition tensorarray.hpp:30
Forward declaration of BlockTensor.
Definition blocktensor.hpp:43
bool is_verbose(std::ostream &os)
Definition core.hpp:831
std::ostream & operator<<(std::ostream &os, const std::array< T, N > &obj)
Print (as string) a std::array of generic objects.
Definition core.hpp:842