CoDiPack  2.2.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
lowLevelFunctionCreationUtilities.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 <tuple>
38
39#include "../../config.h"
40#include "../../misc/byteDataView.hpp"
41#include "../../misc/macros.hpp"
42#include "storeAndRestoreActions.hpp"
43#include "traits/activeArgumentStoreTraits.hpp"
44#include "traits/passiveArgumentStoreTraits.hpp"
45
47namespace codi {
48
127 template<size_t T_ActiveArguments>
129 static size_t constexpr ActiveArguments =
130 CODI_DD(T_ActiveArguments, 1);
131
132 static_assert(ActiveArguments <= 64, "More than 64 active arguments are currently not supported.");
133
134 // clang-format off
137 typename std::conditional<ActiveArguments <= 8, uint8_t,
138 typename std::conditional<ActiveArguments <= 16, uint16_t,
139 typename std::conditional<ActiveArguments <= 32, uint32_t,
140 typename std::conditional<ActiveArguments <= 64, uint64_t,
141 void>::type>::type>::type>::type;
142 // clang-format on
143
145 template<typename T>
147
149 template<typename T, typename S = T>
151
153 static size_t constexpr TokenSize = sizeof(codi::Config::LowLevelFunctionToken);
154
155 /*******************************************************************************/
158
160 CODI_INLINE static RestoreActions createRestoreActions(bool isInput, bool isOutput, bool isInputActive,
161 bool primalRequired) {
162 RestoreActions actions = {};
163
164 if (isInput && primalRequired) {
165 actions |= RestoreAction::PrimalRestore;
166 } else if (isOutput) {
167 actions |= RestoreAction::PrimalCreate;
168 }
169
170 if (isInput && isInputActive) {
171 actions |= RestoreAction::InputIdentifierRestore | RestoreAction::InputGradientCreate;
172 }
173 if (isOutput) {
174 actions |= RestoreAction::OutputIdentifierRestore | RestoreAction::OutputGradientCreate;
175 }
176
177 return actions;
178 }
179
181 CODI_INLINE static StoreActions createStoreActions(bool tapeActive, bool isInput, bool isOutput,
182 bool isInputActive, bool primalRequired) {
183 StoreActions actions = {};
184
185 if (tapeActive && isInput && primalRequired) {
186 actions |= StoreAction::PrimalCreateOnTape;
187 }
188
189 if (isInput) {
190 actions |= StoreAction::PrimalExtract;
191 }
192
193 if (tapeActive) {
194 if (isInput && isInputActive) {
195 actions |= StoreAction::InputIdentifierCreateAndStore;
196 }
197 if (isOutput) {
198 actions |= StoreAction::OutputIdentifierCreate;
199 }
200 }
201
202 return actions;
203 }
204
206 /*******************************************************************************/
209
212 return sizeof(ActivityStoreType);
213 }
214
216 CODI_INLINE static bool getActivity(ActivityStoreType const& activity, size_t arg) {
217 return 0 != (activity & ((ActivityStoreType)1) << arg);
218 }
219
221 CODI_INLINE static void restoreActivity(ByteDataView* fixedStore, ActivityStoreType& activity) {
222 activity = fixedStore->read<ActivityStoreType>();
223 }
224
226 CODI_INLINE static void setActivity(ActivityStoreType& activity, size_t arg, bool active) {
227 activity |= ((ActivityStoreType)active) << arg;
228 }
229
231 CODI_INLINE static void storeActivity(ByteDataView* fixedStore, ActivityStoreType const& activity) {
232 fixedStore->write(activity);
233 }
234
236 };
237}
#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
uint16_t LowLevelFunctionToken
Token type for low level functions in the tapes.
Definition config.h:108
CoDiPack - Code Differentiation Package.
Definition codi.hpp:90
Traits for storing active arguments in byte streams.
Definition activeArgumentStoreTraits.hpp:204
Definition byteDataView.hpp:51
T * read(size_t size)
Read an array of length size of types T.
Definition byteDataView.hpp:85
T * write(T const &data)
Write a single entry of type T.
Definition byteDataView.hpp:114
A bitset with enum items as flags.
Definition enumBitset.hpp:64
Helper structure for storing low level functions and their arguments on a tape.
Definition lowLevelFunctionCreationUtilities.hpp:128
static size_t constexpr ActiveArguments
See LowLevelFunctionCreationUtilities.
Definition lowLevelFunctionCreationUtilities.hpp:129
static void storeActivity(ByteDataView *fixedStore, ActivityStoreType const &activity)
Store the activity structure in the data stream.
Definition lowLevelFunctionCreationUtilities.hpp:231
static StoreActions createStoreActions(bool tapeActive, bool isInput, bool isOutput, bool isInputActive, bool primalRequired)
Collect the store actions for an argument in a StoreActions instance.
Definition lowLevelFunctionCreationUtilities.hpp:181
static size_t countActivitySize()
Return the size of the activity structure.
Definition lowLevelFunctionCreationUtilities.hpp:211
static RestoreActions createRestoreActions(bool isInput, bool isOutput, bool isInputActive, bool primalRequired)
Collect the restore actions for an argument in a RestoreActions instance.
Definition lowLevelFunctionCreationUtilities.hpp:160
static bool getActivity(ActivityStoreType const &activity, size_t arg)
Check the activity structure for activity of a specific argument.
Definition lowLevelFunctionCreationUtilities.hpp:216
static size_t constexpr TokenSize
Token size for the low level function token.
Definition lowLevelFunctionCreationUtilities.hpp:153
static void restoreActivity(ByteDataView *fixedStore, ActivityStoreType &activity)
Restore the activity structure from the data stream.
Definition lowLevelFunctionCreationUtilities.hpp:221
static void setActivity(ActivityStoreType &activity, size_t arg, bool active)
Store the activity of an argument in the activity structure.
Definition lowLevelFunctionCreationUtilities.hpp:226
typename std::conditional< ActiveArguments<=8, uint8_t, typename std::conditional< ActiveArguments<=16, uint16_t, typename std::conditional< ActiveArguments<=32, uint32_t, typename std::conditional< ActiveArguments<=64, uint64_t, void >::type >::type >::type >::type ActivityStoreType
Type for the activity store. Currently limited to 64 variables.
Definition lowLevelFunctionCreationUtilities.hpp:136
Traits for storing passive arguments in byte streams.
Definition passiveArgumentStoreTraits.hpp:57