CoDiPack  2.2.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
reuseIndexManagerBase.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 <algorithm>
38#include <vector>
39
40#include "../../config.h"
41#include "../../misc/macros.hpp"
42#include "../data/emptyData.hpp"
43#include "indexManagerInterface.hpp"
44
46namespace codi {
47
65 template<typename T_Index, typename T_Impl>
66 struct ReuseIndexManagerBase : public IndexManagerInterface<T_Index>, public EmptyData {
67 public:
68
69 using Index = CODI_DD(T_Index, int);
70 using Impl = CODI_DD(T_Impl, CODI_IMPLEMENTATION);
72
74
75 /*******************************************************************************/
78
79 static bool constexpr CopyNeedsStatement = true;
80 static bool constexpr IsLinear = false;
81 static bool constexpr NeedsStaticStorage = true;
82
84
85 protected:
86
87 std::vector<Index> usedIndices;
89
90 std::vector<Index> unusedIndices;
92
94
95 bool valid;
96
97 private:
98 /*******************************************************************************/
101
103 CODI_INLINE Impl& cast() {
104 return static_cast<Impl&>(*this);
105 }
106
108 CODI_NO_INLINE void generateNewIndices() {
109 cast().generateNewIndices();
110 }
111
113
114 public:
115
119 : usedIndices(),
123 indexSizeIncrement(Config::SmallChunkSize),
124 valid(true) {
125 increaseIndicesSize(unusedIndices);
126 }
127
130 valid = false;
131 }
132
133 /*******************************************************************************/
136
138 template<typename Tape>
140 bool generatedNewIndex = false;
141
142 if (Base::InactiveIndex == index) {
143 if (0 == usedIndicesPos) {
144 if (0 == unusedIndicesPos) {
145 generateNewIndices();
146 generatedNewIndex = true;
147 }
148
149 unusedIndicesPos -= 1;
151 } else {
152 usedIndicesPos -= 1;
154 }
155 }
156
158
159 return generatedNewIndex;
160 }
161
163 template<typename Tape>
165 freeIndex<Tape>(index); // Zero check is performed inside.
166
167 bool generatedNewIndex = false;
168 if (0 == unusedIndicesPos) {
169 generateNewIndices();
170 generatedNewIndex = true;
171 }
172
173 unusedIndicesPos -= 1;
175
177
178 return generatedNewIndex;
179 }
180
182 template<typename Tape>
183 CODI_INLINE void copyIndex(Index& lhs, Index const& rhs) {
184 if (Base::InactiveIndex == rhs) {
185 freeIndex<Tape>(lhs);
186 } else {
187 assignIndex<Tape>(lhs);
188 }
189 }
190
192 template<typename Tape>
194 if (valid && Base::InactiveIndex != index) { // Do not free the zero index.
195
197
198 if (usedIndicesPos == usedIndices.size()) {
199 increaseIndicesSize(usedIndices);
200 }
201
203 usedIndicesPos += 1;
204
205 index = Base::InactiveIndex;
206 }
207 }
208
211 size_t totalSize = usedIndicesPos + unusedIndicesPos;
212 if (totalSize > unusedIndices.size()) {
213 increaseIndicesSizeTo(unusedIndices, totalSize);
214 }
215
216 for (size_t pos = 0; pos < usedIndicesPos; ++pos) {
218 }
219 unusedIndicesPos = totalSize;
220 usedIndicesPos = 0;
221
223 if (totalSize == unusedIndices.size()) {
224 std::sort(unusedIndices.begin(), unusedIndices.end());
225 } else {
227 }
228 }
229 }
230
233 void addToTapeValues(TapeValues& values) const {
234 unsigned long storedIndices = this->usedIndicesPos + this->unusedIndicesPos;
235 unsigned long allocatedIndices = this->usedIndices.size() + this->unusedIndices.size();
236
237 double memoryStoredIndices = (double)storedIndices * (double)(sizeof(Index));
238 double memoryAllocatedIndices = (double)allocatedIndices * (double)(sizeof(Index));
239
240 values.addUnsignedLongEntry("Indices stored", storedIndices);
241 values.addDoubleEntry("Memory used", memoryStoredIndices, true, false);
242 values.addDoubleEntry("Memory allocated", memoryAllocatedIndices, false, true);
243 }
244
246
247 private:
248
249 CODI_NO_INLINE void increaseIndicesSize(std::vector<Index>& v) {
250 v.resize(v.size() + indexSizeIncrement);
251 }
252
253 CODI_NO_INLINE void increaseIndicesSizeTo(std::vector<Index>& v, size_t minimalSize) {
254 codiAssert(v.size() < minimalSize);
255
256 size_t increaseMul = (minimalSize - v.size()) / indexSizeIncrement + 1; // +1 always rounds up.
257 v.resize(v.size() + increaseMul * indexSizeIncrement);
258 }
259 };
260}
#define CODI_NO_INLINE
See codi::Config::AvoidedInlines.
Definition config.h:417
#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_IMPLEMENTATION
Used in interface declarations to indicate the type of the implementing class.
Definition macros.hpp:105
bool constexpr SortIndicesOnReset
Reuse index tapes will sort their indices on a reset.
Definition config.h:264
CoDiPack - Code Differentiation Package.
Definition codi.hpp:90
No data is stored in this DataInterface implementation. It is used to terminate the recursive nature ...
Definition emptyData.hpp:54
EmptyPosition Position
No positional data.
Definition emptyData.hpp:59
Empty Position with no nested data.
Definition position.hpp:47
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
Indices enable the mapping of primal values to their adjoint counterparts.
Definition indexManagerInterface.hpp:78
static Index constexpr InactiveIndex
Default inactive index for all index managers.
Definition indexManagerInterface.hpp:86
Identifiers are reused. Freed identifiers are assigned to new variables. Variables keep their indices...
Definition reuseIndexManagerBase.hpp:66
static bool constexpr IsLinear
Identifiers are not coupled to statements.
Definition reuseIndexManagerBase.hpp:80
bool assignUnusedIndex(Index &index)
Call on registering input values.
Definition reuseIndexManagerBase.hpp:164
bool valid
Prevent index free after destruction.
Definition reuseIndexManagerBase.hpp:95
~ReuseIndexManagerBase()
Destructor.
Definition reuseIndexManagerBase.hpp:129
T_Impl Impl
See ReuseIndexManagerBase.
Definition reuseIndexManagerBase.hpp:70
ReuseIndexManagerBase()
Definition reuseIndexManagerBase.hpp:118
bool assignIndex(Index &index)
Call on assignment of a primal value, e.g. on w for w = a + b.
Definition reuseIndexManagerBase.hpp:139
std::vector< Index > unusedIndices
Pool of indices that have not been used in this recording yet.
Definition reuseIndexManagerBase.hpp:90
std::vector< Index > usedIndices
Pool of indices that have already been used in this recording.
Definition reuseIndexManagerBase.hpp:87
static bool constexpr CopyNeedsStatement
No copy optimization is implemented.
Definition reuseIndexManagerBase.hpp:79
void addToTapeValues(TapeValues &values) const
Add storage and other information to the tape values.
Definition reuseIndexManagerBase.hpp:233
static bool constexpr NeedsStaticStorage
Identifiers are managed globally.
Definition reuseIndexManagerBase.hpp:81
T_Index Index
See ReuseIndexManagerBase.
Definition reuseIndexManagerBase.hpp:69
size_t indexSizeIncrement
Block size for index pool enlargement.
Definition reuseIndexManagerBase.hpp:93
size_t unusedIndicesPos
Number of remaining unused indices.
Definition reuseIndexManagerBase.hpp:91
void freeIndex(Index &index)
Call on destruction of a primal value. Usually called from the destructor.
Definition reuseIndexManagerBase.hpp:193
size_t usedIndicesPos
Number of remaining used indices.
Definition reuseIndexManagerBase.hpp:88
void reset()
Reset for a new recording.
Definition reuseIndexManagerBase.hpp:210
void copyIndex(Index &lhs, Index const &rhs)
Call on copy of a primal value, e.g. w = a.
Definition reuseIndexManagerBase.hpp:183
Tape information that can be printed in a pretty print format or a table format.
Definition tapeValues.hpp:73
void addDoubleEntry(std::string const &name, double const &value, bool usedMem=false, bool allocatedMem=false)
Add double entry. If it is a memory entry, it should be in bytes.
Definition tapeValues.hpp:126
void addUnsignedLongEntry(std::string const &name, unsigned long const &value)
Add unsigned long entry.
Definition tapeValues.hpp:150