NEML2 1.4.0
Loading...
Searching...
No Matches
BufferStore.h
1// Copyright 2023, 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
32namespace neml2
33{
36{
37public:
38 BufferStore(const OptionSet & options, NEML2Object * object);
39
43 {
44 return const_cast<BufferStore *>(this)->named_buffers();
45 }
48
50 template <typename T,
51 typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<T>, T>>>
52 T & get_buffer(const std::string & name);
53
54protected:
60 virtual void send_buffers_to(const torch::TensorOptions & options);
61
75 template <typename T,
76 typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<T>, T>>>
77 const T & declare_buffer(const std::string & name, const T & rawval);
78
93 template <typename T,
94 typename = typename std::enable_if_t<std::is_base_of_v<BatchTensorBase<T>, T>>>
95 const T & declare_buffer(const std::string & name, const std::string & input_option_name);
96
97private:
98 NEML2Object * _object;
99
105 const OptionSet _options;
106
109};
110
111template <typename T, typename>
112T &
113BufferStore::get_buffer(const std::string & name)
114{
115 neml_assert(_object->host() == _object, "This method should only be called on the host model.");
116
117 auto base_ptr = _buffer_values.query_value(name);
118 neml_assert(base_ptr, "Buffer named ", name, " does not exist.");
119 auto ptr = dynamic_cast<TensorValue<T> *>(base_ptr);
120 neml_assert_dbg(ptr, "Internal error: Failed to cast buffer to a concrete type.");
121 return ptr->value();
122}
123
124template <typename T, typename>
125const T &
126BufferStore::declare_buffer(const std::string & name, const T & rawval)
127{
128 if (_object->host() != _object)
129 return _object->host<BufferStore>()->declare_buffer(_object->name() + "." + name, rawval);
130
131 // If the buffer already exists, return its reference
132 if (_buffer_values.has_key(name))
133 return get_buffer<T>(name);
134
135 auto val = std::make_unique<TensorValue<T>>(rawval);
136 auto base_ptr = _buffer_values.set_pointer(name, std::move(val));
137 auto ptr = dynamic_cast<TensorValue<T> *>(base_ptr);
138 neml_assert(ptr, "Internal error: Failed to cast buffer to a concrete type.");
139 return ptr->value();
140}
141
142template <typename T, typename>
143const T &
144BufferStore::declare_buffer(const std::string & name, const std::string & input_option_name)
145{
146 if (_options.contains<T>(input_option_name))
147 return declare_buffer(name, _options.get<T>(input_option_name));
148 else if (_options.contains<CrossRef<T>>(input_option_name))
149 return declare_buffer(name, T(_options.get<CrossRef<T>>(input_option_name)));
150
151 throw NEMLException(
152 "Trying to register buffer named " + name + " from input option named " + input_option_name +
153 " of type " + utils::demangle(typeid(T).name()) +
154 ". Make sure you provided the correct buffer name, option name, and buffer type. Note that "
155 "the buffer type can either be a plain type, a cross-reference, or an interpolator.");
156}
157
158} // namespace neml2
Interface for object which can store buffers.
Definition BufferStore.h:36
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:42
const T & declare_buffer(const std::string &name, const T &rawval)
Declare a buffer.
Definition BufferStore.h:126
T & get_buffer(const std::string &name)
}@
Definition BufferStore.h:113
BufferStore(const OptionSet &options, NEML2Object *object)
Definition BufferStore.cxx:29
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:52
The base class of all "manufacturable" objects in the NEML2 library.
Definition NEML2Object.h:39
const std::string & name() const
A readonly reference to the object's name.
Definition NEML2Object.h:66
const T * host() const
Get a readonly pointer to the host.
Definition NEML2Object.h:91
Definition error.h:33
A custom map-like data structure. The keys are strings, and the values can be nonhomogeneously typed.
Definition OptionSet.h:59
std::string demangle(const char *name)
Definition parser_utils.cxx:46
Definition CrossRef.cxx:32
void neml_assert_dbg(bool assertion, Args &&... args)
Definition error.h:85
void neml_assert(bool assertion, Args &&... args)
Definition error.h:73