CoDiPack  3.1.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-2026 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);
72
73 /*******************************************************************************/
76
77 static bool constexpr CopyNeedsStatement = false;
78 static bool constexpr IsLinear = true;
79 static bool constexpr NeedsStaticStorage = false;
80
82 /*******************************************************************************/
85
86 using Position = Index;
87 using NestedData = void;
88 using InternalPosHandle = size_t;
89
91
92 private:
93
94 Index reservedIndices;
95 Index count;
96 public:
97
99 LinearIndexManager(Index reservedIndices) : reservedIndices(reservedIndices), count(reservedIndices) {}
100
101 /*******************************************************************************/
104
107 void addToTapeValues(TapeValues& values) const {
108 TapeValues::LocalReductionOperation constexpr operation =
109 NeedsStaticStorage ? TapeValues::LocalReductionOperation::Max : TapeValues::LocalReductionOperation::Sum;
110
111 values.addLongEntry("Max. live indices", getLargestCreatedIndex(), operation);
112 }
113
116 template<typename Tape>
117 CODI_INLINE void freeIndex(Index& index) const {
119 if (Base::InactiveIndex != index && Base::InvalidIndex != index) {
121 }
122 }
123 index = Base::InactiveIndex;
124 }
125
127 template<typename Tape>
129 if (CODI_ENABLE_CHECK(Config::OverflowCheck, count > count + 1)) {
130 CODI_EXCEPTION("Overflow in linear index handler. Use a larger index type or a reuse index manager.");
131 }
132 count += 1;
134 if (Base::InactiveIndex != index && Base::InvalidIndex != index) {
136 }
138 }
139 index = count;
140 return true;
141 }
142
144 template<typename Tape>
146 return assignIndex<Tape>(index);
147 }
148
150 template<typename Tape>
151 CODI_INLINE void copyIndex(Index& lhs, Index const& rhs) {
153 if (Base::InactiveIndex != lhs && Base::InvalidIndex != lhs) {
155 }
157 }
158 lhs = rhs;
159 }
160
166 return count;
167 }
168
171 index = Index();
172 }
173
176 count = index;
177 }
178
181 return data;
182 }
183
186 return data;
187 }
188
190 void validateRhsIndex(ActiveTypeIndexData const& data) const {
191 codiAssert(data <= count);
192 }
193
195 /*******************************************************************************/
198
200 template<typename TargetPosition>
201 CODI_INLINE TargetPosition extractPosition(Position const& pos) const {
202 return pos; // This is a terminator, no further recursion.
203 }
204
206 CODI_INLINE size_t getDataSize() const {
207 return 0;
208 }
209
213 return count;
214 }
215
218 return count - startPos;
219 }
220
224 return reservedIndices;
225 }
226
229
232 CODI_UNUSED(items);
233
234 return count;
235 }
236
238 void resize(size_t const& totalSize) {
239 CODI_UNUSED(totalSize);
240 }
241
243 CODI_INLINE void resetTo(Position const& pos) {
244 codiAssert(pos >= reservedIndices);
245
246 count = pos;
247 }
248
251 count = reservedIndices;
252 }
253
256 count = reservedIndices;
257 }
258
262 void erase(Position const& start, Position const& end, bool recursive = true) {
263 CODI_UNUSED(start, end, recursive);
264 }
265
268 CODI_UNUSED(v);
269 }
270
273 std::swap(reservedIndices, other.reservedIndices);
274 std::swap(count, other.count);
275 }
276
279 template<int selectedDepth = -1, typename FunctionObject, typename... Args>
280 CODI_INLINE void evaluateForward(Position const& start, Position const& end, FunctionObject function,
281 Args&&... args) {
282 function(std::forward<Args>(args)..., start, end);
283 }
284
287 template<int selectedDepth = -1, typename FunctionObject, typename... Args>
288 CODI_INLINE void evaluateReverse(Position const& start, Position const& end, FunctionObject function,
289 Args&&... args) {
290 function(std::forward<Args>(args)..., start, end);
291 }
292
294 template<typename FunctionObject, typename... Args>
295 CODI_INLINE void forEachChunk(FunctionObject& function, bool recursive, Args&&... args) {
296 CODI_UNUSED(function, recursive, args...);
297 // Do nothing.
298 }
299
301 template<typename FunctionObject, typename... Args>
302 CODI_INLINE void forEachForward(Position const& start, Position const& end, FunctionObject function,
303 Args&&... args) {
304 CODI_UNUSED(start, end, function, args...);
305 }
306
308 template<typename FunctionObject, typename... Args>
309 CODI_INLINE void forEachReverse(Position const& start, Position const& end, FunctionObject function,
310 Args&&... args) {
311 CODI_UNUSED(start, end, function, args...);
312 }
313 };
314}
#define CODI_INLINE
See codi::Config::ForcedInlines.
Definition config.h:469
#define codiAssert(x)
See codi::Config::EnableAssert.
Definition config.h:441
#define CODI_DD(Type, Default)
Abbreviation for CODI_DECLARE_DEFAULT.
Definition macros.hpp:97
#define CODI_ENABLE_CHECK(option, condition)
Definition macros.hpp:62
bool constexpr IndexEvents
Enable index management events. Disabled by default.
Definition config.h:336
bool constexpr OverflowCheck
Check in the index manager if an overflow occurred.
Definition config.h:248
CoDiPack - Code Differentiation Package.
Definition codi.hpp:97
inlinevoid CODI_UNUSED(Args const &...)
Disable unused warnings for an arbitrary number of arguments.
Definition macros.hpp:55
Data stream interface for tape data. Encapsulates data that is written e.g. for each statement or arg...
Definition dataInterface.hpp:149
static inlinevoid notifyIndexCopyListeners(Index const &index)
Invoke callbacks for IndexCopy events.
Definition eventSystem.hpp:833
static inlinevoid notifyIndexAssignListeners(Index const &index)
Invoke callbacks for IndexAssign events.
Definition eventSystem.hpp:778
static inlinevoid notifyIndexFreeListeners(Index const &index)
Invoke callbacks for IndexFree events.
Definition eventSystem.hpp:806
Indices enable the mapping of primal values to their adjoint counterparts.
Definition indexManagerInterface.hpp:78
static Index constexpr InactiveIndex
Definition indexManagerInterface.hpp:87
static Index constexpr InvalidIndex
Definition indexManagerInterface.hpp:88
inlineIndex const & getIndex(ActiveTypeIndexData const &data)
Extract index from data stored in active type.
Definition linearIndexManager.hpp:180
LinearIndexManager(Index reservedIndices)
Constructor.
Definition linearIndexManager.hpp:99
size_t InternalPosHandle
Internal positions coincide with positions.
Definition linearIndexManager.hpp:88
inlinePosition getZeroPosition() const
Definition linearIndexManager.hpp:223
IndexManagerInterface< Index > Base
Base class abbreviation.
Definition linearIndexManager.hpp:71
void validateRhsIndex(ActiveTypeIndexData const &data) const
Check if the rhs index is valid.
Definition linearIndexManager.hpp:190
void swap(LinearIndexManager< Index > &other)
Definition linearIndexManager.hpp:272
void NestedData
Terminator, no further nested data.
Definition linearIndexManager.hpp:87
void addToTapeValues(TapeValues &values) const
Add storage and other information to the tape values.
Definition linearIndexManager.hpp:107
inlinebool assignUnusedIndex(Index &index)
Call on registering input values.
Definition linearIndexManager.hpp:145
inlinevoid copyIndex(Index &lhs, Index const &rhs)
Call on copy of a primal value, e.g. w = a.
Definition linearIndexManager.hpp:151
static bool constexpr CopyNeedsStatement
Copy optimization is implemented.
Definition linearIndexManager.hpp:77
inlinevoid resetTo(Position const &pos)
Definition linearIndexManager.hpp:243
inlinebool assignIndex(Index &index)
Call on assignment of a primal value, e.g. on w for w = a + b.
Definition linearIndexManager.hpp:128
T_Index Index
See LinearIndexManager.
Definition linearIndexManager.hpp:69
inlinesize_t getPushedDataCount(InternalPosHandle const &startPos)
Definition linearIndexManager.hpp:217
Index Position
Positions coincide with indices.
Definition linearIndexManager.hpp:86
inlinevoid 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:309
inlinevoid reset()
Definition linearIndexManager.hpp:250
inlinevoid 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:302
inlinevoid forEachChunk(FunctionObject &function, bool recursive, Args &&... args)
Calls the function object for each continuous segment of data.
Definition linearIndexManager.hpp:295
inlinePosition getPosition() const
Definition linearIndexManager.hpp:212
inlinesize_t getDataSize() const
Definition linearIndexManager.hpp:206
inlineIndex getLargestCreatedIndex() const
Returns the largest created index.
Definition linearIndexManager.hpp:165
void erase(Position const &start, Position const &end, bool recursive=true)
Definition linearIndexManager.hpp:262
inlinevoid 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:280
inlinevoid pushData()
Add data to the storage allocated by the implementation. The method can only be called after a call t...
Definition linearIndexManager.hpp:228
void resize(size_t const &totalSize)
Definition linearIndexManager.hpp:238
inlinevoid resetHard()
Definition linearIndexManager.hpp:255
static bool constexpr NeedsStaticStorage
Linear indices are not meaningful across tape instances.
Definition linearIndexManager.hpp:79
inlineIndex & getIndex(ActiveTypeIndexData &data)
Extract index from data stored in active type.
Definition linearIndexManager.hpp:185
inlinevoid freeIndex(Index &index) const
Definition linearIndexManager.hpp:117
inlinevoid 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:288
inlineInternalPosHandle reserveItems(size_t const &items)
Reserve this many items on the data stream. See pushData for details.
Definition linearIndexManager.hpp:231
static bool constexpr IsLinear
Tightly coupled to statements.
Definition linearIndexManager.hpp:78
inlinevoid initIndex(Index &index)
Initialize the index data. Usually zero everything.
Definition linearIndexManager.hpp:170
inlinevoid updateLargestCreatedIndex(Index const &index)
Update the largest generated index.
Definition linearIndexManager.hpp:175
void setNested(NestedData *v)
Definition linearIndexManager.hpp:267
Index ActiveTypeIndexData
Same as the index.
Definition linearIndexManager.hpp:70
inlineTargetPosition extractPosition(Position const &pos) const
Extract the position of a nested DataInterface from the global position object provide by this interf...
Definition linearIndexManager.hpp:201
Tape information that can be printed in a pretty print format or a table format.
Definition tapeValues.hpp:75
void addLongEntry(std::string const &name, long const &value, LocalReductionOperation operation=LocalReductionOperation::Sum)
Add long entry.
Definition tapeValues.hpp:152