HEBench
hebench_matmult_o.cpp
Go to the documentation of this file.
1 
2 // Copyright (C) 2021 Intel Corporation
3 // SPDX-License-Identifier: Apache-2.0
4 
5 #include <bitset>
6 #include <cassert>
7 #include <cstring>
8 #include <iomanip>
9 #include <iostream>
10 #include <sstream>
11 #include <stdexcept>
12 #include <utility>
13 
14 #include "hebench/modules/timer/include/timer.h"
15 
16 #include "hebench/api_bridge/api.h"
17 #include "hebench/modules/general/include/hebench_math_utils.h"
18 #include "include/hebench_engine.h"
19 
20 #include "../include/hebench_matmult_o.h"
21 
22 namespace hebench {
23 namespace TestHarness {
24 namespace MatrixMultiply {
25 namespace Offline {
26 
27 //----------------------------
28 // class BenchmarkDescription
29 //----------------------------
30 
31 bool BenchmarkDescriptor::m_b_registered = // register the benchmark with the factory
32  hebench::TestHarness::BenchmarkFactory::registerSupportedBenchmark(std::make_shared<BenchmarkDescriptor>());
33 
35  const std::vector<hebench::APIBridge::WorkloadParam> &w_params) const
36 {
37  assert(m_b_registered);
38 
39  // return true if benchmark is supported
40 
41  bool retval =
44 
45  return retval;
46 }
47 
49  const Engine &engine,
50  const BenchmarkDescription::Backend &backend_desc,
51  const BenchmarkDescription::Configuration &config) const
52 {
53  // finish describing workload
54  assert(OpParameterCount == 2);
55  assert(DefaultBatchSize == 100);
56 
57  BenchmarkDescriptorCategory::completeWorkloadDescription(output, engine, backend_desc, config);
58 
59  assert(OpParameterCount == output.operation_params_count);
60 
61  std::stringstream ss;
62  std::uint64_t *batch_sizes = output.concrete_descriptor.cat_params.offline.data_count;
63  std::array<std::pair<std::uint64_t, std::uint64_t>, OpParameterCount> mat_dims =
64  fetchMatrixSizes(config.w_params);
65 
66  std::uint64_t sample_size_fallback =
67  config.fallback_default_sample_size > 0 ?
70  std::uint64_t result_batch_size =
73  config.default_sample_sizes,
74  backend_desc.descriptor,
75  sample_size_fallback,
77 
78  // complete header with workload specifics
79  ss << ", , M = M0 x M1" << std::endl
80  << ", , , Rows, Columns, Batch size" << std::endl;
81  for (std::size_t i = 0; i < OpParameterCount; ++i)
82  {
83  ss << ", , M" << i << ", " << mat_dims[i].first << ", " << mat_dims[i].second << ", " << batch_sizes[i] << std::endl;
84  } // end for
85  ss << ", , M, " << mat_dims[0].first << ", " << mat_dims[1].second << ", " << result_batch_size << std::endl;
86 
87  output.workload_header = ss.str();
88 }
89 
91  const DescriptionToken &description_token)
92 {
93  assert(m_b_registered);
94  Benchmark *retval = nullptr;
95 
96  try
97  {
98  retval = new Benchmark(p_engine, description_token);
99  }
100  catch (...)
101  {
102  if (retval)
103  delete retval;
104  throw;
105  }
106 
107  return retval;
108 }
109 
111 {
112  assert(m_b_registered);
113  if (p_bench)
114  delete p_bench;
115 }
116 
117 //-----------------
118 // class Benchmark
119 //-----------------
120 
121 Benchmark::Benchmark(std::shared_ptr<Engine> p_engine,
122  const IBenchmarkDescriptor::DescriptionToken &description_token) :
123  BenchmarkOffline(p_engine, description_token)
124 {
125 }
126 
128 {
129  hebench::Common::EventTimer timer;
130  hebench::Common::TimingReportEvent::Ptr p_timing_event;
131  std::uint64_t batch_sizes[BenchmarkDescriptor::OpParameterCount];
132  std::stringstream ss;
133 
134  std::array<std::pair<std::uint64_t, std::uint64_t>, BenchmarkDescriptor::OpParameterCount> mat_dims =
136 
137  std::uint64_t sample_size_fallback =
143  this->getBenchmarkConfiguration().default_sample_sizes,
144  this->getBackendDescription().descriptor,
145  sample_size_fallback,
147 
148  std::cout << IOS_MSG_INFO << hebench::Logging::GlobalLogger::log("Preparing workload.") << std::endl;
149 
150  timer.start();
151  if (this->getBenchmarkConfiguration().dataset_filename.empty())
152  {
153  // generates random matrices for input and generates (computes) ground truth
154  std::cout << IOS_MSG_INFO << hebench::Logging::GlobalLogger::log("Generating data...") << std::endl;
155  m_data = DataLoader::create(mat_dims[0].first, mat_dims[0].second, // M0
156  mat_dims[1].second, // M1
157  batch_sizes[0], batch_sizes[1],
158  this->getBackendDescription().descriptor.data_type);
159  } // end if
160  else
161  {
162  std::stringstream ss;
163  ss << "Loading data from external dataset: " << std::endl
164  << "\"" << this->getBenchmarkConfiguration().dataset_filename << "\"";
165  std::cout << IOS_MSG_INFO << hebench::Logging::GlobalLogger::log(ss.str()) << std::endl;
166  // load matrices for input and ground truth from file
167  m_data = DataLoader::create(mat_dims[0].first, mat_dims[0].second, // M0
168  mat_dims[1].second, // M1
169  batch_sizes[0], batch_sizes[1],
170  this->getBackendDescription().descriptor.data_type,
171  this->getBenchmarkConfiguration().dataset_filename);
172  } // end else
173  p_timing_event = timer.stop<std::milli>();
174 
175  ss = std::stringstream();
176  ss << "Total data loaded: " << m_data->getTotalDataLoaded() << " bytes";
177  std::cout << IOS_MSG_DONE << hebench::Logging::GlobalLogger::log(ss.str()) << std::endl;
178  ss = std::stringstream();
179  ss << "Elapsed wall time: " << p_timing_event->elapsedWallTime<std::milli>() << " ms";
180  std::cout << IOS_MSG_INFO << hebench::Logging::GlobalLogger::log(ss.str()) << std::endl;
181  ss = std::stringstream();
182  ss << "Elapsed CPU time: " << p_timing_event->elapsedCPUTime<std::milli>() << " ms";
183  std::cout << IOS_MSG_INFO << hebench::Logging::GlobalLogger::log(ss.str()) << std::endl;
184 }
185 
187  const std::uint64_t *param_data_pack_indices,
188  const std::vector<hebench::APIBridge::NativeDataBuffer *> &outputs,
189  hebench::APIBridge::DataType data_type) const
190 {
191  assert(dataset->getParameterCount() == BenchmarkDescriptorCategory::OpParameterCount
192  && dataset->getResultCount() == BenchmarkDescriptorCategory::OpResultCount);
193 
194  return BenchmarkOffline::validateResult(dataset, param_data_pack_indices, outputs, data_type);
195 }
196 
197 } // namespace Offline
198 } // namespace MatrixMultiply
199 } // namespace TestHarness
200 } // namespace hebench
const hebench::APIBridge::BenchmarkDescriptor & descriptor
Benchmark backend descriptor, as retrieved by backend, corresponding to the registration handle h_des...
std::uint64_t fallback_default_sample_size
Default sample size to be used if a specific size is not specified in the default_sample_sizes collec...
std::vector< std::uint64_t > default_sample_sizes
Default sample size for each operation parameter.
std::vector< hebench::APIBridge::WorkloadParam > w_params
Set of arguments for workload parameters.
std::string dataset_filename
File containing data for the benchmark. If empty string, benchmarks that can auto generate the datase...
static bool registerSupportedBenchmark(std::shared_ptr< IBenchmarkDescriptor > p_desc_obj)
Registers a benchmark description object that represents one of the supported workloads.
Base class for workload benchmarks in the offline category.
Token returned by a successful call to IBenchmarkDescriptor::matchBenchmarkDescriptor().
std::shared_ptr< IDataLoader > Ptr
static std::array< std::pair< std::uint64_t, std::uint64_t >, OpParameterCount > fetchMatrixSizes(const std::vector< hebench::APIBridge::WorkloadParam > &w_params)
fetchMatrixSizes
bool matchBenchmarkDescriptor(const hebench::APIBridge::BenchmarkDescriptor &bench_desc, const std::vector< hebench::APIBridge::WorkloadParam > &w_params) const override
Determines if the represented benchmark can perform the workload described by a specified HEBench ben...
void completeWorkloadDescription(WorkloadDescriptionOutput &output, const Engine &engine, const BenchmarkDescription::Backend &backend_desc, const BenchmarkDescription::Configuration &config) const override
Completes the description for the matched benchmark.
static DataLoader::Ptr create(std::uint64_t rows_a, std::uint64_t cols_a, std::uint64_t cols_b, std::uint64_t batch_size_mat_a, std::uint64_t batch_size_mat_b, hebench::APIBridge::DataType data_type)
void destroyBenchmark(hebench::TestHarness::PartialBenchmark *p_bench) override
Destroys an object returned by createBenchmark().
hebench::TestHarness::PartialBenchmark * createBenchmark(std::shared_ptr< Engine > p_engine, const DescriptionToken &description_token) override
Creates the represented IBenchmark object that can perform the workload specified by the HEBench benc...
bool matchBenchmarkDescriptor(const hebench::APIBridge::BenchmarkDescriptor &bench_desc, const std::vector< hebench::APIBridge::WorkloadParam > &w_params) const override
Determines if the represented benchmark can perform the workload described by a specified HEBench ben...
void completeWorkloadDescription(WorkloadDescriptionOutput &output, const Engine &engine, const BenchmarkDescription::Backend &backend_desc, const BenchmarkDescription::Configuration &config) const override
Completes the description for the matched benchmark.
bool validateResult(IDataLoader::Ptr dataset, const std::uint64_t *param_data_pack_indices, const std::vector< hebench::APIBridge::NativeDataBuffer * > &p_outputs, hebench::APIBridge::DataType data_type) const override
Validates the result of an operation against the ground truth.
void init() override
Initializes the partial benchmark members.
virtual bool validateResult(IDataLoader::Ptr dataset, const std::uint64_t *param_data_pack_indices, const std::vector< hebench::APIBridge::NativeDataBuffer * > &outputs, hebench::APIBridge::DataType data_type) const
Validates the result of an operation against the ground truth.
std::string workload_header
Workload specific information to be added to the report header.
static std::uint64_t computeSampleSizes(std::uint64_t *sample_sizes, std::size_t param_count, const std::vector< std::uint64_t > &config_sample_sizes, const hebench::APIBridge::BenchmarkDescriptor &bench_desc, std::uint64_t default_sample_size_fallback, bool force_config)
Extracts the batch sizes for a workload from a specified HEBench API benchmark descriptor.
static bool getForceConfigValues()
Specifies whether frontend will override backend descriptors using configuration data or not.
std::size_t operation_params_count
Number of parameters for the represented workload operation.
hebench::APIBridge::BenchmarkDescriptor concrete_descriptor
Benchmark descriptor completed with concrete values assigned to configurable fields.
Bundles values that need to be filled by a workload during completeWorkloadDescription().
const BenchmarkDescription::Backend & getBackendDescription() const
Allows read-only access to this benchmark backend description.
const BenchmarkDescription::Configuration & getBenchmarkConfiguration() const
Allows read-only access to this benchmark configuration.
#define IOS_MSG_DONE
#define IOS_MSG_INFO
DataType
Defines data types for a workload.
Definition: types.h:379
CategoryParams cat_params
Parameters for the category.
Definition: types.h:532
Category category
Category for the benchmark.
Definition: types.h:531
Defines a benchmark test.
Definition: types.h:527