n-Dimensional Fast Methods  0.7
 All Classes Functions Variables Typedefs Pages
benchmark.hpp
1 
20 #ifndef BENCHMARK_HPP_
21 #define BENCHMARK_HPP_
22 
23 #include <chrono>
24 #include <limits>
25 
26 #include <boost/filesystem.hpp>
27 #include <boost/progress.hpp>
28 
29 #include <fast_methods/fm/solver.hpp>
30 #include <fast_methods/io/gridwriter.hpp>
31 
32 template <class grid_t>
33 class Benchmark {
34 
35  public:
36 
37  Benchmark
38  (unsigned int saveGrid = 0, bool saveLog = true) :
41  runID_(0),
42  nruns_(10),
43  path_("results"),
44  name_("benchmark"),
45  fromCFG_(false){}
46 
47  virtual ~Benchmark()
48  {
49  clear();
50  }
51 
53  void fromCFG
54  (bool cfg) {
55  fromCFG_ = cfg;
56  }
57 
59  void addSolver
60  (Solver<grid_t>* solver) {
61  solvers_.push_back(solver);
62  }
63 
65  void setSaveGrid
66  (unsigned int s) {
67  saveGrid_ = s;
68  }
69 
71  void setEnvironment
72  (grid_t* grid) {
73  grid_ = grid;
74  }
75 
77  void setNRuns
78  (unsigned int n) {
79  nruns_ = n;
80  }
81 
83  void setPath
84  (const boost::filesystem::path & path) {
85  path_ = path;
86  }
87 
89  void setSaveLog
90  (bool s) {
91  saveLog_ = s;
92  }
93 
96  (const std::vector<unsigned int> & init_points, unsigned int goal_idx) {
97  init_points_ = init_points;
98  goal_idx_ = goal_idx;
99  }
100 
102  void setInitialPoints(const std::vector<unsigned int> & init_points)
103  {
104  setInitialAndGoalPoints(init_points, -1);
105  }
106 
108  void run
109  () {
110  if (saveGrid_)
111  {
112  boost::filesystem::create_directory(path_);
113  boost::filesystem::create_directory(path_ / name_);
114  }
115  else if (saveLog_)
116  boost::filesystem::create_directory(path_);
117 
118  boost::progress_display showProgress (solvers_.size()*nruns_);
119 
120  configSolvers();
121 
122  logConfig();
123 
124  for (Solver<grid_t>* s :solvers_)
125  {
126  for (unsigned int i = 0; i < nruns_; ++i)
127  {
128  ++runID_;
129  s->reset();
130  s->compute();
131  logRun(s);
132 
133  if (saveGrid_ == 2)
134  saveGrid(s);
135 
136  ++showProgress;
137  }
138  if (saveGrid_ == 1)
139  saveGrid(s);
140  s->reset();
141  }
142 
143  if (saveLog_)
144  saveLog();
145  else {
146  console::info("Benchmark log format:");
147  std::cout << "Name\t#Runs\t#Dims\tDim1...DimN\t#Starts\tStartIdx\tGoalIdx"<<'\n';
148  std::cout << "RunID\tName\tTime (ms)" << '\n';
149  std::cout << log_.str() << '\n';
150  }
151  }
152 
154  void logRun
155  (const Solver<grid_t>* s)
156  {
157  std::ios init(NULL);
158  init.copyfmt(std::cout);
159  formatID();
160  log_ << '\n' << fmtID_;
161 
162  std::cout.copyfmt(init);
163  log_ << '\t' << s->getName() << "\t" << s->getTime();
164  }
165 
167  void saveGrid
168  (Solver<grid_t>* s) const {
169  thread_local boost::filesystem::path filename;
170  if (saveGrid_ == 1)
171  filename = path_ / name_ / s->getName();
172  if (saveGrid_ == 2)
173  filename = path_ / name_ / fmtID_;
174 
175  filename.replace_extension(".grid");
176  GridWriter::saveGridValues(filename.string().c_str(), *(s->getGrid()));
177  }
178 
180  void saveLog
181  () const {
182  std::ofstream ofs (path_.string() + "/" + name_ + ".log");
183  ofs << log_.rdbuf();
184  ofs.close();
185  }
186 
188  void setName
189  (const std::string & n)
190  {
191  name_ = n;
192  }
193 
195  void clear
196  () {
197  for (auto & s : solvers_)
198  delete s;
199 
200  solvers_.clear();
201 
202  if(fromCFG_)
203  delete grid_;
204  }
205 
206  private:
208  void logConfig
209  () {
210  log_ << name_ << '\t' << nruns_ << '\t' << grid_->getNDims()
211  << '\t' << grid_->getDimSizesStr();
212 
213  // Saving starting points.
214  log_ << init_points_.size() << '\t';
215  std::copy(init_points_.begin(), init_points_.end(), std::ostream_iterator<unsigned int>(log_, "\t"));
216 
217  if (int(goal_idx_) == -1)
218  log_ << "nan";
219  else
220  log_ << goal_idx_;
221  }
222 
224  void configSolvers
225  () {
226  for (Solver<grid_t>* s :solvers_)
227  {
228  s->setEnvironment(grid_);
230  }
231  }
232 
234  void formatID
235  () {
236  thread_local std::ostringstream oss; // To optimize a bit.
237  oss.str("");
238  oss.clear();
239  oss << std::setw(4) << std::setfill('0') << runID_;
240  fmtID_ = oss.str();
241  }
242 
244  std::vector<Solver<grid_t>*> solvers_;
245 
247  grid_t * grid_;
248 
250  std::vector<unsigned int> init_points_;
251 
253  unsigned int goal_idx_;
254 
256  std::chrono::time_point<std::chrono::system_clock> start_, end_;
257 
259  std::stringstream log_;
260 
263  unsigned int saveGrid_;
264 
266  bool saveLog_;
267 
269  unsigned int runID_;
270 
272  std::string fmtID_;
273 
275  unsigned int nruns_;
276 
278  boost::filesystem::path path_;
279 
281  std::string name_;
282 
284  bool fromCFG_;
285 };
286 
287 #endif /* BENCHMARK_HPP_*/
void setPath(const boost::filesystem::path &path)
Set the path where results will be saved.
Definition: benchmark.hpp:84
std::vector< Solver< grid_t > * > solvers_
Stores the solvers to be run in the benchmark.
Definition: benchmark.hpp:244
std::stringstream log_
Log stream.
Definition: benchmark.hpp:259
virtual void setEnvironment(grid_t *g)
Sets and cleans the grid in which operations will be performed.
Definition: solver.hpp:51
std::string fmtID_
Formatted run ID:
Definition: benchmark.hpp:272
unsigned int saveGrid_
If 1, the resulting grids (times) of the first run of each solver are saved to files. If 2, the grids for all runs of each solver are saved.
Definition: benchmark.hpp:263
static void saveGridValues(const char *filename, nDGridMap< T, ndims > &grid)
Saves grid values in ASCII format into the specified file.
Definition: gridwriter.hpp:48
This class provides the utilities to benchmark Fast Marching Solvers (configuration, running and logging). It works for FMM (any heap and SFMM), FIM and UFMM. It has not been tested with FM2 solvers.
Definition: benchmark.hpp:33
void logConfig()
Logs the benchmark configuration.
Definition: benchmark.hpp:209
void clear()
Clears and free memory allocated by the benchmark.
Definition: benchmark.hpp:196
void setEnvironment(grid_t *grid)
Sets the environment (grid map) the benchmark will be run on.
Definition: benchmark.hpp:72
boost::filesystem::path path_
Path were the results will be saved.
Definition: benchmark.hpp:278
bool fromCFG_
If true, benchmark configured from CFG file, used to selectively free memory.
Definition: benchmark.hpp:284
unsigned int runID_
ID of the current run.
Definition: benchmark.hpp:269
void formatID()
Formats as a string the run ID.
Definition: benchmark.hpp:235
const std::string & getName() const
Returns name of the solver.
Definition: solver.hpp:161
grid_t * grid_
Grid the benchmark will be run on.
Definition: benchmark.hpp:247
void saveGrid(Solver< grid_t > *s) const
Saves the grid values result of the last run of solver s.
Definition: benchmark.hpp:168
bool saveLog_
If true, the log is saved to file. Output on terminal otherwise.
Definition: benchmark.hpp:266
void addSolver(Solver< grid_t > *solver)
Adds a new solver to be benchmarked.
Definition: benchmark.hpp:60
static void info(const std::string &val)
virtual void setInitialAndGoalPoints(const std::vector< unsigned int > &init_points, unsigned int goal_idx)
Sets the initial and goal points by the indices of the grid.
Definition: solver.hpp:58
void configSolvers()
Configures each solver so they are ready to be run.
Definition: benchmark.hpp:225
std::string name_
Benchmark name.
Definition: benchmark.hpp:281
virtual void clear()
Clears the solver, it is not recommended to be used out of the destructor.
Definition: solver.hpp:168
void setInitialPoints(const std::vector< unsigned int > &init_points)
Sets the initial points (indices for the solvers).
Definition: benchmark.hpp:102
unsigned int goal_idx_
Index of the goal point.
Definition: benchmark.hpp:253
unsigned int nruns_
Number of runs for each solver.
Definition: benchmark.hpp:275
void setSaveGrid(unsigned int s)
Sets the saveGrid_ flag.
Definition: benchmark.hpp:66
Abstract class that serves as interface for the actual solvers implemented. It requires (at least) th...
Definition: solver.hpp:40
std::vector< unsigned int > init_points_
Indices of the initial points.
Definition: benchmark.hpp:250
std::chrono::time_point< std::chrono::system_clock > start_
Time measurement variables.
Definition: benchmark.hpp:256
void setNRuns(unsigned int n)
Set number of runs for each solver.
Definition: benchmark.hpp:78
void fromCFG(bool cfg)
Set true if the benchmark is configured from a cfg file.
Definition: benchmark.hpp:54
void logRun(const Solver< grid_t > *s)
Logs the last run of solver s.
Definition: benchmark.hpp:155
void setInitialAndGoalPoints(const std::vector< unsigned int > &init_points, unsigned int goal_idx)
Sets the initial and goal points (indices) for the solvers.
Definition: benchmark.hpp:96
void setSaveLog(bool s)
Sets the flag to save the log to a file (true) or output in terminal (false).
Definition: benchmark.hpp:90
grid_t * getGrid() const
Returns a pointer to the grid used.
Definition: solver.hpp:182
void run()
Automatically runs all the solvers.
Definition: benchmark.hpp:109
void setName(const std::string &n)
Sets the name of the benchmark.
Definition: benchmark.hpp:189
void saveLog() const
Saves the log to a file: benchmark_name.log.
Definition: benchmark.hpp:181