18#include <initializer_list>
26template <std::
size_t N>
using TensorArray = std::array<torch::Tensor, N>;
39template <
typename... Ts>
42 return {
to_tensor(std::forward<std::initializer_list<Ts>>(lists),
49template <
typename... Ts>
51to_tensorArray(torch::IntArrayRef sizes, std::initializer_list<Ts> &&...lists) {
52 return {
to_tensor(std::forward<std::initializer_list<Ts>>(lists), sizes,
60template <
typename... Ts,
typename T>
63 std::initializer_list<Ts> &&...lists) {
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)...};
75template <
typename... Ts,
typename T>
78 std::initializer_list<Ts> &&...lists) {
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,
94template <
typename T, std::
size_t N>
96 return tensor.accessor<T, N>();
107template <
typename T, std::
size_t N>
109 c10::DeviceType deviceType) {
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);
116 auto accessor = tensor.accessor<T, N>();
117 return std::tuple(tensor, accessor);
131template <
typename T, std::size_t N, std::size_t... Is>
133 std::index_sequence<Is...>) {
134 return std::array<torch::TensorAccessor<T, N>,
sizeof...(Is)>{
135 tensorArray[Is].template accessor<T, N>()...};
145template <
typename T, std::size_t N, std::size_t... Is>
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);
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);
183template <
typename T, std::
size_t N, std::
size_t M>
185 return detail::to_tensorAccessor<T, N>(tensorArray,
186 std::make_index_sequence<M>());
196template <
typename T, std::
size_t N, std::
size_t M>
198 c10::DeviceType deviceType) {
199 return detail::to_tensorAccessor<T, N>(tensorArray, deviceType,
200 std::make_index_sequence<M>());
210template <
typename T, std::size_t N, std::size_t... Dims>
212 c10::DeviceType deviceType) {
214 blocktensor, deviceType, std::make_index_sequence<(Dims * ...)>());
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__); \
235template <std::
size_t N>
237 const std::array<torch::Tensor, N> &obj) {
238 at::optional<std::string> name_ = c10::demangle(
typeid(obj).name());
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);
249 os << *name_ <<
"(\n";
250 for (std::size_t i = 0; i < N; ++i) {
251 os << obj[i] <<
"\n";
254 os <<
"[ " << obj[i].options() <<
" ]\n";
The Options class handles the automated determination of dtype from the template argument and the sel...
Definition options.hpp:47
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: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
std::ostream & operator<<(std::ostream &os, const std::array< T, N > &obj)
Prints a std::array of generic objects.
Definition core.hpp:887