77 enum class LocalReductionOperation {
83 enum class EntryType {
93 LocalReductionOperation operation;
96 Entry() : name(), type(), operation(), pos() {}
98 Entry(std::string
const& name, EntryType
const& type, LocalReductionOperation
const& operation,
100 : name(name), type(type), operation(operation), pos(pos) {}
106 std::vector<Entry> data;
108 Section() : name(), data() {}
110 Section(std::string
const& name) : name(name), data() {}
113 std::vector<Section> sections;
115 std::vector<double> doubleData;
116 std::vector<long> longData;
117 std::vector<unsigned long> unsignedLongData;
119 size_t usedMemoryIndex;
120 size_t allocatedMemoryIndex;
126 : sections(), doubleData(), longData(), unsignedLongData(), usedMemoryIndex(0), allocatedMemoryIndex(1) {
128 addEntryInternal(
"Total memory used", EntryType::Double, LocalReductionOperation::Sum, doubleData, 0.0);
129 addEntryInternal(
"Total memory allocated", EntryType::Double, LocalReductionOperation::Sum, doubleData, 0.0);
138 LocalReductionOperation operation = LocalReductionOperation::Sum,
bool usedMem =
false,
139 bool allocatedMem =
false) {
140 addEntryInternal(name, EntryType::Double, operation, doubleData, value);
143 doubleData[usedMemoryIndex] += value;
147 doubleData[allocatedMemoryIndex] += value;
153 LocalReductionOperation operation = LocalReductionOperation::Sum) {
154 addEntryInternal(name, EntryType::Long, operation, longData, value);
159 sections.push_back(Section(name));
164 LocalReductionOperation operation = LocalReductionOperation::Sum) {
165 addEntryInternal(name, EntryType::UnsignedLong, operation, unsignedLongData, value);
174 template<
typename Stream = std::ostream>
176 std::string
const hLine =
"-------------------------------------\n";
178 size_t maxNameSize = getMaximumNameLength();
179 size_t maxValueSize = std::max((
size_t)10, getMaximumValueLength());
182 for (Section
const& section : sections) {
183 out << std::left << section.name <<
"\n";
186 for (Entry
const& entry : section.data) {
187 out <<
" " << std::left << std::setw(maxNameSize) << entry.name <<
" : "
188 << formatEntry(entry, maxValueSize) <<
"\n";
191 if (!section.data.empty()) {
198 template<
typename Stream = std::ostream>
201 for (Section
const& section : sections) {
202 for (Entry
const& entry : section.data) {
208 out << section.name <<
"-" << entry.name;
216 template<
typename Stream = std::ostream>
218 size_t maxValueSize = std::max((
size_t)10, getMaximumValueLength());
221 for (Section
const& section : sections) {
222 for (Entry
const& entry : section.data) {
228 out << formatEntry(entry, maxValueSize);
243 codiAssert(this->sections.size() == other.sections.size());
245 for (
size_t section = 0; section < this->sections.size(); ++section) {
246 auto& thisSection = this->sections[section];
247 auto const& otherSection = other.sections[section];
250 codiAssert(thisSection.name == otherSection.name);
253 codiAssert(thisSection.data.size() == otherSection.data.size());
255 for (
size_t entry = 0; entry < thisSection.data.size(); ++entry) {
256 auto& thisEntry = thisSection.data[entry];
257 auto const& otherEntry = otherSection.data[entry];
260 codiAssert(thisEntry.name == otherEntry.name);
261 codiAssert(thisEntry.type == otherEntry.type);
262 codiAssert(thisEntry.operation == otherEntry.operation);
264 switch (thisEntry.type) {
265 case EntryType::Double: {
266 performLocalReduction(this->doubleData[thisEntry.pos], other.doubleData[otherEntry.pos],
267 thisEntry.operation);
270 case EntryType::Long: {
271 performLocalReduction(this->longData[thisEntry.pos], other.longData[otherEntry.pos],
272 thisEntry.operation);
275 case EntryType::UnsignedLong: {
276 performLocalReduction(this->unsignedLongData[thisEntry.pos], other.unsignedLongData[otherEntry.pos],
277 thisEntry.operation);
298 MPI_Allreduce(MPI_IN_PLACE, doubleData.data(), doubleData.size(), MPI_DOUBLE, MPI_SUM, communicator);
299 MPI_Allreduce(MPI_IN_PLACE, longData.data(), longData.size(), MPI_LONG, MPI_SUM, communicator);
300 MPI_Allreduce(MPI_IN_PLACE, unsignedLongData.data(), unsignedLongData.size(), MPI_UNSIGNED_LONG, MPI_SUM,
304 template<
typename Comm>
312 return doubleData[allocatedMemoryIndex];
317 return doubleData[usedMemoryIndex];
325 void performLocalReduction(T& lhs, T
const& rhs, LocalReductionOperation operation) {
327 case LocalReductionOperation::Sum: {
331 case LocalReductionOperation::Max: {
332 lhs = std::max(lhs, rhs);
339 void addEntryInternal(std::string
const& name, EntryType
const& type, LocalReductionOperation
const& operation,
340 std::vector<T>& vector, T
const& value) {
341 size_t entryPos = vector.size();
342 vector.push_back(value);
344 if (sections.empty()) {
348 sections.back().data.push_back(Entry(name, type, operation, entryPos));
351 std::string formatEntry(Entry
const& entry,
int maximumFieldSize)
const {
352 return formatEntryFull(entry,
true, maximumFieldSize);
355 std::string formatEntryFull(Entry
const& entry,
bool outputType,
int maximumFieldSize)
const {
356 std::stringstream ss;
358 switch (entry.type) {
359 case EntryType::Double: {
360 double formattedData = doubleData[entry.pos];
361 std::string typeString =
"";
364 formatSizeHumanReadable(formattedData, typeString);
366 ss << std::right << std::setiosflags(std::ios::fixed) << std::setprecision(2) << std::setw(maximumFieldSize)
367 << formattedData << typeString;
369 case EntryType::Long:
370 ss << std::right << std::setw(maximumFieldSize) << longData[entry.pos];
372 case EntryType::UnsignedLong:
373 ss << std::right << std::setw(maximumFieldSize) << unsignedLongData[entry.pos];
376 CODI_EXCEPTION(
"Unimplemented switch case.");
383 size_t formatEntryLength(Entry
const& entry)
const {
384 return formatEntryFull(entry,
false, 0).size();
387 void formatSizeHumanReadable(
double& size, std::string& type)
const {
388 char const*
const typeList[] = {
"B",
"KB",
"MB",
"GB",
"TB"};
389 size_t typeListSize =
sizeof(typeList) /
sizeof(typeList[0]);
392 while (pos < typeListSize && size > 1024.0) {
398 type += typeList[pos];
401 size_t getMaximumNameLength()
const {
402 size_t maxLength = 0;
403 for (Section
const& section : sections) {
404 for (Entry
const& data : section.data) {
405 maxLength = std::max(maxLength, data.name.size());
412 size_t getMaximumValueLength()
const {
413 size_t maxLength = 0;
414 for (Section
const& section : sections) {
415 for (Entry
const& data : section.data) {
416 maxLength = std::max(maxLength, formatEntryLength(data));
void addDoubleEntry(std::string const &name, double const &value, LocalReductionOperation operation=LocalReductionOperation::Sum, bool usedMem=false, bool allocatedMem=false)
Add double entry. If it is a memory entry, it should be in bytes.
Definition tapeValues.hpp:137