Compute Library
 21.08
main.cpp File Reference

Main entry point for the tests. More...

Go to the source code of this file.

Namespaces

 arm_compute
 Copyright (c) 2017-2021 Arm Limited.
 
 arm_compute::test
 

Functions

int main (int argc, char **argv)
 

Detailed Description

Main entry point for the tests.

Currently shared between validation and benchmarking.

Definition in file main.cpp.

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 96 of file main.cpp.

References InstrumentsInfo::_scheduler_users, CommandLineParser::add_option(), CommandLineParser::add_positional_option(), Framework::add_printer(), arm_compute::test::framework::ALL, ARM_COMPUTE_ERROR_ON_MSG, arm_compute::build_information(), arm_compute::test::framework::CONFIG, FrameworkConfig::configure_only, FrameworkConfig::cooldown_sec, IScheduler::cpu_info(), arm_compute::cpuinfo::cpu_model_to_string(), arm_compute::create_opencl_context_and_device(), CommonOptions::create_printers(), CLScheduler::default_init_with_context(), arm_compute::test::framework::DISABLED, arm_compute::EXHAUSTIVE, arm_compute::test::fixed_library, CLKernelLibrary::get(), Scheduler::get(), CLScheduler::get(), Framework::get(), CPUInfo::get_cpu_model(), CPUInfo::get_cpu_num(), Framework::get_profiler(), CPUInfo::has_bf16(), CPUInfo::has_dotprod(), CPUInfo::has_fp16(), CPUInfo::has_sve(), CommonOptions::help, FrameworkConfig::id_filter, Framework::init(), FrameworkConfig::instruments, CommonOptions::instruments, arm_compute::test::framework::instruments_info, Option::is_set(), CommonOptions::iterations, arm_compute::test::library, CLTuner::load_from_file(), FrameworkConfig::log_level, CommonOptions::log_level, Profiler::measurements(), FrameworkConfig::mode, FrameworkConfig::name_filter, arm_compute::Native, arm_compute::test::framework::NIGHTLY, arm_compute::test::framework::NONE, arm_compute::NORMAL, FrameworkConfig::num_iterations, arm_compute::opencl_is_available(), arm_compute::test::parameters, CommandLineParser::parse(), GemmTuner::parser, arm_compute::test::framework::PRECOMMIT, CommandLineParser::print_help(), arm_compute::RAPID, CLGEMMHeuristicsHandle::reload_from_file(), Framework::run(), CLTuner::save_to_file(), Framework::set_error_on_missing_assets(), Option::set_help(), Framework::set_instruments_info(), IScheduler::set_num_threads(), Framework::set_stop_on_error(), Framework::set_throw_errors(), CLTuner::set_tune_new_kernels(), CLTuner::set_tuner_mode(), Profiler::start(), Profiler::stop(), CLScheduler::sync(), Framework::test_infos(), CommonOptions::throw_errors, arm_compute::to_string(), arm_compute::support::cpp11::to_string(), CommandLineParser::validate(), EnumOption< T >::value(), EnumListOption< T >::value(), and SimpleOption< T >::value().

