CoDiPack  3.1.0
A Code Differentiation Package
SciComp TU Kaiserslautern
Loading...
Searching...
No Matches
externalFunction.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 "../../config.h"
38#include "../../expressions/lhsExpressionInterface.hpp"
39#include "../../misc/exceptions.hpp"
40#include "../../misc/macros.hpp"
41#include "../data/position.hpp"
42#include "../interfaces/externalFunctionTapeInterface.hpp"
43#include "../misc/vectorAccessInterface.hpp"
44#include "lowLevelFunctionEntry.hpp"
45
47namespace codi {
48
96
117 template<typename T_Tape>
119 public:
120
121 using Tape = CODI_DD(T_Tape,
123 using Real = typename Tape::Real;
124 using Identifier = typename Tape::Identifier;
125
130
131 using CallFunction = void (*)(Tape* tape, void* data,
132 VectorAccess* adjointInterface);
133 using DeleteFunction = void (*)(Tape* tape, void* data);
134 using IterateIdsFunction = void (*)(Tape* tape, void* data, IterCallback callback,
135 void* userData);
136
146
154
156 void deleteData(Tape* tape) {
157 if (funcDelete != nullptr) {
158 funcDelete(tape, data);
159 data = nullptr;
160 }
161 }
162
164 void evaluateReverse(Tape* tape, VectorAccess* adjointInterface) const {
165 if (nullptr != funcReverse) {
166 funcReverse(tape, data, adjointInterface);
167 } else {
168 CODI_EXCEPTION(
169 "Calling an external function in reverse mode without providing a reverse evaluation function.");
170 }
171 }
172
174 void evaluateForward(Tape* tape, VectorAccess* adjointInterface) const {
175 if (nullptr != funcForward) {
176 funcForward(tape, data, adjointInterface);
177 } else {
178 CODI_EXCEPTION(
179 "Calling an external function in forward mode without providing a forward evaluation function.");
180 }
181 }
182
184 void evaluatePrimal(Tape* tape, VectorAccess* adjointInterface) const {
185 if (nullptr != funcPrimal) {
186 funcPrimal(tape, data, adjointInterface);
187 } else {
188 CODI_EXCEPTION("Calling an external function in primal mode without providing a primal evaluation function.");
189 }
190 }
191
193 void iterateInputs(Tape* tape, IterCallback func, void* userData) const {
194 if (nullptr != funcIterIn) {
196 } else {
197 CODI_EXCEPTION(
198 "Calling an external function for iteration of inputs without providing an iteration function.");
199 }
200 }
201
203 void iterateOutputs(Tape* tape, IterCallback func, void* userData) const {
204 if (nullptr != funcIterOut) {
206 } else {
207 CODI_EXCEPTION(
208 "Calling an external function for iteration of outputs without providing an iteration function.");
209 }
210 }
211 };
212
223 template<typename T_Tape, typename T_Real, typename T_Identifier>
225 using Tape = CODI_DD(T_Tape, CODI_DEFAULT_TAPE);
226 using Real = CODI_DD(T_Real, double);
227 using Identifier = CODI_DD(T_Identifier, int);
228
231
235
237 CODI_INLINE static void forward(Tape* tape, ByteDataView& data, VectorAccess* access) {
238 ExtFunc* extFunc = data.read<ExtFunc>(1);
239 extFunc->evaluateForward(tape, access);
240 }
241
243 CODI_INLINE static void primal(Tape* tape, ByteDataView& data, VectorAccess* access) {
244 ExtFunc* extFunc = data.read<ExtFunc>(1);
245 extFunc->evaluatePrimal(tape, access);
246 }
247
249 CODI_INLINE static void reverse(Tape* tape, ByteDataView& data, VectorAccess* access) {
250 ExtFunc* extFunc = data.read<ExtFunc>(1);
251 extFunc->evaluateReverse(tape, access);
252 }
253
255 CODI_INLINE static void del(Tape* tape, ByteDataView& data) {
256 ExtFunc* extFunc = data.read<ExtFunc>(1);
257 extFunc->deleteData(tape);
258 }
259
261 CODI_INLINE static void iterateInputs(Tape* tape, ByteDataView& data, IterCallback func, void* userData) {
262 CODI_UNUSED(tape);
263
264 ExtFunc* extFunc = data.read<ExtFunc>(1);
265 extFunc->iterateInputs(tape, func, userData);
266 }
267
269 CODI_INLINE static void iterateOutputs(Tape* tape, ByteDataView& data, IterCallback func, void* userData) {
270 CODI_UNUSED(tape);
271
272 ExtFunc* extFunc = data.read<ExtFunc>(1);
273 extFunc->iterateOutputs(tape, func, userData);
274 }
275
277 CODI_INLINE static void store(Tape& tape, Config::LowLevelFunctionToken token, ExtFunc const& extFunc) {
278 ByteDataView data = {};
279 tape.pushLowLevelFunction(token, sizeof(ExtFunc), data);
280
281 data.write(extFunc);
282 }
283
289 };
290}
#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
#define CODI_T(...)
Abbreviation for CODI_TEMPLATE.
Definition macros.hpp:117
uint16_t LowLevelFunctionToken
Token type for low level functions in the tapes.
Definition config.h:108
CoDiPack - Code Differentiation Package.
Definition codi.hpp:97
inlinevoid CODI_UNUSED(Args const &...)
Disable unused warnings for an arbitrary number of arguments.
Definition macros.hpp:55
Definition byteDataView.hpp:51
inlineT * read(size_t size)
Read an array of length size of types T.
Definition byteDataView.hpp:98
inlineT * write(T const &data)
Write a single entry of type T.
Definition byteDataView.hpp:132
void(*)(void *id, void *userData) IterCallbackUntyped
Untyped callback function for id iteration.
Definition externalFunction.hpp:56
ExternalFunctionInternalData(CallFunctionUntyped funcReverse, CallFunctionUntyped funcForward, CallFunctionUntyped funcPrimal, DeleteFunctionUntyped funcDelete, IterateIdsFunctionUntyped funcIterIn, IterateIdsFunctionUntyped funcIterOut, void *data)
Constructor.
Definition externalFunction.hpp:84
IterateIdsFunctionUntyped funcIterIn
Iterate over inputs.
Definition externalFunction.hpp:64
void(*)(void *tape, void *data, void *adjointInterface) CallFunctionUntyped
Call function definition.
Definition externalFunction.hpp:52
ExternalFunctionInternalData()
Constructor.
Definition externalFunction.hpp:72
CallFunctionUntyped funcForward
Forward evaluation function pointer.
Definition externalFunction.hpp:61
DeleteFunctionUntyped funcDelete
User data deletion function pointer.
Definition externalFunction.hpp:63
void(*)(void *tape, void *data, IterCallbackUntyped callback, void *userData) IterateIdsFunctionUntyped
Iterate ids function definition.
Definition externalFunction.hpp:57
void * data
User data pointer.
Definition externalFunction.hpp:67
CallFunctionUntyped funcReverse
Reverse evaluation function pointer.
Definition externalFunction.hpp:60
IterateIdsFunctionUntyped funcIterOut
Iterate over outputs.
Definition externalFunction.hpp:65
CallFunctionUntyped funcPrimal
Primal evaluation function pointer.
Definition externalFunction.hpp:62
void(*)(void *tape, void *data) DeleteFunctionUntyped
Delete function definition.
Definition externalFunction.hpp:54
Low level function entry implementation for external functions.
Definition externalFunction.hpp:224
inlinestatic LowLevelFunctionEntry< Tape, Real, Identifier > create()
Create the function entry for the tape registration.
Definition externalFunction.hpp:285
T_Tape Tape
See ExternalFunctionLowLevelEntryMapper.
Definition externalFunction.hpp:225
inlinestatic void store(Tape &tape, Config::LowLevelFunctionToken token, ExtFunc const &extFunc)
Store an external function on the tape.
Definition externalFunction.hpp:277
inlinestatic void del(Tape *tape, ByteDataView &data)
Recovers the external function data and calls deleteData on it.
Definition externalFunction.hpp:255
inlinestatic void iterateInputs(Tape *tape, ByteDataView &data, IterCallback func, void *userData)
Iterate over the inputs of the external function. func is called for each input with userData.
Definition externalFunction.hpp:261
inlinestatic void forward(Tape *tape, ByteDataView &data, VectorAccess *access)
Recovers the external function data and calls evaluateForward on it.
Definition externalFunction.hpp:237
typename LowLevelFunctionEntry< T_Tape, T_Real, T_Identifier >::IterCallback IterCallback
See LowLevelFunctionEntry.
Definition externalFunction.hpp:230
T_Identifier Identifier
See ExternalFunctionLowLevelEntryMapper.
Definition externalFunction.hpp:227
inlinestatic void iterateOutputs(Tape *tape, ByteDataView &data, IterCallback func, void *userData)
Iterate over the outputs of the external function. func is called for each output with userData.
Definition externalFunction.hpp:269
ExternalFunction< Tape > ExtFunc
Definition externalFunction.hpp:232
T_Real Real
See ExternalFunctionLowLevelEntryMapper.
Definition externalFunction.hpp:226
inlinestatic void reverse(Tape *tape, ByteDataView &data, VectorAccess *access)
Recovers the external function data and calls evaluateReverse on it.
Definition externalFunction.hpp:249
inlinestatic void primal(Tape *tape, ByteDataView &data, VectorAccess *access)
Recovers the external function data and calls evaluatePrimal on it.
Definition externalFunction.hpp:243
VectorAccessInterface< Real, Identifier > VectorAccess
Shortcut for VectorAccessInterface.
Definition externalFunction.hpp:234
Add user defined functions to the tape evaluation.
Definition externalFunctionTapeInterface.hpp:76
User-defined evaluation functions for the taping process.
Definition externalFunction.hpp:118
typename Tape::Real Real
See FullTapeInterface.
Definition externalFunction.hpp:123
T_Tape Tape
See ExternalFunction.
Definition externalFunction.hpp:121
void evaluatePrimal(Tape *tape, VectorAccess *adjointInterface) const
Calls the primal function if not nullptr, otherwise throws a CODI_EXCEPTION.
Definition externalFunction.hpp:184
void deleteData(Tape *tape)
Calls the delete function if not nullptr.
Definition externalFunction.hpp:156
ExternalFunction(CallFunction funcReverse, CallFunction funcForward, CallFunction funcPrimal, void *data, DeleteFunction funcDelete, IterateIdsFunction funcIterIn, IterateIdsFunction funcIterOut)
Any arguments can be nullptr if not required.
Definition externalFunction.hpp:138
typename Tape::Identifier Identifier
See FullTapeInterface.
Definition externalFunction.hpp:124
static ExternalFunction create(CallFunction funcReverse, void *data, DeleteFunction funcDelete, CallFunction funcForward=nullptr, CallFunction funcPrimal=nullptr, IterateIdsFunction funcIterIn=nullptr, IterateIdsFunction funcIterOut=nullptr)
Helper function for the creation of an ExternalFunction object.
Definition externalFunction.hpp:148
void evaluateReverse(Tape *tape, VectorAccess *adjointInterface) const
Calls the reverse function if not nullptr, otherwise throws a CODI_EXCEPTION.
Definition externalFunction.hpp:164
void evaluateForward(Tape *tape, VectorAccess *adjointInterface) const
Calls the forward function if not nullptr, otherwise throws a CODI_EXCEPTION.
Definition externalFunction.hpp:174
VectorAccessInterface< Real, Identifier > VectorAccess
Definition externalFunction.hpp:126
void(*)(Tape *tape, void *data, IterCallback callback, void *userData) IterateIdsFunction
Iterate ids function definition.
Definition externalFunction.hpp:134
typename LowLevelFunctionEntry< Tape, Real, Identifier >::IterCallback IterCallback
Callback for iterate ids function.
Definition externalFunction.hpp:128
void iterateInputs(Tape *tape, IterCallback func, void *userData) const
Calls the iterate inputs function if not nullptr, otherwise throws a CODI_EXCEPTION.
Definition externalFunction.hpp:193
void iterateOutputs(Tape *tape, IterCallback func, void *userData) const
Calls the iterate inputs function if not nullptr, otherwise throws a CODI_EXCEPTION.
Definition externalFunction.hpp:203
void(*)(Tape *tape, void *data) DeleteFunction
Delete function definition.
Definition externalFunction.hpp:133
void(*)(Tape *tape, void *data, VectorAccess *adjointInterface) CallFunction
Call function definition.
Definition externalFunction.hpp:131
Low level function entry on the tape. See LowLevelFunctionTapeInterface for details.
Definition lowLevelFunctionEntry.hpp:69
void(*)(Identifier *id, void *userData) IterCallback
Callback function for the identifier iteration.
Definition lowLevelFunctionEntry.hpp:81
Unified access to the adjoint vector and primal vector in a tape evaluation.
Definition vectorAccessInterface.hpp:94