CoDiPack  3.1.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-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 <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);
65 using ParallelToolbox = CODI_DD(T_ParallelToolbox,
66 CODI_DEFAULT_PARALLEL_TOOLBOX);
68 friend Base;
69
70 private:
71
72 template<typename Type>
73 using Atomic = typename ParallelToolbox::template Atomic<Type>;
74
75 using ReadWriteMutex = typename ParallelToolbox::ReadWriteMutex;
76
77 public:
78
79 /*******************************************************************************/
82
84 using Base::IsLinear;
85 static bool constexpr NeedsStaticStorage = false;
87
89
90 private:
91
94 static CODI_NO_INLINE Atomic<T_Index>& globalMaximumIndex() {
95 static Atomic<T_Index> _globalMaximumIndex;
96 return _globalMaximumIndex;
97 }
98
101 static CODI_NO_INLINE bool& globalMaximumIndexInitialized() {
102 static bool _globalMaximumIndexInitialized = false;
103 return _globalMaximumIndexInitialized;
104 }
105
108 static CODI_NO_INLINE ReadWriteMutex& globalMaximumIndexMutex() {
109 static ReadWriteMutex _globalMaximumIndexMutex;
110 return _globalMaximumIndexMutex;
111 }
112
113 public:
114
118 ParallelReuseIndexManager(Index const& reservedIndices) {
119 globalMaximumIndexMutex().lockWrite();
120 if (!globalMaximumIndexInitialized()) {
121 globalMaximumIndex() = reservedIndices;
122 globalMaximumIndexInitialized() = true;
123 }
124 globalMaximumIndexMutex().unlockWrite();
125 generateNewIndices();
126 }
127
130
131 /*******************************************************************************/
134
137 void addToTapeValues(TapeValues& values) const {
138 unsigned long maximumGlobalIndex = globalMaximumIndex();
139
140 // As maximumGlobalIndex is static, it uses a local maximum reduction.
141 TapeValues::LocalReductionOperation constexpr operation = TapeValues::LocalReductionOperation::Max;
142
143 values.addUnsignedLongEntry("Max. live indices", maximumGlobalIndex, operation);
144 // The number of current live indices cannot be computed from one instance alone.
145 // It equals the number of maximum live indices minus the number of indices stored across all instances.
146
147 Base::addToTapeValues(values);
148 }
149
155 return globalMaximumIndex();
156 }
157
159
160 private:
161
162 CODI_NO_INLINE void generateNewIndices() {
163 // This method is only called when unused indices are empty.
164 // Initially, a number of unused indices is created which
165 // equals the number of indices we generate now, therefore
166 // we do not have to check for size.
167
168 codiAssert(this->unusedIndices.size() >= this->indexSizeIncrement);
169
170 Index upperIndexRangeBound = globalMaximumIndex() += this->indexSizeIncrement; // note: atomic operation
171 Index lowerIndexRangeBound = upperIndexRangeBound - this->indexSizeIncrement;
172
173 for (size_t pos = 0; pos < this->indexSizeIncrement; ++pos) {
174 this->unusedIndices[this->unusedIndicesPos + pos] = lowerIndexRangeBound + Index(pos) + 1;
175 }
176
177 this->unusedIndicesPos = this->indexSizeIncrement;
178 }
179 };
180}
#define CODI_NO_INLINE
See codi::Config::AvoidedInlines.
Definition config.h:426
#define CODI_INLINE
See codi::Config::ForcedInlines.
Definition config.h:469
#define codiAssert(x)
See codi::Config::EnableAssert.
Definition config.h:441
#define CODI_DD(Type, Default)
Abbreviation for CODI_DECLARE_DEFAULT.
Definition macros.hpp:97
CoDiPack - Code Differentiation Package.
Definition codi.hpp:97
static bool constexpr NeedsStaticStorage
< See ReuseIndexManagerBase.
Definition parallelReuseIndexManager.hpp:85
T_Index Index
See ParallelReuseIndexManager.
Definition parallelReuseIndexManager.hpp:63
Index ActiveTypeIndexData
Same as the index.
Definition parallelReuseIndexManager.hpp:64
~ParallelReuseIndexManager()
Destructor.
Definition parallelReuseIndexManager.hpp:129
void addToTapeValues(TapeValues &values) const
Add storage and other information to the tape values.
Definition parallelReuseIndexManager.hpp:137
inlineIndex getLargestCreatedIndex() const
Returns the largest created index.
Definition parallelReuseIndexManager.hpp:154
ParallelReuseIndexManager(Index const &reservedIndices)
Definition parallelReuseIndexManager.hpp:118
ReuseIndexManagerBase< Index, ParallelReuseIndexManager > Base
Base class abbreviation.
Definition parallelReuseIndexManager.hpp:67
T_ParallelToolbox ParallelToolbox
See ParallelReuseIndexManager.
Definition parallelReuseIndexManager.hpp:65
codi::ReadWriteMutex< ThreadInformation, Atomic< int > > ReadWriteMutex
See codi::ReadWriteMutex.
Definition parallelToolbox.hpp:90
static bool constexpr IsLinear
Definition reuseIndexManagerBase.hpp:81
static bool constexpr CopyNeedsStatement
Definition reuseIndexManagerBase.hpp:80
void addToTapeValues(TapeValues &values) const
Definition reuseIndexManagerBase.hpp:265
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