97 {
98  framework::Framework &framework = framework::Framework::get();
99 
101 
102  std::set<framework::DatasetMode> allowed_modes
103  {
104  framework::DatasetMode::DISABLED,
105  framework::DatasetMode::PRECOMMIT,
106  framework::DatasetMode::NIGHTLY,
107  framework::DatasetMode::ALL
108  };
109 
110  framework::CommonOptions options(parser);
111 
112  auto dataset_mode = parser.add_option<utils::EnumOption<framework::DatasetMode>>("mode", allowed_modes, framework::DatasetMode::PRECOMMIT);
113  dataset_mode->set_help("For managed datasets select which group to use");
114  auto filter = parser.add_option<utils::SimpleOption<std::string>>("filter", ".*");
115  filter->set_help("Regular expression to select test cases");
116  auto filter_id = parser.add_option<utils::SimpleOption<std::string>>("filter-id");
117  filter_id->set_help("List of test ids. ... can be used to define a range.");
118  auto stop_on_error = parser.add_option<utils::ToggleOption>("stop-on-error");
119  stop_on_error->set_help("Abort execution after the first failed test (useful for debugging)");
120  auto seed = parser.add_option<utils::SimpleOption<std::random_device::result_type>>("seed", std::random_device()());
121  seed->set_help("Global seed for random number generation");
122  auto list_tests = parser.add_option<utils::ToggleOption>("list-tests", false);
123  list_tests->set_help("List all test names");
124  auto test_instruments = parser.add_option<utils::ToggleOption>("test-instruments", false);
125  test_instruments->set_help("Test if the instruments work on the platform");
126  auto error_on_missing_assets = parser.add_option<utils::ToggleOption>("error-on-missing-assets", false);
127  error_on_missing_assets->set_help("Mark a test as failed instead of skipping it when assets are missing");
128  auto assets = parser.add_positional_option<utils::SimpleOption<std::string>>("assets");
129  assets->set_help("Path to the assets directory");
130 #ifdef ARM_COMPUTE_CL
131  auto enable_tuner = parser.add_option<utils::ToggleOption>("enable-tuner");
132  enable_tuner->set_help("Enable OpenCL dynamic tuner");
133 
134  const std::set<CLTunerMode> supported_tuner_modes
135  {
136  CLTunerMode::EXHAUSTIVE,
137  CLTunerMode::NORMAL,
138  CLTunerMode::RAPID
139  };
140  auto tuner_mode = parser.add_option<utils::EnumOption<CLTunerMode>>("tuner-mode", supported_tuner_modes, CLTunerMode::NORMAL);
141  tuner_mode->set_help("Configures the time taken by the tuner to tune. Slow tuner produces the most performant LWS configuration");
142 
143  auto tuner_file = parser.add_option<utils::SimpleOption<std::string>>("tuner-file", "");
144  tuner_file->set_help("File to load/save CLTuner values");
145 
146  auto mlgo_file = parser.add_option<utils::SimpleOption<std::string>>("mlgo-file", "");
147  mlgo_file->set_help("File to load MLGO heuristics");
148 #endif /* ARM_COMPUTE_CL */
149  auto threads = parser.add_option<utils::SimpleOption<int>>("threads", 1);
150  threads->set_help("Number of threads to use");
151  auto cooldown_sec = parser.add_option<utils::SimpleOption<float>>("delay", -1.f);
152  cooldown_sec->set_help("Delay to add between test executions in seconds");
153  auto configure_only = parser.add_option<utils::ToggleOption>("configure-only", false);
154  configure_only->set_help("Only configures kernels, without allocating, running or validating. Needed in order to validate OpenCL kernel run-time compilation, without necessarily running or validating the kernels' execution");
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_CL
183  CLTuner cl_tuner(false);
184  CLGEMMHeuristicsHandle gemm_heuristics;
185  if(opencl_is_available())
186  {
187  auto ctx_dev_err = create_opencl_context_and_device(CLBackendType::Native);
188  ARM_COMPUTE_ERROR_ON_MSG(std::get<2>(ctx_dev_err) != CL_SUCCESS, "Failed to create OpenCL context");
189  gemm_heuristics.reload_from_file(mlgo_file->value());
190  CLScheduler::get().default_init_with_context(std::get<1>(ctx_dev_err), std::get<0>(ctx_dev_err), &cl_tuner, &gemm_heuristics);
191  }
192 
193  if(enable_tuner->is_set())
194  {
195  cl_tuner.set_tune_new_kernels(enable_tuner->value());
196 
197  //set tuner mode
198  cl_tuner.set_tuner_mode(tuner_mode->value());
199 
200  // If that's the first run then the file won't exist yet
201  if(file_exists(tuner_file->value()))
202  {
203  cl_tuner.load_from_file(tuner_file->value());
204  }
205  }
206  else if(!tuner_file->value().empty())
207  {
208  //If we're not tuning and the file doesn't exist then we should raise an error:
209  cl_tuner.load_from_file(tuner_file->value());
210  }
211 #endif /* ARM_COMPUTE_CL */
212  if(options.log_level->value() > framework::LogLevel::NONE)
213  {
214  for(auto &p : printers)
215  {
216  p->print_global_header();
217  }
218  }
219 
220  if(options.log_level->value() >= framework::LogLevel::CONFIG)
221  {
222  for(auto &p : printers)
223  {
224  p->print_entry("Version", build_information());
225  p->print_entry("CommandLine", command_line(argc, argv));
226  p->print_entry("Seed", support::cpp11::to_string(seed->value()));
227 #ifdef ARM_COMPUTE_CL
228  if(opencl_is_available())
229  {
230  p->print_entry("CL_DEVICE_VERSION", CLKernelLibrary::get().get_device_version());
231  }
232  else
233  {
234  p->print_entry("CL_DEVICE_VERSION", "Unavailable");
235  }
236 #endif /* ARM_COMPUTE_CL */
237  const arm_compute::CPUInfo &cpu_info = Scheduler::get().cpu_info();
238  const unsigned int num_cpus = cpu_info.get_cpu_num();
239  p->print_entry("cpu_has_sve", support::cpp11::to_string(cpu_info.has_sve()));
240  p->print_entry("cpu_has_fp16", support::cpp11::to_string(cpu_info.has_fp16()));
241  p->print_entry("cpu_has_bf16", support::cpp11::to_string(cpu_info.has_bf16()));
242  p->print_entry("cpu_has_dotprod", support::cpp11::to_string(cpu_info.has_dotprod()));
243 
244  for(unsigned int j = 0; j < num_cpus; ++j)
245  {
246  const CPUModel model = cpu_info.get_cpu_model(j);
247  p->print_entry("CPU" + support::cpp11::to_string(j), cpuinfo::cpu_model_to_string(model));
248  }
249  p->print_entry("Iterations", support::cpp11::to_string(options.iterations->value()));
250  p->print_entry("Threads", support::cpp11::to_string(threads->value()));
251  {
253  p->print_entry("Dataset mode", to_string(dataset_mode->value()));
254  }
255  }
256  }
257 
258  // Setup instruments meta-data
260  instruments_info._scheduler_users.push_back(cpu_ctx_track.get());
261  framework.set_instruments_info(instruments_info);
262 
263  // Initialize framework
265  fconfig.instruments = options.instruments->value();
266  fconfig.name_filter = filter->value();
267  fconfig.id_filter = filter_id->value();
268  fconfig.num_iterations = options.iterations->value();
269  fconfig.mode = dataset_mode->value();
270  fconfig.log_level = options.log_level->value();
271  fconfig.cooldown_sec = cooldown_sec->value();
272  fconfig.configure_only = configure_only->value();
273  framework.init(fconfig);
274 
275  for(auto &p : printers)
276  {
277  framework.add_printer(p.get());
278  }
279  framework.set_throw_errors(options.throw_errors->value());
280  framework.set_stop_on_error(stop_on_error->value());
281  framework.set_error_on_missing_assets(error_on_missing_assets->value());
282 
283  bool success = true;
284 
285  if(list_tests->value())
286  {
287  for(auto &p : printers)
288  {
289  p->print_list_tests(framework.test_infos());
290  p->print_global_footer();
291  }
292 
293  return 0;
294  }
295 
296  if(test_instruments->value())
297  {
298  framework::Profiler profiler = framework.get_profiler();
299  profiler.start();
300  profiler.stop();
301  for(auto &p : printers)
302  {
303  p->print_measurements(profiler.measurements());
304  }
305 
306  return 0;
307  }
308 
309  library = std::make_unique<AssetsLibrary>(assets->value(), seed->value());
310  fixed_library = std::make_unique<AssetsLibrary>(assets->value(), fixed_seed);
311 
312  if(!parser.validate())
313  {
314  return 1;
315  }
316 
317  success = framework.run();
318 
319  if(options.log_level->value() > framework::LogLevel::NONE)
320  {
321  for(auto &p : printers)
322  {
323  p->print_global_footer();
324  }
325  }
326 
327 #ifdef ARM_COMPUTE_CL
328  if(opencl_is_available())
329  {
330  CLScheduler::get().sync();
331  if(enable_tuner->is_set() && enable_tuner->value() && tuner_file->is_set())
332  {
333  cl_tuner.save_to_file(tuner_file->value());
334  }
335  }
336 #endif /* ARM_COMPUTE_CL */
337 
338  return (success ? 0 : 1);
339  }
340  catch(const std::exception &error)
341  {
342  std::cerr << error.what() << "\n";
343 
344  if(options.throw_errors->value())
345  {
346  throw;
347  }
348 
349  return 1;
350  }
351  return 0;
352 }
Basic implementation of the OpenCL tuner interface.
Definition: CLTuner.h:40
Framework configuration structure.
Definition: Framework.h:57
bool has_sve() const
Checks if the cpu model supports sve.
Definition: CPPTypes.cpp:93
bool has_bf16() const
Checks if the cpu model supports bf16.
Definition: CPPTypes.cpp:63
int num_iterations
Number of iterations per test.
Definition: Framework.h:63
Profiler class to collect benchmark numbers.
Definition: Profiler.h:45
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:265
Implementation of an option that can be either true or false.
Definition: ToggleOption.h:36
std::string to_string(const GEMMConfigNative &config)
Definition: Utils.cpp:156
void set_instruments_info(InstrumentsInfo instr_info)
Sets instruments info.
Definition: Framework.cpp:703
std::unique_ptr< InstrumentsInfo > instruments_info
Definition: Framework.cpp:50
bool validate() const
Validates the previously parsed command line arguments.
CPUModel
CPU models types.
Definition: CPPTypes.h:52
std::tuple< cl::Context, cl::Device, cl_int > create_opencl_context_and_device(CLBackendType cl_backend_type)
This function creates an OpenCL context and a device.
Definition: CLHelpers.cpp:126
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:285
Class to parse command line arguments.
CPUModel get_cpu_model(unsigned int cpuid) const
Gets the cpu model for a given cpuid.
Definition: CPPTypes.cpp:108
float cooldown_sec
Delay between tests in seconds.
Definition: Framework.h:64
T * add_option(const std::string &name, As &&... args)
Function to add a new option to the parser.
LogLevel log_level
Verbosity of the output.
Definition: Framework.h:65
bool run()
Run all enabled test cases.
Definition: Framework.cpp:543
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
std::vector< TestInfo > test_infos() const
List of TestInfo&#39;s.
Definition: Framework.cpp:677
std::string name_filter
Regular expression to filter tests by name.
Definition: Framework.h:60
std::unique_ptr< AssetsLibrary > library
Definition: main.cpp:76
bool reload_from_file(const std::string &filename)
(Re)Load the heuristics from reading a dotmlgo file
std::string cpu_model_to_string(CpuModel model)
Convert a CPU model value to a string.
Definition: CpuModel.cpp:30
std::unique_ptr< ParametersLibrary > parameters
Definition: Framework.cpp:46
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456
bool has_dotprod() const
Checks if the cpu model supports dot product.
Definition: CPPTypes.cpp:73
Profiler get_profiler() const
Factory method to obtain a configured profiler.
Definition: Framework.cpp:643
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:89
unsigned int get_cpu_num() const
Return the maximum number of CPUs present.
Definition: CPPTypes.cpp:53
Handle for loading and retrieving GEMM heuristics.
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
void start()
Call start() on all the added instruments.
Definition: Profiler.cpp:48
std::vector< ISchedulerUser * > _scheduler_users
Definition: Instruments.h:65
std::string build_information()
Returns the arm_compute library build information.
bool configure_only
Only configure kernels.
Definition: Framework.h:66
void add_printer(Printer *printer)
Set the printer used for the output of test results.
Definition: Framework.cpp:672
void stop()
Call stop() on all the added instruments.
Definition: Profiler.cpp:56
void print_help(const std::string &program_name) const
Prints a help message for all configured options.
Common command line options used to configure the framework.
Definition: CommonOptions.h:54
void set_stop_on_error(bool stop_on_error)
Set whether to abort execution after the first failed test.
Definition: Framework.cpp:275
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:154
std::unique_ptr< AssetsLibrary > fixed_library
Definition: main.cpp:79