SNABSuite  0.x
Spiking Neural Architecture Benchmark Suite
benchmark.hpp
Go to the documentation of this file.
1 /*
2  * SNABSuite -- Spiking Neural Architecture Benchmark Suite
3  * Copyright (C) 2016 Christoph Jenzen
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #pragma once
20 
21 #ifndef SNABSUITE_COMMON_BENCHMARK_HPP
22 #define SNABSUITE_COMMON_BENCHMARK_HPP
23 
24 #include <cypress/cypress.hpp>
25 #include <fstream>
26 #include <string>
27 
28 #include "common/snab_registry.hpp"
29 
30 namespace SNAB {
31 
32 using cypress::global_logger;
38 private:
39  // String containing the simulation backend
40  std::string m_backend;
41  // Container for the results
42  cypress::Json results;
43 
47  std::string convert_time(int value)
48  {
49  if (value < 10) {
50  return '0' + std::to_string(value);
51  }
52  return std::to_string(value);
53  }
54 
58  std::string timestamp()
59  {
60  auto ltime = time(NULL);
61  auto Tm = localtime(&ltime);
62  return std::to_string(1900 + Tm->tm_year) + "-" +
63  convert_time(Tm->tm_mon) + "-" + convert_time(Tm->tm_mday) +
64  "T" + convert_time(Tm->tm_hour) + ":" +
65  convert_time(Tm->tm_min) + ":" + convert_time(Tm->tm_sec);
66  }
67 
71  std::vector<std::string> bench_index_str{"Single Core/Smallest Network",
72  "Single Chip", "Small System",
73  "Large System"};
74 
78  size_t check_for_repeat(const cypress::Json &config)
79  {
80  if (config.count("repeat") > 0) {
81  size_t rep = config["repeat"].get<size_t>();
82  if (rep) {
83  // Zero = do not repeat = 1
84  return rep;
85  }
86  }
87  return 1;
88  }
89 
93  cypress::Json merge_repeat_results(
94  const std::vector<cypress::Json> &results);
95 
96 public:
101  BenchmarkExec(std::string backend, std::string benchmark = "all",
102  size_t bench_index = 0)
103  : m_backend(backend)
104  {
105  auto snab_vec = snab_registry(m_backend, bench_index);
106  for (auto i : snab_vec) {
107  if (i->valid() &&
108  (benchmark == "all" || benchmark == i->snab_name())) {
109  global_logger().info("SNABSuite",
110  "Executing " + i->snab_name());
111  size_t repeat = check_for_repeat(i->get_config());
112  if (repeat == 1) {
113  i->build();
114  i->run();
115  results.push_back({{"model", i->snab_name()},
116  {"timestamp", timestamp()},
117  {"task", bench_index_str[bench_index]},
118  {"results", i->evaluate_json()}});
119  }
120  else {
121  std::vector<cypress::Json> repeat_results;
122  i->build();
123  for (size_t j = 0; j < repeat; j++) {
124  i->run();
125  repeat_results.push_back(i->evaluate_json());
126  }
127  results.push_back(
128  {{"model", i->snab_name()},
129  {"timestamp", timestamp()},
130  {"task", bench_index_str[bench_index]},
131  {"results", merge_repeat_results(repeat_results)}});
132  }
133  }
134  }
135  std::cout << results.dump(4) << std::endl;
136  {
137  std::fstream file;
138  file.open(
139  (backend + "_" + std::to_string(bench_index) + ".json").c_str(),
140  std::fstream::out);
141  if (results.size() > 1) {
142  file << results.dump(4) << std::endl;
143  }
144  else {
145  file << results[0].dump(4) << std::endl;
146  }
147  file.close();
148  }
149  };
150 };
151 } // namespace SNAB
152 
153 #endif /* SNABSUITE_COMMON_BENCHMARK_HPP */
std::vector< std::shared_ptr< SNABBase > > snab_registry(std::string backend, size_t bench_index)
BenchmarkExec(std::string backend, std::string benchmark="all", size_t bench_index=0)
Definition: benchmark.hpp:101