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