HEBench
engine.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_Base_Engine_H_7e5fa8c2415240ea93eff148ed73539b
6 #define _HEBench_API_Bridge_Base_Engine_H_7e5fa8c2415240ea93eff148ed73539b
7 
8 #include <cstdint>
9 #include <string>
10 #include <unordered_map>
11 #include <utility>
12 #include <vector>
13 
14 #include "engine_object.hpp"
16 
17 namespace hebench {
18 namespace cpp {
19 
20 class BaseEngine;
21 class BenchmarkDescription;
22 class BaseBenchmark;
23 
28 {
31 };
32 
52 BaseEngine *createEngine(const std::int8_t *p_buffer, std::uint64_t size); // implement this
62 void destroyEngine(BaseEngine *p); // implement this
63 
69 class BaseEngine : public ITaggedObject
70 {
71 private:
73 
74 public:
75  BaseEngine(const BaseEngine &) = delete;
76  BaseEngine &operator=(const BaseEngine &) = delete;
77 
78  BaseEngine(BaseEngine &&) = delete;
80 
81 public:
86  static constexpr std::int64_t tag = 0x8000000000000000; // bit 63
87 public:
88  ~BaseEngine() override {}
89 
90  std::int64_t classTag() const override { return tag; }
91 
101  static const std::string &getErrorDesc(hebench::APIBridge::ErrorCode err_code);
105  static hebench::APIBridge::ErrorCode getLastError() { return m_last_error; }
110  static const std::string &getLastErrorDesc() { return m_s_last_error_description; }
117  static void setLastError(hebench::APIBridge::ErrorCode value);
126  const std::string &err_desc);
127 
132  std::string getBenchmarkDescriptionEx(hebench::APIBridge::Handle h_bench_desc,
133  const hebench::APIBridge::WorkloadParams *p_w_params) const;
134 
140  const std::unordered_map<hebench::APIBridge::Scheme, std::string> &
141  schemeName() const { return m_map_scheme_name; }
147  const std::unordered_map<hebench::APIBridge::Security, std::string> &
148  securityName() const { return m_map_security_name; }
149 
155  std::uint64_t subscribeBenchmarkCount() const { return m_descriptors.size(); }
160  void subscribeBenchmarks(hebench::APIBridge::Handle *p_h_bench_descs, std::uint64_t count) const;
165  std::uint64_t getWorkloadParamsCount(hebench::APIBridge::Handle h_bench_desc) const;
171  std::uint64_t getDefaultWorkloadParamsCount(hebench::APIBridge::Handle h_bench_desc) const;
174  hebench::APIBridge::WorkloadParams *p_default_params,
175  std::uint64_t default_count) const;
181  const hebench::APIBridge::WorkloadParams *p_params);
188 
189  template <class T, typename... Args>
204  hebench::APIBridge::Handle createHandle(std::uint64_t size, std::int64_t extra_tags,
205  Args &&... args) const;
226  hebench::APIBridge::Handle duplicateHandle(hebench::APIBridge::Handle h, std::int64_t new_tag, std::int64_t check_tags) const;
246  hebench::APIBridge::Handle duplicateHandle(hebench::APIBridge::Handle h, std::int64_t check_tags = 0) const;
247  template <class T, typename... Args>
266  T &retrieveFromHandle(hebench::APIBridge::Handle h, std::int64_t extra_tags = 0) const;
267 
268  template <class T, typename... Args>
279  EngineObject *createEngineObj(Args &&... args) const;
280  template <class T, typename... Args>
293  std::shared_ptr<T> createRAII(Args &&... args) const;
294  template <class T, typename... Args>
295  T *createObj(Args &&... args) const;
296  template <class T>
297  void destroyObj(T *p) const
298  {
299  if (p)
300  delete p;
301  }
302 
303 protected:
304  BaseEngine();
316  virtual void init() = 0;
317 
325  std::shared_ptr<BenchmarkDescription> matchBenchmark(hebench::APIBridge::Handle h_desc) const;
326 
334  void addBenchmarkDescription(std::shared_ptr<BenchmarkDescription> p_desc);
343  void addSchemeName(hebench::APIBridge::Scheme scheme, const std::string &name);
352  void addSecurityName(hebench::APIBridge::Security security, const std::string &name);
362  static void addErrorCode(hebench::APIBridge::ErrorCode code, const std::string &description);
363 
364 private:
365  void checkHandleTags(hebench::APIBridge::Handle h, std::int64_t check_tags) const;
366  hebench::APIBridge::Handle duplicateHandleInternal(hebench::APIBridge::Handle h, std::int64_t new_tag) const;
367 
368  static const std::string UnknownErrorMsg;
369  static hebench::APIBridge::ErrorCode m_last_error;
370  static std::string m_s_last_error_description;
371  static std::unordered_map<hebench::APIBridge::ErrorCode, std::string> m_map_error_desc;
372 
373  std::vector<std::shared_ptr<BenchmarkDescription>> m_descriptors;
374  std::unordered_map<hebench::APIBridge::Scheme, std::string> m_map_scheme_name;
375  std::unordered_map<hebench::APIBridge::Security, std::string> m_map_security_name;
376 };
377 
378 template <class T, typename... Args>
379 hebench::APIBridge::Handle BaseEngine::createHandle(std::uint64_t size, std::int64_t extra_tags,
380  Args &&... args) const
381 {
382  if ((extra_tags & ITaggedObject::MaskReservedBits) != 0)
383  throw hebench::cpp::HEBenchError(HEBERROR_MSG_CLASS("Invalid 'extra_tags' detected. Most significant 8 bits of tags are reserved."),
385 
386  hebench::cpp::EngineObject *p_retval = createEngineObj<T>(std::forward<Args>(args)...);
387 
389  retval.p = p_retval;
390  retval.size = size;
391  retval.tag = p_retval->classTag() | extra_tags;
392 
393  return retval;
394 }
395 
396 template <class T, typename... Args>
397 T &BaseEngine::retrieveFromHandle(hebench::APIBridge::Handle h, std::int64_t extra_tags) const
398 {
399  if ((extra_tags & ITaggedObject::MaskReservedBits) != 0)
400  throw hebench::cpp::HEBenchError(HEBERROR_MSG_CLASS("Invalid 'extra_tags' detected. Most significant 8 bits of tags are reserved."),
403  throw hebench::cpp::HEBenchError(HEBERROR_MSG_CLASS("Invalid tag detected. Expected EngineObject::tag."),
405  if ((h.tag & extra_tags) != extra_tags)
406  throw hebench::cpp::HEBenchError(HEBERROR_MSG_CLASS("Invalid tag detected. Expected " + std::to_string(extra_tags) + "."),
408 
409  if (!h.p)
410  throw hebench::cpp::HEBenchError(HEBERROR_MSG_CLASS("Invalid null handle."),
412 
413  // retrieve our internal format object from the handle
414  hebench::cpp::EngineObject *p_obj = reinterpret_cast<hebench::cpp::EngineObject *>(h.p);
415  return p_obj->get<T>();
416 }
417 
418 template <class T, typename... Args>
420 {
421  std::shared_ptr<T> raii = createRAII<T>(std::forward<Args>(args)...);
422  EngineObject *retval = new EngineObject(*this, raii);
423  return retval;
424 }
425 
426 template <class T, typename... Args>
427 std::shared_ptr<T> BaseEngine::createRAII(Args &&... args) const
428 {
429  std::shared_ptr<T> retval = std::shared_ptr<T>(this->createObj<T>(std::forward<Args>(args)...),
430  [this](T *p) {
431  this->destroyObj<T>(p);
432  });
433  return retval;
434 }
435 
436 template <class T, typename... Args>
437 T *BaseEngine::createObj(Args &&... args) const
438 {
439  return new T(std::forward<Args>(args)...);
440 }
441 
442 } // namespace cpp
443 } // namespace hebench
444 
445 #endif // defined _HEBench_API_Bridge_Base_Engine_H_7e5fa8c2415240ea93eff148ed73539b
Top level opaque benchmark class.
Definition: benchmark.hpp:143
Base class that encapsulates common behavior of backend engines.
Definition: engine.hpp:70
std::shared_ptr< BenchmarkDescription > matchBenchmark(hebench::APIBridge::Handle h_desc) const
Retrieves the BenchmarkDescription object pointed by the specified handle.
Definition: engine.cpp:96
T & retrieveFromHandle(hebench::APIBridge::Handle h, std::int64_t extra_tags=0) const
Retrieves the object of type T encapsulated in an opaque HEBench handle by method createHandle().
Definition: engine.hpp:397
hebench::APIBridge::Handle createHandle(std::uint64_t size, std::int64_t extra_tags, Args &&... args) const
Encapsulates an object of type T in an opaque HEBench handle.
Definition: engine.hpp:379
static const std::string & getErrorDesc(hebench::APIBridge::ErrorCode err_code)
Retrieves the description of a specific error code.
Definition: engine.cpp:35
std::shared_ptr< T > createRAII(Args &&... args) const
Creates a smart pointer of the specified template type.
Definition: engine.hpp:427
std::uint64_t getWorkloadParamsCount(hebench::APIBridge::Handle h_bench_desc) const
Retrieves the number of workload parameters for the decribed benchmark.
Definition: engine.cpp:130
const std::unordered_map< hebench::APIBridge::Security, std::string > & securityName() const
Retrieves a dictionary that maps security types known to this benchmark to their human readable names...
Definition: engine.hpp:148
hebench::APIBridge::Handle createBenchmark(hebench::APIBridge::Handle h_bench_desc, const hebench::APIBridge::WorkloadParams *p_params)
Creates the benchmark specified by the descriptor.
Definition: engine.cpp:180
BaseEngine & operator=(BaseEngine &&)=delete
void addBenchmarkDescription(std::shared_ptr< BenchmarkDescription > p_desc)
Adds a new benchmark to the list of benchmarks to register by this backend.
Definition: engine.cpp:67
BaseEngine(const BaseEngine &)=delete
std::string getBenchmarkDescriptionEx(hebench::APIBridge::Handle h_bench_desc, const hebench::APIBridge::WorkloadParams *p_w_params) const
Retrieves backend specific text description for a benchmark descriptor.
Definition: engine.cpp:56
void destroyObj(T *p) const
Definition: engine.hpp:297
const std::unordered_map< hebench::APIBridge::Scheme, std::string > & schemeName() const
Retrieves a dictionary that maps schemes known to this benchmark to their human readable names.
Definition: engine.hpp:141
BaseEngine(BaseEngine &&)=delete
void destroyBenchmark(hebench::APIBridge::Handle h_bench)
Destroys and cleans up a benchmark created by createBenchmark().
Definition: engine.cpp:199
void subscribeBenchmarks(hebench::APIBridge::Handle *p_h_bench_descs, std::uint64_t count) const
Retrieves the benchmarks for which the backend is registering to perform.
Definition: engine.cpp:106
static constexpr std::int64_t tag
Used to identify this class when returned as a handle to Test Harness.
Definition: engine.hpp:86
std::int64_t classTag() const override
Retrieves the tag of the class to which this object belongs.
Definition: engine.hpp:90
void addSchemeName(hebench::APIBridge::Scheme scheme, const std::string &name)
Adds a new supported scheme and name pair to the list of schemes supported by this backend.
Definition: engine.cpp:80
EngineObject * createEngineObj(Args &&... args) const
Creates an EngineObject that wraps an object of type T.
Definition: engine.hpp:419
hebench::APIBridge::Handle duplicateHandle(hebench::APIBridge::Handle h, std::int64_t new_tag, std::int64_t check_tags) const
Duplicates a handle created by createhandle().
Definition: engine.cpp:230
T * createObj(Args &&... args) const
Definition: engine.hpp:437
static hebench::APIBridge::ErrorCode getLastError()
Retrieves the last error that occurred as set by setLastError().
Definition: engine.hpp:105
static void setLastError(hebench::APIBridge::ErrorCode value)
Sets the last error code that occurred.
Definition: engine.cpp:44
virtual void init()=0
Initializes the backend engine and populates the backend description.
std::uint64_t getDefaultWorkloadParamsCount(hebench::APIBridge::Handle h_bench_desc) const
Retrieves the number of sets of default arguments supported by this benchmark's workload.
Definition: engine.cpp:139
static void addErrorCode(hebench::APIBridge::ErrorCode code, const std::string &description)
Adds a new error code and the error description to the list of possible error codes that this backend...
Definition: engine.cpp:72
static const std::string & getLastErrorDesc()
Retrieves the description of the last error that occurred as set by setLastError().
Definition: engine.hpp:110
BaseEngine & operator=(const BaseEngine &)=delete
~BaseEngine() override
Definition: engine.hpp:88
void describeBenchmark(hebench::APIBridge::Handle h_bench_desc, hebench::APIBridge::BenchmarkDescriptor *p_bench_desc, hebench::APIBridge::WorkloadParams *p_default_params, std::uint64_t default_count) const
Definition: engine.cpp:148
std::uint64_t subscribeBenchmarkCount() const
Retrieves the number of benchmarks for which the backend is registering to perform.
Definition: engine.hpp:155
void addSecurityName(hebench::APIBridge::Security security, const std::string &name)
Adds a new supported security and name pair to the list of securitty types supported by this backend.
Definition: engine.cpp:88
Base class for objects representing a specific benchmark based on an HEBench benchmark descriptor.
Definition: benchmark.hpp:36
Helper class to encapsulate objects that will cross the boundary of the API Bridge.
std::int64_t classTag() const override
Retrieves the tag of the class to which this object belongs.
static constexpr std::int64_t tag
Used to identify this class when returned as a handle to Test Harness.
Represents an object with a tag.
static constexpr std::int64_t MaskReservedBits
Mask representing all bits reserved by C++ wrapper.
#define HEBERROR_MSG_CLASS(message)
#define HEBERROR_DECLARE_CLASS_NAME(class_name)
std::int32_t Scheme
Open-ended homomorphic encryption scheme ID.
Definition: types.h:406
std::int32_t Security
Open-ended homomorphic encryption scheme security ID.
Definition: types.h:412
std::uint64_t size
Size of underlying data.
Definition: types.h:564
void * p
Pointer to underlying data.
Definition: types.h:558
std::int32_t ErrorCode
Return value for API bridge functions.
Definition: types.h:34
std::int64_t tag
Optional tag.
Definition: types.h:569
Defines a benchmark test.
Definition: types.h:527
Specifies the parameters for a workload.
Definition: types.h:363
Structure to contain flexible data.
Definition: types.h:552
std::string to_string(const std::string_view &s)
BaseEngine * createEngine(const std::int8_t *p_buffer, std::uint64_t size)
Creates a new BaseEngine object.
void destroyEngine(BaseEngine *p)
Destroys and cleans up resources from an engine created by createEngine().
BenchmarkDescription * p_bench_description
Definition: engine.hpp:30
BaseBenchmark * p_benchmark
Definition: engine.hpp:29
Bundles a benchmark and its description to cross the API Bridge as a handle.
Definition: engine.hpp:28
#define HEBENCH_ECODE_CRITICAL_ERROR
Specifies a critical error.
Definition: types.h:50