IPPL (Independent Parallel Particle Layer)
IPPL
Loading...
Searching...
No Matches
CommunicatorLogging.cpp
Go to the documentation of this file.
2
3#include <fstream>
4#include <iomanip>
5
6#include "Utility/Inform.h"
7
10
11namespace ippl {
12 namespace mpi {
13 void Communicator::printLogs(const std::string& filename) {
14 std::vector<LogEntry> localLogs = gatherLocalLogs();
15
16 std::vector<LogEntry> allLogs;
17 if (rank() == 0) {
18 allLogs = gatherLogsFromAllRanks(localLogs);
19 } else {
20 sendLogsToRank0(localLogs);
21 }
22
23 if (rank() == 0) {
24 writeLogsToFile(allLogs, filename);
25 }
26 }
27
28 std::vector<LogEntry> Communicator::gatherLocalLogs() {
29 std::vector<LogEntry> localLogs;
30
31 buffer_handlers_m.forAll([&](auto& loggingHandler) {
32 const auto& logs = loggingHandler.getLogs();
33 localLogs.insert(localLogs.end(), logs.begin(), logs.end());
34 });
35
36 return localLogs;
37 }
38
39 void Communicator::sendLogsToRank0(const std::vector<LogEntry>& localLogs) {
40 std::vector<char> buffer = serializeLogs(localLogs);
41
42 int logSize = buffer.size();
43
44 this->send(logSize, 1, 0, 0);
45 this->send<char>(buffer.data(), logSize, 0, 0);
46 }
47
49 const std::vector<LogEntry>& localLogs) {
50 std::vector<LogEntry> allLogs = localLogs;
51
52 for (int rank = 1; rank < size_m; ++rank) {
53 int logSize;
54 Status status;
55
56 this->recv(logSize, 1, rank, 0, status);
57
58 std::vector<char> buffer(logSize);
59 this->recv<char>(buffer.data(), logSize, rank, 0, status);
60
61 std::vector<LogEntry> deserializedLogs = deserializeLogs(buffer);
62 allLogs.insert(allLogs.end(), deserializedLogs.begin(), deserializedLogs.end());
63 }
64
65 return allLogs;
66 }
67
68 std::vector<char> serializeLogs(const std::vector<LogEntry>& logs) {
69 std::vector<char> buffer;
70
71 for (const auto& logEntry : logs) {
72 std::vector<char> serializedEntry = logEntry.serialize();
73 buffer.insert(buffer.end(), serializedEntry.begin(), serializedEntry.end());
74 }
75
76 return buffer;
77 }
78
79 std::vector<LogEntry> deserializeLogs(const std::vector<char>& buffer) {
80 std::vector<LogEntry> logs;
81 size_t offset = 0;
82
83 while (offset < buffer.size()) {
84 LogEntry logEntry = LogEntry::deserialize(buffer, offset);
85
86 logs.push_back(logEntry);
87
88 offset += logEntry.serialize().size();
89 }
90 return logs;
91 }
92
93 void Communicator::writeLogsToFile(const std::vector<LogEntry>& allLogs,
94 const std::string& filename) {
95 Inform logFile(0, filename.c_str(), Inform::OVERWRITE, 0);
96 logFile.setOutputLevel(1);
97
98 logFile << "Timestamp,Method,Rank,MemorySpace,usedSize,FreeSize,Parameters" << endl;
99
100 for (const auto& log : allLogs) {
101 auto timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(
102 log.timestamp.time_since_epoch())
103 .count();
104
105 logFile << timestamp << "," << log.methodName << "," << log.rank << ","
106 << log.memorySpace << "," << log.usedSize << "," << log.freeSize;
107
108 logFile << ",\"";
109 bool first = true;
110 for (const auto& [key, value] : log.parameters) {
111 if (!first) {
112 logFile << "; ";
113 }
114 logFile << key << ": " << value;
115 first = false;
116 }
117 logFile << "\"" << endl;
118 }
119
120 logFile.flush();
121 }
122
123 } // namespace mpi
124} // namespace ippl
constexpr KOKKOS_INLINE_FUNCTION auto first()
Definition AbsorbingBC.h:10
Inform & endl(Inform &inf)
Definition Inform.cpp:42
Definition Archive.h:20
std::vector< LogEntry > deserializeLogs(const std::vector< char > &buffer)
std::vector< char > serializeLogs(const std::vector< LogEntry > &logs)
void writeLogsToFile(const std::vector< LogEntry > &allLogs, const std::string &filename)
void send(const T &buffer, int count, int dest, int tag)
void printLogs(const std::string &filename)
int rank() const noexcept
void recv(T &output, int count, int source, int tag, Status &status)
std::vector< LogEntry > gatherLocalLogs()
buffer_handler_type buffer_handlers_m
std::vector< LogEntry > gatherLogsFromAllRanks(const std::vector< LogEntry > &localLogs)
void sendLogsToRank0(const std::vector< LogEntry > &localLogs)
static LogEntry deserialize(const std::vector< char > &buffer, size_t offset=0)
Definition LogEntry.cpp:43
std::vector< char > serialize() const
Definition LogEntry.cpp:20
@ OVERWRITE
Definition Inform.h:44
Inform & setOutputLevel(const int)
Definition Inform.cpp:228
void flush()
Definition Inform.h:113