ADOL-C: Automatic Differentiation in C/C++ 2.7.3
Loading...
Searching...
No Matches
taperecordingcontext.h
Go to the documentation of this file.
1#ifndef ADOLC_TAPE_RECORDING_CONTEXT_H
2#define ADOLC_TAPE_RECORDING_CONTEXT_H
3
6#include <cstddef>
7#include <cstdio>
8#include <cstring>
9#include <utility>
10
11namespace ADOLC::detail {
12
25
29
33
36
38 delete[] signature;
39 signature = nullptr;
40 delete[] paramstore;
41 paramstore = nullptr;
42 }
43
45
48
50 moveData(std::move(other));
51 }
52
54 if (this != &other)
55 moveData(std::move(other));
56 return *this;
57 }
62
63 size_t numInds{0};
64 size_t numDeps{0};
65 // 1 - write taylor stack in taping mode
67
68 size_t num_eq_prod{0};
69
70 // degree to save and saved respectively
71 int deg_save{0};
72 // # of independents for the taylor stack
73 size_t tay_numInds{0};
74 // # of dependents for the taylor stack
75 size_t tay_numDeps{0};
76
77 size_t numSwitches{0};
85 bool nestedReverseEval{false};
86
87 // the next Taylor buffer to read back
89
90 // == 1 if last taylor buffer is still in
91 // in core(first call of reverse)
93 double *signature{nullptr};
94 double *paramstore{nullptr};
95
96private:
97 void moveData(TapeRecordingContext &&other) noexcept {
98 opBuffer_ = std::move(other.opBuffer_);
99 valBuffer_ = std::move(other.valBuffer_);
100 locBuffer_ = std::move(other.locBuffer_);
101 tayBuffer_ = std::move(other.tayBuffer_);
102
103 numInds = other.numInds;
104 numDeps = other.numDeps;
105 keepTaylors = other.keepTaylors;
106 num_eq_prod = other.num_eq_prod;
107 deg_save = other.deg_save;
108 tay_numInds = other.tay_numInds;
109 tay_numDeps = other.tay_numDeps;
110 numSwitches = other.numSwitches;
111 nestedReverseEval = other.nestedReverseEval;
112 nextBufferNumber = other.nextBufferNumber;
113 lastTayBlockInCore = other.lastTayBlockInCore;
114
115 delete[] signature;
116 signature = std::exchange(other.signature, nullptr);
117 delete[] paramstore;
118 paramstore = std::exchange(other.paramstore, nullptr);
119 }
120
121public:
122 // functions for handling loc tape
123 void put_loc(size_t loc) { locBuffer_.writeAndAdvance(loc); }
124
129 template <InfoType<TapeRecordingContext, ErrorType> Info>
130 void openFile(const char *fileName) {
131 using ADOLCError::fail;
133 auto &buffer = Info::getBuffer(*this);
134 if (buffer.file() == nullptr) {
135 if (Info::removeExistingBeforeWrite) {
136 buffer.openFile(fileName, "rb");
137 if (buffer.file() != nullptr) {
138 buffer.closeFile();
139 if (remove(fileName)) {
140 fail(CANNOT_REMOVE_FILE, CURRENT_LOCATION);
141 }
142 }
143 }
144 buffer.openFile(fileName, Info::openWriteMode);
145 }
146 }
147
151 template <InfoType<TapeRecordingContext, ErrorType> Info>
152 void put_block(const char *fileName, size_t lengthBlock) {
153 using ADOLC::detail::write;
154 using ADOLCError::fail;
156
157 openFile<Info>(fileName);
158 const size_t numChunks = lengthBlock / Info::chunkSize;
159
160 for (size_t chunk = 0; chunk < numChunks; chunk++) {
161 auto returnCode = write<TapeRecordingContext, ErrorType, Info>(
162 *this, chunk, Info::chunkSize);
163 if (returnCode != 1)
164 fail(TAPING_FATAL_IO_ERROR, CURRENT_LOCATION);
165 }
166
167 const size_t remain = lengthBlock % Info::chunkSize;
168 if (remain != 0) {
169 auto returnCode = write<TapeRecordingContext, ErrorType, Info>(
170 *this, numChunks, remain);
171 if (returnCode != 1)
172 fail(TAPING_FATAL_IO_ERROR, CURRENT_LOCATION);
173 }
174
175 auto &buffer = Info::getBuffer(*this);
176 buffer.numOnTape(buffer.numOnTape() + lengthBlock);
177 buffer.position(0);
178 }
179
180 /****************************************************************************/
181 /* Write some constants to the buffer without disk access */
182 /****************************************************************************/
183 void put_vals_notWriteBlock(double *vals, size_t numVals) {
184 for (size_t i = 0; i < numVals; ++i) {
186 }
187 }
188 void put_op(OPCODES op, const char *loc_fileName, const char *op_fileName,
189 const char *val_fileName, size_t reserveExtraLocations = 0);
190 void put_vals_writeBlock(double *vals, size_t numVals,
191 const char *op_fileName, const char *val_fileName);
192 size_t get_val_space(const char *op_fileName, const char *val_fileName);
193
194 // writes a single element (x) to the taylor buffer and writes the buffer
195 // to disk if necessary
196 void write_scaylor(double val, const char *tay_fileName) {
199 put_block<TayInfoT>(tay_fileName, tayBuffer_.capacity());
201 }
202
203 /* Write_scaylors writes # size elements from x to the taylor buffer. */
204 /****************************************************************************/
205 void write_scaylors(const double *taylorCoefficientPos, std::ptrdiff_t size,
206 const char *tay_fileName);
207
208 /****************************************************************************/
209 /* Update locations tape to remove assignments involving temp. variables. */
210 /* e.g. t = a + b ; y = t => y = a + b */
211 /****************************************************************************/
212 int upd_resloc(size_t temp, size_t lhs) {
213 // LocBuffer points to the first entry of the Locations and CurrLoc-1 to the
214 // last placed location in the buffer. Thus, the check ask if there is no
215 // element on the tape.
216 if (locBuffer_.position() < 1)
217 return 0;
218 if (temp == locBuffer_[locBuffer_.position() - 1]) {
219 locBuffer_[locBuffer_.position() - 1] = lhs;
220 return 1;
221 }
222 return 0;
223 }
224
225 int upd_resloc_check(const size_t temp) {
226 // LocBuffer points to the first entry of the Locations and CurrLoc-1 to the
227 // last placed location in the buffer. Thus, the check ask if there is no
228 // element on the tape.
229 if (locBuffer_.position() < 1)
230 return 0;
231 // checks if tape-element represented by "tmp" is the last created.
232 if (temp == locBuffer_[locBuffer_.position() - 1]) {
233 return 1;
234 }
235 return 0;
236 }
237
238 /****************************************************************************/
239 /* Update locations and operations tape to remove special operations inv. */
240 /* temporary variables. e.g. t = a * b ; y += t => y += a * b */
241 /****************************************************************************/
242 int upd_resloc_inc_prod(size_t temp, size_t newlhs, unsigned char newop) {
243 if (locBuffer_.position() < 3)
244 return 0;
245 if (opBuffer_.position() < 1)
246 return 0;
247 if (temp == locBuffer_[locBuffer_.position() - 1] &&
249 /* skipping recursive case */
250 newlhs != locBuffer_[locBuffer_.position() - 2] &&
251 newlhs != locBuffer_[locBuffer_.position() - 3]) {
252 locBuffer_[locBuffer_.position() - 1] = newlhs;
253 opBuffer_[opBuffer_.position() - 1] = newop;
254 return 1;
255 }
256 return 0;
257 }
258};
259
260} // namespace ADOLC::detail
261
262#endif // ADOLC_TAPE_RECORDING_CONTEXT_H
#define CURRENT_LOCATION
Definition adolcerror.h:26
size_t position() const
Returns the current buffer index.
Definition bufferstate.h:260
void writeAndAdvance(T val)
Stores val at position(), then advances to the next element.
Definition bufferstate.h:299
size_t capacity() const
Returns the capacity of the owned or viewed buffer.
Definition bufferstate.h:272
void ADOLC_API fail(ErrorType error, const source_location LocInfo, const FailInfo &failinfo=FailInfo())
Definition bufferstate.h:12
OPCODES
Definition oplate.h:23
@ mult_a_a
Definition oplate.h:39
Mutable buffers and bookkeeping used while recording a tape.
Definition taperecordingcontext.h:20
~TapeRecordingContext()
Definition taperecordingcontext.h:37
int upd_resloc(size_t temp, size_t lhs)
Definition taperecordingcontext.h:212
TapeRecordingContext & operator=(TapeRecordingContext &&other) noexcept
Definition taperecordingcontext.h:53
size_t nextBufferNumber
Definition taperecordingcontext.h:88
void put_vals_notWriteBlock(double *vals, size_t numVals)
Definition taperecordingcontext.h:183
static constexpr StatEntries VAL_BUFFER_SIZE
Definition taperecordingcontext.h:30
static constexpr StatEntries TAY_BUFFER_SIZE
Definition taperecordingcontext.h:34
bool nestedReverseEval
Definition taperecordingcontext.h:85
int deg_save
Definition taperecordingcontext.h:71
static constexpr StatEntries LOC_BUFFER_SIZE
Definition taperecordingcontext.h:26
size_t num_eq_prod
Definition taperecordingcontext.h:68
void put_loc(size_t loc)
Definition taperecordingcontext.h:123
ADOLC::detail::TayBuffer tayBuffer_
Definition taperecordingcontext.h:61
ADOLC::detail::OpBuffer opBuffer_
Definition taperecordingcontext.h:58
double * signature
Definition taperecordingcontext.h:93
int upd_resloc_check(const size_t temp)
Definition taperecordingcontext.h:225
void put_vals_writeBlock(double *vals, size_t numVals, const char *op_fileName, const char *val_fileName)
int keepTaylors
Definition taperecordingcontext.h:66
TapeRecordingContext(const TapeRecordingContext &)=delete
ADOLC::detail::LocBuffer locBuffer_
Definition taperecordingcontext.h:60
double * paramstore
Definition taperecordingcontext.h:94
static constexpr StatEntries OP_BUFFER_SIZE
Definition taperecordingcontext.h:22
static constexpr StatEntries LOC_FILE_ACCESS
Definition taperecordingcontext.h:28
void put_block(const char *fileName, size_t lengthBlock)
Flush the current in-memory tape buffer to disk.
Definition taperecordingcontext.h:152
static constexpr StatEntries NUM_LOCATIONS
Definition taperecordingcontext.h:27
void write_scaylors(const double *taylorCoefficientPos, std::ptrdiff_t size, const char *tay_fileName)
size_t numSwitches
Definition taperecordingcontext.h:77
size_t tay_numDeps
Definition taperecordingcontext.h:75
void write_scaylor(double val, const char *tay_fileName)
Definition taperecordingcontext.h:196
ADOLC::detail::ValBuffer valBuffer_
Definition taperecordingcontext.h:59
void put_op(OPCODES op, const char *loc_fileName, const char *op_fileName, const char *val_fileName, size_t reserveExtraLocations=0)
size_t numInds
Definition taperecordingcontext.h:63
static constexpr StatEntries NUM_OPERATIONS
Definition taperecordingcontext.h:23
size_t tay_numInds
Definition taperecordingcontext.h:73
TapeRecordingContext & operator=(const TapeRecordingContext &)=delete
static constexpr StatEntries VAL_FILE_ACCESS
Definition taperecordingcontext.h:32
TapeRecordingContext(TapeRecordingContext &&other) noexcept
Definition taperecordingcontext.h:49
size_t numDeps
Definition taperecordingcontext.h:64
static constexpr StatEntries NUM_VALUES
Definition taperecordingcontext.h:31
static constexpr StatEntries OP_FILE_ACCESS
Definition taperecordingcontext.h:24
char lastTayBlockInCore
Definition taperecordingcontext.h:92
int upd_resloc_inc_prod(size_t temp, size_t newlhs, unsigned char newop)
Definition taperecordingcontext.h:242
size_t get_val_space(const char *op_fileName, const char *val_fileName)
static constexpr StatEntries NUM_TAYS
Definition taperecordingcontext.h:35
void openFile(const char *fileName)
Ensure that the tape file associated with Info exists and is ready for writing.
Definition taperecordingcontext.h:130
Adapter for the Taylor tape (tay tape).
Definition infotype.h:240
StatEntries
Definition tapeinfos.h:27
@ OP_FILE_ACCESS
Definition tapeinfos.h:34
@ NUM_TAYS
Definition tapeinfos.h:31
@ LOC_FILE_ACCESS
Definition tapeinfos.h:36
@ NUM_OPERATIONS
Definition tapeinfos.h:33
@ TAY_BUFFER_SIZE
Definition tapeinfos.h:41
@ VAL_BUFFER_SIZE
Definition tapeinfos.h:40
@ NUM_LOCATIONS
Definition tapeinfos.h:35
@ NUM_VALUES
Definition tapeinfos.h:37
@ LOC_BUFFER_SIZE
Definition tapeinfos.h:39
@ VAL_FILE_ACCESS
Definition tapeinfos.h:38
@ OP_BUFFER_SIZE
Definition tapeinfos.h:32