HEBench
workload_params.hpp
Go to the documentation of this file.
1 
2 // Copyright (C) 2021 Intel Corporation
3 // SPDX-License-Identifier: Apache-2.0
4 
5 #ifndef _HEBench_API_Bridge_WorkloadParamsCPP_H_7e5fa8c2415240ea93eff148ed73539b
6 #define _HEBench_API_Bridge_WorkloadParamsCPP_H_7e5fa8c2415240ea93eff148ed73539b
7 
8 #include <stdexcept>
9 #include <string>
10 #include <vector>
11 
13 #include "utilities.hpp"
14 
15 namespace hebench {
16 namespace cpp {
17 namespace WorkloadParams {
18 
31 class Common
32 {
33 public:
34  Common(std::size_t num_params = 0) :
35  m_w_params(num_params)
36  {
37  }
38  Common(const std::vector<hebench::APIBridge::WorkloadParam> &w_params) :
39  m_w_params(w_params)
40  {
41  }
43  m_w_params(w_params.params, w_params.params + w_params.count)
44  {
45  }
46  virtual ~Common() = default;
47 
48  template <typename T>
54  void add(const T &value, const std::string &name = std::string());
55  template <typename T>
63  void set(std::size_t index, const T &value, const std::string &name);
64  template <typename T>
71  void set(std::size_t index, const T &value);
72  template <typename T>
80  const T &get(std::size_t index) const;
81  const char *getName(std::size_t index) const { return getParams().at(index).name; }
82 
83  const std::vector<hebench::APIBridge::WorkloadParam> &getParams() const { return m_w_params; }
84 
85 protected:
86  std::vector<hebench::APIBridge::WorkloadParam> m_w_params;
87 };
88 
89 //-------------------------------------------------------
90 // class WorkloadParams::Generic template specialization
91 //-------------------------------------------------------
92 
93 // Int64
94 
95 template <>
96 inline void Common::add<std::int64_t>(const std::int64_t &value, const std::string &name)
97 {
98  m_w_params.push_back(hebench::APIBridge::WorkloadParam());
99  hebench::APIBridge::WorkloadParam &w_param = m_w_params.back();
100 
102  w_param.i_param = value;
104 }
105 
106 template <>
107 inline void Common::set<std::int64_t>(std::size_t index, const std::int64_t &value, const std::string &name)
108 {
109  hebench::APIBridge::WorkloadParam &w_param = m_w_params.at(index);
110 
112  w_param.i_param = value;
114 }
115 
116 template <>
117 inline void Common::set<std::int64_t>(std::size_t index, const std::int64_t &value)
118 {
119  hebench::APIBridge::WorkloadParam &w_param = m_w_params.at(index);
120 
122  w_param.i_param = value;
123 }
124 
125 template <>
126 inline const std::int64_t &Common::get<std::int64_t>(std::size_t index) const
127 {
128  return m_w_params.at(index).i_param;
129 }
130 
131 // UInt64
132 
133 template <>
134 inline void Common::add<std::uint64_t>(const std::uint64_t &value, const std::string &name)
135 {
136  m_w_params.push_back(hebench::APIBridge::WorkloadParam());
137  hebench::APIBridge::WorkloadParam &w_param = m_w_params.back();
138 
140  w_param.u_param = value;
142 }
143 
144 template <>
145 inline void Common::set<std::uint64_t>(std::size_t index, const std::uint64_t &value, const std::string &name)
146 {
147  hebench::APIBridge::WorkloadParam &w_param = m_w_params.at(index);
148 
150  w_param.u_param = value;
152 }
153 
154 template <>
155 inline void Common::set<std::uint64_t>(std::size_t index, const std::uint64_t &value)
156 {
157  hebench::APIBridge::WorkloadParam &w_param = m_w_params.at(index);
158 
160  w_param.u_param = value;
161 }
162 
163 template <>
164 inline const std::uint64_t &Common::get<std::uint64_t>(std::size_t index) const
165 {
166  return m_w_params.at(index).u_param;
167 }
168 
169 // Float64
170 
171 template <>
172 inline void Common::add<double>(const double &value, const std::string &name)
173 {
176 
178  w_param.f_param = value;
180 }
181 
182 template <>
183 inline void Common::set<double>(std::size_t index, const double &value, const std::string &name)
184 {
185  hebench::APIBridge::WorkloadParam &w_param = m_w_params.at(index);
186 
188  w_param.f_param = value;
190 }
191 
192 template <>
193 inline void Common::set<double>(std::size_t index, const double &value)
194 {
195  hebench::APIBridge::WorkloadParam &w_param = m_w_params.at(index);
196 
198  w_param.f_param = value;
199 }
200 
201 template <>
202 inline const double &Common::get<double>(std::size_t index) const
203 {
204  return m_w_params.at(index).f_param;
205 }
206 
207 //-------------------------------------------
208 // Workload parameter specialization classes
209 //-------------------------------------------
210 
211 class MatrixMultiply : public Common
212 {
213 public:
214  enum : std::size_t
215  {
220  };
221 
225  std::uint64_t rows_M0() const { return get<std::uint64_t>(Index_RowsM0); }
229  std::uint64_t &rows_M0() { return m_w_params.at(Index_RowsM0).u_param; }
233  std::uint64_t cols_M0() const { return get<std::uint64_t>(Index_ColsM0); }
237  std::uint64_t &cols_M0() { return m_w_params.at(Index_ColsM0).u_param; }
241  std::uint64_t cols_M1() const { return get<std::uint64_t>(Index_ColsM1); }
245  std::uint64_t &cols_M1() { return m_w_params.at(Index_ColsM1).u_param; }
246 
247 public:
255  MatrixMultiply(std::uint64_t _rows_M0 = 0, std::uint64_t _cols_M0 = 0, std::uint64_t _cols_M1 = 0) :
257  {
258  this->set<std::uint64_t>(Index_RowsM0, _rows_M0, "rows_M0");
259  this->set<std::uint64_t>(Index_ColsM0, _cols_M0, "cols_M0");
260  this->set<std::uint64_t>(Index_ColsM1, _cols_M1, "cols_M1");
261  }
262 
275  MatrixMultiply(const std::vector<hebench::APIBridge::WorkloadParam> &w_params) :
276  Common(w_params)
277  {
278  validateParams();
279  }
293  Common(w_params)
294  {
295  validateParams();
296  }
297 
298 private:
299  void validateParams() const
300  {
301  if (m_w_params.size() < MinRequiredParameters)
302  throw std::out_of_range("Workload requires, at least, " + std::to_string(MinRequiredParameters) + " parameters.");
303  for (std::size_t i = 0; i < MinRequiredParameters; ++i)
305  throw std::logic_error("Data type for workload parameter " + std::to_string(i) + " must be WorkloadParamType::UInt64.");
306  }
307 };
308 
309 class VectorSize : public Common
310 {
311 public:
312  enum : std::size_t
313  {
316  };
317 
321  std::uint64_t n() const { return get<std::uint64_t>(Index_N); }
325  std::uint64_t &n() { return m_w_params.at(Index_N).u_param; }
326 
327 public:
333  VectorSize(std::uint64_t _n = 0) :
335  {
336  this->set<std::uint64_t>(Index_N, _n, "n");
337  }
338 
351  VectorSize(const std::vector<hebench::APIBridge::WorkloadParam> &w_params) :
352  Common(w_params)
353  {
354  validateParams();
355  }
356 
370  Common(w_params)
371  {
372  validateParams();
373  }
374 
375 private:
376  void validateParams() const
377  {
378  if (m_w_params.size() < MinRequiredParameters)
379  throw std::out_of_range("Workload requires, at least, " + std::to_string(MinRequiredParameters) + " parameters.");
380  for (std::size_t i = 0; i < MinRequiredParameters; ++i)
382  throw std::logic_error("Data type for workload parameter " + std::to_string(i) + " must be WorkloadParamType::UInt64.");
383  }
384 };
385 
390 class Generic : public Common
391 {
392 public:
393  enum : std::size_t
394  {
397  };
398 
402  const std::uint64_t &n() const { return get<std::uint64_t>(Index_N); }
406  const std::uint64_t &m() const { return get<std::uint64_t>(Index_M); }
411  std::uint64_t length_InputParam(std::size_t index) const
412  {
413  if (index > n())
414  throw std::out_of_range(std::string(__func__) + ": Index out of range; "
415  + std::to_string(index) + ", expected less than " + std::to_string(n()) + ".");
416  return get<std::uint64_t>(index + 2);
417  }
422  std::uint64_t &length_InputParam(std::size_t index)
423  {
424  if (index >= n())
425  throw std::out_of_range(std::string(__func__) + ": Index out of range; "
426  + std::to_string(index) + ", expected less than " + std::to_string(n()) + ".");
427  return m_w_params.at(index + 2).u_param;
428  }
433  std::uint64_t length_ResultComponent(std::size_t index) const
434  {
435  if (index >= m())
436  throw std::out_of_range(std::string(__func__) + ": Index out of range; "
437  + std::to_string(index) + ", expected less than " + std::to_string(m()) + ".");
438  return get<std::uint64_t>(index + n() + 2);
439  }
444  std::uint64_t &length_ResultComponent(std::size_t index)
445  {
446  if (index >= m())
447  throw std::out_of_range(std::string(__func__) + ": Index out of range; "
448  + std::to_string(index) + ", expected less than " + std::to_string(m()) + ".");
449  return m_w_params.at(index + n() + 2).u_param;
450  }
451 
452 public:
466  Generic(std::uint64_t _n = 1, std::uint64_t _m = 1) :
467  Common(_n + _m + 2)
468  {
469  this->set<std::uint64_t>(Index_N, _n, "n");
470  this->set<std::uint64_t>(Index_M, _m, "m");
471  for (std::size_t i = 0; i < _n; ++i)
472  set<std::uint64_t>(2 + i, 1, "length_InputParam" + std::to_string(i));
473  for (std::size_t i = 0; i < _m; ++i)
474  set<std::uint64_t>(2 + _n + i, 1, "length_ResultComponent" + std::to_string(i));
475  }
476 
490  Generic(const std::vector<hebench::APIBridge::WorkloadParam> &w_params) :
491  Common(w_params)
492  {
493  validateParams();
494  }
495 
510  Common(w_params)
511  {
512  validateParams();
513  }
514 
515 private:
516  void validateParams() const
517  {
518  const std::size_t MinRequiredParameters = n() + m() + 2;
519  if (m_w_params.size() < MinRequiredParameters)
520  throw std::out_of_range("Workload requires, at least, " + std::to_string(MinRequiredParameters) + " parameters.");
521  for (std::size_t i = 0; i < MinRequiredParameters; ++i)
523  throw std::logic_error("Data type for workload parameter " + std::to_string(i) + " must be WorkloadParamType::UInt64.");
524  }
525 };
526 
528 {
529 public:
530  enum : std::size_t
531  {
536  };
537 
541  std::uint64_t n() const { return get<std::uint64_t>(Index_N); }
545  std::uint64_t &n() { return m_w_params.at(Index_N).u_param; }
549  std::uint64_t m() const { return get<std::uint64_t>(Index_M); }
553  std::uint64_t &m() { return m_w_params.at(Index_M).u_param; }
557  std::uint64_t k() const { return get<std::uint64_t>(Index_K); }
561  std::uint64_t &k() { return m_w_params.at(Index_K).u_param; }
562 
563 public:
571  SimpleSetIntersection(std::uint64_t _n = 0, std::uint64_t _m = 0, std::uint64_t _k = 1) :
573  {
574  this->set<std::uint64_t>(Index_N, _n, "n");
575  this->set<std::uint64_t>(Index_M, _m, "m");
576  this->set<std::uint64_t>(Index_K, _k, "k");
577  }
578 
591  SimpleSetIntersection(const std::vector<hebench::APIBridge::WorkloadParam> &w_params) :
592  Common(w_params)
593  {
594  validateParams();
595  }
609  Common(w_params)
610  {
611  validateParams();
612  }
613 
614 private:
615  void validateParams() const
616  {
617  if (m_w_params.size() < MinRequiredParameters)
618  throw std::out_of_range("Workload requires, at least, " + std::to_string(MinRequiredParameters) + " parameters.");
619  for (std::size_t i = 0; i < MinRequiredParameters; ++i)
621  throw std::logic_error("Data type for workload parameter " + std::to_string(i) + " must be WorkloadParamType::UInt64.");
622  }
623 };
624 
629 
630 } // namespace WorkloadParams
631 } // namespace cpp
632 } // namespace hebench
633 
634 #endif // defined _HEBench_API_Bridge_WorkloadParamsCPP_H_7e5fa8c2415240ea93eff148ed73539b
Base wrapper around the flexible workload parameters.
Common(const hebench::APIBridge::WorkloadParams &w_params)
std::vector< hebench::APIBridge::WorkloadParam > m_w_params
void set(std::size_t index, const T &value, const std::string &name)
Sets a new value for an existing workload parameter.
void add(const T &value, const std::string &name=std::string())
Adds a new workload parameter to the list.
Common(const std::vector< hebench::APIBridge::WorkloadParam > &w_params)
Common(std::size_t num_params=0)
const std::vector< hebench::APIBridge::WorkloadParam > & getParams() const
const T & get(std::size_t index) const
Retrieves the value for an existing workload parameter.
const char * getName(std::size_t index) const
void set(std::size_t index, const T &value)
Sets a new value for an existing workload parameter.
Wraps around flexible workload parameters required for a generic workload.
std::uint64_t & length_ResultComponent(std::size_t index)
Number of elements in output vector at specified result index.
std::uint64_t & length_InputParam(std::size_t index)
Number of elements in input vector at specified input index.
Generic(const hebench::APIBridge::WorkloadParams &w_params)
Initializes workload parameters for a generic workload from a list of existing workload parameters.
const std::uint64_t & m() const
Number of outputs from the operation.
Generic(std::uint64_t _n=1, std::uint64_t _m=1)
Initializes a new object to represent workload parameters for a generic workload.
std::uint64_t length_ResultComponent(std::size_t index) const
Number of elements in output vector at specified result index.
std::uint64_t length_InputParam(std::size_t index) const
Number of components in input vector at specified input index.
const std::uint64_t & n() const
Number of inputs to the operation.
Generic(const std::vector< hebench::APIBridge::WorkloadParam > &w_params)
Initializes workload parameters for a generic workload from a list of existing workload parameters.
MatrixMultiply(const hebench::APIBridge::WorkloadParams &w_params)
Initializes workload parameters for matrix multiplication from a list of existing workload parameters...
std::uint64_t & cols_M0()
Number of columns in matrix M0 and rows in matrix M1.
std::uint64_t & cols_M1()
Number of columns in matrix M1.
MatrixMultiply(std::uint64_t _rows_M0=0, std::uint64_t _cols_M0=0, std::uint64_t _cols_M1=0)
Initializes a new object to represent workload parameters for matrix multiplication workload.
std::uint64_t & rows_M0()
Number of rows in matrix M0.
std::uint64_t cols_M0() const
Number of columns in matrix M0 and rows in matrix M1.
MatrixMultiply(const std::vector< hebench::APIBridge::WorkloadParam > &w_params)
Initializes workload parameters for matrix multiplication from a list of existing workload parameters...
std::uint64_t cols_M1() const
Number of columns in matrix M1.
std::uint64_t rows_M0() const
Number of rows in matrix M0.
SimpleSetIntersection(std::uint64_t _n=0, std::uint64_t _m=0, std::uint64_t _k=1)
Initializes a new object to represent workload parameters for simple set intersection.
std::uint64_t k() const
Number of items per element.
SimpleSetIntersection(const std::vector< hebench::APIBridge::WorkloadParam > &w_params)
Initializes workload parameters for simple set intersection from a list of existing workload paramete...
std::uint64_t m() const
Number of elements in set B.
std::uint64_t n() const
Number of elements in set A.
std::uint64_t & m()
Number of elements in set B.
std::uint64_t & n()
Number of elements in set A.
SimpleSetIntersection(const hebench::APIBridge::WorkloadParams &w_params)
Initializes workload parameters for simple set intersection from a list of existing workload paramete...
std::uint64_t & k()
Number of items per element.
VectorSize(std::uint64_t _n=0)
Initializes a new object to represent workload parameters for a workload requiring a vector size.
VectorSize(const std::vector< hebench::APIBridge::WorkloadParam > &w_params)
Initializes workload parameters for a workload requiring a vector size from a list of existing worklo...
std::uint64_t & n()
Number elements in a vector.
std::uint64_t n() const
Number elements in a vector.
VectorSize(const hebench::APIBridge::WorkloadParams &w_params)
Initializes workload parameters for a workload requiring a vector size from a list of existing worklo...
@ Float64
64 bits IEEE 754 standard floating point real numbers.
Definition: types.h:306
@ Int64
64 bits signed integers.
Definition: types.h:304
@ UInt64
64 bits unsigned integers.
Definition: types.h:305
char name[HEBENCH_MAX_BUFFER_SIZE]
Null-terminated string containing the name for the parameter.
Definition: types.h:328
WorkloadParamType::WorkloadParamType data_type
Type of the parameter data.
Definition: types.h:322
Defines a single workload parameter.
Definition: types.h:318
Specifies the parameters for a workload.
Definition: types.h:363
std::string to_string(const std::string_view &s)
std::uint64_t copyString(char *dst, std::uint64_t size, const std::string &src)
Copies a C++ string object into a C-style string.
Definition: utilities.cpp:13
#define HEBENCH_MAX_BUFFER_SIZE
Definition: types.h:56