blob: 3e56ea2e646efd4942671f34145106aa890546af [file] [log] [blame]
Anthony Barbier6db0ff52018-01-05 10:59:12 +00001/*
Matthew Bentham314d3e22023-06-23 10:53:52 +00002 * Copyright (c) 2018-2021,2023 Arm Limited.
Anthony Barbier6db0ff52018-01-05 10:59:12 +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 Barbier6db0ff52018-01-05 10:59:12 +000025
26#define BENCHMARK_EXAMPLES
27#include "utils/Utils.cpp"
28
Matthew Bentham314d3e22023-06-23 10:53:52 +000029#include "arm_compute/core/Version.h"
Anthony Barbier6db0ff52018-01-05 10:59:12 +000030#include "arm_compute/runtime/Scheduler.h"
31#include "tests/framework/Framework.h"
32#include "tests/framework/Macros.h"
Anthony Barbier6db0ff52018-01-05 10:59:12 +000033#include "tests/framework/command_line/CommonOptions.h"
34#include "tests/framework/instruments/Instruments.h"
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010035#include "utils/command_line/CommandLineParser.h"
Anthony Barbier6db0ff52018-01-05 10:59:12 +000036
37#ifdef ARM_COMPUTE_CL
SiCong Li4841c972021-02-03 12:17:35 +000038#include "arm_compute/runtime/CL/CLGEMMHeuristicsHandle.h"
Pablo Tellob7c308a2019-01-22 12:59:26 +000039#include "arm_compute/runtime/CL/CLHelpers.h"
Anthony Barbier6db0ff52018-01-05 10:59:12 +000040#include "arm_compute/runtime/CL/CLScheduler.h"
41#endif /* ARM_COMPUTE_CL */
Anthony Barbier6db0ff52018-01-05 10:59:12 +000042
Anthony Barbierfadfc6d2018-02-28 14:02:42 +000043#include <libgen.h>
44
Anthony Barbier6db0ff52018-01-05 10:59:12 +000045using namespace arm_compute;
46using namespace arm_compute::test;
47
Anthony Barbier1fa17c22018-08-23 15:44:20 +010048namespace
49{
50std::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
Anthony Barbier6db0ff52018-01-05 10:59:12 +000060namespace arm_compute
61{
62namespace utils
63{
Anthony Barbier9fb0cac2018-04-20 15:46:21 +010064static std::unique_ptr<Example> g_example = nullptr;
65static std::vector<char *> g_example_argv = {};
Anthony Barbier6db0ff52018-01-05 10:59:12 +000066class ExampleTest : public arm_compute::test::framework::TestCase
67{
68public:
69 ExampleTest() = default;
Anthony Barbier9fb0cac2018-04-20 15:46:21 +010070 void do_setup() override
71 {
72 ARM_COMPUTE_ERROR_ON_NULLPTR(g_example.get());
Anthony Barbiere88b9bb2018-07-12 13:26:27 +010073 _is_setup = g_example->do_setup(g_example_argv.size(), &g_example_argv[0]);
Anthony Barbier9fb0cac2018-04-20 15:46:21 +010074 }
Anthony Barbier6db0ff52018-01-05 10:59:12 +000075 void do_run() override
76 {
Anthony Barbiere88b9bb2018-07-12 13:26:27 +010077 if(_is_setup)
78 {
79 g_example->do_run();
80 }
Anthony Barbier6db0ff52018-01-05 10:59:12 +000081 }
82 void do_teardown() override
83 {
Anthony Barbiere88b9bb2018-07-12 13:26:27 +010084 if(_is_setup)
85 {
86 g_example->do_teardown();
87 }
Anthony Barbier9fb0cac2018-04-20 15:46:21 +010088 g_example = nullptr;
Anthony Barbier6db0ff52018-01-05 10:59:12 +000089 }
Anthony Barbiere88b9bb2018-07-12 13:26:27 +010090
91private:
92 bool _is_setup{ false };
Anthony Barbier6db0ff52018-01-05 10:59:12 +000093};
94
Anthony Barbier9fb0cac2018-04-20 15:46:21 +010095int run_example(int argc, char **argv, std::unique_ptr<Example> example)
Anthony Barbier6db0ff52018-01-05 10:59:12 +000096{
Georgios Pinitas12be7ab2018-07-03 12:06:23 +010097 utils::CommandLineParser parser;
98 framework::CommonOptions options(parser);
99 auto example_args = parser.add_option<utils::ListOption<std::string>>("example_args");
Anthony Barbier979dc4f2018-03-07 09:27:48 +0000100 example_args->set_help("Arguments to pass to the example separated by commas (e.g: arg0,arg1,arg2)");
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000101 framework::Framework &framework = framework::Framework::get();
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();
Anthony Barbier9fb0cac2018-04-20 15:46:21 +0100112 g_example = std::move(example);
113 g_example_argv.clear();
114 g_example_argv.emplace_back(argv[0]);
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000115 for(auto &arg : example_args->value())
116 {
Anthony Barbier9fb0cac2018-04-20 15:46:21 +0100117 g_example_argv.emplace_back(const_cast<char *>(arg.c_str())); // NOLINT
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000118 }
Georgios Pinitas3364c4c2018-02-07 14:00:05 +0000119
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000120 if(options.log_level->value() > framework::LogLevel::NONE)
121 {
122 for(auto &p : printers)
123 {
124 p->print_global_header();
125 }
126 }
127
Pablo Tellob7c308a2019-01-22 12:59:26 +0000128#ifdef ARM_COMPUTE_CL
SiCong Li4841c972021-02-03 12:17:35 +0000129 CLGEMMHeuristicsHandle gemm_h;
Pablo Tellob7c308a2019-01-22 12:59:26 +0000130 if(opencl_is_available())
131 {
Michalis Spyrou402740d2021-04-20 11:26:21 +0100132 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);
Pablo Tellob7c308a2019-01-22 12:59:26 +0000143 ARM_COMPUTE_ERROR_ON_MSG(std::get<2>(ctx_dev_err) != CL_SUCCESS, "Failed to create OpenCL context");
144 CLScheduler::get()
SiCong Li4841c972021-02-03 12:17:35 +0000145 .default_init_with_context(std::get<1>(ctx_dev_err), std::get<0>(ctx_dev_err), nullptr, &gemm_h);
Pablo Tellob7c308a2019-01-22 12:59:26 +0000146 }
147#endif /* ARM_COMPUTE_CL */
148
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000149 if(options.log_level->value() >= framework::LogLevel::CONFIG)
150 {
151 for(auto &p : printers)
152 {
Anthony Barbier9280c922018-03-14 14:23:47 +0000153 p->print_entry("Version", build_information());
Anthony Barbier1fa17c22018-08-23 15:44:20 +0100154 p->print_entry("CommandLine", command_line(argc, argv));
Anthony Barbier847864d2018-03-07 11:35:53 +0000155#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 */
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000165 p->print_entry("Iterations", support::cpp11::to_string(options.iterations->value()));
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000166 }
167 }
168
Georgios Pinitas7f152512019-12-16 19:59:52 +0000169 // Initialize framework
170 framework::FrameworkConfig fconfig;
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
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000176 for(auto &p : printers)
177 {
178 framework.add_printer(p.get());
179 }
180 framework.set_throw_errors(options.throw_errors->value());
181 arm_compute::test::framework::detail::TestSuiteRegistrar suite{ "Examples" };
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000182
Michalis Spyrou748a7c82019-10-07 13:00:44 +0100183#ifdef BARE_METAL
184 framework.add_test_case<ExampleTest>(argv[0], framework::DatasetMode::ALL, arm_compute::test::framework::TestCaseFactory::Status::ACTIVE);
185#else /* BARE_METAL */
186 framework.add_test_case<ExampleTest>(basename(argv[0]), framework::DatasetMode::ALL, arm_compute::test::framework::TestCaseFactory::Status::ACTIVE);
187#endif /* BARE_METAL */
Anthony Barbier6db0ff52018-01-05 10:59:12 +0000188 //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