Compute Library
 21.02
main.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2021 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #include "support/StringSupport.h"
25 #include "tests/AssetsLibrary.h"
29 #include "tests/framework/Macros.h"
38 
39 #ifdef ARM_COMPUTE_CL
45 #include "utils/TypePrinter.h"
46 #endif /* ARM_COMPUTE_CL */
47 #ifdef ARM_COMPUTE_GC
49 #endif /* ARM_COMPUTE_GC */
51 
52 #include <fstream>
53 #include <initializer_list>
54 #include <iostream>
55 #include <memory>
56 #include <random>
57 #include <utility>
58 
59 using namespace arm_compute;
60 using namespace arm_compute::test;
61 
62 namespace
63 {
64 std::string command_line(int argc, char **argv)
65 {
66  std::stringstream ss;
67  for(int i = 0; i < argc; i++)
68  {
69  ss << argv[i] << " ";
70  }
71  return ss.str();
72 }
73 } // namespace
74 namespace arm_compute
75 {
76 namespace test
77 {
78 std::unique_ptr<AssetsLibrary> library;
79 
80 static constexpr uint32_t fixed_seed = 1;
81 std::unique_ptr<AssetsLibrary> fixed_library;
82 
83 extern std::unique_ptr<ParametersLibrary> parameters;
84 } // namespace test
85 } // namespace arm_compute
86 
87 namespace
88 {
89 #ifdef ARM_COMPUTE_CL
90 bool file_exists(const std::string &filename)
91 {
92  std::ifstream file(filename);
93  return file.good();
94 }
95 #endif /* ARM_COMPUTE_CL */
96 } //namespace
97 
98 int main(int argc, char **argv)
99 {
101 
103 
104  std::set<framework::DatasetMode> allowed_modes
105  {
110  };
111 
112  framework::CommonOptions options(parser);
113 
114  auto dataset_mode = parser.add_option<utils::EnumOption<framework::DatasetMode>>("mode", allowed_modes, framework::DatasetMode::PRECOMMIT);
115  dataset_mode->set_help("For managed datasets select which group to use");
116  auto filter = parser.add_option<utils::SimpleOption<std::string>>("filter", ".*");
117  filter->set_help("Regular expression to select test cases");
118  auto filter_id = parser.add_option<utils::SimpleOption<std::string>>("filter-id");
119  filter_id->set_help("List of test ids. ... can be used to define a range.");
120  auto stop_on_error = parser.add_option<utils::ToggleOption>("stop-on-error");
121  stop_on_error->set_help("Abort execution after the first failed test (useful for debugging)");
122  auto seed = parser.add_option<utils::SimpleOption<std::random_device::result_type>>("seed", std::random_device()());
123  seed->set_help("Global seed for random number generation");
124  auto list_tests = parser.add_option<utils::ToggleOption>("list-tests", false);
125  list_tests->set_help("List all test names");
126  auto test_instruments = parser.add_option<utils::ToggleOption>("test-instruments", false);
127  test_instruments->set_help("Test if the instruments work on the platform");
128  auto error_on_missing_assets = parser.add_option<utils::ToggleOption>("error-on-missing-assets", false);
129  error_on_missing_assets->set_help("Mark a test as failed instead of skipping it when assets are missing");
130  auto assets = parser.add_positional_option<utils::SimpleOption<std::string>>("assets");
131  assets->set_help("Path to the assets directory");
132 #ifdef ARM_COMPUTE_CL
133  auto enable_tuner = parser.add_option<utils::ToggleOption>("enable-tuner");
134  enable_tuner->set_help("Enable OpenCL dynamic tuner");
135 
136  const std::set<CLTunerMode> supported_tuner_modes
137  {
141  };
142  auto tuner_mode = parser.add_option<utils::EnumOption<CLTunerMode>>("tuner-mode", supported_tuner_modes, CLTunerMode::NORMAL);
143  tuner_mode->set_help("Configures the time taken by the tuner to tune. Slow tuner produces the most performant LWS configuration");
144 
145  auto tuner_file = parser.add_option<utils::SimpleOption<std::string>>("tuner-file", "");
146  tuner_file->set_help("File to load/save CLTuner values");
147 
148  auto mlgo_file = parser.add_option<utils::SimpleOption<std::string>>("mlgo-file", "");
149  mlgo_file->set_help("File to load MLGO heuristics");
150 #endif /* ARM_COMPUTE_CL */
151  auto threads = parser.add_option<utils::SimpleOption<int>>("threads", 1);
152  threads->set_help("Number of threads to use");
153  auto cooldown_sec = parser.add_option<utils::SimpleOption<float>>("delay", -1.f);
154  cooldown_sec->set_help("Delay to add between test executions in seconds");
155 
156  try
157  {
158  parser.parse(argc, argv);
159 
160  if(options.help->is_set() && options.help->value())
161  {
162  parser.print_help(argv[0]);
163  return 0;
164  }
165 
166  std::vector<std::unique_ptr<framework::Printer>> printers = options.create_printers();
167 
168  // Setup CPU Scheduler
169  Scheduler::get().set_num_threads(threads->value());
170 
171  // Create CPU context
172  auto cpu_ctx = std::make_unique<RuntimeContext>();
173  cpu_ctx->set_scheduler(&Scheduler::get());
174 
175  // Track CPU context
176  auto cpu_ctx_track = std::make_unique<ContextSchedulerUser>(cpu_ctx.get());
177 
178  // Create parameters
179  parameters = std::make_unique<ParametersLibrary>();
180  parameters->set_cpu_ctx(std::move(cpu_ctx));
181 
182 #ifdef ARM_COMPUTE_GC
183  // Setup OpenGL context
184  {
185  auto gles_ctx = std::make_unique<GCRuntimeContext>();
186  ARM_COMPUTE_ERROR_ON(gles_ctx == nullptr);
187  {
188  // Legacy singletons API: This has been deprecated and the singletons will be removed in future releases
189  // Setup singleton for backward compatibility
191  }
192  parameters->set_gc_ctx(std::move(gles_ctx));
193  };
194 #endif /* ARM_COMPUTE_GC */
195 
196 #ifdef ARM_COMPUTE_CL
197  CLTuner cl_tuner(false);
198  CLGEMMHeuristicsHandle gemm_heuristics;
199  if(opencl_is_available())
200  {
201  auto ctx_dev_err = create_opencl_context_and_device();
202  ARM_COMPUTE_ERROR_ON_MSG(std::get<2>(ctx_dev_err) != CL_SUCCESS, "Failed to create OpenCL context");
203  gemm_heuristics.reload_from_file(mlgo_file->value());
204  CLScheduler::get().default_init_with_context(std::get<1>(ctx_dev_err), std::get<0>(ctx_dev_err), &cl_tuner, &gemm_heuristics);
205  }
206 
207  if(enable_tuner->is_set())
208  {
209  cl_tuner.set_tune_new_kernels(enable_tuner->value());
210 
211  //set tuner mode
212  cl_tuner.set_tuner_mode(tuner_mode->value());
213 
214  // If that's the first run then the file won't exist yet
215  if(file_exists(tuner_file->value()))
216  {
217  cl_tuner.load_from_file(tuner_file->value());
218  }
219  }
220  else if(!tuner_file->value().empty())
221  {
222  //If we're not tuning and the file doesn't exist then we should raise an error:
223  cl_tuner.load_from_file(tuner_file->value());
224  }
225 #endif /* ARM_COMPUTE_CL */
226  if(options.log_level->value() > framework::LogLevel::NONE)
227  {
228  for(auto &p : printers)
229  {
230  p->print_global_header();
231  }
232  }
233 
235  {
236  for(auto &p : printers)
237  {
238  p->print_entry("Version", build_information());
239  p->print_entry("CommandLine", command_line(argc, argv));
240  p->print_entry("Seed", support::cpp11::to_string(seed->value()));
241 #ifdef ARM_COMPUTE_CL
242  if(opencl_is_available())
243  {
244  p->print_entry("CL_DEVICE_VERSION", CLKernelLibrary::get().get_device_version());
245  }
246  else
247  {
248  p->print_entry("CL_DEVICE_VERSION", "Unavailable");
249  }
250 #endif /* ARM_COMPUTE_CL */
251  const arm_compute::CPUInfo &cpu_info = Scheduler::get().cpu_info();
252  const unsigned int num_cpus = cpu_info.get_cpu_num();
253  p->print_entry("cpu_has_fp16", support::cpp11::to_string(cpu_info.has_fp16()));
254  p->print_entry("cpu_has_dotprod", support::cpp11::to_string(cpu_info.has_dotprod()));
255 
256  for(unsigned int j = 0; j < num_cpus; ++j)
257  {
258  const CPUModel model = cpu_info.get_cpu_model(j);
259  p->print_entry("CPU" + support::cpp11::to_string(j), cpu_model_to_string(model));
260  }
261  p->print_entry("Iterations", support::cpp11::to_string(options.iterations->value()));
262  p->print_entry("Threads", support::cpp11::to_string(threads->value()));
263  {
265  p->print_entry("Dataset mode", to_string(dataset_mode->value()));
266  }
267  }
268  }
269 
270  // Setup instruments meta-data
272  instruments_info._scheduler_users.push_back(cpu_ctx_track.get());
273  framework.set_instruments_info(instruments_info);
274 
275  // Initialize framework
277  fconfig.instruments = options.instruments->value();
278  fconfig.name_filter = filter->value();
279  fconfig.id_filter = filter_id->value();
280  fconfig.num_iterations = options.iterations->value();
281  fconfig.mode = dataset_mode->value();
282  fconfig.log_level = options.log_level->value();
283  fconfig.cooldown_sec = cooldown_sec->value();
284  framework.init(fconfig);
285 
286  for(auto &p : printers)
287  {
288  framework.add_printer(p.get());
289  }
290  framework.set_throw_errors(options.throw_errors->value());
291  framework.set_stop_on_error(stop_on_error->value());
292  framework.set_error_on_missing_assets(error_on_missing_assets->value());
293 
294  bool success = true;
295 
296  if(list_tests->value())
297  {
298  for(auto &p : printers)
299  {
300  p->print_list_tests(framework.test_infos());
301  p->print_global_footer();
302  }
303 
304  return 0;
305  }
306 
307  if(test_instruments->value())
308  {
309  framework::Profiler profiler = framework.get_profiler();
310  profiler.start();
311  profiler.stop();
312  for(auto &p : printers)
313  {
314  p->print_measurements(profiler.measurements());
315  }
316 
317  return 0;
318  }
319 
320  library = std::make_unique<AssetsLibrary>(assets->value(), seed->value());
321  fixed_library = std::make_unique<AssetsLibrary>(assets->value(), fixed_seed);
322 
323  if(!parser.validate())
324  {
325  return 1;
326  }
327 
328  success = framework.run();
329 
330  if(options.log_level->value() > framework::LogLevel::NONE)
331  {
332  for(auto &p : printers)
333  {
334  p->print_global_footer();
335  }
336  }
337 
338 #ifdef ARM_COMPUTE_CL
339  if(opencl_is_available())
340  {
342  if(enable_tuner->is_set() && enable_tuner->value() && tuner_file->is_set())
343  {
344  cl_tuner.save_to_file(tuner_file->value());
345  }
346  }
347 #endif /* ARM_COMPUTE_CL */
348 
349  return (success ? 0 : 1);
350  }
351  catch(const std::exception &error)
352  {
353  std::cerr << error.what() << "\n";
354 
355  if(options.throw_errors->value())
356  {
357  throw;
358  }
359 
360  return 1;
361  }
362  return 0;
363 }
Basic implementation of the OpenCL tuner interface.
Definition: CLTuner.h:40
Framework configuration structure.
Definition: Framework.h:57
void set_tuner_mode(CLTunerMode mode)
Set OpenCL tuner mode.
Definition: CLTuner.cpp:61
arm_compute::utils::ToggleOption * help
Show help option.
Definition: CommonOptions.h:74
int main(int argc, char **argv)
Definition: main.cpp:98
int num_iterations
Number of iterations per test.
Definition: Framework.h:63
static CLScheduler & get()
Access the scheduler singleton.
Profiler class to collect benchmark numbers.
Definition: Profiler.h:45
virtual void set_num_threads(unsigned int num_threads)=0
Sets the number of threads the scheduler will use to run the kernels.
void default_init_with_context(cl::Device &device, cl::Context &ctx, ICLTuner *cl_tuner=nullptr, CLGEMMHeuristicsHandle *gemm_h=nullptr)
Initialises the scheduler with context and device provided by the user.
std::string to_string(T &&value)
Convert integer and float values to string.
void load_from_file(const std::string &filename)
Load the tuning parameters table from file.
Definition: CLTuner.cpp:280
bool has_fp16() const
Checks if the cpu model supports fp16.
Definition: CPPTypes.cpp:58
void set_throw_errors(bool throw_errors)
Set whether errors are caught or thrown by the framework.
Definition: Framework.cpp:263
Implementation of an option that can be either true or false.
Definition: ToggleOption.h:36
const std::vector< T > & value() const
Get the values of the option.
void set_instruments_info(InstrumentsInfo instr_info)
Sets instruments info.
Definition: Framework.cpp:699
std::unique_ptr< InstrumentsInfo > instruments_info
Definition: Framework.cpp:50
std::stringstream ss(mlgo_str)
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
bool validate() const
Validates the previously parsed command line arguments.
CPUModel
CPU models - we only need to detect CPUs we have microarchitecture-specific code for.
Definition: CPPTypes.h:40
CPUInfo & cpu_info()
Get CPU info.
Definition: IScheduler.cpp:42
std::tuple< cl::Context, cl::Device, cl_int > create_opencl_context_and_device()
This function creates an OpenCL context and a device.
Definition: CLHelpers.cpp:89
void set_error_on_missing_assets(bool error_on_missing_assets)
Set whether a test should be considered as failed if its assets cannot be found.
Definition: Framework.cpp:283
Class to parse command line arguments.
Copyright (c) 2017-2021 Arm Limited.
CPUModel get_cpu_model(unsigned int cpuid) const
Gets the cpu model for a given cpuid.
Definition: CPPTypes.cpp:68
float cooldown_sec
Delay between tests in seconds.
Definition: Framework.h:64
static Framework & get()
Access to the singleton.
Definition: Framework.cpp:124
static GCScheduler & get()
Access the scheduler singleton.
Definition: GCScheduler.cpp:70
Searches a minimal subset of LWS configurations while tuning.
T * add_option(const std::string &name, As &&... args)
Function to add a new option to the parser.
std::string cpu_model_to_string(CPUModel val)
Convert a cpumodel value to a string.
Definition: CPPTypes.h:71
Interface to enqueue OpenCL kernels and get/set the OpenCL CommandQueue and ICLTuner.
LogLevel log_level
Verbosity of the output.
Definition: Framework.h:65
bool run()
Run all enabled test cases.
Definition: Framework.cpp:540
void parse(int argc, char **argv)
Parses the command line arguments and updates the options accordingly.
std::vector< framework::InstrumentsDescription > instruments
Instrument types that will be used for benchmarking.
Definition: Framework.h:59
bool save_to_file(const std::string &filename) const
Save the content of the tuning parameters table to file.
Definition: CLTuner.cpp:332
std::vector< std::unique_ptr< Printer > > create_printers()
Create the printers based on parsed command line options.
std::vector< TestInfo > test_infos() const
List of TestInfo&#39;s.
Definition: Framework.cpp:673
std::string name_filter
Regular expression to filter tests by name.
Definition: Framework.h:60
std::unique_ptr< AssetsLibrary > library
Definition: main.cpp:78
bool reload_from_file(const std::string &filename)
(Re)Load the heuristics from reading a dotmlgo file
std::unique_ptr< ParametersLibrary > parameters
Definition: Framework.cpp:46
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456
void set_tune_new_kernels(bool tune_new_kernels)
Setter for tune_new_kernels option.
Definition: CLTuner.cpp:52
bool has_dotprod() const
Checks if the cpu model supports dot product.
Definition: CPPTypes.cpp:63
Profiler get_profiler() const
Factory method to obtain a configured profiler.
Definition: Framework.cpp:639
const T & value() const
Get the option value.
Definition: SimpleOption.h:112
T * add_positional_option(As &&... args)
Function to add a new positional argument to the parser.
const MeasurementsMap & measurements() const
Return measurements for all instruments.
Definition: Profiler.cpp:87
unsigned int get_cpu_num() const
Return the maximum number of CPUs present.
Definition: CPPTypes.cpp:54
arm_compute::utils::SimpleOption< int > * iterations
Number of iterations option.
Definition: CommonOptions.h:76
arm_compute::utils::EnumListOption< InstrumentsDescription > * instruments
Instruments option.
Definition: CommonOptions.h:75
void sync()
Blocks until all commands in the associated command queue have finished.
Definition: CLScheduler.cpp:72
Handle for loading and retrieving GEMM heuristics.
arm_compute::utils::EnumOption< LogLevel > * log_level
Logging level option.
Definition: CommonOptions.h:79
std::string id_filter
String to match selected test ids.
Definition: Framework.h:61
Implementation of a simple option that accepts a value from a fixed set.
Definition: EnumOption.h:40
std::string to_string(const ICLTensor &arg)
void start()
Call start() on all the added instruments.
Definition: Profiler.cpp:48
Wrapper to configure the Khronos OpenCL C++ header.
std::vector< ISchedulerUser * > _scheduler_users
Definition: Instruments.h:65
std::string build_information()
Returns the arm_compute library build information.
Interface to enqueue GLES kernels and get/set the GLES CommandQueue.
void add_printer(Printer *printer)
Set the printer used for the output of test results.
Definition: Framework.cpp:668
void stop()
Call stop() on all the added instruments.
Definition: Profiler.cpp:56
bool is_set() const
Has a value been assigned to the option?
Definition: Option.h:135
void print_help(const std::string &program_name) const
Prints a help message for all configured options.
const T & value() const
Get the selected value.
Common command line options used to configure the framework.
Definition: CommonOptions.h:54
Searches all possible LWS configurations while tuning.
void set_stop_on_error(bool stop_on_error)
Set whether to abort execution after the first failed test.
Definition: Framework.cpp:273
void default_init()
Initialises the context and command queue used by the scheduler to default values and sets a default ...
Definition: GCScheduler.cpp:48
void set_help(std::string help)
Set the help message for the option.
Definition: Option.h:125
void init(const FrameworkConfig &config)
Init the framework.
Definition: Framework.cpp:130
bool opencl_is_available()
Check if OpenCL is available.
Definition: OpenCL.cpp:152
static IScheduler & get()
Access the scheduler singleton.
Definition: Scheduler.cpp:94
std::unique_ptr< AssetsLibrary > fixed_library
Definition: main.cpp:81
arm_compute::utils::ToggleOption * throw_errors
Throw errors option.
Definition: CommonOptions.h:80