Compute Library
 22.05
RunExample.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-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 "utils/Utils.h"
25 
26 #define BENCHMARK_EXAMPLES
27 #include "utils/Utils.cpp"
28 
29 #include "ValidateExample.h"
32 #include "tests/AssetsLibrary.h"
33 #include "tests/Globals.h"
35 #include "tests/framework/Macros.h"
39 
40 #ifdef ARM_COMPUTE_CL
42 #endif /* ARM_COMPUTE_CL */
43 
44 #include <libgen.h>
45 
46 using namespace arm_compute;
47 using namespace arm_compute::test;
48 
49 namespace arm_compute
50 {
51 namespace test
52 {
53 std::unique_ptr<AssetsLibrary> library;
54 static constexpr uint32_t fixed_seed = 1;
55 std::unique_ptr<AssetsLibrary> fixed_library;
56 } // namespace test
57 namespace utils
58 {
59 static std::unique_ptr<ValidateExample> g_example = nullptr;
60 static std::vector<char *> g_example_argv = {};
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 
74 template <bool validate>
75 class ExampleTest : public arm_compute::test::framework::TestCase
76 {
77 public:
78  ExampleTest() = default;
79  void do_setup() override
80  {
81  ARM_COMPUTE_ERROR_ON_NULLPTR(g_example.get());
82  _is_setup = g_example->do_setup(g_example_argv.size(), &g_example_argv[0]);
83  }
84  void do_run() override
85  {
86  if(_is_setup)
87  {
88  g_example->do_run();
89  }
90  }
91  void do_teardown() override
92  {
93  if(_is_setup)
94  {
95  if(validate)
96  {
97  g_example->do_validate();
98  }
99  g_example->do_teardown();
100  }
101  g_example = nullptr;
102  }
103 
104 private:
105  bool _is_setup{ false };
106 };
107 
108 } // namespace
109 int run_example(int argc, char **argv, std::unique_ptr<ValidateExample> example)
110 {
112  framework::CommonOptions options(parser);
113  auto example_args = parser.add_option<utils::ListOption<std::string>>("example_args");
114  example_args->set_help("Arguments to pass to the example separated by commas (e.g: arg0,arg1,arg2)");
115  auto seed = parser.add_option<utils::SimpleOption<std::random_device::result_type>>("seed", std::random_device()());
116  seed->set_help("Global seed for random number generation");
117  auto validate = parser.add_option<utils::SimpleOption<int>>("validate", 1);
118  validate->set_help("Enable / disable output validation (0/1)");
119 
121 
122  parser.parse(argc, argv);
123 
124  if(options.help->is_set() && options.help->value())
125  {
126  parser.print_help(argv[0]);
127  return 0;
128  }
129 
130  std::vector<std::unique_ptr<framework::Printer>> printers = options.create_printers();
131  g_example = std::move(example);
132  g_example_argv.clear();
133  g_example_argv.emplace_back(argv[0]);
134  for(auto &arg : example_args->value())
135  {
136  g_example_argv.emplace_back(const_cast<char *>(arg.c_str())); // NOLINT
137  }
138 
139  library = std::make_unique<AssetsLibrary>("." /* Only using random values */, seed->value());
140  fixed_library = std::make_unique<AssetsLibrary>(".", fixed_seed);
141 
142  if(options.log_level->value() > framework::LogLevel::NONE)
143  {
144  for(auto &p : printers)
145  {
146  p->print_global_header();
147  }
148  }
149 
150 #ifdef ARM_COMPUTE_CL
151  if(opencl_is_available())
152  {
153  CLBackendType backend_type = CLBackendType::Native;
154  for(auto &arg : example_args->value())
155  {
156  if(arg.find("--target=clvk") != std::string::npos)
157  {
158  backend_type = CLBackendType::Clvk;
159  break;
160  }
161  }
162  auto ctx_dev_err = create_opencl_context_and_device(backend_type);
163  ARM_COMPUTE_ERROR_ON_MSG(std::get<2>(ctx_dev_err) != CL_SUCCESS, "Failed to create OpenCL context");
164  CLScheduler::get().default_init_with_context(std::get<1>(ctx_dev_err), std::get<0>(ctx_dev_err), nullptr);
165  }
166 #endif /* ARM_COMPUTE_CL */
167 
169  {
170  for(auto &p : printers)
171  {
172  p->print_entry("Version", build_information());
173  p->print_entry("CommandLine", command_line(argc, argv));
174  p->print_entry("Seed", support::cpp11::to_string(seed->value()));
175 #ifdef ARM_COMPUTE_CL
176  if(opencl_is_available())
177  {
178  p->print_entry("CL_DEVICE_VERSION", CLKernelLibrary::get().get_device_version());
179  }
180  else
181  {
182  p->print_entry("CL_DEVICE_VERSION", "Unavailable");
183  }
184 #endif /* ARM_COMPUTE_CL */
185  p->print_entry("Iterations", support::cpp11::to_string(options.iterations->value()));
186  g_example->print_parameters(*p);
187  }
188  }
189 
190  // Initialize framework
192  fconfig.instruments = options.instruments->value();
193  fconfig.num_iterations = options.iterations->value();
194  fconfig.log_level = options.log_level->value();
195  framework.init(fconfig);
196 
197  for(auto &p : printers)
198  {
199  framework.add_printer(p.get());
200  }
201 
202  framework.set_throw_errors(options.throw_errors->value());
204  if(validate->value() != 0)
205  {
207  }
208  else
209  {
211  }
212 
213  //func(argc, argv);
214  bool success = framework.run();
215  if(options.log_level->value() > framework::LogLevel::NONE)
216  {
217  for(auto &p : printers)
218  {
219  p->print_global_footer();
220  }
221  }
222 
223  return (success ? 0 : 1);
224 }
225 
226 } // namespace utils
227 } // namespace arm_compute
Framework configuration structure.
Definition: Framework.h:57
arm_compute::utils::ToggleOption * help
Show help option.
Definition: CommonOptions.h:74
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
int num_iterations
Number of iterations per test.
Definition: Framework.h:63
static CLScheduler & get()
Access the scheduler singleton.
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 set_throw_errors(bool throw_errors)
Set whether errors are caught or thrown by the framework.
Definition: Framework.cpp:265
const std::vector< T > & value() const
Get the values of the option.
OpenCL native backend.
void add_test_case(std::string test_name, DatasetMode mode, TestCaseFactory::Status status)
Add a test case to the framework.
Definition: Framework.h:374
std::stringstream ss(mlgo_str)
static CLKernelLibrary & get()
Access the KernelLibrary singleton.
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
Class to parse command line arguments.
Copyright (c) 2017-2022 Arm Limited.
Implementation of an option that accepts any number of values.
Definition: ListOption.h:41
static Framework & get()
Access to the singleton.
Definition: Framework.cpp:124
T * add_option(const std::string &name, As &&... args)
Function to add a new option to the parser.
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: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< std::unique_ptr< Printer > > create_printers()
Create the printers based on parsed command line options.
std::unique_ptr< AssetsLibrary > library
Definition: main.cpp:76
#define ARM_COMPUTE_ERROR_ON_MSG(cond, msg)
Definition: Error.h:456
const T & value() const
Get the option value.
Definition: SimpleOption.h:112
Abstract test case class.
Definition: TestCase.h:40
Helper class to statically begin and end a test suite.
Definition: Registrars.h:66
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
CLBackendType
List the possible OpenCL backends.
Definition: CLTypes.h:53
arm_compute::utils::EnumOption< LogLevel > * log_level
Logging level option.
Definition: CommonOptions.h:79
std::string build_information()
Returns the arm_compute library build information.
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:157
void add_printer(Printer *printer)
Set the printer used for the output of test results.
Definition: Framework.cpp:672
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
Implementation of an option that accepts a single value.
Definition: SimpleOption.h:39
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
Status validate(const ITensorInfo *scores_in, const ITensorInfo *boxes_in, const ITensorInfo *batch_splits_in, const ITensorInfo *scores_out, const ITensorInfo *boxes_out, const ITensorInfo *classes, const ITensorInfo *batch_splits_out, const ITensorInfo *keeps, const ITensorInfo *keeps_size, const BoxNMSLimitInfo info)
bool opencl_is_available()
Check if OpenCL is available.
Definition: OpenCL.cpp:154
std::unique_ptr< AssetsLibrary > fixed_library
Definition: main.cpp:79
arm_compute::utils::ToggleOption * throw_errors
Throw errors option.
Definition: CommonOptions.h:80