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 "openMPAtomic.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;
168 typename Tape::Gradient* adjoints = &tape->gradient(0);
173 AtomicGradient* safeAdjoints = (AtomicGradient*)adjoints;
174 tape->evaluate(*start, *end, safeAdjoints);
176 NonAtomicGradient* unsafeAdjoints = (NonAtomicGradient*)adjoints;
177 tape->evaluate(*start, *end, unsafeAdjoints);
181 void reset(
void* tapePtr,
bool clearAdjoints) {
182 Tape* tape = (Tape*)tapePtr;
183 tape->reset(clearAdjoints);
186 void reset(
void* tapePtr,
void* positionPtr,
bool clearAdjoints) {
187 Tape* tape = (Tape*)tapePtr;
188 Position* position = (Position*)positionPtr;
189 tape->resetTo(*position, clearAdjoints);
192 void* getThreadLocalTape() {
193 return (
void*)CoDiType::getTapePtr();
196 void setThreadLocalTape(
void* tapePtr) {
197 Tape* tape = (Tape*)tapePtr;
198 CoDiType::setTapePtr(tape);
201 void pushExternalFunction(
void* tapePtr, opdi::Handle
const* handle) {
202 Tape* tape = (Tape*)tapePtr;
204 CoDiOpDiLibTool::callHandleDelete));
207 void erase(
void* tapePtr,
void* startPtr,
void* endPtr) {
208 Tape* tape = (Tape*)tapePtr;
209 Position* start = (Position*)startPtr;
210 Position* end = (Position*)endPtr;
212 tape->erase(*start, *end);
215 void append(
void* dstTapePtr,
void* srcTapePtr,
void* startPtr,
void* endPtr) {
216 Tape* dstTape = (Tape*)dstTapePtr;
217 Tape* srcTape = (Tape*)srcTapePtr;
218 Position* start = (Position*)startPtr;
219 Position* end = (Position*)endPtr;
221 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
Atomic implementation for OpenMP.
Definition openMPAtomic.hpp:61
Unified access to the adjoint vector and primal vector in a tape evaluation.
Definition vectorAccessInterface.hpp:91