1#ifndef ADOLC_TAPE_EVALUATION_CONTEXT_H
2#define ADOLC_TAPE_EVALUATION_CONTEXT_H
11#include <shared_mutex>
18struct TapeEvaluationContext;
22 std::is_same_v<T, TapeRecordingContext>;
50 if (originCtx_ !=
nullptr) {
62 std::shared_lock<std::shared_mutex> &&lock,
63 std::shared_lock<std::shared_mutex> &&dataAccessLock)
64 : lock_(
std::move(lock)), dataAccessLock_(
std::move(dataAccessLock)) {
65 copyData(tapeCtx, stats);
70 std::shared_lock<std::shared_mutex> &&dataAccessLock) noexcept
71 : lock_(std::move(lock)), dataAccessLock_(std::move(dataAccessLock)),
73 moveData(std::move(other));
80 : lock_(std::move(other.lock_)),
81 dataAccessLock_(std::move(other.dataAccessLock_)),
82 originCtx_(std::exchange(other.originCtx_,
nullptr)) {
83 moveData(std::move(other));
109 using Lock = std::variant<std::unique_lock<std::shared_mutex>,
110 std::shared_lock<std::shared_mutex>>;
113 std::shared_lock<std::shared_mutex> dataAccessLock_;
135 if (other.signature !=
nullptr) {
139 if (other.paramstore !=
nullptr) {
145 template <EvalOrRecordContextType Context>
146 void moveData(Context &&other)
noexcept {
163 signature = std::exchange(other.signature,
nullptr);
164 paramstore = std::exchange(other.paramstore,
nullptr);
197 originCtx_ =
nullptr;
200 template <InfoType<TapeEvaluationContext, ErrorType> Info>
204 auto &buffer = Info::getBuffer(*
this);
205 if (buffer.file() ==
nullptr) {
206 if (Info::removeExistingBeforeWrite) {
207 buffer.openFile(fileName,
"rb");
208 if (buffer.file() !=
nullptr) {
210 if (remove(fileName)) {
215 buffer.openFile(fileName, Info::openWriteMode);
219 template <InfoType<TapeEvaluationContext, ErrorType> Info>
220 void put_block(
const char *fileName,
size_t lengthBlock) {
221 using ADOLC::detail::write;
225 openFile<Info>(fileName);
226 const size_t numChunks = lengthBlock / Info::chunkSize;
228 for (
size_t chunk = 0; chunk < numChunks; chunk++) {
229 auto returnCode = write<TapeEvaluationContext, ErrorType, Info>(
230 *
this, chunk, Info::chunkSize);
235 const size_t remain = lengthBlock % Info::chunkSize;
237 auto returnCode = write<TapeEvaluationContext, ErrorType, Info>(
238 *
this, numChunks, remain);
243 auto &buffer = Info::getBuffer(*
this);
244 buffer.numOnTape(buffer.numOnTape() + lengthBlock);
248 template <InfoType<TapeEvaluationContext, ErrorType> Info>
251 auto &buffer = Info::getBuffer(*
this);
252 if (!buffer.isOwner()) {
253 buffer.allocateAndOwn();
255 const size_t numChunks = blockSize / Info::chunkSize;
256 for (
size_t chunk = 0; chunk < numChunks; chunk++) {
258 fread(buffer.begin() + (chunk * Info::chunkSize),
259 Info::chunkSize *
sizeof(
typename Info::value_type), 1,
265 const size_t remain = blockSize % Info::chunkSize;
268 fread(buffer.begin() + (numChunks * Info::chunkSize),
269 remain *
sizeof(
typename Info::value_type), 1, buffer.file());
276 template <InfoType<TapeEvaluationContext, ErrorType> Info>
278 auto &buffer = Info::getBuffer(*
this);
279 const size_t blockSize = std::min(bufferSize, buffer.numOnTape());
280 loadBlockIntoBuffer<Info>(blockSize);
281 Info::updateBufferStatsForward(*
this, blockSize);
282 Info::updateBufferPositionForward(*
this);
285 template <InfoType<TapeEvaluationContext, ErrorType> Info>
289 auto &buffer = Info::getBuffer(*
this);
290 const long pos = Info::reverseSeekOffset(*
this, blockSize);
291 const auto ret = fseek(buffer.file(), pos, SEEK_SET);
296 loadBlockIntoBuffer<Info>(blockSize);
297 Info::updateBufferStatsReverse(*
this, blockSize);
298 Info::updateBufferPositionReverse(*
this, blockSize);
301 template <InfoType<TapeEvaluationContext, ErrorType> Info>
303 auto &buffer = Info::getBuffer(*
this);
304 Info::ensureReverseReadable(*
this, blockSize);
305 return buffer.retreatAndRead();
308 template <InfoType<TapeEvaluationContext, ErrorType> Info>
310 auto &buffer = Info::getBuffer(*
this);
311 return buffer.readAndAdvance();
328 loadBlockIntoBufferReverse<ValInfoT>(valueBufferSize);
338 remove(tay_fileName);
342 const char *tay_fileName) {
372 const char *tay_fileName);
375 const char *tay_fileName);
385 int numDir,
const char *tay_fileName) {
387 for (
int j = 0; j < numDir; ++j) {
388 for (
int i = 0; i < keep; ++i) {
393 ++taylorCoefficientPos;
396 taylorCoefficientPos += degree - keep;
400 void get_taylors(
double *taylorCoefficients, std::ptrdiff_t degree,
401 size_t taylorBufferSize);
404 size_t taylorBufferSize);
#define CURRENT_LOCATION
Definition adolcerror.h:26
T * current()
Returns a pointer to the element at position().
Definition bufferstate.h:305
size_t position() const
Returns the current buffer index.
Definition bufferstate.h:260
void closeFile()
Closes the owned FILE handle, if present.
Definition bufferstate.h:206
T * begin()
Returns the beginning of the owned or viewed buffer.
Definition bufferstate.h:232
void allocIfNull(size_t capacity)
Allocates a new owned buffer if no buffer is currently present.
Definition bufferstate.h:251
void writeAndAdvance(T val)
Stores val at position(), then advances to the next element.
Definition bufferstate.h:299
FILE * file()
Returns the owned FILE handle without transferring ownership.
Definition bufferstate.h:202
size_t numOnTape() const
Returns the number of elements recorded on the tape.
Definition bufferstate.h:275
size_t capacity() const
Returns the capacity of the owned or viewed buffer.
Definition bufferstate.h:272
Definition tapeevaluationcontext.h:21
void ADOLC_API fail(ErrorType error, const source_location LocInfo, const FailInfo &failinfo=FailInfo())
Definition bufferstate.h:12
BufferState< double > ValBuffer
Definition bufferstate.h:329
BufferState< size_t > LocBuffer
Definition bufferstate.h:330
BufferState< unsigned char > OpBuffer
Operation tape buffer.
Definition bufferstate.h:328
constexpr BufferViewTag bufferView
Definition bufferstate.h:51
BufferState< double > TayBuffer
Definition bufferstate.h:331
Per-sweep state used while evaluating a recorded tape.
Definition tapeevaluationcontext.h:32
void loadBlockIntoBufferReverse(size_t blockSize)
Definition tapeevaluationcontext.h:286
TapeEvaluationContext(const TapeEvaluationContext &)=delete
TapeEvaluationContext & operator=(TapeEvaluationContext &&other) noexcept=delete
double * get_val_v_r(size_t size)
Definition tapeevaluationcontext.h:320
ADOLC::detail::LocBuffer locBuffer_
Definition tapeevaluationcontext.h:91
void loadBlockIntoBuffer(size_t blockSize)
Definition tapeevaluationcontext.h:249
void write_taylor(double *taylorCoefficientPos, std::ptrdiff_t keep, const char *tay_fileName)
int deg_save
Definition tapeevaluationcontext.h:98
TapeEvaluationContext & operator=(const TapeEvaluationContext &)=delete
size_t taylor_close(const char *tay_fileName)
Definition tapeevaluationcontext.h:356
size_t numSwitches
Definition tapeevaluationcontext.h:101
void discard_params_r(size_t valueBufferSize, size_t numParam)
size_t tay_numInds
Definition tapeevaluationcontext.h:99
static constexpr StatEntries VAL_FILE_ACCESS
Definition tapeevaluationcontext.h:44
ADOLC::detail::TayBuffer tayBuffer_
Definition tapeevaluationcontext.h:92
void releaseTo(TapeRecordingContext &recordCtx)
Definition tapeevaluationcontext.h:174
void taylor_back(size_t taylorBufferSize, short tapeId, const char *tay_fileName)
static constexpr StatEntries NUM_OPERATIONS
Definition tapeevaluationcontext.h:35
void closeSweepFiles()
Definition tapeevaluationcontext.h:168
TapeEvaluationContext(TapeRecordingContext &&other, std::unique_lock< std::shared_mutex > &&lock, std::shared_lock< std::shared_mutex > &&dataAccessLock) noexcept
Definition tapeevaluationcontext.h:68
TapeEvaluationContext(const TapeRecordingContext &tapeCtx, TapeInfos::StatArray stats, std::shared_lock< std::shared_mutex > &&lock, std::shared_lock< std::shared_mutex > &&dataAccessLock)
Definition tapeevaluationcontext.h:60
static constexpr StatEntries NUM_VALUES
Definition tapeevaluationcontext.h:43
static constexpr StatEntries LOC_BUFFER_SIZE
Definition tapeevaluationcontext.h:38
bool nestedReverseEval
Definition tapeevaluationcontext.h:102
void get_taylors(double *taylorCoefficients, std::ptrdiff_t degree, size_t taylorBufferSize)
static constexpr StatEntries LOC_FILE_ACCESS
Definition tapeevaluationcontext.h:40
void write_scaylor(double val, const char *tay_fileName)
Definition tapeevaluationcontext.h:377
void taylor_begin(int degreeSave, size_t taylorBufferSize, const char *tay_fileName)
Definition tapeevaluationcontext.h:341
void write_taylors(double *taylorCoefficientPos, int keep, int degree, int numDir, const char *tay_fileName)
Definition tapeevaluationcontext.h:384
double * get_val_v_f(size_t size)
Definition tapeevaluationcontext.h:314
void loadBlockIntoBufferForward(size_t bufferSize)
Definition tapeevaluationcontext.h:277
static constexpr StatEntries OP_BUFFER_SIZE
Definition tapeevaluationcontext.h:34
TapeEvaluationContext()=delete
static constexpr StatEntries VAL_BUFFER_SIZE
Definition tapeevaluationcontext.h:42
static constexpr StatEntries NUM_TAYS
Definition tapeevaluationcontext.h:47
size_t numDeps
Definition tapeevaluationcontext.h:95
ADOLC::detail::ValBuffer valBuffer_
Definition tapeevaluationcontext.h:90
static constexpr StatEntries TAY_BUFFER_SIZE
Definition tapeevaluationcontext.h:46
ADOLC::detail::OpBuffer opBuffer_
Definition tapeevaluationcontext.h:89
static constexpr StatEntries OP_FILE_ACCESS
Definition tapeevaluationcontext.h:36
int keepTaylors
Definition tapeevaluationcontext.h:96
size_t tay_numDeps
Definition tapeevaluationcontext.h:100
size_t num_eq_prod
Definition tapeevaluationcontext.h:97
double * paramstore
Definition tapeevaluationcontext.h:106
static constexpr StatEntries NUM_LOCATIONS
Definition tapeevaluationcontext.h:39
~TapeEvaluationContext()
Definition tapeevaluationcontext.h:49
TapeEvaluationContext(TapeEvaluationContext &&other) noexcept
Definition tapeevaluationcontext.h:79
void finish_tay_file(const char *tay_fileName)
Definition tapeevaluationcontext.h:334
void reset_val_r(size_t valueBufferSize)
Definition tapeevaluationcontext.h:325
void openFile(const char *fileName)
Definition tapeevaluationcontext.h:201
Info::value_type loadNextReverse(size_t blockSize)
Definition tapeevaluationcontext.h:302
void put_block(const char *fileName, size_t lengthBlock)
Definition tapeevaluationcontext.h:220
void get_taylors_p(double *taylorCoefficients, int degree, int numDir, size_t taylorBufferSize)
double * signature
Definition tapeevaluationcontext.h:105
size_t numInds
Definition tapeevaluationcontext.h:94
char lastTayBlockInCore
Definition tapeevaluationcontext.h:104
Info::value_type loadNextForward()
Definition tapeevaluationcontext.h:309
size_t nextBufferNumber
Definition tapeevaluationcontext.h:103
Mutable buffers and bookkeeping used while recording a tape.
Definition taperecordingcontext.h:20
size_t nextBufferNumber
Definition taperecordingcontext.h:88
bool nestedReverseEval
Definition taperecordingcontext.h:85
int deg_save
Definition taperecordingcontext.h:71
size_t num_eq_prod
Definition taperecordingcontext.h:68
ADOLC::detail::TayBuffer tayBuffer_
Definition taperecordingcontext.h:61
ADOLC::detail::OpBuffer opBuffer_
Definition taperecordingcontext.h:58
double * signature
Definition taperecordingcontext.h:93
int keepTaylors
Definition taperecordingcontext.h:66
ADOLC::detail::LocBuffer locBuffer_
Definition taperecordingcontext.h:60
double * paramstore
Definition taperecordingcontext.h:94
size_t numSwitches
Definition taperecordingcontext.h:77
size_t tay_numDeps
Definition taperecordingcontext.h:75
ADOLC::detail::ValBuffer valBuffer_
Definition taperecordingcontext.h:59
size_t numInds
Definition taperecordingcontext.h:63
size_t tay_numInds
Definition taperecordingcontext.h:73
size_t numDeps
Definition taperecordingcontext.h:64
char lastTayBlockInCore
Definition taperecordingcontext.h:92
Adapter for the Taylor tape (tay tape).
Definition infotype.h:240
Adapter for the values tape (val tape).
Definition infotype.h:202
StatEntries
Definition tapeinfos.h:27
@ OP_FILE_ACCESS
Definition tapeinfos.h:34
@ NUM_TAYS
Definition tapeinfos.h:31
@ NUM_SWITCHES
Definition tapeinfos.h:44
@ NUM_PARAM
Definition tapeinfos.h:45
@ 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
std::array< size_t, STAT_SIZE > StatArray
Definition tapeinfos.h:102
Numeric limits for taped active scalars.
Definition adtb_types.h:1417