ADOL-C: Automatic Differentiation in C/C++ 2.7.3
Loading...
Searching...
No Matches
tapeinfos.h
Go to the documentation of this file.
1#ifndef ADOLC_TAPEINFOS_H
2#define ADOLC_TAPEINFOS_H
3
4#include <adolc/adalloc.h>
5#include <adolc/adolcerror.h>
6#include <adolc/oplate.h>
8#include <array>
9#include <atomic> /* handling file names over different threads*/
10#include <cstdio>
11#include <cstring>
12#include <string>
13#include <utility>
14
24struct TapeInfos {
25
26 // named indices of to print out value tape stats
28 NUM_INDEPENDENTS, /* # of independent variables */
29 NUM_DEPENDENTS, /* # of dependent variables */
30 NUM_MAX_LIVES, /* max # of live variables */
31 NUM_TAYS, /* # of values in the taylor (value) stack */
32 OP_BUFFER_SIZE, /* # of operations per buffer == OBUFSIZE (usrparms.h) */
33 NUM_OPERATIONS, /* overall # of operations */
34 OP_FILE_ACCESS, /* operations file written or not */
35 NUM_LOCATIONS, /* overall # of locations */
36 LOC_FILE_ACCESS, /* locations file written or not */
37 NUM_VALUES, /* overall # of values */
38 VAL_FILE_ACCESS, /* values file written or not */
39 LOC_BUFFER_SIZE, /* # of locations per buffer == LBUFSIZE (usrparms.h) */
40 VAL_BUFFER_SIZE, /* # of values per buffer == CBUFSIZE(usrparms.h) */
41 TAY_BUFFER_SIZE, /* # of taylors per buffer <= TBUFSIZE (usrparms.h) */
42 NUM_EQ_PROD, /* # of eq_*_prod for sparsity pattern */
43 NO_MIN_MAX, /* no use of min_op, deferred to abs_op for piecewise stuff */
44 NUM_SWITCHES, /* # of abs calls that can switch branch */
45 NUM_PARAM, /* no of parameters (doubles) interchangeable without retaping */
46 STAT_SIZE /* represents the size of the stats vector */
47 };
48 // storage order used by the tape I/O layer
50
51 // tape types => used for file name generation
53
55 for (auto &fileName : fileNames) {
56 if (fileName) {
57 if (keepTape == 0 || skipFileCleanup == 0)
58 remove(fileName);
59 delete[] fileName;
60 fileName = nullptr;
61 }
62 }
63 };
64 TapeInfos() = default;
71 TapeInfos(short tapeId, std::array<std::string, 4> &&tapeBaseNames)
72 : tapeBaseNames_(std::move(tapeBaseNames)), tapeId_(tapeId) {
77 };
78 TapeInfos(const TapeInfos &) = delete;
79 TapeInfos &operator=(const TapeInfos &) = delete;
80 TapeInfos(TapeInfos &&other) noexcept
81 : stats(other.stats), fileNames(other.fileNames),
82 tapeBaseNames_(std::move(other.tapeBaseNames_)), tapeId_(other.tapeId_),
83 keepTape(other.keepTape), skipFileCleanup(other.skipFileCleanup) {
84 other.fileNames.fill(nullptr);
85 }
86 TapeInfos &operator=(TapeInfos &&other) noexcept {
87 if (this != &other) {
88 for (auto &fileName : fileNames) {
89 delete[] fileName;
90 }
91 stats = std::move(other.stats);
92 tapeBaseNames_ = std::move(other.tapeBaseNames_);
93 fileNames = std::move(other.fileNames);
94 tapeId_ = other.tapeId_;
95 keepTape = other.keepTape;
96 skipFileCleanup = other.skipFileCleanup;
97 other.fileNames.fill(nullptr);
98 }
99 return *this;
100 }
101
102 using StatArray = std::array<size_t, STAT_SIZE>;
104 std::array<char *, 4> fileNames{};
105 // the base names of every tape type
106 std::array<std::string, 4> tapeBaseNames_;
107 short tapeId_{-1};
108
109 constexpr static size_t maxLocsPerOp{10}; // used in tape_loc_...
110
111 // - remember if tapes shall be written out to disk
112 // - this information can only be given at taping time and must survive all
113 // other actions on the tape
114 int keepTape{0};
115
116 // defaults to 0, if 1 skips file removal (when file operations are costly)
118
119 /****************************************************************************/
120 /* Tries to read a local config file containing, e.g., buffer sizes */
121 /****************************************************************************/
122 static char *duplicatestr(const char *instr) {
123 size_t len = std::strlen(instr);
124 char *outstr = new char[len + 1];
125 std::strncpy(outstr, instr, len);
126 return outstr;
127 }
128
135 static std::atomic<int> nextId{0};
136 thread_local int id = nextId++;
137 return id;
138 }
139 /****************************************************************************/
140 /* Returns the char*: tapeBaseName+thread-threadNumber+tapeId+.tap+\0 */
141 /* The result string must be freed be the caller! */
142 /****************************************************************************/
143 char *createFileName(short tapeId, int tapeType) {
144 std::string fileName(tapeBaseNames_[tapeType]);
145
146 int threadId = getThreadIndex();
147 fileName += "thread-" + std::to_string(threadId) + "_";
148
149 fileName += "tape-" + std::to_string(tapeId) + ".tap";
150
151 // don't forget space for null termination
152 char *ret_char = new char[fileName.size() + 1];
153 std::strcpy(ret_char, fileName.c_str()); // ensures null terminatoin
154 return ret_char;
155 }
156};
157
158#endif // ADOLC_TAPEINFOS_H
ErrorType
Definition adolcerror.h:89
Identity, statistics, file names, and recording metadata of one tape.
Definition tapeinfos.h:24
int getThreadIndex()
Generates an id for the thread within the function is called.
Definition tapeinfos.h:134
TapeInfos & operator=(const TapeInfos &)=delete
static char * duplicatestr(const char *instr)
Definition tapeinfos.h:122
std::array< std::string, 4 > tapeBaseNames_
Definition tapeinfos.h:106
TapeInfos(TapeInfos &&other) noexcept
Definition tapeinfos.h:80
std::array< char *, 4 > fileNames
Definition tapeinfos.h:104
int skipFileCleanup
Definition tapeinfos.h:117
TapeInfos & operator=(TapeInfos &&other) noexcept
Definition tapeinfos.h:86
TAPENAMES
Definition tapeinfos.h:52
@ TAYLORS_TAPE
Definition tapeinfos.h:52
@ VALUES_TAPE
Definition tapeinfos.h:52
@ LOCATIONS_TAPE
Definition tapeinfos.h:52
@ OPERATIONS_TAPE
Definition tapeinfos.h:52
TapeInfos(short tapeId, std::array< std::string, 4 > &&tapeBaseNames)
Definition tapeinfos.h:71
StatEntries
Definition tapeinfos.h:27
@ OP_FILE_ACCESS
Definition tapeinfos.h:34
@ NUM_DEPENDENTS
Definition tapeinfos.h:29
@ NUM_TAYS
Definition tapeinfos.h:31
@ NUM_EQ_PROD
Definition tapeinfos.h:42
@ NUM_SWITCHES
Definition tapeinfos.h:44
@ NUM_PARAM
Definition tapeinfos.h:45
@ LOC_FILE_ACCESS
Definition tapeinfos.h:36
@ NUM_INDEPENDENTS
Definition tapeinfos.h:28
@ 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
@ NO_MIN_MAX
Definition tapeinfos.h:43
@ NUM_VALUES
Definition tapeinfos.h:37
@ LOC_BUFFER_SIZE
Definition tapeinfos.h:39
@ VAL_FILE_ACCESS
Definition tapeinfos.h:38
@ STAT_SIZE
Definition tapeinfos.h:46
@ NUM_MAX_LIVES
Definition tapeinfos.h:30
@ OP_BUFFER_SIZE
Definition tapeinfos.h:32
TapeInfos(short tapeId)
Definition tapeinfos.h:65
FILES
Definition tapeinfos.h:49
@ OPERATIONS_FILE
Definition tapeinfos.h:49
@ LOCATIONS_FILE
Definition tapeinfos.h:49
@ TAYLORS_FILE
Definition tapeinfos.h:49
@ VALUES_FILE
Definition tapeinfos.h:49
char * createFileName(short tapeId, int tapeType)
Definition tapeinfos.h:143
StatArray stats
Definition tapeinfos.h:103
int keepTape
Definition tapeinfos.h:114
static constexpr size_t maxLocsPerOp
Definition tapeinfos.h:109
TapeInfos()=default
~TapeInfos()
Definition tapeinfos.h:54
std::array< size_t, STAT_SIZE > StatArray
Definition tapeinfos.h:102
short tapeId_
Definition tapeinfos.h:107
TapeInfos(const TapeInfos &)=delete
Numeric limits for taped active scalars.
Definition adtb_types.h:1417