NEML2 2.1.0
Loading...
Searching...
No Matches
LabeledAxis.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 "neml2/misc/types.h"
30#include "neml2/base/LabeledAxisAccessor.h"
31#include "neml2/tensors/indexing.h"
32
33namespace neml2
34{
35class LabeledAxis;
36
48{
49public:
51 LabeledAxis() = default;
52
55
57 void clear();
58
60 bool is_setup() const { return _setup; }
61
64
67
69 LabeledAxis & add_subaxis(const std::string & name);
70
73 TensorShapeRef intmd_sizes,
74 TensorShapeRef base_sizes);
75
78
80 Size size() const;
81
84
86 indexing::Slice slice(const LabeledAxisAccessor & name) const;
87
91 std::size_t nvariable() const;
95 std::size_t variable_id(const LabeledAxisAccessor & name) const;
97 std::vector<LabeledAxisAccessor> variable_names_unsrt() const;
99 const std::vector<LabeledAxisAccessor> & variable_names() const;
101 const std::vector<std::pair<Size, Size>> & variable_slices() const;
103 const std::pair<Size, Size> & variable_slice(const LabeledAxisAccessor & name) const;
105 const std::vector<Size> & variable_sizes() const;
109 const std::vector<TensorShape> & variable_intmd_sizes() const;
111 const std::vector<TensorShape> & variable_base_sizes() const;
113
117 std::size_t nsubaxis() const;
121 std::size_t subaxis_id(const std::string & name) const;
123 const std::vector<const LabeledAxis *> & subaxes() const;
129 std::vector<std::string> subaxis_names_unsrt() const;
131 const std::vector<std::string> & subaxis_names() const;
133 const std::vector<std::pair<Size, Size>> & subaxis_slices() const;
135 std::pair<Size, Size> subaxis_slice(const LabeledAxisAccessor & name) const;
137 const std::vector<Size> & subaxis_sizes() const;
141
143 bool equals(const LabeledAxis & other) const;
144
145 friend std::ostream & operator<<(std::ostream & os, const LabeledAxis & axis);
146
147private:
149 void clear_data();
150
152 void ensure_setup_dbg() const;
153
155 bool _setup = false;
156
158 const LabeledAxisAccessor _prefix = {};
159
161 Size _size = 0;
162
164 std::map<std::string, std::pair<TensorShape, TensorShape>> _variables;
165
167 std::map<std::string, std::shared_ptr<LabeledAxis>> _subaxes;
168
177 std::map<LabeledAxisAccessor, std::size_t> _variable_to_id_map;
179 std::vector<LabeledAxisAccessor> _id_to_variable_map;
181 std::vector<Size> _id_to_variable_size_map;
183 std::vector<std::pair<Size, Size>> _id_to_variable_slice_map;
185 std::vector<TensorShape> _id_to_intmd_sizes_map;
187 std::vector<TensorShape> _id_to_base_sizes_map;
189
198 std::vector<const LabeledAxis *> _sorted_subaxes;
200 std::map<std::string, std::size_t> _subaxis_to_id_map;
202 std::vector<std::string> _id_to_subaxis_map;
204 std::vector<Size> _id_to_subaxis_size_map;
206 std::vector<std::pair<Size, Size>> _id_to_subaxis_slice_map;
208};
209
210std::ostream & operator<<(std::ostream & os, const LabeledAxis & axis);
211
212bool operator==(const LabeledAxis & a, const LabeledAxis & b);
213
214bool operator!=(const LabeledAxis & a, const LabeledAxis & b);
215} // namespace neml2
The accessor containing all the information needed to access an item in a LabeledAxis.
Definition LabeledAxisAccessor.h:56
A labeled axis is used to associate layout of a tensor with human-interpretable names.
Definition LabeledAxis.h:48
bool equals(const LabeledAxis &other) const
Check to see if two axes are equivalent.
std::size_t nsubaxis() const
LabeledAxis()=default
Empty constructor.
bool is_setup() const
Whether the axis has been set up.
Definition LabeledAxis.h:60
bool has_variable(const LabeledAxisAccessor &name) const
Check the existence of a variable by its name.
LabeledAxis & subaxis(const LabeledAxisAccessor &name)
Get a sub-axis by name.
const std::vector< TensorShape > & variable_base_sizes() const
Get the variable base shapes (in assembly order).
LabeledAxisAccessor disqualify(const LabeledAxisAccessor &accessor) const
Return the disqualified name of an item (i.e. remove the prefix).
const std::vector< std::pair< Size, Size > > & variable_slices() const
Get the variable slicing indices (in assembly order).
const std::vector< Size > & variable_sizes() const
Get the variable storage sizes (in assembly order).
void add_variable(const LabeledAxisAccessor &name, TensorShapeRef intmd_sizes, TensorShapeRef base_sizes)
Add a variable with known storage size.
LabeledAxis(LabeledAxisAccessor prefix)
Sub-axis constructor.
friend std::ostream & operator<<(std::ostream &os, const LabeledAxis &axis)
std::size_t nvariable() const
const std::pair< Size, Size > & variable_slice(const LabeledAxisAccessor &name) const
Get the slicing indices of a variable by name.
std::vector< LabeledAxisAccessor > variable_names_unsrt() const
Get variable names in unsorted order.
const std::vector< std::pair< Size, Size > > & subaxis_slices() const
Get the sub-axis slicing indices (in assembly order).
std::vector< std::string > subaxis_names_unsrt() const
Get subaxis names in unsorted order.
LabeledAxis & add_subaxis(const std::string &name)
Add a sub-axis.
const std::vector< TensorShape > & variable_intmd_sizes() const
Get the variable left-batch shapes (in assembly order).
bool has_subaxis(const LabeledAxisAccessor &name) const
Check the existence of a subaxis by its name.
std::pair< Size, Size > subaxis_slice(const LabeledAxisAccessor &name) const
Get the slicing indices of a sub-axis by name.
indexing::Slice slice(const LabeledAxisAccessor &name) const
Get the slicing indices of a variable or a local sub-axis.
void setup_layout()
Setup the layout of all items recursively.
Size subaxis_size(const LabeledAxisAccessor &name) const
Get the storage size of a sub-axis by name.
const std::vector< LabeledAxisAccessor > & variable_names() const
Get the variable names.
Size size(const LabeledAxisAccessor &name) const
Get the storage size of a variable or a local sub-axis.
std::size_t variable_id(const LabeledAxisAccessor &name) const
Get the assembly ID of a variable.
Size variable_size(const LabeledAxisAccessor &name) const
Get the storage size of a variable by name.
void clear()
De-initialize the axis.
Size size() const
Get the storage size of the entire axis.
const std::vector< Size > & subaxis_sizes() const
Get the sub-axis storage sizes (in assembly order).
std::size_t subaxis_id(const std::string &name) const
Get the assembly ID of a sub-axis.
LabeledAxisAccessor qualify(const LabeledAxisAccessor &accessor) const
Return the fully qualified name of an item (i.e. useful when this axis is a sub-axis).
const std::vector< std::string > & subaxis_names() const
Get the sub-axis names.
const LabeledAxis & subaxis(const LabeledAxisAccessor &name) const
Get a sub-axis by name.
const std::vector< const LabeledAxis * > & subaxes() const
Get the sub-axes (in assembly order).
Definition DiagnosticsInterface.h:31
std::ostream & operator<<(std::ostream &, const EnumSelection &)
std::string name(ElasticConstant p)
bool operator==(const LabeledAxis &a, const LabeledAxis &b)
bool operator!=(const LabeledAxis &a, const LabeledAxis &b)
int64_t Size
Definition types.h:71
c10::ArrayRef< Size > TensorShapeRef
Definition types.h:73