COMPMID-838 Implement CLPermute

Change-Id: I6d97b649f1ebc289c9e6f8949e67740a6b3cbcb2
Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/116636
Tested-by: Jenkins <bsgcomp@arm.com>
Reviewed-by: Pablo Tello <pablo.tello@arm.com>
Reviewed-by: Anthony Barbier <anthony.barbier@arm.com>
diff --git a/arm_compute/core/CL/CLKernels.h b/arm_compute/core/CL/CLKernels.h
index 64687fb..e9a1fde 100644
--- a/arm_compute/core/CL/CLKernels.h
+++ b/arm_compute/core/CL/CLKernels.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, 2018 ARM Limited.
+ * Copyright (c) 2016-2018 ARM Limited.
  *
  * SPDX-License-Identifier: MIT
  *
@@ -88,6 +88,7 @@
 #include "arm_compute/core/CL/kernels/CLNonLinearFilterKernel.h"
 #include "arm_compute/core/CL/kernels/CLNonMaximaSuppression3x3Kernel.h"
 #include "arm_compute/core/CL/kernels/CLNormalizationLayerKernel.h"
+#include "arm_compute/core/CL/kernels/CLPermuteKernel.h"
 #include "arm_compute/core/CL/kernels/CLPixelWiseMultiplicationKernel.h"
 #include "arm_compute/core/CL/kernels/CLPoolingLayerKernel.h"
 #include "arm_compute/core/CL/kernels/CLQuantizationLayerKernel.h"
diff --git a/arm_compute/core/CL/kernels/CLPermuteKernel.h b/arm_compute/core/CL/kernels/CLPermuteKernel.h
new file mode 100644
index 0000000..8f96529
--- /dev/null
+++ b/arm_compute/core/CL/kernels/CLPermuteKernel.h
@@ -0,0 +1,67 @@
+/*
+ * 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_CLPERMUTEKERNEL_H__
+#define __ARM_COMPUTE_CLPERMUTEKERNEL_H__
+
+#include "arm_compute/core/CL/ICLKernel.h"
+
+namespace arm_compute
+{
+class ICLTensor;
+
+/** OpenCL kernel to perform tensor permutation.
+ *
+ * Permutes given a permutation vector
+ */
+class CLPermuteKernel : public ICLKernel
+{
+public:
+    /** Default constructor */
+    CLPermuteKernel();
+    /** Prevent instances of this class from being copied (As this class contains pointers) */
+    CLPermuteKernel(const CLPermuteKernel &) = delete;
+    /** Prevent instances of this class from being copied (As this class contains pointers) */
+    CLPermuteKernel &operator=(const CLPermuteKernel &) = delete;
+    /** Allow instances of this class to be moved */
+    CLPermuteKernel(CLPermuteKernel &&) = default;
+    /** Allow instances of this class to be moved */
+    CLPermuteKernel &operator=(CLPermuteKernel &&) = default;
+    /** Set the input and output of the kernel.
+     *
+     * @param[in] input  The input tensor to permute. Data types supported: U8/S8/QS8/QASYMM8/U16/S16/QS16/F16/U32/S32/F32
+     * @param[in] output The output tensor. Data types supported: Same as @p input
+     * @param[in] perm   Permutation vector
+     */
+    void configure(const ICLTensor *input, ICLTensor *output, const PermutationVector &perm);
+
+    // Inherited methods overridden:
+    void run(const Window &window, cl::CommandQueue &queue) override;
+
+private:
+    const ICLTensor *_input;
+    ICLTensor        *_output;
+    PermutationVector _perm;
+};
+} // arm_compute
+#endif /*__ARM_COMPUTE_CLPERMUTEKERNEL_H__ */
diff --git a/arm_compute/core/Window.h b/arm_compute/core/Window.h
index 654f5ed..c890bf8 100644
--- a/arm_compute/core/Window.h
+++ b/arm_compute/core/Window.h
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, 2017 ARM Limited.
+ * Copyright (c) 2016-2018 ARM Limited.
  *
  * SPDX-License-Identifier: MIT
  *
@@ -245,6 +245,14 @@
     {
         return first_slice_window<3>();
     };
+    /** First 4D slice of the window
+     *
+     * @return The first slice of the window.
+     */
+    Window first_slice_window_4D() const
+    {
+        return first_slice_window<4>();
+    };
     /** Slide the passed 1D window slice.
      *
      * If slice contains the last slice then it will remain unchanged and false will be returned.
@@ -305,6 +313,17 @@
      */
     Window collapse_if_possible(const Window &full_window, size_t first) const;
 
+    /* Collapse the dimensions higher than @p first.
+     *
+     * A dimension is collapsable if it starts from 0 and matches the corresponding dimension in the full_window
+     *
+     * @param[in] full_window Full window @p window has been created from.
+     * @param[in] first       Dimensions into which the following are collapsed.
+     *
+     * @return Collapsed window if successful.
+     */
+    Window collapse(const Window &full_window, size_t first) const;
+
 private:
     /** First slice of the window
      *
diff --git a/arm_compute/core/Window.inl b/arm_compute/core/Window.inl
index e46a0ec..1b21820 100644
--- a/arm_compute/core/Window.inl
+++ b/arm_compute/core/Window.inl
@@ -72,6 +72,22 @@
     return collapsed;
 }
 
+inline Window Window::collapse(const Window &full_window, size_t first) const
+{
+    Window collapsed = collapse_if_possible(full_window, first);
+    // Make sure that the window has collapsed
+    int end   = _dims[first].end();
+    int start = 0;
+    ARM_COMPUTE_UNUSED(start);
+    for(size_t d = first + 1; d < Coordinates::num_max_dimensions; ++d)
+    {
+        start = end * _dims[d].start();
+        end *= _dims[d].end();
+    }
+    ARM_COMPUTE_ERROR_ON((collapsed[first].end() != end) || (collapsed[first].start() != start));
+    return collapsed;
+}
+
 inline void Window::shift(size_t dimension, int shift_value)
 {
     ARM_COMPUTE_ERROR_ON(dimension >= Coordinates::num_max_dimensions);