CoDiPack  2.2.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
multiUseIndexManager.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/macros.hpp"
41#include "reuseIndexManager.hpp"
42
44namespace codi {
45
56 template<typename T_Index>
57 struct MultiUseIndexManager : public ReuseIndexManager<T_Index> {
58 public:
59
60 using Index = CODI_DD(T_Index, int);
62
63 /*******************************************************************************/
66
67 static bool constexpr CopyNeedsStatement =
69 static bool constexpr IsLinear = false;
71
73
74 private:
75
76 std::vector<Index> indexUse;
77
78 public:
79
81 MultiUseIndexManager(Index const& reservedIndices) : Base(reservedIndices), indexUse(Config::SmallChunkSize) {
82 resizeUseVector();
83 }
84
85 /*******************************************************************************/
88
91 void addToTapeValues(TapeValues& values) const {
93
94 double memoryindexUseVector = (double)indexUse.size() * (double)(sizeof(Index));
95
96 values.addDoubleEntry("Memory: index use vector", memoryindexUseVector, true, true);
97 }
98
100 template<typename Tape>
102 bool generatedNewIndex = false;
103
104 if (Base::InactiveIndex != index) {
105 indexUse[index] -= 1;
106 }
107
108 if (Base::InactiveIndex != index && 0 == indexUse[index]) {
111 indexUse[index] = 1;
112 // Index would be freed and used again so we keep it.
113 } else {
114 index = Base::InactiveIndex; // Reset index here such that the base class will return a new one.
115 generatedNewIndex = Base::template assignIndex<Tape>(index);
116 if (generatedNewIndex) {
117 resizeUseVector();
118 }
119
120 indexUse[index] = 1;
121 }
122
123 return generatedNewIndex;
124 }
125
127 template<typename Tape>
129 freeIndex<Tape>(index); // Zero check is performed inside.
130
131 bool generatedNewIndex = Base::template assignUnusedIndex<Tape>(index);
132 if (generatedNewIndex) {
133 resizeUseVector();
134 }
135
136 indexUse[index] = 1;
137
138 return generatedNewIndex;
139 }
140
142 template<typename Tape>
143 CODI_INLINE void copyIndex(Index& lhs, Index const& rhs) {
145 // Skip the logic if the indices are the same.
146 // This also prevents the bug that if &lhs == &rhs, the left hand side will always be deactivated.
147 if (lhs != rhs) {
148 freeIndex<Tape>(lhs);
149
150 if (Base::InactiveIndex != rhs) { // Do not handle the zero index.
152
153 indexUse[rhs] += 1;
154 lhs = rhs;
155 }
156 }
157 } else {
158 // Path if copy optimizations are disabled.
159 assignIndex<Tape>(lhs);
160 }
161 }
162
164 template<typename Tape>
166 if (Base::valid && Base::InactiveIndex != index) { // Do not free the zero index.
167 indexUse[index] -= 1;
168
169 if (indexUse[index] == 0) { // Only free the index if it is not used any longer.
170 Base::template freeIndex<Tape>(index);
171 } else {
172 index = Base::InactiveIndex;
173 }
174 }
175 }
176
178
179 private:
180
181 CODI_NO_INLINE void resizeUseVector() {
182 indexUse.resize(this->getLargestCreatedIndex() + 1);
183 }
184 };
185}
#define CODI_NO_INLINE
See codi::Config::AvoidedInlines.
Definition config.h:417
#define CODI_INLINE
See codi::Config::ForcedInlines.
Definition config.h:457
#define CODI_DD(Type, Default)
Abbreviation for CODI_DECLARE_DEFAULT.
Definition macros.hpp:94
bool constexpr CopyOptimization
Do not store copy statements like a = b; if the identity handler allows it.
Definition config.h:186
CoDiPack - Code Differentiation Package.
Definition codi.hpp:90
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
static Index constexpr InactiveIndex
Default inactive index for all index managers.
Definition indexManagerInterface.hpp:86
Extends the ReuseIndexManager with a copy optimization.
Definition multiUseIndexManager.hpp:57
bool assignIndex(Index &index)
Call on assignment of a primal value, e.g. on w for w = a + b.
Definition multiUseIndexManager.hpp:101
void addToTapeValues(TapeValues &values) const
Add storage and other information to the tape values.
Definition multiUseIndexManager.hpp:91
T_Index Index
See MultiUseIndexManager.
Definition multiUseIndexManager.hpp:60
void freeIndex(Index &index)
Call on destruction of a primal value. Usually called from the destructor.
Definition multiUseIndexManager.hpp:165
void copyIndex(Index &lhs, Index const &rhs)
Call on copy of a primal value, e.g. w = a.
Definition multiUseIndexManager.hpp:143
MultiUseIndexManager(Index const &reservedIndices)
Constructor.
Definition multiUseIndexManager.hpp:81
static bool constexpr IsLinear
See ReuseIndexManager.
Definition multiUseIndexManager.hpp:69
bool assignUnusedIndex(Index &index)
Call on registering input values.
Definition multiUseIndexManager.hpp:128
static bool constexpr CopyNeedsStatement
Copy optimization only active if configured.
Definition multiUseIndexManager.hpp:67
bool valid
Prevent index free after destruction.
Definition reuseIndexManagerBase.hpp:95
Reuse index manager with a many-to-one relation between tapes and index manager.
Definition reuseIndexManager.hpp:53
Index getLargestCreatedIndex() const
Returns the largest created index.
Definition reuseIndexManager.hpp:106
static bool constexpr NeedsStaticStorage
< See ReuseIndexManagerBase.
Definition reuseIndexManagerBase.hpp:81
void addToTapeValues(TapeValues &values) const
Add storage and other information to the tape values.
Definition reuseIndexManager.hpp:91
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