blob: 5d5291abfbeaf5fbb1cf756cf067cb37017ee376 [file] [log] [blame]
Anthony Barbier979dc4f2018-03-07 09:27:48 +00001/*
Georgios Pinitas7f152512019-12-16 19:59:52 +00002 * Copyright (c) 2018-2020 ARM Limited.
Anthony Barbier979dc4f2018-03-07 09:27:48 +00003 *
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"
Anthony Barbier979dc4f2018-03-07 09:27:48 +000025
26#define BENCHMARK_EXAMPLES
27#include "utils/Utils.cpp"
28
29#include "ValidateExample.h"
30#include "arm_compute/runtime/Scheduler.h"
31#include "tests/AssetsLibrary.h"
32#include "tests/Globals.h"
33#include "tests/framework/Framework.h"
34#include "tests/framework/Macros.h"
Anthony Barbier979dc4f2018-03-07 09:27:48 +000035#include "tests/framework/command_line/CommonOptions.h"
36#include "tests/framework/instruments/Instruments.h"
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010037#include "utils/command_line/CommandLineParser.h"
Anthony Barbier979dc4f2018-03-07 09:27:48 +000038
39#ifdef ARM_COMPUTE_CL
40#include "arm_compute/runtime/CL/CLScheduler.h"
41#endif /* ARM_COMPUTE_CL */
42#ifdef ARM_COMPUTE_GC
43#include "arm_compute/runtime/GLES_COMPUTE/GCScheduler.h"
44#endif /* ARM_COMPUTE_GC */
45
46#include <libgen.h>
47
48using namespace arm_compute;
49using namespace arm_compute::test;
50
51namespace arm_compute
52{
53namespace test
54{
55std::unique_ptr<AssetsLibrary> library;
56} // namespace test
57namespace utils
58{
Anthony Barbier9fb0cac2018-04-20 15:46:21 +010059static std::unique_ptr<ValidateExample> g_example = nullptr;
60static std::vector<char *> g_example_argv = {};
61
Anthony Barbier1fa17c22018-08-23 15:44:20 +010062namespace
63{
64std::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
Anthony Barbier9fb0cac2018-04-20 15:46:21 +010074template <bool validate>
Anthony Barbier979dc4f2018-03-07 09:27:48 +000075class ExampleTest : public arm_compute::test::framework::TestCase
76{
77public:
78 ExampleTest() = default;
Anthony Barbier9fb0cac2018-04-20 15:46:21 +010079 void do_setup() override
80 {
81 ARM_COMPUTE_ERROR_ON_NULLPTR(g_example.get());
Anthony Barbiere88b9bb2018-07-12 13:26:27 +010082 _is_setup = g_example->do_setup(g_example_argv.size(), &g_example_argv[0]);
Anthony Barbier9fb0cac2018-04-20 15:46:21 +010083 }
Anthony Barbier979dc4f2018-03-07 09:27:48 +000084 void do_run() override
85 {
Anthony Barbiere88b9bb2018-07-12 13:26:27 +010086 if(_is_setup)
87 {
88 g_example->do_run();
89 }
Anthony Barbier979dc4f2018-03-07 09:27:48 +000090 }
91 void do_teardown() override
92 {
Anthony Barbiere88b9bb2018-07-12 13:26:27 +010093 if(_is_setup)
Anthony Barbier979dc4f2018-03-07 09:27:48 +000094 {
Anthony Barbiere88b9bb2018-07-12 13:26:27 +010095 if(validate)
96 {
97 g_example->do_validate();
98 }
99 g_example->do_teardown();
Anthony Barbier979dc4f2018-03-07 09:27:48 +0000100 }
Anthony Barbier9fb0cac2018-04-20 15:46:21 +0100101 g_example = nullptr;
Anthony Barbier979dc4f2018-03-07 09:27:48 +0000102 }
Anthony Barbiere88b9bb2018-07-12 13:26:27 +0100103
104private:
105 bool _is_setup{ false };
Anthony Barbier979dc4f2018-03-07 09:27:48 +0000106};
107
Anthony Barbier1fa17c22018-08-23 15:44:20 +0100108} // namespace
Anthony Barbier9fb0cac2018-04-20 15:46:21 +0100109int run_example(int argc, char **argv, std::unique_ptr<ValidateExample> example)
Anthony Barbier979dc4f2018-03-07 09:27:48 +0000110{
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100111 utils::CommandLineParser parser;
112 framework::CommonOptions options(parser);
113 auto example_args = parser.add_option<utils::ListOption<std::string>>("example_args");
Anthony Barbier979dc4f2018-03-07 09:27:48 +0000114 example_args->set_help("Arguments to pass to the example separated by commas (e.g: arg0,arg1,arg2)");
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100115 auto seed = parser.add_option<utils::SimpleOption<std::random_device::result_type>>("seed", std::random_device()());
Anthony Barbier979dc4f2018-03-07 09:27:48 +0000116 seed->set_help("Global seed for random number generation");
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100117 auto validate = parser.add_option<utils::SimpleOption<int>>("validate", 1);
Anthony Barbier979dc4f2018-03-07 09:27:48 +0000118 validate->set_help("Enable / disable output validation (0/1)");
119
120 framework::Framework &framework = framework::Framework::get();
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();
Anthony Barbier9fb0cac2018-04-20 15:46:21 +0100131 g_example = std::move(example);
132 g_example_argv.clear();
133 g_example_argv.emplace_back(argv[0]);
Anthony Barbier979dc4f2018-03-07 09:27:48 +0000134 for(auto &arg : example_args->value())
135 {
Anthony Barbier9fb0cac2018-04-20 15:46:21 +0100136 g_example_argv.emplace_back(const_cast<char *>(arg.c_str())); // NOLINT
Anthony Barbier979dc4f2018-03-07 09:27:48 +0000137 }
138
Anthony Barbier979dc4f2018-03-07 09:27:48 +0000139 library = support::cpp14::make_unique<AssetsLibrary>("." /* Only using random values */, seed->value());
140
Anthony Barbier979dc4f2018-03-07 09:27:48 +0000141 if(options.log_level->value() > framework::LogLevel::NONE)
142 {
143 for(auto &p : printers)
144 {
145 p->print_global_header();
146 }
147 }
148
149 if(options.log_level->value() >= framework::LogLevel::CONFIG)
150 {
151 for(auto &p : printers)
152 {
153 p->print_entry("Version", build_information());
Anthony Barbier1fa17c22018-08-23 15:44:20 +0100154 p->print_entry("CommandLine", command_line(argc, argv));
Anthony Barbier979dc4f2018-03-07 09:27:48 +0000155 p->print_entry("Seed", support::cpp11::to_string(seed->value()));
156#ifdef ARM_COMPUTE_CL
157 if(opencl_is_available())
158 {
Anthony Barbier9fb0cac2018-04-20 15:46:21 +0100159 if(!CLScheduler::get().is_initialised())
160 {
161 CLScheduler::get().default_init();
162 }
Anthony Barbier979dc4f2018-03-07 09:27:48 +0000163 p->print_entry("CL_DEVICE_VERSION", CLKernelLibrary::get().get_device_version());
164 }
165 else
166 {
167 p->print_entry("CL_DEVICE_VERSION", "Unavailable");
168 }
169#endif /* ARM_COMPUTE_CL */
170 p->print_entry("Iterations", support::cpp11::to_string(options.iterations->value()));
Anthony Barbier9fb0cac2018-04-20 15:46:21 +0100171 g_example->print_parameters(*p);
Anthony Barbier979dc4f2018-03-07 09:27:48 +0000172 }
173 }
174
Georgios Pinitas7f152512019-12-16 19:59:52 +0000175 // Initialize framework
176 framework::FrameworkConfig fconfig;
177 fconfig.instruments = options.instruments->value();
178 fconfig.num_iterations = options.iterations->value();
179 fconfig.log_level = options.log_level->value();
180 framework.init(fconfig);
181
Anthony Barbier979dc4f2018-03-07 09:27:48 +0000182 for(auto &p : printers)
183 {
184 framework.add_printer(p.get());
185 }
186
187 framework.set_throw_errors(options.throw_errors->value());
188 arm_compute::test::framework::detail::TestSuiteRegistrar suite{ "Examples" };
189 if(validate->value() != 0)
190 {
191 framework.add_test_case<ExampleTest<true>>(basename(argv[0]), framework::DatasetMode::ALL, arm_compute::test::framework::TestCaseFactory::Status::ACTIVE);
192 }
193 else
194 {
195 framework.add_test_case<ExampleTest<false>>(basename(argv[0]), framework::DatasetMode::ALL, arm_compute::test::framework::TestCaseFactory::Status::ACTIVE);
196 }
197
198 //func(argc, argv);
199 bool success = framework.run();
200 if(options.log_level->value() > framework::LogLevel::NONE)
201 {
202 for(auto &p : printers)
203 {
204 p->print_global_footer();
205 }
206 }
207
208 return (success ? 0 : 1);
209}
210
211} // namespace utils
212} // namespace arm_compute