43#include "../../misc/exceptions.hpp"
75 enum class EntryType {
87 Entry() : name(), type(), pos() {}
89 Entry(std::string
const& name, EntryType
const& type,
size_t const& pos) : name(name), type(type), pos(pos) {}
95 std::vector<Entry> data;
97 Section() : name(), data() {}
99 Section(std::string
const& name) : name(name), data() {}
102 std::vector<Section> sections;
104 std::vector<double> doubleData;
105 std::vector<long> longData;
106 std::vector<unsigned long> unsignedLongData;
108 size_t usedMemoryIndex;
109 size_t allocatedMemoryIndex;
115 : sections(), doubleData(), longData(), unsignedLongData(), usedMemoryIndex(0), allocatedMemoryIndex(1) {
117 addEntryInternal(
"Total memory used", EntryType::Double, doubleData, 0.0);
118 addEntryInternal(
"Total memory allocated", EntryType::Double, doubleData, 0.0);
126 void addDoubleEntry(std::string
const& name,
double const& value,
bool usedMem =
false,
127 bool allocatedMem =
false) {
128 addEntryInternal(name, EntryType::Double, doubleData, value);
131 doubleData[usedMemoryIndex] += value;
135 doubleData[allocatedMemoryIndex] += value;
141 addEntryInternal(name, EntryType::Long, longData, value);
146 sections.push_back(Section(name));
151 addEntryInternal(name, EntryType::UnsignedLong, unsignedLongData, value);
160 template<
typename Stream = std::ostream>
162 std::string
const hLine =
"-------------------------------------\n";
164 size_t maxNameSize = getMaximumNameLength();
165 size_t maxValueSize = std::max((
size_t)10, getMaximumValueLength());
168 for (Section
const& section : sections) {
169 out << std::left << section.name <<
"\n";
172 for (Entry
const& entry : section.data) {
173 out <<
" " << std::left << std::setw(maxNameSize) << entry.name <<
" : "
174 << formatEntry(entry, maxValueSize) <<
"\n";
177 if (!section.data.empty()) {
184 template<
typename Stream = std::ostream>
187 for (Section
const& section : sections) {
188 for (Entry
const& entry : section.data) {
194 out << section.name <<
"-" << entry.name;
202 template<
typename Stream = std::ostream>
204 size_t maxValueSize = std::max((
size_t)10, getMaximumValueLength());
207 for (Section
const& section : sections) {
208 for (Entry
const& entry : section.data) {
214 out << formatEntry(entry, maxValueSize);
229 MPI_Allreduce(MPI_IN_PLACE, doubleData.data(), doubleData.size(), MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
230 MPI_Allreduce(MPI_IN_PLACE, longData.data(), longData.size(), MPI_LONG, MPI_SUM, MPI_COMM_WORLD);
231 MPI_Allreduce(MPI_IN_PLACE, unsignedLongData.data(), unsignedLongData.size(), MPI_UNSIGNED_LONG, MPI_SUM,
238 return doubleData[allocatedMemoryIndex];
243 return doubleData[usedMemoryIndex];
251 void addEntryInternal(std::string
const& name, EntryType
const& type, std::vector<T>& vector, T
const& value) {
252 size_t entryPos = vector.size();
253 vector.push_back(value);
255 if (sections.empty()) {
259 sections.back().data.push_back(Entry(name, type, entryPos));
262 std::string formatEntry(Entry
const& entry,
int maximumFieldSize)
const {
263 return formatEntryFull(entry,
true, maximumFieldSize);
266 std::string formatEntryFull(Entry
const& entry,
bool outputType,
int maximumFieldSize)
const {
267 std::stringstream ss;
269 switch (entry.type) {
270 case EntryType::Double: {
271 double formattedData = doubleData[entry.pos];
272 std::string typeString =
"";
275 formatSizeHumanReadable(formattedData, typeString);
277 ss << std::right << std::setiosflags(std::ios::fixed) << std::setprecision(2) << std::setw(maximumFieldSize)
278 << formattedData << typeString;
280 case EntryType::Long:
281 ss << std::right << std::setw(maximumFieldSize) << longData[entry.pos];
283 case EntryType::UnsignedLong:
284 ss << std::right << std::setw(maximumFieldSize) << unsignedLongData[entry.pos];
287 CODI_EXCEPTION(
"Unimplemented switch case.");
294 size_t formatEntryLength(Entry
const& entry)
const {
295 return formatEntryFull(entry,
false, 0).size();
298 void formatSizeHumanReadable(
double& size, std::string& type)
const {
299 char const*
const typeList[] = {
"B",
"KB",
"MB",
"GB",
"TB"};
300 size_t typeListSize =
sizeof(typeList) /
sizeof(typeList[0]);
303 while (pos < typeListSize && size > 1024.0) {
309 type += typeList[pos];
312 size_t getMaximumNameLength()
const {
313 size_t maxLength = 0;
314 for (Section
const& section : sections) {
315 for (Entry
const& data : section.data) {
316 maxLength = std::max(maxLength, data.name.size());
323 size_t getMaximumValueLength()
const {
324 size_t maxLength = 0;
325 for (Section
const& section : sections) {
326 for (Entry
const& data : section.data) {
327 maxLength = std::max(maxLength, formatEntryLength(data));
CoDiPack - Code Differentiation Package.
Definition codi.hpp:90
Tape information that can be printed in a pretty print format or a table format.
Definition tapeValues.hpp:73
double getAllocatedMemorySize()
Get the allocated memory in bytes.
Definition tapeValues.hpp:237
void addDoubleEntry(std::string const &name, double const &value, bool usedMem=false, bool allocatedMem=false)
Add double entry. If it is a memory entry, it should be in bytes.
Definition tapeValues.hpp:126
TapeValues(std::string const &tapeName)
Constructor.
Definition tapeValues.hpp:114
double getUsedMemorySize()
Get the used memory in bytes.
Definition tapeValues.hpp:242
void formatHeader(Stream &out=std::cout) const
Output the header for a table output.
Definition tapeValues.hpp:185
void formatDefault(Stream &out=std::cout) const
Output in a human readable format. One row per entry.
Definition tapeValues.hpp:161
void addUnsignedLongEntry(std::string const &name, unsigned long const &value)
Add unsigned long entry.
Definition tapeValues.hpp:150
void formatRow(Stream &out=std::cout) const
Output this data in one row. One entry per column.
Definition tapeValues.hpp:203
void combineData()
Perform an MPI_Allreduce with MPI_COMM_WORLD.
Definition tapeValues.hpp:227
void addSection(std::string const &name)
Add section. All further entries are added under this section.
Definition tapeValues.hpp:145
void addLongEntry(std::string const &name, long const &value)
Add long entry.
Definition tapeValues.hpp:140