NEML2 2.0.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 <map>
28
29#include <torch/csrc/jit/frontend/tracer.h>
30
31#include "neml2/misc/types.h"
32
33namespace neml2
34{
35class NEML2Object;
36template <typename T>
37struct TensorName;
38class TensorValueBase;
39template <typename T>
40class TensorBase;
41
42namespace jit
43{
44using namespace torch::jit;
45}
46
49{
50public:
51 BufferStore(NEML2Object * object);
52
53 BufferStore(const BufferStore &) = delete;
55 BufferStore & operator=(const BufferStore &) = delete;
57 virtual ~BufferStore() = default;
58
61 const std::map<std::string, std::unique_ptr<TensorValueBase>> & named_buffers() const
62 {
63 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
64 return const_cast<BufferStore *>(this)->named_buffers();
65 }
66 std::map<std::string, std::unique_ptr<TensorValueBase>> & named_buffers();
68
70 const TensorValueBase & get_buffer(const std::string & name) const
71 {
72 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
73 return const_cast<BufferStore *>(this)->get_buffer(name);
74 }
76 TensorValueBase & get_buffer(const std::string & name);
77
78protected:
84 virtual void send_buffers_to(const TensorOptions & options);
85
99 template <typename T, typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
100 const T & declare_buffer(const std::string & name, const T & rawval);
101
113 template <typename T, typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
114 const T & declare_buffer(const std::string & name, const TensorName<T> & tensorname);
115
128 template <typename T, typename = typename std::enable_if_t<std::is_base_of_v<TensorBase<T>, T>>>
129 const T & declare_buffer(const std::string & name, const std::string & input_option_name);
130
132 void assign_buffer_stack(jit::Stack & stack);
133
135 jit::Stack collect_buffer_stack() const;
136
137private:
138 NEML2Object * _object;
139
141 std::map<std::string, std::unique_ptr<TensorValueBase>> _buffer_values;
142
144 std::map<std::string, Size> _buffer_intmd_dims;
145};
146
147} // namespace neml2
Interface for object which can store buffers.
Definition BufferStore.h:49
virtual ~BufferStore()=default
BufferStore(const BufferStore &)=delete
void assign_buffer_stack(jit::Stack &stack)
Assign stack to buffers.
Definition BufferStore.cxx:117
const T & declare_buffer(const std::string &name, const T &rawval)
Declare a buffer.
Definition BufferStore.cxx:65
const std::map< std::string, std::unique_ptr< TensorValueBase > > & named_buffers() const
Definition BufferStore.h:61
const TensorValueBase & get_buffer(const std::string &name) const
}@
Definition BufferStore.h:70
virtual void send_buffers_to(const TensorOptions &options)
Send all buffers to options.
Definition BufferStore.cxx:57
jit::Stack collect_buffer_stack() const
Collect stack from buffers.
Definition BufferStore.cxx:141
BufferStore & operator=(const BufferStore &)=delete
BufferStore & operator=(BufferStore &&)=delete
BufferStore(BufferStore &&)=delete
BufferStore(NEML2Object *object)
Definition BufferStore.cxx:35
The base class of all "manufacturable" objects in the NEML2 library.
Definition NEML2Object.h:51
The base class to allow us to set up a polymorphic container of Tensors. The concrete definitions wil...
Definition TensorValue.h:40
Definition DiagnosticsInterface.cxx:30
std::string name(ElasticConstant p)
Definition ElasticityConverter.cxx:30
c10::TensorOptions TensorOptions
Definition types.h:60
The name of a tensor object that can be referenced in the input files.
Definition VariableStore.h:43