CoDiPack  2.2.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-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 "../../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
51 protected:
52 typedef void (*CallFunctionUntyped)(void* tape, void* data,
53 void* adjointInterface);
54 typedef void (*DeleteFunctionUntyped)(void* tape, void* data);
55
60
61 void* data;
62
63 public:
64
67 : funcReverse(nullptr), funcForward(nullptr), funcPrimal(nullptr), funcDelete(nullptr), data(nullptr) {}
68
69 protected:
70
78 data(data) {}
79 };
80
101 template<typename T_Tape>
103 public:
104
105 using Tape = CODI_DD(T_Tape,
107
111 typedef void (*CallFunction)(Tape* tape, void* data,
112 VectorAccess* adjointInterface);
113 typedef void (*DeleteFunction)(Tape* tape, void* data);
114
122
125 CallFunction funcForward = nullptr, CallFunction funcPrimal = nullptr) {
127 }
128
130 void deleteData(Tape* tape) {
131 if (funcDelete != nullptr) {
132 funcDelete(tape, data);
133 data = nullptr;
134 }
135 }
136
138 void evaluateReverse(Tape* tape, VectorAccess* adjointInterface) const {
139 if (nullptr != funcReverse) {
140 funcReverse(tape, data, adjointInterface);
141 } else {
142 CODI_EXCEPTION(
143 "Calling an external function in reverse mode without providing a reverse evaluation function.");
144 }
145 }
146
148 void evaluateForward(Tape* tape, VectorAccess* adjointInterface) const {
149 if (nullptr != funcForward) {
150 funcForward(tape, data, adjointInterface);
151 } else {
152 CODI_EXCEPTION(
153 "Calling an external function in forward mode without providing a forward evaluation function.");
154 }
155 }
156
158 void evaluatePrimal(Tape* tape, VectorAccess* adjointInterface) const {
159 if (nullptr != funcPrimal) {
160 funcPrimal(tape, data, adjointInterface);
161 } else {
162 CODI_EXCEPTION("Calling an external function in primal mode without providing a primal evaluation function.");
163 }
164 }
165 };
166
177 template<typename T_Tape, typename T_Real, typename T_Identifier>
179 using Tape = CODI_DD(T_Tape, CODI_DEFAULT_TAPE);
180 using Real = CODI_DD(T_Real, double);
181 using Identifier = CODI_DD(T_Identifier, int);
182
186
188 CODI_INLINE static void forward(Tape* tape, ByteDataView& data, VectorAccess* access) {
189 ExtFunc* extFunc = data.read<ExtFunc>(1);
190 extFunc->evaluateForward(tape, access);
191 }
192
194 CODI_INLINE static void primal(Tape* tape, ByteDataView& data, VectorAccess* access) {
195 ExtFunc* extFunc = data.read<ExtFunc>(1);
196 extFunc->evaluatePrimal(tape, access);
197 }
198
200 CODI_INLINE static void reverse(Tape* tape, ByteDataView& data, VectorAccess* access) {
201 ExtFunc* extFunc = data.read<ExtFunc>(1);
202 extFunc->evaluateReverse(tape, access);
203 }
204
206 CODI_INLINE static void del(Tape* tape, ByteDataView& data) {
207 ExtFunc* extFunc = data.read<ExtFunc>(1);
208 extFunc->deleteData(tape);
209 }
210
212 CODI_INLINE static void store(Tape& tape, Config::LowLevelFunctionToken token, ExtFunc const& extFunc) {
213 ByteDataView data = {};
214 tape.pushLowLevelFunction(token, sizeof(ExtFunc), data);
215
216 data.write(extFunc);
217 }
218
222 }
223 };
224}
#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
uint16_t LowLevelFunctionToken
Token type for low level functions in the tapes.
Definition: config.h:108
CoDiPack - Code Differentiation Package.
Definition: codi.hpp:90
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
Internal untyped data for an external function.
Definition: externalFunction.hpp:50
ExternalFunctionInternalData()
Constructor.
Definition: externalFunction.hpp:66
ExternalFunctionInternalData(CallFunctionUntyped funcReverse, CallFunctionUntyped funcForward, CallFunctionUntyped funcPrimal, DeleteFunctionUntyped funcDelete, void *data)
Constructor.
Definition: externalFunction.hpp:72
CallFunctionUntyped funcForward
Forward evaluation function pointer.
Definition: externalFunction.hpp:57
void(* DeleteFunctionUntyped)(void *tape, void *data)
Delete function definition.
Definition: externalFunction.hpp:54
DeleteFunctionUntyped funcDelete
User data deletion function pointer.
Definition: externalFunction.hpp:59
void * data
User data pointer.
Definition: externalFunction.hpp:61
void(* CallFunctionUntyped)(void *tape, void *data, void *adjointInterface)
Call function definition.
Definition: externalFunction.hpp:52
CallFunctionUntyped funcReverse
Reverse evaluation function pointer.
Definition: externalFunction.hpp:56
CallFunctionUntyped funcPrimal
Primal evaluation function pointer.
Definition: externalFunction.hpp:58
Low level function entry implementation for external functions.
Definition: externalFunction.hpp:178
T_Real Real
See ExternalFunctionLowLevelEntryMapper.
Definition: externalFunction.hpp:180
static void del(Tape *tape, ByteDataView &data)
Recovers the external function data and calls deleteData on it.
Definition: externalFunction.hpp:206
static void primal(Tape *tape, ByteDataView &data, VectorAccess *access)
Recovers the external function data and calls evaluatePrimal on it.
Definition: externalFunction.hpp:194
static void store(Tape &tape, Config::LowLevelFunctionToken token, ExtFunc const &extFunc)
Store an external function on the tape.
Definition: externalFunction.hpp:212
static LowLevelFunctionEntry< Tape, Real, Identifier > create()
Create the function entry for the tape registration.
Definition: externalFunction.hpp:220
T_Identifier Identifier
See ExternalFunctionLowLevelEntryMapper.
Definition: externalFunction.hpp:181
static void reverse(Tape *tape, ByteDataView &data, VectorAccess *access)
Recovers the external function data and calls evaluateReverse on it.
Definition: externalFunction.hpp:200
static void forward(Tape *tape, ByteDataView &data, VectorAccess *access)
Recovers the external function data and calls evaluateForward on it.
Definition: externalFunction.hpp:188
T_Tape Tape
See ExternalFunctionLowLevelEntryMapper.
Definition: externalFunction.hpp:179
Add user defined functions to the tape evaluation.
Definition: externalFunctionTapeInterface.hpp:76
User-defined evaluation functions for the taping process.
Definition: externalFunction.hpp:102
ExternalFunction(CallFunction funcReverse, CallFunction funcForward, CallFunction funcPrimal, void *data, DeleteFunction funcDelete)
Any arguments can be nullptr if not required.
Definition: externalFunction.hpp:116
void evaluatePrimal(Tape *tape, VectorAccess *adjointInterface) const
Calls the primal function if not nullptr, otherwise throws a CODI_EXCEPTION.
Definition: externalFunction.hpp:158
void(* DeleteFunction)(Tape *tape, void *data)
Delete function definition.
Definition: externalFunction.hpp:113
static ExternalFunction create(CallFunction funcReverse, void *data, DeleteFunction funcDelete, CallFunction funcForward=nullptr, CallFunction funcPrimal=nullptr)
Helper function for the creation of an ExternalFunction object.
Definition: externalFunction.hpp:124
void deleteData(Tape *tape)
Calls the delete function if not nullptr.
Definition: externalFunction.hpp:130
void evaluateReverse(Tape *tape, VectorAccess *adjointInterface) const
Calls the reverse function if not nullptr, otherwise throws a CODI_EXCEPTION.
Definition: externalFunction.hpp:138
void evaluateForward(Tape *tape, VectorAccess *adjointInterface) const
Calls the forward function if not nullptr, otherwise throws a CODI_EXCEPTION.
Definition: externalFunction.hpp:148
T_Tape Tape
See ExternalFunction.
Definition: externalFunction.hpp:106
void(* CallFunction)(Tape *tape, void *data, VectorAccess *adjointInterface)
Call function definition.
Definition: externalFunction.hpp:111
Low level function entry on the tape. See LowLevelFunctionTapeInterface for details.
Definition: lowLevelFunctionEntry.hpp:67
Unified access to the adjoint vector and primal vector in a tape evaluation.
Definition: vectorAccessInterface.hpp:91