xref: /aosp_15_r20/external/ComputeLibrary/tests/benchmark_examples/RunExample.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1*c217d954SCole Faust /*
2*c217d954SCole Faust  * Copyright (c) 2018-2021 Arm Limited.
3*c217d954SCole Faust  *
4*c217d954SCole Faust  * SPDX-License-Identifier: MIT
5*c217d954SCole Faust  *
6*c217d954SCole Faust  * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust  * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust  * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust  * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust  * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust  *
13*c217d954SCole Faust  * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust  * copies or substantial portions of the Software.
15*c217d954SCole Faust  *
16*c217d954SCole Faust  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust  * SOFTWARE.
23*c217d954SCole Faust  */
24*c217d954SCole Faust #include "utils/Utils.h"
25*c217d954SCole Faust 
26*c217d954SCole Faust #define BENCHMARK_EXAMPLES
27*c217d954SCole Faust #include "utils/Utils.cpp"
28*c217d954SCole Faust 
29*c217d954SCole Faust #include "arm_compute/runtime/Scheduler.h"
30*c217d954SCole Faust #include "tests/framework/Framework.h"
31*c217d954SCole Faust #include "tests/framework/Macros.h"
32*c217d954SCole Faust #include "tests/framework/command_line/CommonOptions.h"
33*c217d954SCole Faust #include "tests/framework/instruments/Instruments.h"
34*c217d954SCole Faust #include "utils/command_line/CommandLineParser.h"
35*c217d954SCole Faust 
36*c217d954SCole Faust #ifdef ARM_COMPUTE_CL
37*c217d954SCole Faust #include "arm_compute/runtime/CL/CLGEMMHeuristicsHandle.h"
38*c217d954SCole Faust #include "arm_compute/runtime/CL/CLHelpers.h"
39*c217d954SCole Faust #include "arm_compute/runtime/CL/CLScheduler.h"
40*c217d954SCole Faust #endif /* ARM_COMPUTE_CL */
41*c217d954SCole Faust 
42*c217d954SCole Faust #include <libgen.h>
43*c217d954SCole Faust 
44*c217d954SCole Faust using namespace arm_compute;
45*c217d954SCole Faust using namespace arm_compute::test;
46*c217d954SCole Faust 
47*c217d954SCole Faust namespace
48*c217d954SCole Faust {
command_line(int argc,char ** argv)49*c217d954SCole Faust std::string command_line(int argc, char **argv)
50*c217d954SCole Faust {
51*c217d954SCole Faust     std::stringstream ss;
52*c217d954SCole Faust     for(int i = 0; i < argc; i++)
53*c217d954SCole Faust     {
54*c217d954SCole Faust         ss << argv[i] << " ";
55*c217d954SCole Faust     }
56*c217d954SCole Faust     return ss.str();
57*c217d954SCole Faust }
58*c217d954SCole Faust } // namespace
59*c217d954SCole Faust namespace arm_compute
60*c217d954SCole Faust {
61*c217d954SCole Faust namespace utils
62*c217d954SCole Faust {
63*c217d954SCole Faust static std::unique_ptr<Example> g_example      = nullptr;
64*c217d954SCole Faust static std::vector<char *>      g_example_argv = {};
65*c217d954SCole Faust class ExampleTest : public arm_compute::test::framework::TestCase
66*c217d954SCole Faust {
67*c217d954SCole Faust public:
68*c217d954SCole Faust     ExampleTest() = default;
do_setup()69*c217d954SCole Faust     void do_setup() override
70*c217d954SCole Faust     {
71*c217d954SCole Faust         ARM_COMPUTE_ERROR_ON_NULLPTR(g_example.get());
72*c217d954SCole Faust         _is_setup = g_example->do_setup(g_example_argv.size(), &g_example_argv[0]);
73*c217d954SCole Faust     }
do_run()74*c217d954SCole Faust     void do_run() override
75*c217d954SCole Faust     {
76*c217d954SCole Faust         if(_is_setup)
77*c217d954SCole Faust         {
78*c217d954SCole Faust             g_example->do_run();
79*c217d954SCole Faust         }
80*c217d954SCole Faust     }
do_teardown()81*c217d954SCole Faust     void do_teardown() override
82*c217d954SCole Faust     {
83*c217d954SCole Faust         if(_is_setup)
84*c217d954SCole Faust         {
85*c217d954SCole Faust             g_example->do_teardown();
86*c217d954SCole Faust         }
87*c217d954SCole Faust         g_example = nullptr;
88*c217d954SCole Faust     }
89*c217d954SCole Faust 
90*c217d954SCole Faust private:
91*c217d954SCole Faust     bool _is_setup{ false };
92*c217d954SCole Faust };
93*c217d954SCole Faust 
run_example(int argc,char ** argv,std::unique_ptr<Example> example)94*c217d954SCole Faust int run_example(int argc, char **argv, std::unique_ptr<Example> example)
95*c217d954SCole Faust {
96*c217d954SCole Faust     utils::CommandLineParser parser;
97*c217d954SCole Faust     framework::CommonOptions options(parser);
98*c217d954SCole Faust     auto                     example_args = parser.add_option<utils::ListOption<std::string>>("example_args");
99*c217d954SCole Faust     example_args->set_help("Arguments to pass to the example separated by commas (e.g: arg0,arg1,arg2)");
100*c217d954SCole Faust     framework::Framework &framework = framework::Framework::get();
101*c217d954SCole Faust 
102*c217d954SCole Faust     parser.parse(argc, argv);
103*c217d954SCole Faust 
104*c217d954SCole Faust     if(options.help->is_set() && options.help->value())
105*c217d954SCole Faust     {
106*c217d954SCole Faust         parser.print_help(argv[0]);
107*c217d954SCole Faust         return 0;
108*c217d954SCole Faust     }
109*c217d954SCole Faust 
110*c217d954SCole Faust     std::vector<std::unique_ptr<framework::Printer>> printers = options.create_printers();
111*c217d954SCole Faust     g_example                                                 = std::move(example);
112*c217d954SCole Faust     g_example_argv.clear();
113*c217d954SCole Faust     g_example_argv.emplace_back(argv[0]);
114*c217d954SCole Faust     for(auto &arg : example_args->value())
115*c217d954SCole Faust     {
116*c217d954SCole Faust         g_example_argv.emplace_back(const_cast<char *>(arg.c_str())); // NOLINT
117*c217d954SCole Faust     }
118*c217d954SCole Faust 
119*c217d954SCole Faust     if(options.log_level->value() > framework::LogLevel::NONE)
120*c217d954SCole Faust     {
121*c217d954SCole Faust         for(auto &p : printers)
122*c217d954SCole Faust         {
123*c217d954SCole Faust             p->print_global_header();
124*c217d954SCole Faust         }
125*c217d954SCole Faust     }
126*c217d954SCole Faust 
127*c217d954SCole Faust #ifdef ARM_COMPUTE_CL
128*c217d954SCole Faust     CLGEMMHeuristicsHandle gemm_h;
129*c217d954SCole Faust     if(opencl_is_available())
130*c217d954SCole Faust     {
131*c217d954SCole Faust         CLBackendType backend_type = CLBackendType::Native;
132*c217d954SCole Faust         for(auto &arg : example_args->value())
133*c217d954SCole Faust         {
134*c217d954SCole Faust             if(arg.find("--target=clvk") != std::string::npos)
135*c217d954SCole Faust             {
136*c217d954SCole Faust                 backend_type = CLBackendType::Clvk;
137*c217d954SCole Faust                 break;
138*c217d954SCole Faust             }
139*c217d954SCole Faust         }
140*c217d954SCole Faust 
141*c217d954SCole Faust         auto ctx_dev_err = create_opencl_context_and_device(backend_type);
142*c217d954SCole Faust         ARM_COMPUTE_ERROR_ON_MSG(std::get<2>(ctx_dev_err) != CL_SUCCESS, "Failed to create OpenCL context");
143*c217d954SCole Faust         CLScheduler::get()
144*c217d954SCole Faust         .default_init_with_context(std::get<1>(ctx_dev_err), std::get<0>(ctx_dev_err), nullptr, &gemm_h);
145*c217d954SCole Faust     }
146*c217d954SCole Faust #endif /* ARM_COMPUTE_CL */
147*c217d954SCole Faust 
148*c217d954SCole Faust     if(options.log_level->value() >= framework::LogLevel::CONFIG)
149*c217d954SCole Faust     {
150*c217d954SCole Faust         for(auto &p : printers)
151*c217d954SCole Faust         {
152*c217d954SCole Faust             p->print_entry("Version", build_information());
153*c217d954SCole Faust             p->print_entry("CommandLine", command_line(argc, argv));
154*c217d954SCole Faust #ifdef ARM_COMPUTE_CL
155*c217d954SCole Faust             if(opencl_is_available())
156*c217d954SCole Faust             {
157*c217d954SCole Faust                 p->print_entry("CL_DEVICE_VERSION", CLKernelLibrary::get().get_device_version());
158*c217d954SCole Faust             }
159*c217d954SCole Faust             else
160*c217d954SCole Faust             {
161*c217d954SCole Faust                 p->print_entry("CL_DEVICE_VERSION", "Unavailable");
162*c217d954SCole Faust             }
163*c217d954SCole Faust #endif /* ARM_COMPUTE_CL */
164*c217d954SCole Faust             p->print_entry("Iterations", support::cpp11::to_string(options.iterations->value()));
165*c217d954SCole Faust         }
166*c217d954SCole Faust     }
167*c217d954SCole Faust 
168*c217d954SCole Faust     // Initialize framework
169*c217d954SCole Faust     framework::FrameworkConfig fconfig;
170*c217d954SCole Faust     fconfig.instruments    = options.instruments->value();
171*c217d954SCole Faust     fconfig.num_iterations = options.iterations->value();
172*c217d954SCole Faust     fconfig.log_level      = options.log_level->value();
173*c217d954SCole Faust     framework.init(fconfig);
174*c217d954SCole Faust 
175*c217d954SCole Faust     for(auto &p : printers)
176*c217d954SCole Faust     {
177*c217d954SCole Faust         framework.add_printer(p.get());
178*c217d954SCole Faust     }
179*c217d954SCole Faust     framework.set_throw_errors(options.throw_errors->value());
180*c217d954SCole Faust     arm_compute::test::framework::detail::TestSuiteRegistrar suite{ "Examples" };
181*c217d954SCole Faust 
182*c217d954SCole Faust #ifdef BARE_METAL
183*c217d954SCole Faust     framework.add_test_case<ExampleTest>(argv[0], framework::DatasetMode::ALL, arm_compute::test::framework::TestCaseFactory::Status::ACTIVE);
184*c217d954SCole Faust #else  /* BARE_METAL */
185*c217d954SCole Faust     framework.add_test_case<ExampleTest>(basename(argv[0]), framework::DatasetMode::ALL, arm_compute::test::framework::TestCaseFactory::Status::ACTIVE);
186*c217d954SCole Faust #endif /* BARE_METAL */
187*c217d954SCole Faust     //func(argc, argv);
188*c217d954SCole Faust     bool success = framework.run();
189*c217d954SCole Faust     if(options.log_level->value() > framework::LogLevel::NONE)
190*c217d954SCole Faust     {
191*c217d954SCole Faust         for(auto &p : printers)
192*c217d954SCole Faust         {
193*c217d954SCole Faust             p->print_global_footer();
194*c217d954SCole Faust         }
195*c217d954SCole Faust     }
196*c217d954SCole Faust 
197*c217d954SCole Faust     return (success ? 0 : 1);
198*c217d954SCole Faust }
199*c217d954SCole Faust 
200*c217d954SCole Faust } // namespace utils
201*c217d954SCole Faust } // namespace arm_compute
202