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