37#include <opdi/tool/toolInterface.hpp>
40#include "../../../expressions/lhsExpressionInterface.hpp"
41#include "../../../expressions/parallelActiveType.hpp"
43#include "../../../tapes/interfaces/editingTapeInterface.hpp"
44#include "../../../tapes/misc/vectorAccessInterface.hpp"
45#include "../../../traits/atomicTraits.hpp"
46#include "openMPReverseAtomic.hpp"
48#ifndef DOXYGEN_DISABLE
50template<
typename T_CoDiType>
51struct CoDiOpDiLibTool :
public opdi::ToolInterface {
53 using CoDiType =
CODI_DD(T_CoDiType, codi::CODI_DEFAULT_PARALLEL_ACTIVE_TYPE);
54 using Real =
typename CoDiType::Real;
55 using Identifier =
typename CoDiType::Identifier;
56 using Tape =
typename CoDiType::Tape;
57 using Position =
typename Tape::Position;
62 static void callHandleReverse(Tape*,
void* handlePtr,
VAI*) {
63 opdi::Handle* handle = (opdi::Handle*)handlePtr;
64 handle->reverseFunc(handle->data);
67 static void callHandleDelete(Tape*,
void* handlePtr) {
68 opdi::Handle* handle = (opdi::Handle*)handlePtr;
69 if (handle->deleteFunc !=
nullptr) {
70 handle->deleteFunc(handle->data);
81 return (
void*)
new Tape;
84 void deleteTape(
void* tapePtr) {
85 Tape* tape = (Tape*)tapePtr;
89 void* allocPosition() {
90 return new Position();
93 void freePosition(
void* positionPtr) {
94 Position* position = (Position*)positionPtr;
98 size_t getPositionSize() {
99 return sizeof(Position);
102 std::string positionToString(
void* positionPtr) {
103 Position* position = (Position*)positionPtr;
104 std::stringstream conv;
109 void getTapePosition(
void* tapePtr,
void* positionPtr) {
110 Tape* tape = (Tape*)tapePtr;
111 Position* position = (Position*)positionPtr;
113 *position = tape->getPosition();
116 void getZeroPosition(
void* tapePtr,
void* positionPtr) {
117 Tape* tape = (Tape*)tapePtr;
118 Position* position = (Position*)positionPtr;
120 *position = tape->getZeroPosition();
123 void copyPosition(
void* dstPtr,
void* srcPtr) {
124 Position* dst = (Position*)dstPtr;
125 Position* src = (Position*)srcPtr;
130 int comparePosition(
void* lhsPtr,
void* rhsPtr) {
131 Position* lhs = (Position*)lhsPtr;
132 Position* rhs = (Position*)rhsPtr;
145 bool isActive(
void* tapePtr) {
146 Tape* tape = (Tape*)tapePtr;
147 return tape->isActive();
150 void setActive(
void* tapePtr,
bool active) {
151 Tape* tape = (Tape*)tapePtr;
159 void evaluate(
void* tapePtr,
void* startPtr,
void* endPtr,
bool useAtomics =
true) {
160 Tape* tape = (Tape*)tapePtr;
161 Position* start = (Position*)startPtr;
162 Position* end = (Position*)endPtr;
164 if (tape->isActive()) {
165 std::cerr <<
"Warning: OpDiLib evaluation of an active tape." << std::endl;
172 tape->evaluate(*start, *end,
reinterpret_cast<AtomicGradient*
>(&tape->gradient(0)));
174 tape->evaluate(*start, *end,
reinterpret_cast<NonAtomicGradient*
>(&tape->gradient(0)));
178 void reset(
void* tapePtr,
bool clearAdjoints) {
179 Tape* tape = (Tape*)tapePtr;
180 tape->reset(clearAdjoints);
183 void reset(
void* tapePtr,
void* positionPtr,
bool clearAdjoints) {
184 Tape* tape = (Tape*)tapePtr;
185 Position* position = (Position*)positionPtr;
186 tape->resetTo(*position, clearAdjoints);
189 void* getThreadLocalTape() {
190 return (
void*)CoDiType::getTapePtr();
193 void setThreadLocalTape(
void* tapePtr) {
194 Tape* tape = (Tape*)tapePtr;
195 CoDiType::setTapePtr(tape);
198 void pushExternalFunction(
void* tapePtr, opdi::Handle
const* handle) {
199 Tape* tape = (Tape*)tapePtr;
201 CoDiOpDiLibTool::callHandleDelete));
204 void erase(
void* tapePtr,
void* startPtr,
void* endPtr) {
205 Tape* tape = (Tape*)tapePtr;
206 Position* start = (Position*)startPtr;
207 Position* end = (Position*)endPtr;
209 tape->erase(*start, *end);
212 void append(
void* dstTapePtr,
void* srcTapePtr,
void* startPtr,
void* endPtr) {
213 Tape* dstTape = (Tape*)dstTapePtr;
214 Tape* srcTape = (Tape*)srcTapePtr;
215 Position* start = (Position*)startPtr;
216 Position* end = (Position*)endPtr;
218 dstTape->append(*srcTape, *start, *end);
#define CODI_DD(Type, Default)
Abbreviation for CODI_DECLARE_DEFAULT.
Definition macros.hpp:94
typename RemoveAtomicImpl< Type >::Type RemoveAtomic
Wrapper for removing atomic from a type.
Definition atomicTraits.hpp:83
Represents a concrete lvalue in the CoDiPack expression tree.
Definition activeType.hpp:52
User-defined evaluation functions for the taping process.
Definition externalFunction.hpp:102
Reverse atomic implementation for OpenMP.
Definition openMPReverseAtomic.hpp:61
Unified access to the adjoint vector and primal vector in a tape evaluation.
Definition vectorAccessInterface.hpp:91