John Richardson | 7d08231 | 2018-05-21 15:19:41 +0100 | [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 | #ifndef ARM_COMPUTE_TEST_SOBEL_FIXTURE |
| 25 | #define ARM_COMPUTE_TEST_SOBEL_FIXTURE |
| 26 | |
| 27 | #include "arm_compute/core/TensorShape.h" |
| 28 | #include "arm_compute/core/Types.h" |
| 29 | #include "tests/Globals.h" |
| 30 | #include "tests/Types.h" |
| 31 | #include "tests/Utils.h" |
| 32 | #include "tests/framework/Fixture.h" |
| 33 | |
| 34 | namespace arm_compute |
| 35 | { |
| 36 | class CLSobel3x3; |
| 37 | class CLSobel5x5; |
| 38 | class CLSobel7x7; |
| 39 | class NESobel3x3; |
| 40 | class NESobel5x5; |
| 41 | class NESobel7x7; |
| 42 | |
| 43 | namespace test |
| 44 | { |
| 45 | namespace benchmark |
| 46 | { |
| 47 | namespace |
| 48 | { |
| 49 | template <typename Function> |
| 50 | struct info; |
| 51 | |
| 52 | template <> |
| 53 | struct info<NESobel3x3> |
| 54 | { |
| 55 | static const Format dst_format = Format::S16; |
| 56 | }; |
| 57 | |
| 58 | template <> |
| 59 | struct info<CLSobel3x3> |
| 60 | { |
| 61 | static const Format dst_format = Format::S16; |
| 62 | }; |
| 63 | |
| 64 | template <> |
| 65 | struct info<NESobel5x5> |
| 66 | { |
| 67 | static const Format dst_format = Format::S16; |
| 68 | }; |
| 69 | |
| 70 | template <> |
| 71 | struct info<CLSobel5x5> |
| 72 | { |
| 73 | static const Format dst_format = Format::S16; |
| 74 | }; |
| 75 | |
| 76 | template <> |
| 77 | struct info<NESobel7x7> |
| 78 | { |
| 79 | static const Format dst_format = Format::S32; |
| 80 | }; |
| 81 | |
| 82 | template <> |
| 83 | struct info<CLSobel7x7> |
| 84 | { |
| 85 | static const Format dst_format = Format::S32; |
| 86 | }; |
| 87 | } // namespace |
| 88 | |
| 89 | template <typename TensorType, typename Function, typename Accessor> |
| 90 | class SobelFixture : public framework::Fixture |
| 91 | { |
| 92 | public: |
| 93 | template <typename...> |
| 94 | void setup(const TensorShape &input_shape, BorderMode border_mode, GradientDimension gradient_dimension, Format input_format) |
| 95 | { |
| 96 | // Generate a random constant value |
| 97 | std::mt19937 gen(library->seed()); |
| 98 | std::uniform_int_distribution<uint8_t> int_dist(0, 255); |
| 99 | const uint8_t constant_border_value = int_dist(gen); |
| 100 | |
| 101 | // Create source tensor |
| 102 | src = create_tensor<TensorType>(input_shape, input_format); |
| 103 | |
| 104 | // Create destination tensors and configure function |
| 105 | switch(gradient_dimension) |
| 106 | { |
| 107 | case GradientDimension::GRAD_X: |
| 108 | dst_x = create_tensor<TensorType>(input_shape, info<Function>::dst_format); |
| 109 | sobel_func.configure(&src, &dst_x, nullptr, border_mode, constant_border_value); |
| 110 | break; |
| 111 | case GradientDimension::GRAD_Y: |
| 112 | dst_y = create_tensor<TensorType>(input_shape, info<Function>::dst_format); |
| 113 | sobel_func.configure(&src, nullptr, &dst_y, border_mode, constant_border_value); |
| 114 | break; |
| 115 | case GradientDimension::GRAD_XY: |
| 116 | dst_x = create_tensor<TensorType>(input_shape, info<Function>::dst_format); |
| 117 | dst_y = create_tensor<TensorType>(input_shape, info<Function>::dst_format); |
| 118 | sobel_func.configure(&src, &dst_x, &dst_y, border_mode, constant_border_value); |
| 119 | break; |
| 120 | default: |
| 121 | ARM_COMPUTE_ERROR("Gradient dimension not supported"); |
| 122 | } |
| 123 | |
| 124 | // Allocate tensors |
| 125 | src.allocator()->allocate(); |
| 126 | dst_x.allocator()->allocate(); |
| 127 | dst_y.allocator()->allocate(); |
| 128 | } |
| 129 | |
| 130 | void run() |
| 131 | { |
| 132 | sobel_func.run(); |
| 133 | } |
| 134 | |
| 135 | void sync() |
| 136 | { |
| 137 | sync_if_necessary<TensorType>(); |
| 138 | sync_tensor_if_necessary<TensorType>(dst_x); |
| 139 | sync_tensor_if_necessary<TensorType>(dst_y); |
| 140 | } |
| 141 | |
| 142 | void teardown() |
| 143 | { |
| 144 | src.allocator()->free(); |
| 145 | dst_x.allocator()->free(); |
| 146 | dst_y.allocator()->free(); |
| 147 | } |
| 148 | |
| 149 | private: |
| 150 | TensorType src{}; |
| 151 | TensorType dst_x{}; |
| 152 | TensorType dst_y{}; |
| 153 | Function sobel_func{}; |
| 154 | }; |
| 155 | } // namespace benchmark |
| 156 | } // namespace test |
| 157 | } // namespace arm_compute |
| 158 | #endif /* ARM_COMPUTE_TEST_SOBEL_FIXTURE */ |