CoDiPack  2.3.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
parallelReuseIndexManager.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 "../../tools/parallel/parallelToolbox.hpp"
43#include "reuseIndexManagerBase.hpp"
44
46namespace codi {
47
58 template<typename T_Index, typename T_ParallelToolbox>
60 : public ReuseIndexManagerBase<T_Index, ParallelReuseIndexManager<T_Index, T_ParallelToolbox>> {
61 public:
62
63 using Index = CODI_DD(T_Index, int);
64 using ParallelToolbox = CODI_DD(T_ParallelToolbox,
65 CODI_DEFAULT_PARALLEL_TOOLBOX);
67 friend Base;
68
69 private:
70
71 template<typename Type>
72 using Atomic = typename ParallelToolbox::template Atomic<Type>;
73
74 using ReadWriteMutex = typename ParallelToolbox::ReadWriteMutex;
75
76 public:
77
78 /*******************************************************************************/
81
83 using Base::IsLinear;
84 static bool constexpr NeedsStaticStorage = false;
86
88
89 private:
90
93 static CODI_NO_INLINE Atomic<T_Index>& globalMaximumIndex() {
94 static Atomic<T_Index> _globalMaximumIndex;
95 return _globalMaximumIndex;
96 }
97
100 static CODI_NO_INLINE bool& globalMaximumIndexInitialized() {
101 static bool _globalMaximumIndexInitialized = false;
102 return _globalMaximumIndexInitialized;
103 }
104
107 static CODI_NO_INLINE ReadWriteMutex& globalMaximumIndexMutex() {
108 static ReadWriteMutex _globalMaximumIndexMutex;
109 return _globalMaximumIndexMutex;
110 }
111
112 public:
113
117 ParallelReuseIndexManager(Index const& reservedIndices) {
118 globalMaximumIndexMutex().lockWrite();
119 if (!globalMaximumIndexInitialized()) {
120 globalMaximumIndex() = reservedIndices;
121 globalMaximumIndexInitialized() = true;
122 }
123 globalMaximumIndexMutex().unlockWrite();
124 generateNewIndices();
125 }
126
129
130 /*******************************************************************************/
133
136 void addToTapeValues(TapeValues& values) const {
137 unsigned long maximumGlobalIndex = globalMaximumIndex();
138
139 // As maximumGlobalIndex is static, it uses a local maximum reduction.
140 TapeValues::LocalReductionOperation constexpr operation = TapeValues::LocalReductionOperation::Max;
141
142 values.addUnsignedLongEntry("Max. live indices", maximumGlobalIndex, operation);
143 // The number of current live indices cannot be computed from one instance alone.
144 // It equals the number of maximum live indices minus the number of indices stored across all instances.
145
146 Base::addToTapeValues(values);
147 }
148
154 return globalMaximumIndex();
155 }
156
158
159 private:
160
161 CODI_NO_INLINE void generateNewIndices() {
162 // This method is only called when unused indices are empty.
163 // Initially, a number of unused indices is created which
164 // equals the number of indices we generate now, therefore
165 // we do not have to check for size.
166
167 codiAssert(this->unusedIndices.size() >= this->indexSizeIncrement);
168
169 Index upperIndexRangeBound = globalMaximumIndex() += this->indexSizeIncrement; // note: atomic operation
170 Index lowerIndexRangeBound = upperIndexRangeBound - this->indexSizeIncrement;
171
172 for (size_t pos = 0; pos < this->indexSizeIncrement; ++pos) {
173 this->unusedIndices[this->unusedIndicesPos + pos] = lowerIndexRangeBound + Index(pos) + 1;
174 }
175
177 }
178 };
179}
#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
CoDiPack - Code Differentiation Package.
Definition codi.hpp:91
Reuse index manager with a one-to-one relation between tapes and index manager.
Definition parallelReuseIndexManager.hpp:60
static bool constexpr NeedsStaticStorage
< See ReuseIndexManagerBase.
Definition parallelReuseIndexManager.hpp:84
T_Index Index
See ParallelReuseIndexManager.
Definition parallelReuseIndexManager.hpp:63
~ParallelReuseIndexManager()
Destructor.
Definition parallelReuseIndexManager.hpp:128
void addToTapeValues(TapeValues &values) const
Add storage and other information to the tape values.
Definition parallelReuseIndexManager.hpp:136
Index getLargestCreatedIndex() const
Returns the largest created index.
Definition parallelReuseIndexManager.hpp:153
friend Base
Allow the base class to access protected and private members.
Definition parallelReuseIndexManager.hpp:67
ParallelReuseIndexManager(Index const &reservedIndices)
Definition parallelReuseIndexManager.hpp:117
T_ParallelToolbox ParallelToolbox
See ParallelReuseIndexManager.
Definition parallelReuseIndexManager.hpp:64
codi::ReadWriteMutex< ThreadInformation, Atomic< int > > ReadWriteMutex
See codi::ReadWriteMutex.
Definition parallelToolbox.hpp:90
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
std::vector< Index > unusedIndices
Pool of indices that have not been used in this recording yet.
Definition reuseIndexManagerBase.hpp:90
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:259
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
Tape information that can be printed in a pretty print format or a table format.
Definition tapeValues.hpp:75
void addUnsignedLongEntry(std::string const &name, unsigned long const &value, LocalReductionOperation operation=LocalReductionOperation::Sum)
Add unsigned long entry.
Definition tapeValues.hpp:163