TCPSPSuite
solverconfig.hpp
1 #ifndef SOLVERCONFIG_H
2 #define SOLVERCONFIG_H
3 
4 #include "../datastructures/maybe.hpp"
5 #include "../util/log.hpp"
6 
7 #include <json.hpp>
8 
9 using json = nlohmann::json;
10 using json_entry = json::basic_json::value_type;
11 
12 #include <fstream>
13 #include <map>
14 #include <regex>
15 #include <string>
16 #include <unordered_set>
17 #include <vector>
18 
19 class SolverConfig {
20 public:
21  SolverConfig();
22  SolverConfig(std::string name, std::string id_str,
23  std::map<std::string, json_entry> values,
24  Maybe<unsigned int> time_limit, bool enable_memory_metrics,
25  unsigned int meminfo_sampling_time,
26  std::vector<std::string> papi_metrics, Maybe<int> seed);
27 
28  const json_entry & operator[](const std::string & key) const;
29  bool has_config(const std::string & key) const;
30  const std::map<std::string, json_entry> & get_kvs() const;
31  bool as_bool(const std::string & key) const;
32 
33  const std::string & get_name() const;
34  const std::string & get_id() const;
35  bool match(std::string match_id) const;
36  Maybe<unsigned int> get_time_limit() const;
37 
38  bool are_memory_metrics_enabled() const;
39  unsigned int get_meminfo_sampling_time() const;
40  const std::vector<std::string> & get_papi_metrics() const;
41 
42  bool was_seed_set() const;
43  void override_seed(int seed);
44  void override_config(const std::string & key, const std::string & value);
45  int get_seed() const;
46 
47  void check_all_keys_queried() const;
48 
49  static std::vector<SolverConfig> read_configs(const std::string & filename);
50  static std::vector<SolverConfig> read_configs(json jsonConfig);
51 
52 private:
53  std::map<std::string, json_entry> values;
54  // TODO this is unclean
55  mutable std::unordered_set<std::string> requested_keys;
56  std::string name;
57 
58  std::regex id_matcher;
59  std::string id_str;
60  Maybe<unsigned int> time_limit;
61 
62  bool enable_memory_metrics;
63  unsigned int meminfo_sampling_time;
64  std::vector<std::string> papi_metrics;
65 
66  Maybe<int> seed;
67 
68  std::string dbg_serialization;
69 
70  Log l;
71 };
72 
73 /*
74  * Hashing is needed for a stable order inside the partitions in the
75  * Parallelizer!
76  */
77 namespace std {
78 template <>
79 struct hash<SolverConfig>
80 {
81  size_t
82  operator()(const SolverConfig & sc) const noexcept
83  {
84  size_t hashval = std::hash<std::string>{}(sc.get_name());
85  hashval ^= std::hash<std::string>{}(sc.get_id());
86  if (sc.get_time_limit().valid()) {
87  hashval ^= std::hash<unsigned int>{}(sc.get_time_limit().value());
88  }
89  // TODO should the seed be hashed?
90  // hash ^= std::hash<int>{}(sc.get_seed());
91 
92  for (const auto & kv_entry : sc.get_kvs()) {
93  hashval ^= (std::hash<std::string>{}(kv_entry.first) *
94  std::hash<std::string>{}(kv_entry.second.dump()));
95  }
96 
97  return hashval;
98  }
99 };
100 } // namespace std
101 
102 #endif