OPAL (Object Oriented Parallel Accelerator Library)  2021.1.99
OPAL
LBalWriter.cpp
Go to the documentation of this file.
1 //
2 // Class LBalWriter
3 // This class writes a SDDS file with MPI load balancing information.
4 //
5 // Copyright (c) 2019, Matthias Frey, Paul Scherrer Institut, Villigen PSI, Switzerland
6 // Christof Metzger-Kraus, Open Sourcerer
7 // All rights reserved
8 //
9 // This file is part of OPAL.
10 //
11 // OPAL is free software: you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation, either version 3 of the License, or
14 // (at your option) any later version.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with OPAL. If not, see <https://www.gnu.org/licenses/>.
18 //
19 #include "LBalWriter.h"
20 
21 #include "OPALconfig.h"
23 #include "Utilities/Util.h"
24 #include "Utilities/Timer.h"
26 
27 #ifdef ENABLE_AMR
29 #endif
30 
31 LBalWriter::LBalWriter(const std::string& fname, bool restart)
32  : SDDSWriter(fname, restart)
33 { }
34 
35 
36 #ifdef ENABLE_AMR
38 #else
40 #endif
41  if (this->hasColumns()) {
42  return;
43  }
44 
45  columns_m.addColumn("t", "double", "ns", "Time");
46 
47  for (int p = 0; p < Ippl::getNodes(); ++p) {
48  std::stringstream tmp1;
49  tmp1 << "\"processor-" << p << "\"";
50 
51  std::stringstream tmp2;
52  tmp2 << "Number of particles of processor " << p;
53 
54  columns_m.addColumn(tmp1.str(), "long", "1", tmp2.str());
55  }
56 
57 #ifdef ENABLE_AMR
58  if ( AmrPartBunch* amrbeam = dynamic_cast<AmrPartBunch*>(beam) ) {
59 
60  int nLevel = (amrbeam->getAmrObject())->maxLevel() + 1;
61 
62  for (int lev = 0; lev < nLevel; ++lev) {
63  std::stringstream tmp1;
64  tmp1 << "\"level-" << lev << "\"";
65 
66  std::stringstream tmp2;
67  tmp2 << "Number of particles at level " << lev;
68  columns_m.addColumn(tmp1.str(), "long", "1", tmp2.str());
69  }
70  }
71 #endif
72 
73  if ( mode_m == std::ios::app )
74  return;
75 
76  OPALTimer::Timer simtimer;
77 
78  std::string dateStr(simtimer.date());
79  std::string timeStr(simtimer.time());
80 
81  std::stringstream ss;
82  ss << "Processor statistics '"
83  << OpalData::getInstance()->getInputFn() << "' "
84  << dateStr << "" << timeStr;
85 
86  this->addDescription(ss.str(), "lbal parameters");
87 
88  this->addDefaultParameters();
89 
90 
91  this->addInfo("ascii", 1);
92 }
93 
94 
95 #ifdef ENABLE_AMR
97 
98  if ( AmrPartBunch* amrbeam = dynamic_cast<AmrPartBunch*>(beam) ) {
99  amrbeam->gatherLevelStatistics();
100  }
101 #else
103 #endif
104 
105  if ( Ippl::myNode() != 0 )
106  return;
107 
108 #ifdef ENABLE_AMR
109  this->fillHeader(beam);
110 #else
111  this->fillHeader();
112 #endif
113 
114  this->open();
115 
116  this->writeHeader();
117 
118  columns_m.addColumnValue("t", beam->getT() * 1e9); // 1
119 
120  size_t nProcs = Ippl::getNodes();
121  for (size_t p = 0; p < nProcs; ++ p) {
122  std::stringstream ss;
123  ss << "\"processor-" << p << "\"";
124  columns_m.addColumnValue(ss.str(), beam->getLoadBalance(p));
125  }
126 
127 #ifdef ENABLE_AMR
128  if ( AmrPartBunch* amrbeam = dynamic_cast<AmrPartBunch*>(beam) ) {
129  int nLevel = (amrbeam->getAmrObject())->maxLevel() + 1;
130  for (int lev = 0; lev < nLevel; ++lev) {
131  std::stringstream ss;
132  ss << "\"level-" << lev << "\"";
133  columns_m.addColumnValue(ss.str(), amrbeam->getLevelStatistics(lev));
134  }
135  }
136 #endif
137 
138  this->writeRow();
139 
140  this->close();
141 }
size_t getLoadBalance(int p) const
double getT() const
std::string getInputFn()
get opals input filename
Definition: OpalData.cpp:654
static OpalData * getInstance()
Definition: OpalData.cpp:195
LBalWriter(const std::string &fname, bool restart)
Definition: LBalWriter.cpp:31
void write(PartBunchBase< double, 3 > *beam)
Definition: LBalWriter.cpp:96
void fillHeader(PartBunchBase< double, 3 > *beam)
Definition: LBalWriter.cpp:37
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