SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
Jobs.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <benchmax/tools/Tools.h>
5 
6 #include <cmath>
7 #include <numeric>
8 
9 namespace benchmax {
10 
11 /// Typedef for a job, consisting of a tool and an input file.
12 using Job = std::pair<const Tool*, std::filesystem::path>;
13 
14 /**
15  * Provides iteration over a given std::vector in a pseudo-randomized order.
16  * Internally, it computes a factor that is coprime to the number of jobs (and thus generates all indices).
17  * We choose the factor in the order of the square root of the number of jobs.
18  */
19 template<typename T>
21 public:
22  struct iterator: public std::iterator<std::forward_iterator_tag, T, long, const T*, T&> {
23  private:
24  std::size_t mK = 0;
25  std::size_t mFactor;
26  const std::vector<T>& mData;
27  public:
28  /// Create a randomized iterator.
29  iterator(std::size_t k, std::size_t factor, const std::vector<T>& data):
30  mK(k), mFactor(factor), mData(data)
31  {}
32  /// Dereference iterator.
33  const T& operator*() {
34  return mData[(mK * mFactor) % mData.size()];
35  }
36  /// Increment iterator.
38  ++mK;
39  return *this;
40  }
41  /// Compare two iterators.
42  bool operator!=(const iterator& rhs) const {
43  return mK != rhs.mK;
44  }
45  };
46 private:
47  const std::vector<T>& mData;
48  std::size_t mFactor;
49 public:
50  /// Construct a randomized view on the given vector.
51  RandomizationAdaptor(const std::vector<T>& data): mData(data) {
52  mFactor = static_cast<std::size_t>(std::sqrt(static_cast<double>(mData.size())));
53  while (std::gcd(mData.size(), mFactor) != 1) {
54  ++mFactor;
55  }
56  }
57  /// Return begin of the randomized range.
58  auto begin() const {
59  return iterator{ 0, mFactor, mData };
60  }
61  /// Return end of the randomized range.
62  auto end() const {
63  return iterator{ mData.size(), mFactor, mData };
64  }
65 };
66 
67 /**
68  * Represents a set of jobs, constructed as the cartesian product of a set of tools and a set of benchmarks.
69  */
70 class Jobs {
71  const Tools& mTools;
73  std::vector<Job> mJobs;
74 public:
75  /// Construct jobs from a set of tools and a set of benchmark files.
76  Jobs(const Tools& tools, const BenchmarkSet& benchmarks):
77  mTools(tools), mFiles(benchmarks) {
78  for (const auto& t: tools) {
79  for (const auto& b: benchmarks) {
80  if (t->canHandle(b)) {
81  mJobs.emplace_back(t.get(), b);
82  }
83  }
84  }
85  }
86 
87  /// Returns the set of tools.
88  const auto& tools() const {
89  return mTools;
90  }
91  /// Returns the set of files.
92  const auto& files() const {
93  return mFiles;
94  }
95  /// Returns the overall number of jobs.
96  auto size() const {
97  return mJobs.size();
98  }
99 
100  /// Begin iterator.
101  auto begin() const {
102  return mJobs.begin();
103  }
104  /// End iterator.
105  auto end() const {
106  return mJobs.end();
107  }
108  /// Returns all jobs in a pseudo-randomized order.
109  auto randomized() const {
110  return RandomizationAdaptor(mJobs);
111  }
112 };
113 
114 }
A set of benchmarks from some common base directory.
Definition: BenchmarkSet.h:12
Represents a set of jobs, constructed as the cartesian product of a set of tools and a set of benchma...
Definition: Jobs.h:70
const BenchmarkSet & mFiles
Definition: Jobs.h:72
const auto & files() const
Returns the set of files.
Definition: Jobs.h:92
std::vector< Job > mJobs
Definition: Jobs.h:73
auto begin() const
Begin iterator.
Definition: Jobs.h:101
Jobs(const Tools &tools, const BenchmarkSet &benchmarks)
Construct jobs from a set of tools and a set of benchmark files.
Definition: Jobs.h:76
auto size() const
Returns the overall number of jobs.
Definition: Jobs.h:96
auto end() const
End iterator.
Definition: Jobs.h:105
const Tools & mTools
Definition: Jobs.h:71
const auto & tools() const
Returns the set of tools.
Definition: Jobs.h:88
auto randomized() const
Returns all jobs in a pseudo-randomized order.
Definition: Jobs.h:109
Provides iteration over a given std::vector in a pseudo-randomized order.
Definition: Jobs.h:20
const std::vector< T > & mData
Definition: Jobs.h:47
RandomizationAdaptor(const std::vector< T > &data)
Construct a randomized view on the given vector.
Definition: Jobs.h:51
auto end() const
Return end of the randomized range.
Definition: Jobs.h:62
auto begin() const
Return begin of the randomized range.
Definition: Jobs.h:58
std::pair< const Tool *, std::filesystem::path > Job
Typedef for a job, consisting of a tool and an input file.
Definition: Jobs.h:12
std::vector< ToolPtr > Tools
A vector of ToolPtr.
Definition: Tools.h:17
Numeric gcd(const Numeric &_valueA, const Numeric &_valueB)
Calculates the greatest common divisor of the two arguments.
Definition: Numeric.cpp:317
bool operator!=(const iterator &rhs) const
Compare two iterators.
Definition: Jobs.h:42
iterator(std::size_t k, std::size_t factor, const std::vector< T > &data)
Create a randomized iterator.
Definition: Jobs.h:29
iterator & operator++()
Increment iterator.
Definition: Jobs.h:37
const T & operator*()
Dereference iterator.
Definition: Jobs.h:33
const std::vector< T > & mData
Definition: Jobs.h:26