CoDiPack  2.3.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 TapeValues::LocalReductionOperation constexpr operation =
97 NeedsStaticStorage ? TapeValues::LocalReductionOperation::Max : TapeValues::LocalReductionOperation::Sum;
98
99 values.addDoubleEntry("Memory: index use vector", memoryindexUseVector, operation, true, true);
100 }
101
103 template<typename Tape>
105 bool generatedNewIndex = false;
106
107 if (Base::InactiveIndex != index) {
108 indexUse[index] -= 1;
109 }
110
111 if (Base::InactiveIndex != index && 0 == indexUse[index]) {
114 indexUse[index] = 1;
115 // Index would be freed and used again so we keep it.
116 } else {
117 index = Base::InactiveIndex; // Reset index here such that the base class will return a new one.
118 generatedNewIndex = Base::template assignIndex<Tape>(index);
119 if (generatedNewIndex) {
120 resizeUseVector();
121 }
122
123 indexUse[index] = 1;
124 }
125
126 return generatedNewIndex;
127 }
128
130 template<typename Tape>
132 freeIndex<Tape>(index); // Zero check is performed inside.
133
134 bool generatedNewIndex = Base::template assignUnusedIndex<Tape>(index);
135 if (generatedNewIndex) {
136 resizeUseVector();
137 }
138
139 indexUse[index] = 1;
140
141 return generatedNewIndex;
142 }
143
145 template<typename Tape>
146 CODI_INLINE void copyIndex(Index& lhs, Index const& rhs) {
148 // Skip the logic if the indices are the same.
149 // This also prevents the bug that if &lhs == &rhs, the left hand side will always be deactivated.
150 if (lhs != rhs) {
151 freeIndex<Tape>(lhs);
152
153 if (Base::InactiveIndex != rhs) { // Do not handle the zero index.
155
156 indexUse[rhs] += 1;
157 lhs = rhs;
158 }
159 }
160 } else {
161 // Path if copy optimizations are disabled.
162 assignIndex<Tape>(lhs);
163 }
164 }
165
167 template<typename Tape>
169 if (Base::valid && Base::InactiveIndex != index) { // Do not free the zero index.
170 indexUse[index] -= 1;
171
172 if (indexUse[index] == 0) { // Only free the index if it is not used any longer.
173 Base::template freeIndex<Tape>(index);
174 } else {
175 index = Base::InactiveIndex;
176 }
177 }
178 }
179
181
182 private:
183
184 CODI_NO_INLINE void resizeUseVector() {
185 indexUse.resize(this->getLargestCreatedIndex() + 1);
186 }
187 };
188}
#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:91
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:104
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:168
void copyIndex(Index &lhs, Index const &rhs)
Call on copy of a primal value, e.g. w = a.
Definition multiUseIndexManager.hpp:146
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:131
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:109
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: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