CoDiPack  2.3.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
openMPReverseAtomic.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 <type_traits>
38
39#include "../../../expressions/activeType.hpp"
40#include "../../../traits/atomicTraits.hpp"
41#include "../../../traits/realTraits.hpp"
42#include "../../../traits/tapeTraits.hpp"
43#include "../reverseAtomicInterface.hpp"
44#include "macros.hpp"
45
47namespace codi {
48
60 template<typename T_Type, typename T_Sfinae = void>
61 struct OpenMPReverseAtomicImpl : public ReverseAtomicInterface<T_Type, OpenMPReverseAtomicImpl<T_Type, T_Sfinae>> {
62 public:
63 using Type = T_Type;
64
67 };
68
69#ifndef DOXYGEN_DISABLE
70
71 // Specialization for arithmetic types.
72 template<typename T_Type>
73 struct OpenMPReverseAtomicImpl<T_Type, typename std::enable_if<std::is_arithmetic<T_Type>::value>::type>
75 T_Type, OpenMPReverseAtomicImpl<T_Type, typename std::enable_if<std::is_arithmetic<T_Type>::value>::type>> {
76 public:
77 using Type = T_Type;
78 using Base = ReverseAtomicInterface<
80
81 private:
82 Type value;
83
84 public:
85 CODI_INLINE OpenMPReverseAtomicImpl() : Base(), value() {}
86
87 CODI_INLINE OpenMPReverseAtomicImpl(OpenMPReverseAtomicImpl const& other) : Base(), value(other.value) {}
88
89 CODI_INLINE OpenMPReverseAtomicImpl(Type const& other) : Base(), value(other) {}
90
92 return operator=(other.value);
93 }
94
96 this->value = other;
97 return *this;
98 }
99
101 operator+=(other.value);
102 }
103
104 CODI_INLINE void operator+=(Type const& other) {
105 CODI_OMP_ATOMIC(update)
106 this->value += other;
107 }
108
109 CODI_INLINE operator Type() const {
110 return value;
111 }
112 };
113
114 // Specialization for forward CoDiPack types. Acts on value and gradient with individual atomic operations.
115 template<typename T_Type>
116 struct OpenMPReverseAtomicImpl<T_Type, TapeTraits::EnableIfForwardTape<typename T_Type::Tape>>
117 : public ReverseAtomicInterface<
118 T_Type, OpenMPReverseAtomicImpl<T_Type, TapeTraits::EnableIfForwardTape<typename T_Type::Tape>>>,
119 public T_Type {
120 public:
121 using Type = CODI_DD(T_Type, CODI_DEFAULT_LHS_EXPRESSION);
122 using Base = ReverseAtomicInterface<
123 T_Type, OpenMPReverseAtomicImpl<T_Type, TapeTraits::EnableIfForwardTape<typename T_Type::Tape>>>;
124 using Tape = typename Type::Tape;
125 using Real = typename Type::Real;
126 using Gradient = typename Type::Gradient;
127
129
130 CODI_INLINE OpenMPReverseAtomicImpl(OpenMPReverseAtomicImpl const& other) : Base(), Type(other) {}
131
132 CODI_INLINE OpenMPReverseAtomicImpl(Type const& other) : Base(), Type(other) {}
133
135 return operator=(static_cast<Type const&>(other));
136 }
137
139 Type::operator=(other);
140 return *this;
141 }
142
144 operator+=(static_cast<Type const&>(other));
145 }
146
147 CODI_INLINE void operator+=(Type const& other) {
148 OpenMPReverseAtomicImpl<Real>* atomicValue = reinterpret_cast<OpenMPReverseAtomicImpl<Real>*>(&this->value());
149 OpenMPReverseAtomicImpl<Gradient>* atomicGradient =
150 reinterpret_cast<OpenMPReverseAtomicImpl<Gradient>*>(&this->gradient());
151
152 *atomicValue += other.value();
153 *atomicGradient += other.gradient();
154 }
155
156 CODI_INLINE operator Type() const {
157 return static_cast<Type>(*this);
158 }
159 };
160
161#endif
162
165 template<typename Type>
167
169 template<typename T_Type>
170 struct AtomicTraits::IsAtomic<OpenMPReverseAtomic<T_Type>> : std::true_type {};
171
172#ifndef DOXYGEN_DISABLE
173 // Specialize IsTotalZero for OpenMPReverseAtomic on arithmetic types.
174 template<typename T_Type>
176 OpenMPReverseAtomicImpl<T_Type, typename std::enable_if<std::is_arithmetic<T_Type>::value>::type>> {
177 public:
178
179 using Type = CODI_DD(
180 CODI_T(OpenMPReverseAtomicImpl<T_Type, typename std::enable_if<std::is_arithmetic<T_Type>::value>::type>),
182
183 static CODI_INLINE bool isTotalZero(Type const& v) {
184 return typename Type::Type() == v;
185 }
186 };
187#endif
188}
#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
typename std::enable_if< IsForwardTape< Tape >::value >::type EnableIfForwardTape
Enable if wrapper for IsForwardTape.
Definition tapeTraits.hpp:93
CoDiPack - Code Differentiation Package.
Definition codi.hpp:91
Represents a concrete lvalue in the CoDiPack expression tree.
Definition activeType.hpp:52
Indicate whether a type is atomic.
Definition atomicTraits.hpp:51
Reverse atomic implementation for OpenMP.
Definition openMPReverseAtomic.hpp:61
T_Type Type
See OpenMPReverseAtomicImpl.
Definition openMPReverseAtomic.hpp:63
Function for checking if the value of the type is completely zero.
Definition realTraits.hpp:100
T_Type Type
See IsTotalZero.
Definition realTraits.hpp:103
static bool isTotalZero(Type const &v)
Checks if the values are completely zero.
Definition realTraits.hpp:106
Provides a data type on which += update operations are performed atomically.
Definition reverseAtomicInterface.hpp:60
Impl & operator=(Impl const &other)
Assignment operator with implementing type as rhs. Not atomic.
void operator+=(Impl const &other)
Atomic incremental update with implementing type as rhs.
ReverseAtomicInterface()
Constructor.
Definition reverseAtomicInterface.hpp:65