ADOL-C: Automatic Differentiation in C/C++ 2.7.3
Loading...
Searching...
No Matches
checkpointing.h
Go to the documentation of this file.
1/*----------------------------------------------------------------------------
2 ADOL-C -- Automatic Differentiation by Overloading in C++
3 File: checkpointing.h
4 Revision: $Id$
5 Contents: Provides all checkointing interfaces.
6
7 Copyright (c) Andreas Kowarz
8
9 This file is part of ADOL-C. This software is provided as open source.
10 Any use, reproduction, or distribution of the software constitutes
11 recipient's acceptance of the terms of the accompanying license file.
12
13----------------------------------------------------------------------------*/
14
15#ifndef ADOLC_CHECKPOINTING_H
16#define ADOLC_CHECKPOINTING_H
17
18#include <adolc/adolcexport.h>
20#include <array>
21#include <stack>
22
23class adouble;
24
25namespace ADOLC::CP {
26
32using TimeStepFunction = int(size_t dimX, adouble *state);
33
39using TimeStepFunctionDouble = int(size_t dimX, double *state);
40
44using SaveFunction = void *(void);
45
50using RestoreFunction = void(void *);
51
52namespace detail {
53
60using StackElement = std::array<double *, 2>;
61
69struct Infos {
71 // id of the outer tape, used to get checkpoint in the cp_fos_forward... and
72 // reverse methods later
73 short tapeId{0};
78 int steps{0};
80
81 // Id of the tape that stores the checkpointing steps. This id
82 // should not be confused with the id of the tape that calls
83 // the checkpointing process later
84 short cpTapeId{0};
85 bool retaping{0}; /* != 0 forces retaping before every reverse step */
86
87 int dim{0}; /* number of variables in input and output (n=m) */
88 adouble *adp_x{nullptr}; /* input of the first step */
89 adouble *adp_y{nullptr}; /* output of the last step; will be set by ADOLC */
90
91 int check{0};
92 int capo{0};
93 int fine{0};
94 int info{0};
95 int currentCP{0};
96 double *dp_internal_for{nullptr};
97 size_t index{0};
98 // unsued but required to be stored in our custom buffer type
99 char *allmem{nullptr};
100 std::stack<StackElement> cpStack;
101
104
106 void restore();
107
109 void release();
110
113
115 void tapeStep(int keep) const;
116
118 void revolveForward(int keep);
119
122};
123} // namespace detail
131public:
132 Context() = delete;
133
141 Context(short tapeId, short tapeIdCheck, TimeStepFunction timeStepFunction) {
142 registerTimeStepFunction(tapeId, tapeIdCheck, timeStepFunction);
143 }
144
145 ~Context() = default;
146
152 void setDoubleFct(TimeStepFunctionDouble timeStepFunction) {
153 cpInfos->function_double = timeStepFunction;
154 }
155
162 void setSaveFct(SaveFunction saveState) {
163 cpInfos->saveNonAdoubles = saveState;
164 }
165
172 void setRestoreFct(RestoreFunction restoreState) {
173 cpInfos->restoreNonAdoubles = restoreState;
174 }
175
181 void setNumberOfSteps(int number) { cpInfos->steps = number; }
182
188 void setNumberOfCheckpoints(int number) { cpInfos->checkpoints = number; }
189
195 void setDimensionXY(int dim) { cpInfos->dim = dim; }
196
202 void setInput(adouble *x) { cpInfos->adp_x = x; }
203
209 void setOutput(adouble *y) { cpInfos->adp_y = y; }
210
217 void setAlwaysRetaping(bool state) { cpInfos->retaping = state; }
218
229 int checkpointing(short tapeId);
230
231private:
234 void registerTimeStepFunction(short tapeId, short tapeIdCheck,
235 TimeStepFunction timeStepFunction);
236 detail::Infos *cpInfos;
237};
238
239} // namespace ADOLC::CP
240
247
248/****************************************************************************/
249#endif /* ADOLC_CHECKPOINTING_H */
#define ADOLC_API
Definition adolcexport.h:16
ADOLC::CP::TimeStepFunctionDouble ADOLC_TimeStepFuncion_double
Definition checkpointing.h:242
ADOLC::CP::TimeStepFunction ADOLC_TimeStepFuncion
Definition checkpointing.h:241
ADOLC::CP::SaveFunction ADOLC_saveFct
Definition checkpointing.h:243
ADOLC::CP::RestoreFunction ADOLC_restoreFct
Definition checkpointing.h:244
Public checkpointing setup and execution context.
Definition checkpointing.h:130
void setInput(adouble *x)
Set the active input state of the checkpointed loop.
Definition checkpointing.h:202
void setSaveFct(SaveFunction saveState)
Register an optional callback that saves extra non-adouble state at each checkpoint.
Definition checkpointing.h:162
int checkpointing(short tapeId)
Replace the explicit time loop by a checkpointed external function call during taping.
void setNumberOfSteps(int number)
Set the total number of time steps in the checkpointed loop.
Definition checkpointing.h:181
void setOutput(adouble *y)
Set the active output state of the checkpointed loop.
Definition checkpointing.h:209
Context(short tapeId, short tapeIdCheck, TimeStepFunction timeStepFunction)
Create a checkpointing context for one time-stepping routine.
Definition checkpointing.h:141
void setDoubleFct(TimeStepFunctionDouble timeStepFunction)
Register the passive one-step function used for untaped replay.
Definition checkpointing.h:152
void setAlwaysRetaping(bool state)
Control whether the one-step tape is rebuilt before every step.
Definition checkpointing.h:217
void setRestoreFct(RestoreFunction restoreState)
Register an optional callback that restores extra non-adouble state from a checkpoint.
Definition checkpointing.h:172
void setNumberOfCheckpoints(int number)
Set the number of checkpoints available to revolve.
Definition checkpointing.h:188
void setDimensionXY(int dim)
Set the dimension of the in-place state vector.
Definition checkpointing.h:195
Taped active scalar for reusable forward and reverse differentiation.
Definition adtb_types.h:211
std::array< double *, 2 > StackElement
Snapshot entry stored on the checkpoint stack.
Definition checkpointing.h:60
Definition checkpointing.h:25
int(size_t dimX, double *state) TimeStepFunctionDouble
Signature of the passive one-step function.
Definition checkpointing.h:39
void(void *) RestoreFunction
Signature of an optional callback that restores saved non-adouble state.
Definition checkpointing.h:50
void *(void) SaveFunction
Signature of an optional callback that saves extra non-adouble state.
Definition checkpointing.h:44
int(size_t dimX, adouble *state) TimeStepFunction
Signature of the active one-step function.
Definition checkpointing.h:32
Internal checkpointing state shared between the public context and the recorded external function.
Definition checkpointing.h:69
void revolveForward(int keep)
Run the forward checkpoint schedule for the current state.
RestoreFunction * restoreNonAdoubles
Definition checkpointing.h:77
int check
Definition checkpointing.h:91
int checkpoints
Definition checkpointing.h:79
TimeStepFunctionDouble * function_double
Definition checkpointing.h:75
void clearStack()
Free all stored checkpoint snapshots.
void revolveError()
Raise an ADOL-C error for the current revolve status code.
void restore()
Restore the current checkpoint state from the top of the stack.
adouble * adp_x
Definition checkpointing.h:88
int steps
Definition checkpointing.h:78
TimeStepFunction * function
Definition checkpointing.h:74
void tapeStep(int keep) const
Tape one time step at the current internal state.
void takeSnapshot()
Save the current checkpoint state on the internal stack.
int info
Definition checkpointing.h:94
int currentCP
Definition checkpointing.h:95
adouble * adp_y
Definition checkpointing.h:89
char * allmem
Definition checkpointing.h:99
int capo
Definition checkpointing.h:92
int fine
Definition checkpointing.h:93
short tapeId
Definition checkpointing.h:73
short cpTapeId
Definition checkpointing.h:84
void release()
Drop the most recent checkpoint snapshot.
SaveFunction * saveNonAdoubles
Definition checkpointing.h:76
size_t index
Definition checkpointing.h:97
bool retaping
Definition checkpointing.h:85
std::stack< StackElement > cpStack
Definition checkpointing.h:100
~Infos()
Definition checkpointing.h:70
double * dp_internal_for
Definition checkpointing.h:96
int dim
Definition checkpointing.h:87