blob: 36bf5875514cceb0f6fc27e7c70b21c65c0f4731 [file] [log] [blame]
Anthony Barbier979dc4f2018-03-07 09:27:48 +00001/*
Mohammed Suhail Munshie37c3182023-12-12 15:43:40 +00002 * Copyright (c) 2018-2021, 2023 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"
Mirko leonardo Serra laband828b462020-11-19 16:22:59 +000030#include "arm_compute/runtime/CL/CLHelpers.h"
Georgios Pinitas40f51a62020-11-21 03:04:18 +000031#include "arm_compute/runtime/Scheduler.h"
Mohammed Suhail Munshie37c3182023-12-12 15:43:40 +000032#include "arm_compute/core/Version.h"
Anthony Barbier979dc4f2018-03-07 09:27:48 +000033#include "tests/AssetsLibrary.h"
34#include "tests/Globals.h"
35#include "tests/framework/Framework.h"
36#include "tests/framework/Macros.h"
Anthony Barbier979dc4f2018-03-07 09:27:48 +000037#include "tests/framework/command_line/CommonOptions.h"
38#include "tests/framework/instruments/Instruments.h"
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010039#include "utils/command_line/CommandLineParser.h"
Anthony Barbier979dc4f2018-03-07 09:27:48 +000040
41#ifdef ARM_COMPUTE_CL
42#include "arm_compute/runtime/CL/CLScheduler.h"
43#endif /* ARM_COMPUTE_CL */
Anthony Barbier979dc4f2018-03-07 09:27:48 +000044
45#include <libgen.h>
46
47using namespace arm_compute;
48using namespace arm_compute::test;
49
50namespace arm_compute
51{
52namespace test
53{
54std::unique_ptr<AssetsLibrary> library;
Sang-Hoon Park06cf9332020-03-31 15:59:41 +010055static constexpr uint32_t fixed_seed = 1;
56std::unique_ptr<AssetsLibrary> fixed_library;
Anthony Barbier979dc4f2018-03-07 09:27:48 +000057} // namespace test
58namespace utils
59{
Anthony Barbier9fb0cac2018-04-20 15:46:21 +010060static std::unique_ptr<ValidateExample> g_example = nullptr;
61static std::vector<char *> g_example_argv = {};
62
Anthony Barbier1fa17c22018-08-23 15:44:20 +010063namespace
64{
65std::string command_line(int argc, char **argv)
66{
67 std::stringstream ss;
68 for(int i = 0; i < argc; i++)
69 {
70 ss << argv[i] << " ";
71 }
72 return ss.str();
73}
74
Anthony Barbier9fb0cac2018-04-20 15:46:21 +010075template <bool validate>
Anthony Barbier979dc4f2018-03-07 09:27:48 +000076class ExampleTest : public arm_compute::test::framework::TestCase
77{
78public:
79 ExampleTest() = default;
Anthony Barbier9fb0cac2018-04-20 15:46:21 +010080 void do_setup() override
81 {
82 ARM_COMPUTE_ERROR_ON_NULLPTR(g_example.get());
Anthony Barbiere88b9bb2018-07-12 13:26:27 +010083 _is_setup = g_example->do_setup(g_example_argv.size(), &g_example_argv[0]);
Anthony Barbier9fb0cac2018-04-20 15:46:21 +010084 }
Anthony Barbier979dc4f2018-03-07 09:27:48 +000085 void do_run() override
86 {
Anthony Barbiere88b9bb2018-07-12 13:26:27 +010087 if(_is_setup)
88 {
89 g_example->do_run();
90 }
Anthony Barbier979dc4f2018-03-07 09:27:48 +000091 }
92 void do_teardown() override
93 {
Anthony Barbiere88b9bb2018-07-12 13:26:27 +010094 if(_is_setup)
Anthony Barbier979dc4f2018-03-07 09:27:48 +000095 {
Anthony Barbiere88b9bb2018-07-12 13:26:27 +010096 if(validate)
97 {
98 g_example->do_validate();
99 }
100 g_example->do_teardown();
Anthony Barbier979dc4f2018-03-07 09:27:48 +0000101 }
Anthony Barbier9fb0cac2018-04-20 15:46:21 +0100102 g_example = nullptr;
Anthony Barbier979dc4f2018-03-07 09:27:48 +0000103 }
Anthony Barbiere88b9bb2018-07-12 13:26:27 +0100104
105private:
106 bool _is_setup{ false };
Anthony Barbier979dc4f2018-03-07 09:27:48 +0000107};
108
Anthony Barbier1fa17c22018-08-23 15:44:20 +0100109} // namespace
Anthony Barbier9fb0cac2018-04-20 15:46:21 +0100110int run_example(int argc, char **argv, std::unique_ptr<ValidateExample> example)
Anthony Barbier979dc4f2018-03-07 09:27:48 +0000111{
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100112 utils::CommandLineParser parser;
113 framework::CommonOptions options(parser);
114 auto example_args = parser.add_option<utils::ListOption<std::string>>("example_args");
Anthony Barbier979dc4f2018-03-07 09:27:48 +0000115 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 +0100116 auto seed = parser.add_option<utils::SimpleOption<std::random_device::result_type>>("seed", std::random_device()());
Anthony Barbier979dc4f2018-03-07 09:27:48 +0000117 seed->set_help("Global seed for random number generation");
Georgios Pinitas12be7ab2018-07-03 12:06:23 +0100118 auto validate = parser.add_option<utils::SimpleOption<int>>("validate", 1);
Anthony Barbier979dc4f2018-03-07 09:27:48 +0000119 validate->set_help("Enable / disable output validation (0/1)");
120
121 framework::Framework &framework = framework::Framework::get();
122
123 parser.parse(argc, argv);
124
125 if(options.help->is_set() && options.help->value())
126 {
127 parser.print_help(argv[0]);
128 return 0;
129 }
130
131 std::vector<std::unique_ptr<framework::Printer>> printers = options.create_printers();
Anthony Barbier9fb0cac2018-04-20 15:46:21 +0100132 g_example = std::move(example);
133 g_example_argv.clear();
134 g_example_argv.emplace_back(argv[0]);
Anthony Barbier979dc4f2018-03-07 09:27:48 +0000135 for(auto &arg : example_args->value())
136 {
Anthony Barbier9fb0cac2018-04-20 15:46:21 +0100137 g_example_argv.emplace_back(const_cast<char *>(arg.c_str())); // NOLINT
Anthony Barbier979dc4f2018-03-07 09:27:48 +0000138 }
139
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000140 library = std::make_unique<AssetsLibrary>("." /* Only using random values */, seed->value());
141 fixed_library = std::make_unique<AssetsLibrary>(".", fixed_seed);
Anthony Barbier979dc4f2018-03-07 09:27:48 +0000142
Anthony Barbier979dc4f2018-03-07 09:27:48 +0000143 if(options.log_level->value() > framework::LogLevel::NONE)
144 {
145 for(auto &p : printers)
146 {
147 p->print_global_header();
148 }
149 }
150
Mirko leonardo Serra laband828b462020-11-19 16:22:59 +0000151#ifdef ARM_COMPUTE_CL
152 if(opencl_is_available())
153 {
Michalis Spyrou402740d2021-04-20 11:26:21 +0100154 CLBackendType backend_type = CLBackendType::Native;
Michalis Spyrou660eea42021-04-28 13:29:17 +0100155 for(auto &arg : example_args->value())
Michalis Spyrou402740d2021-04-20 11:26:21 +0100156 {
Michalis Spyrou660eea42021-04-28 13:29:17 +0100157 if(arg.find("--target=clvk") != std::string::npos)
158 {
159 backend_type = CLBackendType::Clvk;
160 break;
161 }
Michalis Spyrou402740d2021-04-20 11:26:21 +0100162 }
163 auto ctx_dev_err = create_opencl_context_and_device(backend_type);
Mirko leonardo Serra laband828b462020-11-19 16:22:59 +0000164 ARM_COMPUTE_ERROR_ON_MSG(std::get<2>(ctx_dev_err) != CL_SUCCESS, "Failed to create OpenCL context");
165 CLScheduler::get().default_init_with_context(std::get<1>(ctx_dev_err), std::get<0>(ctx_dev_err), nullptr);
166 }
167#endif /* ARM_COMPUTE_CL */
168
Anthony Barbier979dc4f2018-03-07 09:27:48 +0000169 if(options.log_level->value() >= framework::LogLevel::CONFIG)
170 {
171 for(auto &p : printers)
172 {
173 p->print_entry("Version", build_information());
Anthony Barbier1fa17c22018-08-23 15:44:20 +0100174 p->print_entry("CommandLine", command_line(argc, argv));
Anthony Barbier979dc4f2018-03-07 09:27:48 +0000175 p->print_entry("Seed", support::cpp11::to_string(seed->value()));
176#ifdef ARM_COMPUTE_CL
177 if(opencl_is_available())
178 {
179 p->print_entry("CL_DEVICE_VERSION", CLKernelLibrary::get().get_device_version());
180 }
181 else
182 {
183 p->print_entry("CL_DEVICE_VERSION", "Unavailable");
184 }
185#endif /* ARM_COMPUTE_CL */
186 p->print_entry("Iterations", support::cpp11::to_string(options.iterations->value()));
Anthony Barbier9fb0cac2018-04-20 15:46:21 +0100187 g_example->print_parameters(*p);
Anthony Barbier979dc4f2018-03-07 09:27:48 +0000188 }
189 }
190
Georgios Pinitas7f152512019-12-16 19:59:52 +0000191 // Initialize framework
192 framework::FrameworkConfig fconfig;
193 fconfig.instruments = options.instruments->value();
194 fconfig.num_iterations = options.iterations->value();
195 fconfig.log_level = options.log_level->value();
196 framework.init(fconfig);
197
Anthony Barbier979dc4f2018-03-07 09:27:48 +0000198 for(auto &p : printers)
199 {
200 framework.add_printer(p.get());
201 }
202
203 framework.set_throw_errors(options.throw_errors->value());
204 arm_compute::test::framework::detail::TestSuiteRegistrar suite{ "Examples" };
205 if(validate->value() != 0)
206 {
207 framework.add_test_case<ExampleTest<true>>(basename(argv[0]), framework::DatasetMode::ALL, arm_compute::test::framework::TestCaseFactory::Status::ACTIVE);
208 }
209 else
210 {
211 framework.add_test_case<ExampleTest<false>>(basename(argv[0]), framework::DatasetMode::ALL, arm_compute::test::framework::TestCaseFactory::Status::ACTIVE);
212 }
213
214 //func(argc, argv);
215 bool success = framework.run();
216 if(options.log_level->value() > framework::LogLevel::NONE)
217 {
218 for(auto &p : printers)
219 {
220 p->print_global_footer();
221 }
222 }
223
224 return (success ? 0 : 1);
225}
226
227} // namespace utils
228} // namespace arm_compute