IgANet
IgANets - Isogeometric Analysis Networks
Loading...
Searching...
No Matches
options.hpp
Go to the documentation of this file.
1
15#pragma once
16
17#include <core.hpp>
18#include <utils/fqn.hpp>
19#include <utils/getenv.hpp>
20
21#include <torch/torch.h>
22
23namespace iganet {
24
25struct half {};
26
33template <typename T> inline constexpr torch::Dtype dtype();
34
35template <> inline constexpr torch::Dtype dtype<bool>() { return torch::kBool; }
36
37template <> inline constexpr torch::Dtype dtype<char>() { return torch::kChar; }
38
39template <> inline constexpr torch::Dtype dtype<short>() {
40 return torch::kShort;
41}
42
43template <> inline constexpr torch::Dtype dtype<int>() { return torch::kInt; }
44
45 template <> inline constexpr torch::Dtype dtype<long>() { return torch::kLong; }
46
47 template <> inline constexpr torch::Dtype dtype<long long>() { return torch::kLong; }
48
49template <> inline constexpr torch::Dtype dtype<half>() { return torch::kHalf; }
50
51template <> inline constexpr torch::Dtype dtype<float>() {
52 return torch::kFloat;
53}
54
55template <> inline constexpr torch::Dtype dtype<double>() {
56 return torch::kDouble;
57}
58
59template <> inline constexpr torch::Dtype dtype<std::complex<half>>() {
60 return at::kComplexHalf;
61}
62
63template <> inline constexpr torch::Dtype dtype<std::complex<float>>() {
64 return at::kComplexFloat;
65}
66
67template <> inline constexpr torch::Dtype dtype<std::complex<double>>() {
68 return at::kComplexDouble;
69}
71
72inline int guess_device_index() {
73#ifdef IGANET_WITH_MPI
74 int rank;
76 return rank %
77 utils::getenv("IGANET_DEVICE_COUNT", (torch::cuda::is_available()
78 ? torch::cuda::device_count()
79 : (torch::xpu::is_available() ? torch::xpu::device_count() ? 1)));
80#else
81 return 0;
82#endif
83}
84
89template <typename real_t>
91public:
94 : options_(
95 torch::TensorOptions()
96 .dtype(::iganet::dtype<real_t>())
97 .device_index(utils::getenv("IGANET_DEVICE_INDEX",
99 .device(
100 (utils::getenv("IGANET_DEVICE", std::string{}) == "CPU")
101 ? torch::kCPU
102 : (utils::getenv("IGANET_DEVICE", std::string{}) == "CUDA")
103 ? torch::kCUDA
104 : (utils::getenv("IGANET_DEVICE", std::string{}) == "HIP")
105 ? torch::kHIP
106 : (utils::getenv("IGANET_DEVICE", std::string{}) == "MPS")
107 ? torch::kMPS
108 : (utils::getenv("IGANET_DEVICE", std::string{}) == "XLA")
109 ? torch::kXLA
110 : (utils::getenv("IGANET_DEVICE", std::string{}) == "XPU")
111 ? torch::kXPU
112 : (torch::cuda::is_available() ? torch::kCUDA
113 : (torch::xpu::is_available() ? torch::kXPU : torch::kCPU)))) {}
114
116 explicit Options(torch::TensorOptions &&options)
117 : options_(options.dtype(::iganet::dtype<real_t>())) {}
118
120 operator torch::TensorOptions() const { return options_; }
121
123 torch::Device device() const noexcept { return options_.device(); }
124
126 int32_t device_index() const noexcept { return options_.device_index(); }
127
129 torch::Dtype dtype() const noexcept { return ::iganet::dtype<real_t>(); }
130
132 torch::Layout layout() const noexcept { return options_.layout(); }
133
135 bool requires_grad() const noexcept { return options_.requires_grad(); }
136
138 bool pinned_memory() const noexcept { return options_.pinned_memory(); }
139
141 bool is_sparse() const noexcept { return options_.is_sparse(); }
142
144 Options<real_t> device(torch::Device device) const noexcept {
145 return Options(options_.device(device));
146 }
147
151 return Options(options_.device_index(device_index));
152 }
153
155 template <typename other_t> Options<other_t> dtype() const noexcept {
157 }
158
160 Options<real_t> layout(torch::Layout layout) const noexcept {
161 return Options(options_.layout(layout));
162 }
163
167 return Options(options_.requires_grad(requires_grad));
168 }
169
173 return Options(options_.pinned_memory(pinned_memory));
174 }
175
179 memory_format(torch::MemoryFormat memory_format) const noexcept {
180 return Options(options_.memory_format(memory_format));
181 }
182
184 using value_type = real_t;
185
187 inline virtual void
188 pretty_print(std::ostream &os = Log(log::info)) const noexcept override {
189 os << name() << "(\noptions = " << options_ << "\n)";
190 }
191
192private:
194 const torch::TensorOptions options_;
195};
196
198template <typename real_t>
199inline std::ostream &operator<<(std::ostream &os, const Options<real_t> &obj) {
200 obj.pretty_print(os);
201 return os;
202}
203
205template <typename real_t>
206class Options<Options<real_t>> : public Options<real_t> {
207 using Options<real_t>::Options;
208};
209
210} // namespace iganet
The Options class handles the automated determination of dtype from the template argument and the sel...
Definition options.hpp:90
Options< real_t > memory_format(torch::MemoryFormat memory_format) const noexcept
Returns a new Options object with the memory_format property as given.
Definition options.hpp:179
Options()
Default constructor.
Definition options.hpp:93
Options(torch::TensorOptions &&options)
Constructor from torch::TensorOptions.
Definition options.hpp:116
Options< real_t > requires_grad(bool requires_grad) const noexcept
Returns a new Options object with the requires_grad property as given.
Definition options.hpp:166
torch::Device device() const noexcept
Returns the device property.
Definition options.hpp:123
bool requires_grad() const noexcept
Returns the requires_grad property.
Definition options.hpp:135
bool is_sparse() const noexcept
Returns if the layout is sparse.
Definition options.hpp:141
operator torch::TensorOptions() const
Implicit conversion operator.
Definition options.hpp:120
Options< real_t > device(torch::Device device) const noexcept
Returns a new Options object with the device property as given.
Definition options.hpp:144
int32_t device_index() const noexcept
Returns the device_index property.
Definition options.hpp:126
real_t value_type
Data type.
Definition options.hpp:184
torch::Layout layout() const noexcept
Returns the layout property.
Definition options.hpp:132
Options< real_t > device_index(int16_t device_index) const noexcept
Returns a new Options object with the device_index property as given.
Definition options.hpp:150
Options< real_t > pinned_memory(bool pinned_memory) const noexcept
Returns a new Options object with the pinned_memory property as given.
Definition options.hpp:172
virtual void pretty_print(std::ostream &os=Log(log::info)) const noexcept override
Returns a string representation of the Options object.
Definition options.hpp:188
const torch::TensorOptions options_
Tensor options.
Definition options.hpp:194
torch::Dtype dtype() const noexcept
Returns the dtype property.
Definition options.hpp:129
Options< other_t > dtype() const noexcept
Returns a new Options object with the dtype property as given.
Definition options.hpp:155
Options< real_t > layout(torch::Layout layout) const noexcept
Returns a new Options object with the layout property as given.
Definition options.hpp:160
bool pinned_memory() const noexcept
Returns the pinned_memory property.
Definition options.hpp:138
Full qualified name descriptor.
Definition fqn.hpp:26
virtual const std::string & name() const noexcept
Returns the full qualified name of the object.
Definition fqn.hpp:31
Core components.
Full qualified name utility functions.
Environment utility function.
T getenv(std::string variable, const T &default_value)
Returns the value from an environment variable.
Definition getenv.hpp:24
Definition boundary.hpp:22
constexpr torch::Dtype dtype< char >()
Definition options.hpp:37
constexpr torch::Dtype dtype()
constexpr torch::Dtype dtype< float >()
Definition options.hpp:51
constexpr torch::Dtype dtype< bool >()
Definition options.hpp:35
constexpr torch::Dtype dtype< double >()
Definition options.hpp:55
int guess_device_index()
Definition options.hpp:72
constexpr torch::Dtype dtype< long long >()
Definition options.hpp:47
constexpr bool is_SplineType_v
Alias to the value of is_SplineType.
Definition bspline.hpp:3243
constexpr torch::Dtype dtype< half >()
Definition options.hpp:49
std::ostream & operator<<(std::ostream &os, const Boundary< Spline > &obj)
Print (as string) a Boundary object.
Definition boundary.hpp:1978
struct iganet::@0 Log
Logger.
constexpr torch::Dtype dtype< int >()
Definition options.hpp:43
constexpr torch::Dtype dtype< short >()
Definition options.hpp:39
constexpr torch::Dtype dtype< long >()
Definition options.hpp:45
Definition options.hpp:25
STL namespace.