CoDiPack  2.2.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
91 static Atomic<T_Index> globalMaximumIndex;
92 static bool globalMaximumIndexInitialized;
93 static ReadWriteMutex globalMaximumIndexMutex;
94
95 public:
96
100 ParallelReuseIndexManager(Index const& reservedIndices) {
101 globalMaximumIndexMutex.lockWrite();
102 if (!globalMaximumIndexInitialized) {
103 globalMaximumIndex = reservedIndices;
104 globalMaximumIndexInitialized = true;
105 }
106 globalMaximumIndexMutex.unlockWrite();
107 generateNewIndices();
108 }
109
112
113 /*******************************************************************************/
116
119 void addToTapeValues(TapeValues& values) const {
120 unsigned long maximumGlobalIndex = globalMaximumIndex;
121
122 values.addUnsignedLongEntry("Max. live indices", maximumGlobalIndex);
123 // The number of current live indices cannot be computed from one instance alone.
124 // It equals the number of maximum live indices minus the number of indices stored across all instances.
125
126 Base::addToTapeValues(values);
127 }
128
134 return globalMaximumIndex;
135 }
136
138
139 private:
140
141 CODI_NO_INLINE void generateNewIndices() {
142 // This method is only called when unused indices are empty.
143 // Initially, a number of unused indices is created which
144 // equals the number of indices we generate now, therefore
145 // we do not have to check for size.
146
147 codiAssert(this->unusedIndices.size() >= this->indexSizeIncrement);
148
149 Index upperIndexRangeBound = globalMaximumIndex += this->indexSizeIncrement; // note: atomic operation
150 Index lowerIndexRangeBound = upperIndexRangeBound - this->indexSizeIncrement;
151
152 for (size_t pos = 0; pos < this->indexSizeIncrement; ++pos) {
153 this->unusedIndices[this->unusedIndicesPos + pos] = lowerIndexRangeBound + Index(pos) + 1;
154 }
155
157 }
158 };
159
160 template<typename Index, typename ParallelToolbox>
161 typename ParallelReuseIndexManager<Index, ParallelToolbox>::template Atomic<Index>
162 ParallelReuseIndexManager<Index, ParallelToolbox>::globalMaximumIndex;
163
164 template<typename Index, typename ParallelToolbox>
165 bool ParallelReuseIndexManager<Index, ParallelToolbox>::globalMaximumIndexInitialized = false;
166
167 template<typename Index, typename ParallelToolbox>
168 typename ParallelReuseIndexManager<Index, ParallelToolbox>::ReadWriteMutex
169 ParallelReuseIndexManager<Index, ParallelToolbox>::globalMaximumIndexMutex;
170}
#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:90
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:111
void addToTapeValues(TapeValues &values) const
Add storage and other information to the tape values.
Definition parallelReuseIndexManager.hpp:119
Index getLargestCreatedIndex() const
Returns the largest created index.
Definition parallelReuseIndexManager.hpp:133
friend Base
Allow the base class to access protected and private members.
Definition parallelReuseIndexManager.hpp:67
ParallelReuseIndexManager(Index const &reservedIndices)
Definition parallelReuseIndexManager.hpp:100
T_ParallelToolbox ParallelToolbox
See ParallelReuseIndexManager.
Definition parallelReuseIndexManager.hpp:64
codi::ReadWriteMutex< ThreadInformation, Atomic< int > > ReadWriteMutex
See codi::ReadWriteMutex.
Definition parallelToolbox.hpp:84
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:233
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:73
void addUnsignedLongEntry(std::string const &name, unsigned long const &value)
Add unsigned long entry.
Definition tapeValues.hpp:150