Anthony Barbier | 979dc4f | 2018-03-07 09:27:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 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 | //FIXME / INTERNAL_ONLY: This file should not be released! |
| 26 | |
| 27 | #define BENCHMARK_EXAMPLES |
| 28 | #include "utils/Utils.cpp" |
| 29 | |
| 30 | #include "ValidateExample.h" |
| 31 | #include "arm_compute/runtime/Scheduler.h" |
| 32 | #include "tests/AssetsLibrary.h" |
| 33 | #include "tests/Globals.h" |
| 34 | #include "tests/framework/Framework.h" |
| 35 | #include "tests/framework/Macros.h" |
Anthony Barbier | 979dc4f | 2018-03-07 09:27:48 +0000 | [diff] [blame] | 36 | #include "tests/framework/command_line/CommonOptions.h" |
| 37 | #include "tests/framework/instruments/Instruments.h" |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 38 | #include "utils/command_line/CommandLineParser.h" |
Anthony Barbier | 979dc4f | 2018-03-07 09:27:48 +0000 | [diff] [blame] | 39 | |
| 40 | #ifdef ARM_COMPUTE_CL |
| 41 | #include "arm_compute/runtime/CL/CLScheduler.h" |
| 42 | #endif /* ARM_COMPUTE_CL */ |
| 43 | #ifdef ARM_COMPUTE_GC |
| 44 | #include "arm_compute/runtime/GLES_COMPUTE/GCScheduler.h" |
| 45 | #endif /* ARM_COMPUTE_GC */ |
| 46 | |
| 47 | #include <libgen.h> |
| 48 | |
| 49 | using namespace arm_compute; |
| 50 | using namespace arm_compute::test; |
| 51 | |
| 52 | namespace arm_compute |
| 53 | { |
| 54 | namespace test |
| 55 | { |
| 56 | std::unique_ptr<AssetsLibrary> library; |
| 57 | } // namespace test |
| 58 | namespace utils |
| 59 | { |
Anthony Barbier | 9fb0cac | 2018-04-20 15:46:21 +0100 | [diff] [blame] | 60 | static std::unique_ptr<ValidateExample> g_example = nullptr; |
| 61 | static std::vector<char *> g_example_argv = {}; |
| 62 | |
| 63 | template <bool validate> |
Anthony Barbier | 979dc4f | 2018-03-07 09:27:48 +0000 | [diff] [blame] | 64 | class ExampleTest : public arm_compute::test::framework::TestCase |
| 65 | { |
| 66 | public: |
| 67 | ExampleTest() = default; |
Anthony Barbier | 9fb0cac | 2018-04-20 15:46:21 +0100 | [diff] [blame] | 68 | void do_setup() override |
| 69 | { |
| 70 | ARM_COMPUTE_ERROR_ON_NULLPTR(g_example.get()); |
| 71 | g_example->do_setup(g_example_argv.size(), &g_example_argv[0]); |
| 72 | } |
Anthony Barbier | 979dc4f | 2018-03-07 09:27:48 +0000 | [diff] [blame] | 73 | void do_run() override |
| 74 | { |
| 75 | g_example->do_run(); |
| 76 | } |
| 77 | void do_teardown() override |
| 78 | { |
| 79 | if(validate) |
| 80 | { |
| 81 | g_example->do_validate(); |
| 82 | } |
| 83 | g_example->do_teardown(); |
Anthony Barbier | 9fb0cac | 2018-04-20 15:46:21 +0100 | [diff] [blame] | 84 | g_example = nullptr; |
Anthony Barbier | 979dc4f | 2018-03-07 09:27:48 +0000 | [diff] [blame] | 85 | } |
| 86 | }; |
| 87 | |
Anthony Barbier | 9fb0cac | 2018-04-20 15:46:21 +0100 | [diff] [blame] | 88 | int run_example(int argc, char **argv, std::unique_ptr<ValidateExample> example) |
Anthony Barbier | 979dc4f | 2018-03-07 09:27:48 +0000 | [diff] [blame] | 89 | { |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 90 | utils::CommandLineParser parser; |
| 91 | framework::CommonOptions options(parser); |
| 92 | auto example_args = parser.add_option<utils::ListOption<std::string>>("example_args"); |
Anthony Barbier | 979dc4f | 2018-03-07 09:27:48 +0000 | [diff] [blame] | 93 | example_args->set_help("Arguments to pass to the example separated by commas (e.g: arg0,arg1,arg2)"); |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 94 | auto seed = parser.add_option<utils::SimpleOption<std::random_device::result_type>>("seed", std::random_device()()); |
Anthony Barbier | 979dc4f | 2018-03-07 09:27:48 +0000 | [diff] [blame] | 95 | seed->set_help("Global seed for random number generation"); |
Georgios Pinitas | 12be7ab | 2018-07-03 12:06:23 +0100 | [diff] [blame] | 96 | auto validate = parser.add_option<utils::SimpleOption<int>>("validate", 1); |
Anthony Barbier | 979dc4f | 2018-03-07 09:27:48 +0000 | [diff] [blame] | 97 | validate->set_help("Enable / disable output validation (0/1)"); |
| 98 | |
| 99 | framework::Framework &framework = framework::Framework::get(); |
| 100 | |
| 101 | parser.parse(argc, argv); |
| 102 | |
| 103 | if(options.help->is_set() && options.help->value()) |
| 104 | { |
| 105 | parser.print_help(argv[0]); |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | std::vector<std::unique_ptr<framework::Printer>> printers = options.create_printers(); |
Anthony Barbier | 9fb0cac | 2018-04-20 15:46:21 +0100 | [diff] [blame] | 110 | g_example = std::move(example); |
| 111 | g_example_argv.clear(); |
| 112 | g_example_argv.emplace_back(argv[0]); |
Anthony Barbier | 979dc4f | 2018-03-07 09:27:48 +0000 | [diff] [blame] | 113 | for(auto &arg : example_args->value()) |
| 114 | { |
Anthony Barbier | 9fb0cac | 2018-04-20 15:46:21 +0100 | [diff] [blame] | 115 | g_example_argv.emplace_back(const_cast<char *>(arg.c_str())); // NOLINT |
Anthony Barbier | 979dc4f | 2018-03-07 09:27:48 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | // Set number of threads in Scheduler |
| 119 | Scheduler::get().set_num_threads(options.threads->value()); |
| 120 | library = support::cpp14::make_unique<AssetsLibrary>("." /* Only using random values */, seed->value()); |
| 121 | |
Anthony Barbier | 979dc4f | 2018-03-07 09:27:48 +0000 | [diff] [blame] | 122 | if(options.log_level->value() > framework::LogLevel::NONE) |
| 123 | { |
| 124 | for(auto &p : printers) |
| 125 | { |
| 126 | p->print_global_header(); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | if(options.log_level->value() >= framework::LogLevel::CONFIG) |
| 131 | { |
| 132 | for(auto &p : printers) |
| 133 | { |
| 134 | p->print_entry("Version", build_information()); |
| 135 | p->print_entry("Seed", support::cpp11::to_string(seed->value())); |
| 136 | #ifdef ARM_COMPUTE_CL |
| 137 | if(opencl_is_available()) |
| 138 | { |
Anthony Barbier | 9fb0cac | 2018-04-20 15:46:21 +0100 | [diff] [blame] | 139 | if(!CLScheduler::get().is_initialised()) |
| 140 | { |
| 141 | CLScheduler::get().default_init(); |
| 142 | } |
Anthony Barbier | 979dc4f | 2018-03-07 09:27:48 +0000 | [diff] [blame] | 143 | p->print_entry("CL_DEVICE_VERSION", CLKernelLibrary::get().get_device_version()); |
| 144 | } |
| 145 | else |
| 146 | { |
| 147 | p->print_entry("CL_DEVICE_VERSION", "Unavailable"); |
| 148 | } |
| 149 | #endif /* ARM_COMPUTE_CL */ |
| 150 | p->print_entry("Iterations", support::cpp11::to_string(options.iterations->value())); |
| 151 | p->print_entry("Threads", support::cpp11::to_string(options.threads->value())); |
Anthony Barbier | 9fb0cac | 2018-04-20 15:46:21 +0100 | [diff] [blame] | 152 | g_example->print_parameters(*p); |
Anthony Barbier | 979dc4f | 2018-03-07 09:27:48 +0000 | [diff] [blame] | 153 | } |
| 154 | } |
| 155 | |
| 156 | framework.init(options.instruments->value(), options.iterations->value(), framework::DatasetMode::ALL, "", "", options.log_level->value()); |
| 157 | for(auto &p : printers) |
| 158 | { |
| 159 | framework.add_printer(p.get()); |
| 160 | } |
| 161 | |
| 162 | framework.set_throw_errors(options.throw_errors->value()); |
| 163 | arm_compute::test::framework::detail::TestSuiteRegistrar suite{ "Examples" }; |
| 164 | if(validate->value() != 0) |
| 165 | { |
| 166 | framework.add_test_case<ExampleTest<true>>(basename(argv[0]), framework::DatasetMode::ALL, arm_compute::test::framework::TestCaseFactory::Status::ACTIVE); |
| 167 | } |
| 168 | else |
| 169 | { |
| 170 | framework.add_test_case<ExampleTest<false>>(basename(argv[0]), framework::DatasetMode::ALL, arm_compute::test::framework::TestCaseFactory::Status::ACTIVE); |
| 171 | } |
| 172 | |
| 173 | //func(argc, argv); |
| 174 | bool success = framework.run(); |
| 175 | if(options.log_level->value() > framework::LogLevel::NONE) |
| 176 | { |
| 177 | for(auto &p : printers) |
| 178 | { |
| 179 | p->print_global_footer(); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | return (success ? 0 : 1); |
| 184 | } |
| 185 | |
| 186 | } // namespace utils |
| 187 | } // namespace arm_compute |