CoDiPack  2.2.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
linearIndexManager.hpp
1/*
2 * CoDiPack, a Code Differentiation Package
3 *
4 * Copyright (C) 2015-2024 Chair for Scientific Computing (SciComp), University of Kaiserslautern-Landau
5 * Homepage: http://scicomp.rptu.de
6 * Contact: Prof. Nicolas R. Gauger (codi@scicomp.uni-kl.de)
7 *
8 * Lead developers: Max Sagebaum, Johannes Blühdorn (SciComp, University of Kaiserslautern-Landau)
9 *
10 * This file is part of CoDiPack (http://scicomp.rptu.de/software/codi).
11 *
12 * CoDiPack is free software: you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, either version 3 of the
15 * License, or (at your option) any later version.
16 *
17 * CoDiPack is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty
19 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
20 *
21 * See the GNU General Public License for more details.
22 * You should have received a copy of the GNU
23 * General Public License along with CoDiPack.
24 * If not, see <http://www.gnu.org/licenses/>.
25 *
26 * For other licensing options please contact us.
27 *
28 * Authors:
29 * - SciComp, University of Kaiserslautern-Landau:
30 * - Max Sagebaum
31 * - Johannes Blühdorn
32 * - Former members:
33 * - Tim Albring
34 */
35#pragma once
36
37#include <vector>
38
39#include "../../config.h"
40#include "../../misc/eventSystem.hpp"
41#include "../../misc/macros.hpp"
42#include "../data/dataInterface.hpp"
43#include "indexManagerInterface.hpp"
44
46namespace codi {
47
65 template<typename T_Index>
66 struct LinearIndexManager : public IndexManagerInterface<T_Index>, public DataInterface<> {
67 public:
68
69 using Index = CODI_DD(T_Index, int);
71
72 /*******************************************************************************/
75
76 static bool constexpr CopyNeedsStatement = false;
77 static bool constexpr IsLinear = true;
78 static bool constexpr NeedsStaticStorage = false;
79
81 /*******************************************************************************/
84
85 using Position = Index;
86 using NestedData = void;
87 using InternalPosHandle = size_t;
88
90
91 private:
92
93 Index reservedIndices;
94 Index count;
95
96 public:
97
99 LinearIndexManager(Index reservedIndices) : reservedIndices(reservedIndices), count(reservedIndices) {}
100
101 /*******************************************************************************/
104
107 void addToTapeValues(TapeValues& values) const {
108 values.addLongEntry("Max. live indices", getLargestCreatedIndex());
109 }
110
113 template<typename Tape>
114 CODI_INLINE void freeIndex(Index& index) const {
116 if (Base::InactiveIndex != index && Base::InvalidIndex != index) {
118 }
119 }
120 index = Base::InactiveIndex;
121 }
122
124 template<typename Tape>
126 if (CODI_ENABLE_CHECK(Config::OverflowCheck, count > count + 1)) {
127 CODI_EXCEPTION("Overflow in linear index handler. Use a larger index type or a reuse index manager.");
128 }
129 count += 1;
131 if (Base::InactiveIndex != index && Base::InvalidIndex != index) {
133 }
135 }
136 index = count;
137 return true;
138 }
139
141 template<typename Tape>
143 return assignIndex<Tape>(index);
144 }
145
147 template<typename Tape>
148 CODI_INLINE void copyIndex(Index& lhs, Index const& rhs) {
150 if (Base::InactiveIndex != lhs && Base::InvalidIndex != lhs) {
152 }
154 }
155 lhs = rhs;
156 }
157
163 return count;
164 }
165
167 /*******************************************************************************/
170
172 template<typename TargetPosition>
173 CODI_INLINE TargetPosition extractPosition(Position const& pos) const {
174 return pos; // This is a terminator, no further recursion.
175 }
176
178 CODI_INLINE size_t getDataSize() const {
179 return 0;
180 }
181
185 return count;
186 }
187
190 return count - startPos;
191 }
192
196 return reservedIndices;
197 }
198
201
204 CODI_UNUSED(items);
205
206 return count;
207 }
208
210 void resize(size_t const& totalSize) {
211 CODI_UNUSED(totalSize);
212 }
213
215 CODI_INLINE void resetTo(Position const& pos) {
216 codiAssert(pos >= reservedIndices);
217
218 count = pos;
219 }
220
223 count = reservedIndices;
224 }
225
228 count = reservedIndices;
229 }
230
234 void erase(Position const& start, Position const& end, bool recursive = true) {
235 CODI_UNUSED(start, end, recursive);
236 }
237
240 CODI_UNUSED(v);
241 }
242
245 std::swap(reservedIndices, other.reservedIndices);
246 std::swap(count, other.count);
247 }
248
251 template<int selectedDepth = -1, typename FunctionObject, typename... Args>
252 CODI_INLINE void evaluateForward(Position const& start, Position const& end, FunctionObject function,
253 Args&&... args) {
254 function(std::forward<Args>(args)..., start, end);
255 }
256
259 template<int selectedDepth = -1, typename FunctionObject, typename... Args>
260 CODI_INLINE void evaluateReverse(Position const& start, Position const& end, FunctionObject function,
261 Args&&... args) {
262 function(std::forward<Args>(args)..., start, end);
263 }
264
266 template<typename FunctionObject, typename... Args>
267 CODI_INLINE void forEachChunk(FunctionObject& function, bool recursive, Args&&... args) {
268 CODI_UNUSED(function, recursive, args...);
269 // Do nothing.
270 }
271
273 template<typename FunctionObject, typename... Args>
274 CODI_INLINE void forEachForward(Position const& start, Position const& end, FunctionObject function,
275 Args&&... args) {
276 CODI_UNUSED(start, end, function, args...);
277 }
278
280 template<typename FunctionObject, typename... Args>
281 CODI_INLINE void forEachReverse(Position const& start, Position const& end, FunctionObject function,
282 Args&&... args) {
283 CODI_UNUSED(start, end, function, args...);
284 }
285 };
286}
#define CODI_INLINE
See codi::Config::ForcedInlines.
Definition config.h:457
#define codiAssert(x)
See codi::Config::EnableAssert.
Definition config.h:432
#define CODI_DD(Type, Default)
Abbreviation for CODI_DECLARE_DEFAULT.
Definition macros.hpp:94
#define CODI_ENABLE_CHECK(option, condition)
Definition macros.hpp:53
bool constexpr IndexEvents
Enable index management events. Disabled by default.
Definition config.h:327
bool constexpr OverflowCheck
Check in the index manager if an overflow occurred.
Definition config.h:248
CoDiPack - Code Differentiation Package.
Definition codi.hpp:90
void CODI_UNUSED(Args const &...)
Disable unused warnings for an arbitrary number of arguments.
Definition macros.hpp:46
Data stream interface for tape data. Encapsulates data that is written e.g. for each statement or arg...
Definition dataInterface.hpp:149
static void notifyIndexAssignListeners(Index const &index)
Invoke callbacks for IndexAssign events.
Definition eventSystem.hpp:778
static void notifyIndexFreeListeners(Index const &index)
Invoke callbacks for IndexFree events.
Definition eventSystem.hpp:806
static void notifyIndexCopyListeners(Index const &index)
Invoke callbacks for IndexCopy events.
Definition eventSystem.hpp:833
Indices enable the mapping of primal values to their adjoint counterparts.
Definition indexManagerInterface.hpp:78
static Index constexpr InvalidIndex
Default invalid index for all index mangers (max value for unsigned types).
Definition indexManagerInterface.hpp:87
static Index constexpr InactiveIndex
Default inactive index for all index managers.
Definition indexManagerInterface.hpp:86
Identifiers are created in a linear fashion. Each assign creates a new index which is counted up.
Definition linearIndexManager.hpp:66
LinearIndexManager(Index reservedIndices)
Constructor.
Definition linearIndexManager.hpp:99
InternalPosHandle reserveItems(size_t const &items)
Reserve this many items on the data stream. See pushData for details.
Definition linearIndexManager.hpp:203
size_t getDataSize() const
Definition linearIndexManager.hpp:178
size_t InternalPosHandle
Internal positions coincide with positions.
Definition linearIndexManager.hpp:87
void forEachChunk(FunctionObject &function, bool recursive, Args &&... args)
Calls the function object for each continuous segment of data.
Definition linearIndexManager.hpp:267
void swap(LinearIndexManager< Index > &other)
Definition linearIndexManager.hpp:244
void NestedData
Terminator, no further nested data.
Definition linearIndexManager.hpp:86
void addToTapeValues(TapeValues &values) const
Add storage and other information to the tape values.
Definition linearIndexManager.hpp:107
Position getZeroPosition() const
Definition linearIndexManager.hpp:195
void forEachForward(Position const &start, Position const &end, FunctionObject function, Args &&... args)
Calls the function object for each item in the data stream. This call is not recursive.
Definition linearIndexManager.hpp:274
void resetHard()
Definition linearIndexManager.hpp:227
static bool constexpr CopyNeedsStatement
Copy optimization is implemented.
Definition linearIndexManager.hpp:76
void forEachReverse(Position const &start, Position const &end, FunctionObject function, Args &&... args)
Calls the function object for each item in the data stream. This call is not recursive.
Definition linearIndexManager.hpp:281
T_Index Index
See LinearIndexManager.
Definition linearIndexManager.hpp:69
Index Position
Positions coincide with indices.
Definition linearIndexManager.hpp:85
bool assignIndex(Index &index)
Call on assignment of a primal value, e.g. on w for w = a + b.
Definition linearIndexManager.hpp:125
void pushData()
Add data to the storage allocated by the implementation. The method can only be called after a call t...
Definition linearIndexManager.hpp:200
void freeIndex(Index &index) const
Call on destruction of a primal value. Usually called from the destructor.
Definition linearIndexManager.hpp:114
void evaluateForward(Position const &start, Position const &end, FunctionObject function, Args &&... args)
Evaluates the function object with segments of continuous and valid data for all nested DataInterface...
Definition linearIndexManager.hpp:252
void erase(Position const &start, Position const &end, bool recursive=true)
Definition linearIndexManager.hpp:234
void copyIndex(Index &lhs, Index const &rhs)
Call on copy of a primal value, e.g. w = a.
Definition linearIndexManager.hpp:148
void resize(size_t const &totalSize)
Definition linearIndexManager.hpp:210
static bool constexpr NeedsStaticStorage
Linear indices are not meaningful across tape instances.
Definition linearIndexManager.hpp:78
size_t getPushedDataCount(InternalPosHandle const &startPos)
Definition linearIndexManager.hpp:189
void evaluateReverse(Position const &start, Position const &end, FunctionObject function, Args &&... args)
Evaluates the function object with segments of continuous and valid data for all nested DataInterface...
Definition linearIndexManager.hpp:260
void reset()
Definition linearIndexManager.hpp:222
static bool constexpr IsLinear
Tightly coupled to statements.
Definition linearIndexManager.hpp:77
Index getLargestCreatedIndex() const
Returns the largest created index.
Definition linearIndexManager.hpp:162
TargetPosition extractPosition(Position const &pos) const
Extract the position of a nested DataInterface from the global position object provide by this interf...
Definition linearIndexManager.hpp:173
void setNested(NestedData *v)
Definition linearIndexManager.hpp:239
Position getPosition() const
Definition linearIndexManager.hpp:184
bool assignUnusedIndex(Index &index)
Call on registering input values.
Definition linearIndexManager.hpp:142
void resetTo(Position const &pos)
Definition linearIndexManager.hpp:215
Tape information that can be printed in a pretty print format or a table format.
Definition tapeValues.hpp:73
void addLongEntry(std::string const &name, long const &value)
Add long entry.
Definition tapeValues.hpp:140