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 <iganet/core/dtype.hpp>
18#include <iganet/utils/fqn.hpp>
20
21namespace iganet {
22
28inline int guess_device_index() {
29#ifdef IGANET_WITH_MPI
30 int rank;
31 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
32 return rank %
33 utils::getenv("IGANET_DEVICE_COUNT", (torch::cuda::is_available()
34 ? torch::cuda::device_count()
35 : (torch::xpu::is_available() ? torch::xpu::device_count() ? 1)));
36#else
37 return 0;
38#endif
39}
40
45template <typename real_t>
46 requires DType<real_t>
48public:
51 : options_(
53 .dtype(::iganet::dtype_v<real_t>)
54 .device_index(utils::getenv("IGANET_DEVICE_INDEX",
56 .device(
57 (utils::getenv("IGANET_DEVICE", std::string{}) == "CPU")
58 ? torch::kCPU
59 : (utils::getenv("IGANET_DEVICE", std::string{}) == "CUDA")
60 ? torch::kCUDA
61 : (utils::getenv("IGANET_DEVICE", std::string{}) == "HIP")
62 ? torch::kHIP
63 : (utils::getenv("IGANET_DEVICE", std::string{}) == "MPS")
64 ? torch::kMPS
65 : (utils::getenv("IGANET_DEVICE", std::string{}) == "XLA")
66 ? torch::kXLA
67 : (utils::getenv("IGANET_DEVICE", std::string{}) == "XPU")
68 ? torch::kXPU
69 : (torch::cuda::is_available()
70 ? torch::kCUDA
71 : (torch::xpu::is_available() ? torch::kXPU
72 : torch::kCPU)))) {
73 }
74
77 explicit Options(torch::TensorOptions &&options)
78 : options_(options.dtype(::iganet::dtype_v<real_t>)) {}
79
86 operator torch::TensorOptions() const { return options_; }
87
90 inline torch::Device device() const noexcept { return options_.device(); }
91
94 inline int32_t device_index() const noexcept {
95 return options_.device_index();
96 }
97
100 static inline torch::Dtype dtype() noexcept {
101 return ::iganet::dtype_v<real_t>;
102 }
103
106 inline torch::Layout layout() const noexcept { return options_.layout(); }
107
110 inline bool requires_grad() const noexcept {
111 return options_.requires_grad();
112 }
113
116 inline bool pinned_memory() const noexcept {
117 return options_.pinned_memory();
118 }
119
122 inline bool is_sparse() const noexcept { return options_.is_sparse(); }
123
127 inline Options<real_t> device(torch::Device device) const noexcept {
128 return Options(options_.device(device));
129 }
130
135 inline Options<real_t> device_index(int16_t device_index) const noexcept {
136 return Options(options_.device_index(device_index));
137 }
138
142 template <typename other_t> inline Options<other_t> dtype() const noexcept {
143 return Options<other_t>(options_.dtype(::iganet::dtype_v<other_t>));
144 }
145
149 inline Options<real_t> layout(torch::Layout layout) const noexcept {
150 return Options(options_.layout(layout));
151 }
152
157 inline Options<real_t> requires_grad(bool requires_grad) const noexcept {
158 return Options(options_.requires_grad(requires_grad));
159 }
160
165 inline Options<real_t> pinned_memory(bool pinned_memory) const noexcept {
166 return Options(options_.pinned_memory(pinned_memory));
167 }
168
173 inline Options<real_t>
174 memory_format(torch::MemoryFormat memory_format) const noexcept {
175 return Options(options_.memory_format(memory_format));
176 }
177
179 using value_type = real_t;
180
183 inline void pretty_print(std::ostream &os) const noexcept override {
184 os << name() << "(\noptions = " << options_ << "\n)";
185 }
186
187private:
189 const torch::TensorOptions options_;
190};
191
197template <typename real_t>
198inline std::ostream &operator<<(std::ostream &os, const Options<real_t> &obj) {
199 obj.pretty_print(os);
200 return os;
201}
202
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:47
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:174
Options()
Default constructor.
Definition options.hpp:50
static torch::Dtype dtype() noexcept
Returns the dtype property.
Definition options.hpp:100
Options(torch::TensorOptions &&options)
Constructor from torch::TensorOptions.
Definition options.hpp:77
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:157
torch::Device device() const noexcept
Returns the device property.
Definition options.hpp:90
void pretty_print(std::ostream &os) const noexcept override
Returns a string representation of the Options object.
Definition options.hpp:183
bool requires_grad() const noexcept
Returns the requires_grad property.
Definition options.hpp:110
bool is_sparse() const noexcept
Returns whether the layout is sparse.
Definition options.hpp:122
operator torch::TensorOptions() const
Implicit conversion operator.
Definition options.hpp:86
Options< real_t > device(torch::Device device) const noexcept
Returns a new Options object with the device property as given.
Definition options.hpp:127
int32_t device_index() const noexcept
Returns the device_index property.
Definition options.hpp:94
real_t value_type
Data type.
Definition options.hpp:179
torch::Layout layout() const noexcept
Returns the layout property.
Definition options.hpp:106
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:135
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:165
const torch::TensorOptions options_
Tensor options.
Definition options.hpp:189
Options< other_t > dtype() const noexcept
Returns a new Options object with the dtype property as given.
Definition options.hpp:142
Options< real_t > layout(torch::Layout layout) const noexcept
Returns a new Options object with the layout property as given.
Definition options.hpp:149
bool pinned_memory() const noexcept
Returns the pinned_memory property.
Definition options.hpp:116
Full qualified name descriptor.
Definition fqn.hpp:22
virtual const std::string & name() const noexcept
Returns the full qualified name of the object.
Definition fqn.hpp:28
DType traits.
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:28
Definition core.hpp:73
int guess_device_index()
Guesses the accelerator device index for the current process.
Definition options.hpp:28
std::ostream & operator<<(std::ostream &os, const MemoryDebugger< id > &obj)
Prints a memory debugger object.
Definition memory.hpp:145
constexpr torch::Dtype dtype_v
Determines the LibTorch dtype from a template parameter.
Definition dtype.hpp:75
STL namespace.
Definition optimizer.hpp:61