CoDiPack  3.1.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
localAdjoints.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 "../../traits/adjointVectorTraits.hpp"
41#include "internalAdjointsInterface.hpp"
42
44namespace codi {
45
53 template<typename T_Gradient, typename T_Identifier, typename T_Tape>
54 struct LocalAdjoints : public InternalAdjointsInterface<T_Gradient, T_Identifier, T_Tape> {
55 public:
56
57 using Tape = CODI_DD(T_Tape, CODI_DEFAULT_TAPE);
58 using Gradient = CODI_DD(T_Gradient, double);
59 using Identifier = CODI_DD(T_Identifier, int);
60
61 private:
62
63 std::vector<Gradient> adjoints;
64
65 public:
66
68 LocalAdjoints(size_t initialSize)
69 : InternalAdjointsInterface<Gradient, Identifier, Tape>(initialSize), adjoints(initialSize) {}
70
73 return adjoints[(size_t)identifier];
74 }
75
77 CODI_INLINE Gradient const& operator[](Identifier const& identifier) const {
78 return adjoints[(size_t)identifier];
79 }
80
83 return adjoints.data();
84 }
85
87 CODI_INLINE size_t size() const {
88 return adjoints.size();
89 }
90
92 CODI_NO_INLINE void resize(Identifier const& newSize) {
93 adjoints.resize((size_t)newSize);
94 }
95
97 CODI_INLINE void zeroAll(Identifier const& maxIdentifier) {
98 Identifier maxSize = std::min(maxIdentifier + 1, (Identifier)adjoints.size());
99 for (Identifier i = 0; i < maxSize; i += 1) {
100 adjoints[i] = Gradient();
101 }
102 }
103
106 std::swap(adjoints, other.adjoints);
107 }
108
111
114 };
115
116#ifndef DOXYGEN_DISABLE
117
119 namespace AdjointVectorTraits {
120 template<typename T_Gradient, typename T_Identifier, typename T_Tape>
121 struct GradientImplementation<LocalAdjoints<T_Gradient, T_Identifier, T_Tape>> {
122 public:
123 using Gradient = T_Gradient;
124 };
125 }
126#endif
127}
#define CODI_NO_INLINE
See codi::Config::AvoidedInlines.
Definition config.h:426
#define CODI_INLINE
See codi::Config::ForcedInlines.
Definition config.h:469
#define CODI_DD(Type, Default)
Abbreviation for CODI_DECLARE_DEFAULT.
Definition macros.hpp:97
CoDiPack - Code Differentiation Package.
Definition codi.hpp:97
InternalAdjointsInterface(size_t initialSize)
Definition internalAdjointsInterface.hpp:90
LocalAdjoints(size_t initialSize)
Constructor.
Definition localAdjoints.hpp:68
inlineGradient * data()
Return an array-like object for data access, for example a pointer to an underlying array or a refere...
Definition localAdjoints.hpp:82
inlinevoid beginUse()
Declare that the adjoints are in use, e.g., during a tape evaluation, and cannot be resized right now...
Definition localAdjoints.hpp:110
inlinevoid endUse()
Declare that the adjoints are no longer occupied.
Definition localAdjoints.hpp:113
inlinesize_t size() const
Returns the number of adjoint variables. Internally, declares usage of the adjoints.
Definition localAdjoints.hpp:87
inlineGradient const & operator[](Identifier const &identifier) const
Constant reference access to the adjoint variable identified by identifier.
Definition localAdjoints.hpp:77
T_Tape Tape
See LocalAdjoints.
Definition localAdjoints.hpp:57
inlineGradient & operator[](Identifier const &identifier)
Reference access to the adjoint variable identified by identifier.
Definition localAdjoints.hpp:72
inlinevoid zeroAll(Identifier const &maxIdentifier)
Set all adjoint variables up to and including maxIdentifier to Gradient().
Definition localAdjoints.hpp:97
T_Gradient Gradient
See LocalAdjoints.
Definition localAdjoints.hpp:58
inlinevoid swap(LocalAdjoints &other)
Swap two sets of adjoint variables. Internally, declares usage of the adjoints.
Definition localAdjoints.hpp:105
T_Identifier Identifier
See LocalAdjoints.
Definition localAdjoints.hpp:59
void resize(Identifier const &newSize)
Ensure that identifiers up to newSize can be passed to operator[] without error.
Definition localAdjoints.hpp:92