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 "arm_compute/runtime/Scheduler.h"
30 #include "tests/framework/Framework.h"
31 #include "tests/framework/Macros.h"
32 #include "tests/framework/command_line/CommonOptions.h"
33 #include "tests/framework/instruments/Instruments.h"
34 #include "utils/command_line/CommandLineParser.h"
35
36 #ifdef ARM_COMPUTE_CL
37 #include "arm_compute/runtime/CL/CLGEMMHeuristicsHandle.h"
38 #include "arm_compute/runtime/CL/CLHelpers.h"
39 #include "arm_compute/runtime/CL/CLScheduler.h"
40 #endif /* ARM_COMPUTE_CL */
41
42 #include <libgen.h>
43
44 using namespace arm_compute;
45 using namespace arm_compute::test;
46
47 namespace
48 {
command_line(int argc,char ** argv)49 std::string command_line(int argc, char **argv)
50 {
51 std::stringstream ss;
52 for(int i = 0; i < argc; i++)
53 {
54 ss << argv[i] << " ";
55 }
56 return ss.str();
57 }
58 } // namespace
59 namespace arm_compute
60 {
61 namespace utils
62 {
63 static std::unique_ptr<Example> g_example = nullptr;
64 static std::vector<char *> g_example_argv = {};
65 class ExampleTest : public arm_compute::test::framework::TestCase
66 {
67 public:
68 ExampleTest() = default;
do_setup()69 void do_setup() override
70 {
71 ARM_COMPUTE_ERROR_ON_NULLPTR(g_example.get());
72 _is_setup = g_example->do_setup(g_example_argv.size(), &g_example_argv[0]);
73 }
do_run()74 void do_run() override
75 {
76 if(_is_setup)
77 {
78 g_example->do_run();
79 }
80 }
do_teardown()81 void do_teardown() override
82 {
83 if(_is_setup)
84 {
85 g_example->do_teardown();
86 }
87 g_example = nullptr;
88 }
89
90 private:
91 bool _is_setup{ false };
92 };
93
run_example(int argc,char ** argv,std::unique_ptr<Example> example)94 int run_example(int argc, char **argv, std::unique_ptr<Example> example)
95 {
96 utils::CommandLineParser parser;
97 framework::CommonOptions options(parser);
98 auto example_args = parser.add_option<utils::ListOption<std::string>>("example_args");
99 example_args->set_help("Arguments to pass to the example separated by commas (e.g: arg0,arg1,arg2)");
100 framework::Framework &framework = framework::Framework::get();
101
102 parser.parse(argc, argv);
103
104 if(options.help->is_set() && options.help->value())
105 {
106 parser.print_help(argv[0]);
107 return 0;
108 }
109
110 std::vector<std::unique_ptr<framework::Printer>> printers = options.create_printers();
111 g_example = std::move(example);
112 g_example_argv.clear();
113 g_example_argv.emplace_back(argv[0]);
114 for(auto &arg : example_args->value())
115 {
116 g_example_argv.emplace_back(const_cast<char *>(arg.c_str())); // NOLINT
117 }
118
119 if(options.log_level->value() > framework::LogLevel::NONE)
120 {
121 for(auto &p : printers)
122 {
123 p->print_global_header();
124 }
125 }
126
127 #ifdef ARM_COMPUTE_CL
128 CLGEMMHeuristicsHandle gemm_h;
129 if(opencl_is_available())
130 {
131 CLBackendType backend_type = CLBackendType::Native;
132 for(auto &arg : example_args->value())
133 {
134 if(arg.find("--target=clvk") != std::string::npos)
135 {
136 backend_type = CLBackendType::Clvk;
137 break;
138 }
139 }
140
141 auto ctx_dev_err = create_opencl_context_and_device(backend_type);
142 ARM_COMPUTE_ERROR_ON_MSG(std::get<2>(ctx_dev_err) != CL_SUCCESS, "Failed to create OpenCL context");
143 CLScheduler::get()
144 .default_init_with_context(std::get<1>(ctx_dev_err), std::get<0>(ctx_dev_err), nullptr, &gemm_h);
145 }
146 #endif /* ARM_COMPUTE_CL */
147
148 if(options.log_level->value() >= framework::LogLevel::CONFIG)
149 {
150 for(auto &p : printers)
151 {
152 p->print_entry("Version", build_information());
153 p->print_entry("CommandLine", command_line(argc, argv));
154 #ifdef ARM_COMPUTE_CL
155 if(opencl_is_available())
156 {
157 p->print_entry("CL_DEVICE_VERSION", CLKernelLibrary::get().get_device_version());
158 }
159 else
160 {
161 p->print_entry("CL_DEVICE_VERSION", "Unavailable");
162 }
163 #endif /* ARM_COMPUTE_CL */
164 p->print_entry("Iterations", support::cpp11::to_string(options.iterations->value()));
165 }
166 }
167
168 // Initialize framework
169 framework::FrameworkConfig fconfig;
170 fconfig.instruments = options.instruments->value();
171 fconfig.num_iterations = options.iterations->value();
172 fconfig.log_level = options.log_level->value();
173 framework.init(fconfig);
174
175 for(auto &p : printers)
176 {
177 framework.add_printer(p.get());
178 }
179 framework.set_throw_errors(options.throw_errors->value());
180 arm_compute::test::framework::detail::TestSuiteRegistrar suite{ "Examples" };
181
182 #ifdef BARE_METAL
183 framework.add_test_case<ExampleTest>(argv[0], framework::DatasetMode::ALL, arm_compute::test::framework::TestCaseFactory::Status::ACTIVE);
184 #else /* BARE_METAL */
185 framework.add_test_case<ExampleTest>(basename(argv[0]), framework::DatasetMode::ALL, arm_compute::test::framework::TestCaseFactory::Status::ACTIVE);
186 #endif /* BARE_METAL */
187 //func(argc, argv);
188 bool success = framework.run();
189 if(options.log_level->value() > framework::LogLevel::NONE)
190 {
191 for(auto &p : printers)
192 {
193 p->print_global_footer();
194 }
195 }
196
197 return (success ? 0 : 1);
198 }
199
200 } // namespace utils
201 } // namespace arm_compute
202