COMPMID-761: Add CL/NEON OpticalFlow benchmark tests

Change-Id: I9ced2fc2cc6c8e5a17017cef60916a86eb87a4e2
Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/134145
Tested-by: Jenkins <bsgcomp@arm.com>
Reviewed-by: Anthony Barbier <anthony.barbier@arm.com>
diff --git a/tests/benchmark/CL/OpticalFlow.cpp b/tests/benchmark/CL/OpticalFlow.cpp
new file mode 100644
index 0000000..4794d8a
--- /dev/null
+++ b/tests/benchmark/CL/OpticalFlow.cpp
@@ -0,0 +1,70 @@
+/*
+ * Copyright (c) 2018 ARM Limited.
+ *
+ * SPDX-License-Identifier: MIT
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+#include "arm_compute/core/Types.h"
+#include "arm_compute/runtime/CL/CLArray.h"
+#include "arm_compute/runtime/CL/CLTensor.h"
+#include "arm_compute/runtime/CL/functions/CLGaussianPyramid.h"
+#include "arm_compute/runtime/CL/functions/CLOpticalFlow.h"
+#include "tests/CL/CLAccessor.h"
+#include "tests/CL/CLArrayAccessor.h"
+#include "tests/benchmark/fixtures/OpticalFlowFixture.h"
+#include "tests/datasets/BorderModeDataset.h"
+#include "tests/datasets/ImageFileDatasets.h"
+#include "tests/datasets/OpticalFlowDataset.h"
+#include "tests/framework/Macros.h"
+#include "tests/framework/datasets/Datasets.h"
+#include "utils/TypePrinter.h"
+
+namespace arm_compute
+{
+namespace test
+{
+namespace benchmark
+{
+using CLOpticalFlowFixture = OpticalFlowFixture<CLTensor, CLOpticalFlow, CLAccessor, CLKeyPointArray, CLArrayAccessor<KeyPoint>, CLPyramid, CLGaussianPyramidHalf>;
+
+TEST_SUITE(CL)
+TEST_SUITE(OpticalFlow)
+
+// *INDENT-OFF*
+// clang-format off
+REGISTER_FIXTURE_DATA_TEST_CASE(RunSmall, CLOpticalFlowFixture, framework::DatasetMode::PRECOMMIT,
+                                combine(combine(
+                                datasets::SmallOpticalFlowDataset(),
+                                framework::dataset::make("Format", Format::U8)),
+                                datasets::BorderModes()));
+
+REGISTER_FIXTURE_DATA_TEST_CASE(RunLarge, CLOpticalFlowFixture, framework::DatasetMode::NIGHTLY,
+                                combine(combine(
+                                datasets::LargeOpticalFlowDataset(),
+                                framework::dataset::make("Format", Format::U8)),
+                                datasets::BorderModes()));
+// clang-format on
+// *INDENT-ON*
+
+TEST_SUITE_END() // OpticalFlow
+TEST_SUITE_END() // CL
+} // namespace benchmark
+} // namespace test
+} // namespace arm_compute
diff --git a/tests/benchmark/NEON/OpticalFlow.cpp b/tests/benchmark/NEON/OpticalFlow.cpp
new file mode 100644
index 0000000..30672a1
--- /dev/null
+++ b/tests/benchmark/NEON/OpticalFlow.cpp
@@ -0,0 +1,70 @@
+/*
+ * Copyright (c) 2018 ARM Limited.
+ *
+ * SPDX-License-Identifier: MIT
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+#include "arm_compute/core/Types.h"
+#include "arm_compute/runtime/Array.h"
+#include "arm_compute/runtime/NEON/functions/NEGaussianPyramid.h"
+#include "arm_compute/runtime/NEON/functions/NEOpticalFlow.h"
+#include "arm_compute/runtime/Tensor.h"
+#include "tests/NEON/Accessor.h"
+#include "tests/NEON/ArrayAccessor.h"
+#include "tests/benchmark/fixtures/OpticalFlowFixture.h"
+#include "tests/datasets/BorderModeDataset.h"
+#include "tests/datasets/ImageFileDatasets.h"
+#include "tests/datasets/OpticalFlowDataset.h"
+#include "tests/framework/Macros.h"
+#include "tests/framework/datasets/Datasets.h"
+#include "utils/TypePrinter.h"
+
+namespace arm_compute
+{
+namespace test
+{
+namespace benchmark
+{
+using NEOpticalFlowFixture = OpticalFlowFixture<Tensor, NEOpticalFlow, Accessor, KeyPointArray, ArrayAccessor<KeyPoint>, Pyramid, NEGaussianPyramidHalf>;
+
+TEST_SUITE(NEON)
+TEST_SUITE(OpticalFlow)
+
+// *INDENT-OFF*
+// clang-format off
+REGISTER_FIXTURE_DATA_TEST_CASE(RunSmall, NEOpticalFlowFixture, framework::DatasetMode::PRECOMMIT,
+                                combine(combine(
+                                datasets::SmallOpticalFlowDataset(),
+                                framework::dataset::make("Format", Format::U8)),
+                                datasets::BorderModes()));
+
+REGISTER_FIXTURE_DATA_TEST_CASE(RunLarge, NEOpticalFlowFixture, framework::DatasetMode::NIGHTLY,
+                                combine(combine(
+                                datasets::LargeOpticalFlowDataset(),
+                                framework::dataset::make("Format", Format::U8)),
+                                datasets::BorderModes()));
+// clang-format on
+// *INDENT-ON*
+
+TEST_SUITE_END() // OpticalFlow
+TEST_SUITE_END() // NEON
+} // namespace benchmark
+} // namespace test
+} // namespace arm_compute
diff --git a/tests/benchmark/fixtures/OpticalFlowFixture.h b/tests/benchmark/fixtures/OpticalFlowFixture.h
new file mode 100644
index 0000000..f781875
--- /dev/null
+++ b/tests/benchmark/fixtures/OpticalFlowFixture.h
@@ -0,0 +1,148 @@
+/*
+ * Copyright (c) 2018 ARM Limited.
+ *
+ * SPDX-License-Identifier: MIT
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to
+ * deal in the Software without restriction, including without limitation the
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+ * sell copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+#ifndef ARM_COMPUTE_TEST_OPTICAL_FLOW_FIXTURE
+#define ARM_COMPUTE_TEST_OPTICAL_FLOW_FIXTURE
+
+#include "arm_compute/core/TensorShape.h"
+#include "arm_compute/core/Types.h"
+#include "tests/Globals.h"
+#include "tests/Types.h"
+#include "tests/Utils.h"
+#include "tests/framework/Fixture.h"
+
+namespace arm_compute
+{
+namespace test
+{
+namespace benchmark
+{
+template <typename TensorType,
+          typename Function,
+          typename Accessor,
+          typename ArrayType,
+          typename ArrayAccessor,
+          typename PyramidType,
+          typename PyramidFunctionType>
+class OpticalFlowFixture : public framework::Fixture
+{
+public:
+    template <typename...>
+    void setup(std::string old_image_name, std::string new_image_name, OpticalFlowParameters params,
+               size_t num_levels, size_t num_keypoints, Format format, BorderMode border_mode)
+    {
+        const uint8_t                         constant_border_value = 0;
+        const std::random_device::result_type seed                  = 0;
+
+        // Create keypoints
+        old_keypoints           = generate_random_keypoints(library->get_image_shape(old_image_name), num_keypoints, seed, num_levels);
+        new_keypoints_estimates = old_keypoints;
+
+        // Create tensors
+        old_image = create_tensor<TensorType>(library->get_image_shape(old_image_name), format);
+        new_image = create_tensor<TensorType>(library->get_image_shape(new_image_name), format);
+
+        // Load keypoints
+        fill_array(ArrayAccessor(old_points), old_keypoints);
+        fill_array(ArrayAccessor(new_points_estimates), new_keypoints_estimates);
+
+        // Create pyramid images
+        PyramidInfo pyramid_info(num_levels, SCALE_PYRAMID_HALF, old_image.info()->tensor_shape(), format);
+        old_pyramid = create_pyramid<PyramidType>(pyramid_info);
+        new_pyramid = create_pyramid<PyramidType>(pyramid_info);
+
+        // Create and configure pyramid functions
+        old_gaussian_pyramid_func.configure(&old_image, &old_pyramid, border_mode, constant_border_value);
+        new_gaussian_pyramid_func.configure(&new_image, &new_pyramid, border_mode, constant_border_value);
+
+        optical_flow_func.configure(&old_pyramid,
+                                    &new_pyramid,
+                                    &old_points,
+                                    &new_points_estimates,
+                                    &new_points,
+                                    params.termination,
+                                    params.epsilon,
+                                    params.num_iterations,
+                                    params.window_dimension,
+                                    params.use_initial_estimate,
+                                    border_mode,
+                                    constant_border_value);
+
+        // Allocate input tensors
+        old_image.allocator()->allocate();
+        new_image.allocator()->allocate();
+
+        // Allocate pyramids
+        old_pyramid.allocate();
+        new_pyramid.allocate();
+
+        // Copy image data to tensors
+        library->fill(Accessor(old_image), old_image_name, format);
+        library->fill(Accessor(new_image), new_image_name, format);
+
+        // Compute gaussian pyramids
+        old_gaussian_pyramid_func.run();
+        new_gaussian_pyramid_func.run();
+    }
+
+    void run()
+    {
+        optical_flow_func.run();
+    }
+
+    void sync()
+    {
+        sync_if_necessary<TensorType>();
+    }
+
+    void teardown()
+    {
+        old_image.allocator()->free();
+        new_image.allocator()->free();
+    }
+
+private:
+    static const size_t max_keypoints = 10000;
+
+    std::vector<KeyPoint> old_keypoints{};
+    std::vector<KeyPoint> new_keypoints_estimates{};
+
+    TensorType old_image{};
+    TensorType new_image{};
+
+    ArrayType old_points{ max_keypoints };
+    ArrayType new_points{ max_keypoints };
+    ArrayType new_points_estimates{ max_keypoints };
+
+    PyramidType old_pyramid{};
+    PyramidType new_pyramid{};
+
+    PyramidFunctionType old_gaussian_pyramid_func{};
+    PyramidFunctionType new_gaussian_pyramid_func{};
+
+    Function optical_flow_func{};
+};
+} // namespace benchmark
+} // namespace test
+} // namespace arm_compute
+#endif /* ARM_COMPUTE_TEST_OPTICAL_FLOW_FIXTURE */