OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
MemoryWriter.cpp
Go to the documentation of this file.
1 //
2 // Class MemoryWriter
3 // This class writes a SDDS file with virtual memory usage information.
4 //
5 // Copyright (c) 2019, Matthias Frey, Paul Scherrer Institut, Villigen PSI, Switzerland
6 // All rights reserved
7 //
8 // Implemented as part of the PhD thesis
9 // "Precise Simulations of Multibunches in High Intensity Cyclotrons"
10 //
11 // This file is part of OPAL.
12 //
13 // OPAL is free software: you can redistribute it and/or modify
14 // it under the terms of the GNU General Public License as published by
15 // the Free Software Foundation, either version 3 of the License, or
16 // (at your option) any later version.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with OPAL. If not, see <https://www.gnu.org/licenses/>.
20 //
21 #include "MemoryWriter.h"
22 
25 #include "Utilities/Timer.h"
26 #include "Ippl.h"
27 
28 MemoryWriter::MemoryWriter(const std::string& fname, bool restart)
29  : SDDSWriter(fname, restart)
30 { }
31 
32 
34 
35  if (this->hasColumns()) {
36  return;
37  }
38 
39  columns_m.addColumn("t", "double", "ns", "Time");
40 
41  columns_m.addColumn("s", "double", "m", "Path length");
42 
44  columns_m.addColumn("memory", "double", memory->getUnit(), "Total Memory");
45 
46  for (int p = 0; p < Ippl::getNodes(); ++p) {
47  std::stringstream tmp1;
48  tmp1 << "\"processor-" << p << "\"";
49 
50  std::stringstream tmp2;
51  tmp2 << "Memory per processor " << p;
52  columns_m.addColumn(tmp1.str(), "double", memory->getUnit(), tmp2.str());
53  }
54 
55  if ( mode_m == std::ios::app )
56  return;
57 
58  OPALTimer::Timer simtimer;
59 
60  std::string dateStr(simtimer.date());
61  std::string timeStr(simtimer.time());
62 
63 
64  std::stringstream ss;
65 
66  ss << "Memory statistics '"
67  << OpalData::getInstance()->getInputFn() << "' "
68  << dateStr << "" << timeStr;
69 
70  this->addDescription(ss.str(), "memory parameters");
71 
72  this->addDefaultParameters();
73 
74 
75  this->addInfo("ascii", 1);
76 }
77 
78 
80 {
82  memory->sample();
83 
84  if (Ippl::myNode() != 0) {
85  return;
86  }
87 
88  double pathLength = beam->get_sPos();
89 
90  fillHeader();
91 
92  this->open();
93 
94  this->writeHeader();
95 
96  columns_m.addColumnValue("t", beam->getT() * 1e9); // 1
97  columns_m.addColumnValue("s", pathLength); // 2
98 
99  int nProcs = Ippl::getNodes();
100  double total = 0.0;
101  for (int p = 0; p < nProcs; ++p) {
102  total += memory->getMemoryUsage(p);
103  }
104 
105  columns_m.addColumnValue("memory", total);
106 
107  for (int p = 0; p < nProcs; p++) {
108  std::stringstream ss;
109  ss << "\"processor-" << p << "\"";
110  columns_m.addColumnValue(ss.str(), memory->getMemoryUsage(p));
111  }
112 
113  this->writeRow();
114 
115  this->close();
116 }
double get_sPos() const
double getT() const
std::string getInputFn()
get opals input filename
Definition: OpalData.cpp:654
static OpalData * getInstance()
Definition: OpalData.cpp:195
void write(const PartBunchBase< double, 3 > *beam) override
MemoryWriter(const std::string &fname, bool restart)
void fillHeader()
void addColumn(const std::string &name, const std::string &type, const std::string &unit, const std::string &desc, std::ios_base::fmtflags flags=std::ios_base::scientific, unsigned short precision=15)
void addColumnValue(const std::string &name, const T &val)
Definition: SDDSColumnSet.h:63
SDDSColumnSet columns_m
Definition: SDDSWriter.h:126
bool hasColumns() const
Definition: SDDSWriter.h:207
void addDefaultParameters()
Definition: SDDSWriter.cpp:211
void addDescription(const std::string &text, const std::string &content)
Definition: SDDSWriter.h:164
void open()
Definition: SDDSWriter.cpp:134
void writeHeader()
Write SDDS header.
Definition: SDDSWriter.cpp:151
std::ios_base::openmode mode_m
First write to the statistics output file.
Definition: SDDSWriter.h:124
void close()
Definition: SDDSWriter.cpp:144
void writeRow()
Definition: SDDSWriter.h:192
void addInfo(const std::string &mode, const size_t &no_row_counts)
Definition: SDDSWriter.h:185
std::string date() const
Return date.
Definition: Timer.cpp:35
std::string time() const
Return time.
Definition: Timer.cpp:42
static int getNodes()
Definition: IpplInfo.cpp:670
static int myNode()
Definition: IpplInfo.cpp:691
const std::string & getUnit() const
static IpplMemory_p getInstance(Unit unit=Unit::GB, bool reset=true)
double getMemoryUsage(int core) const