NEML2 1.4.0
Loading...
Searching...
No Matches
BufferStore.h
1// Copyright 2024, UChicago Argonne, LLC
2// All Rights Reserved
3// Software Name: NEML2 -- the New Engineering material Model Library, version 2
4// By: Argonne National Laboratory
5// OPEN SOURCE LICENSE (MIT)
6//
7// Permission is hereby granted, free of charge, to any person obtaining a copy
8// of this software and associated documentation files (the "Software"), to deal
9// in the Software without restriction, including without limitation the rights
10// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11// copies of the Software, and to permit persons to whom the Software is
12// furnished to do so, subject to the following conditions:
13//
14// The above copyright notice and this permission notice shall be included in
15// all copies or substantial portions of the Software.
16//
17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23// THE SOFTWARE.
24
25#pragma once
26
27#include "neml2/base/NEML2Object.h"
28#include "neml2/base/OptionSet.h"
29#include "neml2/base/Storage.h"
30#include "neml2/tensors/TensorValue.h"
31
32// The following are not directly used by BufferStore itself.
33// We put them here so that derived classes can add expected options of these types.
34#include "neml2/base/CrossRef.h"
35#include "neml2/base/EnumSelection.h"
36
37namespace neml2
38{
41{
42public:
43 BufferStore(const OptionSet & options, NEML2Object * object);
44
48 {
49 return const_cast<BufferStore *>(this)->named_buffers();
50 }
53
55 template <typename T, typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
56 TensorValue<T> & get_buffer(const std::string & name);
57
58protected:
64 virtual void send_buffers_to(const torch::TensorOptions & options);
65
79 template <typename T, typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
80 const T & declare_buffer(const std::string & name, const T & rawval);
81
96 template <typename T, typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
97 const T & declare_buffer(const std::string & name, const std::string & input_option_name);
98
99private:
100 NEML2Object * _object;
101
107 const OptionSet _options;
108
111};
112
113template <typename T, typename>
114TensorValue<T> &
115BufferStore::get_buffer(const std::string & name)
116{
117 neml_assert(_object->host() == _object, "This method should only be called on the host model.");
118
119 auto base_ptr = _buffer_values.query_value(name);
120 neml_assert(base_ptr, "Buffer named ", name, " does not exist.");
121 auto ptr = dynamic_cast<TensorValue<T> *>(base_ptr);
122 neml_assert_dbg(ptr, "Internal error: Failed to cast buffer to a concrete type.");
123 return *ptr;
124}
125
126template <typename T, typename>
127const T &
128BufferStore::declare_buffer(const std::string & name, const T & rawval)
129{
130 if (_object->host() != _object)
131 return _object->host<BufferStore>()->declare_buffer(
132 _object->name() + buffer_name_separator() + name, rawval);
133
134 // If the buffer already exists, return its reference
135 if (_buffer_values.has_key(name))
136 return get_buffer<T>(name).value();
137
138 auto val = std::make_unique<TensorValue<T>>(rawval);
139 auto base_ptr = _buffer_values.set_pointer(name, std::move(val));
140 auto ptr = dynamic_cast<TensorValue<T> *>(base_ptr);
141 neml_assert(ptr, "Internal error: Failed to cast buffer to a concrete type.");
142 return ptr->value();
143}
144
145template <typename T, typename>
146const T &
147BufferStore::declare_buffer(const std::string & name, const std::string & input_option_name)
148{
149 if (_options.contains<T>(input_option_name))
150 return declare_buffer(name, _options.get<T>(input_option_name));
151 else if (_options.contains<CrossRef<T>>(input_option_name))
152 return declare_buffer(name, T(_options.get<CrossRef<T>>(input_option_name)));
153
154 throw NEMLException(
155 "Trying to register buffer named " + name + " from input option named " + input_option_name +
156 " of type " + utils::demangle(typeid(T).name()) +
157 ". Make sure you provided the correct buffer name, option name, and buffer type. Note that "
158 "the buffer type can either be a plain type, a cross-reference, or an interpolator.");
159}
160
161} // namespace neml2
Interface for object which can store buffers.
Definition BufferStore.h:41
virtual void send_buffers_to(const torch::TensorOptions &options)
Send all buffers to options.
Definition BufferStore.cxx:44
const Storage< std::string, TensorValueBase > & named_buffers() const
Definition BufferStore.h:47
const T & declare_buffer(const std::string &name, const T &rawval)
Declare a buffer.
Definition BufferStore.h:128
BufferStore(const OptionSet &options, NEML2Object *object)
Definition BufferStore.cxx:29
TensorValue< T > & get_buffer(const std::string &name)
}@
Definition BufferStore.h:115
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:56
The base class of all "manufacturable" objects in the NEML2 library.
Definition NEML2Object.h:38
const std::string & name() const
A readonly reference to the object's name.
Definition NEML2Object.h:65
const T * host() const
Get a readonly pointer to the host.
Definition NEML2Object.h:90
Definition error.h:33
A custom map-like data structure. The keys are strings, and the values can be nonhomogeneously typed.
Definition OptionSet.h:100
std::string demangle(const char *name)
Demangle a piece of cxx abi type information.
Definition utils.cxx:33
Definition CrossRef.cxx:30
void neml_assert_dbg(bool assertion, Args &&... args)
Definition error.h:76
std::string & buffer_name_separator()
Default nested buffer name separator.
Definition types.cxx:88
void neml_assert(bool assertion, Args &&... args)
Definition error.h:64