Compute Library
 23.08
RunExample.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2021,2023 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 "utils/Utils.h"
25 
26 #define BENCHMARK_EXAMPLES
27 #include "utils/Utils.cpp"
28 
32 #include "tests/framework/Macros.h"
36 
37 #ifdef ARM_COMPUTE_CL
41 #endif /* ARM_COMPUTE_CL */
42 
43 #include <libgen.h>
44 
45 using namespace arm_compute;
46 using namespace arm_compute::test;
47 
48 namespace
49 {
50 std::string command_line(int argc, char **argv)
51 {
52  std::stringstream ss;
53  for(int i = 0; i < argc; i++)
54  {
55  ss << argv[i] << " ";
56  }
57  return ss.str();
58 }
59 } // namespace
60 namespace arm_compute
61 {
62 namespace utils
63 {
64 static std::unique_ptr<Example> g_example = nullptr;
65 static std::vector<char *> g_example_argv = {};
66 class ExampleTest : public arm_compute::test::framework::TestCase
67 {
68 public:
69  ExampleTest() = default;
70  void do_setup() override
71  {
72  ARM_COMPUTE_ERROR_ON_NULLPTR(g_example.get());
73  _is_setup = g_example->do_setup(g_example_argv.size(), &g_example_argv[0]);
74  }
75  void do_run() override
76  {
77  if(_is_setup)
78  {
79  g_example->do_run();
80  }
81  }
82  void do_teardown() override
83  {
84  if(_is_setup)
85  {
86  g_example->do_teardown();
87  }
88  g_example = nullptr;
89  }
90 
91 private:
92  bool _is_setup{ false };
93 };
94 
95 int run_example(int argc, char **argv, std::unique_ptr<Example> example)
96 {
99  auto example_args = parser.add_option<utils::ListOption<std::string>>("example_args");
100  example_args->set_help("Arguments to pass to the example separated by commas (e.g: arg0,arg1,arg2)");
102 
103  parser.parse(argc, argv);
104 
105  if(options.help->is_set() && options.help->value())
106  {
107  parser.print_help(argv[0]);
108  return 0;
109  }
110 
111  std::vector<std::unique_ptr<framework::Printer>> printers = options.create_printers();
112  g_example = std::move(example);
113  g_example_argv.clear();
114  g_example_argv.emplace_back(argv[0]);
115  for(auto &arg : example_args->value())
116  {
117  g_example_argv.emplace_back(const_cast<char *>(arg.c_str())); // NOLINT
118  }
119 
120  if(options.log_level->value() > framework::LogLevel::NONE)
121  {
122  for(auto &p : printers)
123  {
124  p->print_global_header();
125  }
126  }
127 
128 #ifdef ARM_COMPUTE_CL
129  CLGEMMHeuristicsHandle gemm_h;
130  if(opencl_is_available())
131  {
132  CLBackendType backend_type = CLBackendType::Native;
133  for(auto &arg : example_args->value())
134  {
135  if(arg.find("--target=clvk") != std::string::npos)
136  {
137  backend_type = CLBackendType::Clvk;
138  break;
139  }
140  }
141 
142  auto ctx_dev_err = create_opencl_context_and_device(backend_type);
143  ARM_COMPUTE_ERROR_ON_MSG(std::get<2>(ctx_dev_err) != CL_SUCCESS, "Failed to create OpenCL context");
145  .default_init_with_context(std::get<1>(ctx_dev_err), std::get<0>(ctx_dev_err), nullptr, &gemm_h);
146  }
147 #endif /* ARM_COMPUTE_CL */
148 
150  {
151  for(auto &p : printers)
152  {
153  p->print_entry("Version", build_information());
154  p->print_entry("CommandLine", command_line(argc, argv));
155 #ifdef ARM_COMPUTE_CL
156  if(opencl_is_available())
157  {
158  p->print_entry("CL_DEVICE_VERSION", CLKernelLibrary::get().get_device_version());
159  }
160  else
161  {
162  p->print_entry("CL_DEVICE_VERSION", "Unavailable");
163  }
164 #endif /* ARM_COMPUTE_CL */
165  p->print_entry("Iterations", support::cpp11::to_string(options.iterations->value()));
166  }
167  }
168 
169  // Initialize framework
171  fconfig.instruments = options.instruments->value();
172  fconfig.num_iterations = options.iterations->value();
173  fconfig.log_level = options.log_level->value();
174  framework.init(fconfig);
175 
176  for(auto &p : printers)
177  {
178  framework.add_printer(p.get());
179  }
180  framework.set_throw_errors(options.throw_errors->value());
182 
183 #ifdef BARE_METAL
185 #else /* BARE_METAL */
187 #endif /* BARE_METAL */
188  //func(argc, argv);
189  bool success = framework.run();
190  if(options.log_level->value() > framework::LogLevel::NONE)
191  {
192  for(auto &p : printers)
193  {
194  p->print_global_footer();
195  }
196  }
197 
198  return (success ? 0 : 1);
199 }
200 
201 } // namespace utils
202 } // namespace arm_compute
arm_compute::support::cpp11::to_string
std::string to_string(T &&value)
Convert integer and float values to string.
Definition: StringSupport.h:168
CLGEMMHeuristicsHandle.h
arm_compute::opencl_is_available
bool opencl_is_available()
Check if OpenCL is available.
Definition: OpenCL.cpp:203
arm_compute::test::framework::FrameworkConfig
Framework configuration structure.
Definition: Framework.h:57
arm_compute::test::framework::CommonOptions
Common command line options used to configure the framework.
Definition: CommonOptions.h:54
arm_compute::test::framework::FrameworkConfig::num_iterations
int num_iterations
Number of iterations per test.
Definition: Framework.h:63
arm_compute::utils::run_example
int run_example(int argc, char **argv, std::unique_ptr< ValidateExample > example)
Run an example and handle the potential exceptions it throws.
Definition: RunExample.cpp:109
arm_compute::test::framework::Framework::add_printer
void add_printer(Printer *printer)
Set the printer used for the output of test results.
Definition: Framework.cpp:678
arm_compute::test
Definition: tests.dox:26
arm_compute::create_opencl_context_and_device
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
arm_compute::test::framework::Framework::add_test_case
void add_test_case(std::string test_name, DatasetMode mode, TestCaseFactory::Status status)
Add a test case to the framework.
Definition: Framework.h:376
arm_compute::test::framework::TestCaseFactory::Status::ACTIVE
@ ACTIVE
arm_compute::test::framework::CommonOptions::help
arm_compute::utils::ToggleOption * help
Show help option.
Definition: CommonOptions.h:74
arm_compute::test::framework::LogLevel::NONE
@ NONE
GemmTuner.parser
parser
Definition: GemmTuner.py:640
arm_compute::CLKernelLibrary::get
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
Definition: CLKernelLibrary.cpp:39
arm_compute::test::framework::FrameworkConfig::log_level
LogLevel log_level
Verbosity of the output.
Definition: Framework.h:65
arm_compute::utils::ListOption
Implementation of an option that accepts any number of values.
Definition: ListOption.h:41
arm_compute::test::framework::CommonOptions::create_printers
std::vector< std::unique_ptr< Printer > > create_printers()
Create the printers based on parsed command line options.
Definition: CommonOptions.cpp:102
arm_compute::CLBackendType::Clvk
@ Clvk
CLVK backend.
ARM_COMPUTE_ERROR_ON_NULLPTR
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
arm_compute::utils::CommandLineParser
Class to parse command line arguments.
Definition: CommandLineParser.h:45
arm_compute::test::framework::DatasetMode::ALL
@ ALL
arm_compute::utils::EnumListOption::value
const std::vector< T > & value() const
Get the values of the option.
Definition: EnumListOption.h:144
arm_compute::test::validation::ss
std::stringstream ss(mlgo_str)
arm_compute::test::framework::Framework
Main framework class.
Definition: Framework.h:97
ARM_COMPUTE_ERROR_ON_MSG
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:457
CommandLineParser.h
arm_compute::test::framework::Framework::set_throw_errors
void set_throw_errors(bool throw_errors)
Set whether errors are caught or thrown by the framework.
Definition: Framework.cpp:267
CLScheduler.h
Interface to enqueue OpenCL kernels and get/set the OpenCL CommandQueue and ICLTuner.
arm_compute::test::framework::FrameworkConfig::instruments
std::vector< framework::InstrumentsDescription > instruments
Instrument types that will be used for benchmarking.
Definition: Framework.h:59
arm_compute::test::framework::Framework::run
bool run()
Run all enabled test cases.
Definition: Framework.cpp:546
Macros.h
arm_compute::utils::Option::is_set
bool is_set() const
Has a value been assigned to the option?
Definition: Option.h:135
arm_compute::CLGEMMHeuristicsHandle
Handle for loading and retrieving GEMM heuristics.
Definition: CLGEMMHeuristicsHandle.h:38
Instruments.h
arm_compute::utils::EnumOption::value
const T & value() const
Get the selected value.
Definition: EnumOption.h:130
CLHelpers.h
Utils.cpp
arm_compute::test::framework::Framework::get
static Framework & get()
Access to the singleton.
Definition: Framework.cpp:124
arm_compute::CLScheduler::get
static CLScheduler & get()
Access the scheduler singleton.
Definition: CLScheduler.cpp:103
CommonOptions.h
Scheduler.h
arm_compute::CLBackendType
CLBackendType
List the possible OpenCL backends.
Definition: CLTypes.h:55
arm_compute::utils::Option::set_help
void set_help(std::string help)
Set the help message for the option.
Definition: Option.h:125
arm_compute::test::framework::detail::TestSuiteRegistrar
Helper class to statically begin and end a test suite.
Definition: Registrars.h:66
arm_compute::test::framework::TestCase
Abstract test case class.
Definition: TestCase.h:40
arm_compute::CLScheduler::default_init_with_context
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.
Definition: CLScheduler.cpp:110
arm_compute::CLBackendType::Native
@ Native
OpenCL native backend.
arm_compute::test::framework::LogLevel::CONFIG
@ CONFIG
Version.h
arm_compute::build_information
std::string build_information()
Returns the arm_compute library build information.
arm_compute::test::framework::CommonOptions::iterations
arm_compute::utils::SimpleOption< int > * iterations
Number of iterations option.
Definition: CommonOptions.h:76
arm_compute::utils::SimpleOption::value
const T & value() const
Get the option value.
Definition: SimpleOption.h:112
arm_compute::test::framework::CommonOptions::instruments
arm_compute::utils::EnumListOption< InstrumentsDescription > * instruments
Instruments option.
Definition: CommonOptions.h:75
arm_compute
Copyright (c) 2017-2023 Arm Limited.
Definition: introduction.dox:24
arm_compute::test::framework::CommonOptions::log_level
arm_compute::utils::EnumOption< LogLevel > * log_level
Logging level option.
Definition: CommonOptions.h:79
arm_compute::test::framework::Framework::init
void init(const FrameworkConfig &config)
Init the framework.
Definition: Framework.cpp:130
arm_compute::test::framework::CommonOptions::throw_errors
arm_compute::utils::ToggleOption * throw_errors
Throw errors option.
Definition: CommonOptions.h:80
Framework.h