CoDiPack  3.1.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-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/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);
63
64 /*******************************************************************************/
67
68 static bool constexpr CopyNeedsStatement =
70 static bool constexpr IsLinear = false;
72
74
75 private:
76
77 std::vector<Index> indexUse;
78
79 public:
80
82 MultiUseIndexManager(Index const& reservedIndices) : Base(reservedIndices), indexUse(Config::SmallChunkSize) {
83 resizeUseVector();
84 }
85
86 /*******************************************************************************/
89
92 void addToTapeValues(TapeValues& values) const {
94
95 double memoryindexUseVector = (double)indexUse.size() * (double)(sizeof(Index));
96
97 TapeValues::LocalReductionOperation constexpr operation =
98 NeedsStaticStorage ? TapeValues::LocalReductionOperation::Max : TapeValues::LocalReductionOperation::Sum;
99
100 values.addDoubleEntry("Memory: index use vector", memoryindexUseVector, operation, true, true);
101 }
102
104 template<typename Tape>
106 bool generatedNewIndex = false;
107
108 if (Base::InactiveIndex != index) {
109 indexUse[index] -= 1;
110 }
111
112 if (Base::InactiveIndex != index && 0 == indexUse[index]) {
115 indexUse[index] = 1;
116 // Index would be freed and used again so we keep it.
117 } else {
118 index = Base::InactiveIndex; // Reset index here such that the base class will return a new one.
119 generatedNewIndex = Base::template assignIndex<Tape>(index);
120 if (generatedNewIndex) {
121 resizeUseVector();
122 }
123
124 indexUse[index] = 1;
125 }
126
127 return generatedNewIndex;
128 }
129
131 template<typename Tape>
133 freeIndex<Tape>(index); // Zero check is performed inside.
134
135 bool generatedNewIndex = Base::template assignUnusedIndex<Tape>(index);
136 if (generatedNewIndex) {
137 resizeUseVector();
138 }
139
140 indexUse[index] = 1;
141
142 return generatedNewIndex;
143 }
144
146 template<typename Tape>
147 CODI_INLINE void copyIndex(Index& lhs, Index const& rhs) {
149 // Skip the logic if the indices are the same.
150 // This also prevents the bug that if &lhs == &rhs, the left hand side will always be deactivated.
151 if (lhs != rhs) {
152 freeIndex<Tape>(lhs);
153
154 if (Base::InactiveIndex != rhs) { // Do not handle the zero index.
156
157 indexUse[rhs] += 1;
158 lhs = rhs;
159 }
160 }
161 } else {
162 // Path if copy optimizations are disabled.
164 }
165 }
166
168 template<typename Tape>
170 if (Base::valid && Base::InactiveIndex != index) { // Do not free the zero index.
171 indexUse[index] -= 1;
172
173 if (indexUse[index] == 0) { // Only free the index if it is not used any longer.
174 Base::template freeIndex<Tape>(index);
175 } else {
176 index = Base::InactiveIndex;
177 }
178 }
179 }
180
182
183 private:
184
185 CODI_NO_INLINE void resizeUseVector() {
186 indexUse.resize(this->getLargestCreatedIndex() + 1);
187 }
188 };
189}
#define CODI_NO_INLINE
See codi::Config::AvoidedInlines.
Definition config.h:426
#define CODI_INLINE
See codi::Config::ForcedInlines.
Definition config.h:469
#define CODI_DD(Type, Default)
Abbreviation for CODI_DECLARE_DEFAULT.
Definition macros.hpp:97
Configuration options for CoDiPack.
Definition config.h:65
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:97
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
static Index constexpr InactiveIndex
Default inactive index for all index managers.
Definition indexManagerInterface.hpp:87
void addToTapeValues(TapeValues &values) const
Add storage and other information to the tape values.
Definition multiUseIndexManager.hpp:92
T_Index Index
See MultiUseIndexManager.
Definition multiUseIndexManager.hpp:60
Index ActiveTypeIndexData
Same as the index.
Definition multiUseIndexManager.hpp:61
inlinevoid copyIndex(Index &lhs, Index const &rhs)
Call on copy of a primal value, e.g. w = a.
Definition multiUseIndexManager.hpp:147
inlinebool assignIndex(Index &index)
Call on assignment of a primal value, e.g. on w for w = a + b.
Definition multiUseIndexManager.hpp:105
MultiUseIndexManager(Index const &reservedIndices)
Constructor.
Definition multiUseIndexManager.hpp:82
inlinevoid freeIndex(Index &index)
Definition multiUseIndexManager.hpp:169
inlinebool assignUnusedIndex(Index &index)
Call on registering input values.
Definition multiUseIndexManager.hpp:132
static bool constexpr IsLinear
See ReuseIndexManager.
Definition multiUseIndexManager.hpp:70
static bool constexpr CopyNeedsStatement
Copy optimization only active if configured.
Definition multiUseIndexManager.hpp:68
ReuseIndexManager< Index > Base
Base class abbreviation.
Definition multiUseIndexManager.hpp:62
bool valid
Prevent index free after destruction.
Definition reuseIndexManagerBase.hpp:96
static bool constexpr NeedsStaticStorage
ReuseIndexManager(Index const &reservedIndices)
Constructor.
Definition reuseIndexManager.hpp:79
inlineIndex getLargestCreatedIndex() const
Returns the largest created index.
Definition reuseIndexManager.hpp:110
void addToTapeValues(TapeValues &values) const
Definition reuseIndexManager.hpp:92
Tape information that can be printed in a pretty print format or a table format.
Definition tapeValues.hpp:75
void addDoubleEntry(std::string const &name, double const &value, LocalReductionOperation operation=LocalReductionOperation::Sum, bool usedMem=false, bool allocatedMem=false)
Add double entry. If it is a memory entry, it should be in bytes.
Definition tapeValues.hpp:137