HEBench
hebench_matmult_l.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/logging/include/logging.h"
15 #include "hebench/modules/timer/include/timer.h"
16 
17 #include "hebench/api_bridge/api.h"
18 #include "hebench/modules/general/include/hebench_math_utils.h"
19 #include "include/hebench_engine.h"
20 
21 #include "../include/hebench_matmult_l.h"
22 
23 namespace hebench {
24 namespace TestHarness {
25 namespace MatrixMultiply {
26 namespace Latency {
27 
28 //----------------------------
29 // class BenchmarkDescription
30 //----------------------------
31 
32 bool BenchmarkDescriptor::m_b_registered = // register the benchmark with the factory
33  hebench::TestHarness::BenchmarkFactory::registerSupportedBenchmark(std::make_shared<BenchmarkDescriptor>());
34 
36  const std::vector<hebench::APIBridge::WorkloadParam> &w_params) const
37 {
38  assert(m_b_registered);
39 
40  // return true if benchmark is supported
41 
42  bool retval =
45 
46  return retval;
47 }
48 
50  const Engine &engine,
51  const BenchmarkDescription::Backend &backend_desc,
52  const BenchmarkDescription::Configuration &config) const
53 {
54  assert(OpParameterCount == 2);
55  assert(DefaultBatchSize == 1);
56 
57  BenchmarkDescriptorCategory::completeWorkloadDescription(output, engine, backend_desc, config);
58 
59  assert(OpParameterCount == output.operation_params_count);
60 
61  // finish benchmark header description
62 
63  std::stringstream ss;
64  std::uint64_t batch_sizes[OpParameterCount];
65  std::array<std::pair<std::uint64_t, std::uint64_t>, OpParameterCount> mat_dims =
66  fetchMatrixSizes(config.w_params);
67 
68  ss = std::stringstream();
69 
70  std::uint64_t result_batch_size = 1;
71  for (std::size_t param_i = 0; param_i < OpParameterCount; ++param_i)
72  {
73  batch_sizes[param_i] = DefaultBatchSize;
74  result_batch_size *= batch_sizes[param_i];
75  } // end for
76  // complete header with workload specifics
77  ss << ", , M = M0 x M1" << std::endl
78  << ", , , Rows, Columns, Batch size" << std::endl;
79  for (std::size_t i = 0; i < OpParameterCount; ++i)
80  {
81  ss << ", , M" << i << ", " << mat_dims[i].first << ", " << mat_dims[i].second << ", " << batch_sizes[i] << std::endl;
82  } // end for
83  ss << ", , M, " << mat_dims[0].first << ", " << mat_dims[1].second << ", " << result_batch_size << std::endl;
84 
85  output.workload_header = ss.str();
86 }
87 
89  const DescriptionToken &description_token)
90 {
91  assert(m_b_registered);
92  Benchmark *retval = nullptr;
93 
94  try
95  {
96  retval = new Benchmark(p_engine, description_token);
97  }
98  catch (...)
99  {
100  if (retval)
101  delete retval;
102  throw;
103  }
104 
105  return retval;
106 }
107 
109 {
110  assert(m_b_registered);
111  if (p_bench)
112  delete p_bench;
113 }
114 
115 //-----------------
116 // class Benchmark
117 //-----------------
118 
119 Benchmark::Benchmark(std::shared_ptr<Engine> p_engine,
120  const IBenchmarkDescriptor::DescriptionToken &description_token) :
121  BenchmarkLatency(p_engine, description_token)
122 {
123 }
124 
126 {
127  hebench::Common::EventTimer timer;
128  hebench::Common::TimingReportEvent::Ptr p_timing_event;
129  std::uint64_t batch_sizes[BenchmarkDescriptor::OpParameterCount];
130  std::stringstream ss;
131 
132  std::array<std::pair<std::uint64_t, std::uint64_t>, BenchmarkDescriptor::OpParameterCount> mat_dims =
134  for (std::size_t param_i = 0; param_i < BenchmarkDescriptor::OpParameterCount; ++param_i)
135  batch_sizes[param_i] = BenchmarkDescriptor::DefaultBatchSize;
136 
137  std::cout << IOS_MSG_INFO << hebench::Logging::GlobalLogger::log("Preparing workload.") << std::endl;
138 
139  timer.start();
140  if (this->getBenchmarkConfiguration().dataset_filename.empty())
141  {
142  // generates random matrices for input and generates (computes) ground truth
143  std::cout << IOS_MSG_INFO << hebench::Logging::GlobalLogger::log("Generating data...") << std::endl;
144  m_data = DataLoader::create(mat_dims[0].first, mat_dims[0].second, // M0
145  mat_dims[1].second, // M1
146  batch_sizes[0], batch_sizes[1],
147  this->getBackendDescription().descriptor.data_type);
148  } // end if
149  else
150  {
151  std::stringstream ss;
152  ss << "Loading data from external dataset: " << std::endl
153  << "\"" << this->getBenchmarkConfiguration().dataset_filename << "\"";
154  std::cout << IOS_MSG_INFO << hebench::Logging::GlobalLogger::log(ss.str()) << std::endl;
155  // load matrices for input and ground truth from file
156  m_data = DataLoader::create(mat_dims[0].first, mat_dims[0].second, // M0
157  mat_dims[1].second, // M1
158  batch_sizes[0], batch_sizes[1],
159  this->getBackendDescription().descriptor.data_type,
160  this->getBenchmarkConfiguration().dataset_filename);
161  } // end else
162  p_timing_event = timer.stop<std::milli>();
163 
164  ss = std::stringstream();
165  ss << "Total data loaded: " << m_data->getTotalDataLoaded() << " bytes";
166  std::cout << IOS_MSG_DONE << hebench::Logging::GlobalLogger::log(ss.str()) << std::endl;
167  ss = std::stringstream();
168  ss << "Elapsed wall time: " << p_timing_event->elapsedWallTime<std::milli>() << " ms";
169  std::cout << IOS_MSG_INFO << hebench::Logging::GlobalLogger::log(ss.str()) << std::endl;
170  ss = std::stringstream();
171  ss << "Elapsed CPU time: " << p_timing_event->elapsedCPUTime<std::milli>() << " ms";
172  std::cout << IOS_MSG_INFO << hebench::Logging::GlobalLogger::log(ss.str()) << std::endl;
173 }
174 
176  const std::uint64_t *param_data_pack_indices,
177  const std::vector<hebench::APIBridge::NativeDataBuffer *> &outputs,
178  hebench::APIBridge::DataType data_type) const
179 {
180  assert(dataset->getParameterCount() == BenchmarkDescriptorCategory::OpParameterCount
181  && dataset->getResultCount() == BenchmarkDescriptorCategory::OpResultCount);
182 
183  return BenchmarkLatency::validateResult(dataset, param_data_pack_indices, outputs, data_type);
184 }
185 
186 } // namespace Latency
187 } // namespace MatrixMultiply
188 } // namespace TestHarness
189 } // namespace hebench
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 latency 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)
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.
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...
void destroyBenchmark(hebench::TestHarness::PartialBenchmark *p_bench) override
Destroys an object returned by createBenchmark().
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.
std::size_t operation_params_count
Number of parameters for the represented workload operation.
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
Category category
Category for the benchmark.
Definition: types.h:531
Defines a benchmark test.
Definition: types.h:527