CoDiPack  2.2.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
threadSafeGlobalAdjoints.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://www.scicomp.uni-kl.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://www.scicomp.uni-kl.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 "../../tools/parallel/parallelToolbox.hpp"
38#include "internalAdjointsInterface.hpp"
39
41namespace codi {
42
51 template<typename T_Gradient, typename T_Identifier, typename T_Tape, typename T_ParallelToolbox>
52 struct ThreadSafeGlobalAdjoints : public InternalAdjointsInterface<T_Gradient, T_Identifier, T_Tape> {
53 public:
54
57 using Gradient = CODI_DD(T_Gradient, double);
58 using Identifier = CODI_DD(T_Identifier, int);
60 using ParallelToolbox = CODI_DD(T_ParallelToolbox, CODI_DEFAULT_PARALLEL_TOOLBOX);
61
65
66 private:
67
68 static std::vector<Gradient> adjoints;
69
72 static ReadWriteMutex adjointsMutex;
73
74 public:
75
77 ThreadSafeGlobalAdjoints(size_t initialSize)
79
83 return adjoints[(size_t)identifier];
84 }
85
88 CODI_INLINE Gradient const& operator[](Identifier const& identifier) const {
89 return adjoints[(size_t)identifier];
90 }
91
94 LockForUse lock(adjointsMutex);
95 return adjoints.data();
96 }
97
99 CODI_INLINE size_t size() const {
100 LockForUse lock(adjointsMutex);
101 return adjoints.size();
102 }
103
105 CODI_NO_INLINE void resize(Identifier const& newSize) {
106 LockForRealloc lock(adjointsMutex);
107 adjoints.resize((size_t)newSize);
108 }
109
112 for (Gradient& gradient : adjoints) {
113 gradient = Gradient();
114 }
115 }
116
119 /* Adjoints in this implementation are a static global member. Therefore, there is no need to swap them. */
120 }
121
125 adjointsMutex.lockRead();
126 }
127
131 adjointsMutex.unlockRead();
132 }
133 };
134
135 template<typename Gradient, typename Identifier, typename Tape, typename ParallelToolbox>
136 std::vector<CODI_DD(Gradient, double)>
137 ThreadSafeGlobalAdjoints<Gradient, Identifier, Tape, ParallelToolbox>::adjoints(1);
138
139 template<typename Gradient, typename Identifier, typename Tape, typename ParallelToolbox>
140 typename CODI_DD(ParallelToolbox, CODI_DEFAULT_PARALLEL_TOOLBOX)::ReadWriteMutex
141 ThreadSafeGlobalAdjoints<Gradient, Identifier, Tape, ParallelToolbox>::adjointsMutex;
142}
#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
#define CODI_T(...)
Abbreviation for CODI_TEMPLATE.
Definition: macros.hpp:111
CoDiPack - Code Differentiation Package.
Definition: codi.hpp:90
Full tape interface that supports all features of CoDiPack.
Definition: fullTapeInterface.hpp:82
Abstracts the internal set of adjoint variables provided as part of the tape.
Definition: internalAdjointsInterface.hpp:80
codi::LockForRead< ReadWriteMutex > LockForRead
See codi::LockForRead.
Definition: parallelToolbox.hpp:85
codi::ReadWriteMutex< ThreadInformation, Atomic< int > > ReadWriteMutex
See codi::ReadWriteMutex.
Definition: parallelToolbox.hpp:84
codi::LockForWrite< ReadWriteMutex > LockForWrite
See codi::LockForWrite.
Definition: parallelToolbox.hpp:86
Provides global adjoint variables owned by a tape type. Thread-safe for use in parallel taping.
Definition: threadSafeGlobalAdjoints.hpp:52
Gradient & operator[](Identifier const &identifier)
Reference access to the adjoint variable identified by identifier.
Definition: threadSafeGlobalAdjoints.hpp:82
void beginUse()
Declare that the adjoints are in use, e.g., during a tape evaluation, and cannot be resized right now...
Definition: threadSafeGlobalAdjoints.hpp:124
void zeroAll()
Set all adjoint variables to Gradient().
Definition: threadSafeGlobalAdjoints.hpp:111
void endUse()
Declare that the adjoints are no longer occupied.
Definition: threadSafeGlobalAdjoints.hpp:130
void swap(ThreadSafeGlobalAdjoints &)
Swap two sets of adjoint variables. Internally, declares usage of the adjoints.
Definition: threadSafeGlobalAdjoints.hpp:118
T_Tape Tape
See ThreadSafeGlobalAdjoints.
Definition: threadSafeGlobalAdjoints.hpp:56
Gradient const & operator[](Identifier const &identifier) const
Constant reference access to the adjoint variable identified by identifier.
Definition: threadSafeGlobalAdjoints.hpp:88
typename ParallelToolbox::LockForWrite LockForRealloc
See ParallelToolbox.
Definition: threadSafeGlobalAdjoints.hpp:64
Gradient * data()
Pointer to an underlying array implementation.
Definition: threadSafeGlobalAdjoints.hpp:93
typename ParallelToolbox::ReadWriteMutex ReadWriteMutex
See ParallelToolbox.
Definition: threadSafeGlobalAdjoints.hpp:62
void resize(Identifier const &newSize)
Ensure that identifiers up to newSize can be passed to operator[] without error.
Definition: threadSafeGlobalAdjoints.hpp:105
size_t size() const
Returns the number of adjoint variables. Internally, declares usage of the adjoints.
Definition: threadSafeGlobalAdjoints.hpp:99
ThreadSafeGlobalAdjoints(size_t initialSize)
Constructor.
Definition: threadSafeGlobalAdjoints.hpp:77
typename ParallelToolbox::LockForRead LockForUse
See ParallelToolbox.
Definition: threadSafeGlobalAdjoints.hpp:63
T_Identifier Identifier
Definition: threadSafeGlobalAdjoints.hpp:58
T_Gradient Gradient
See ThreadSafeGlobalAdjoints.
Definition: threadSafeGlobalAdjoints.hpp:57
T_ParallelToolbox ParallelToolbox
See ThreadSafeGlobalAdjoints.
Definition: threadSafeGlobalAdjoints.hpp:60