COMPMID-485: Memory Manager

Change-Id: Ib421b7622838f050038cd81e7426bb1413a7d6e6
Reviewed-on: http://mpd-gerrit.cambridge.arm.com/87376
Tested-by: Kaizen <jeremy.johnson+kaizengerrit@arm.com>
Reviewed-by: Anthony Barbier <anthony.barbier@arm.com>
diff --git a/arm_compute/runtime/CL/CLBufferAllocator.h b/arm_compute/runtime/CL/CLBufferAllocator.h
new file mode 100644
index 0000000..05b0363
--- /dev/null
+++ b/arm_compute/runtime/CL/CLBufferAllocator.h
@@ -0,0 +1,51 @@
+/*
+ * Copyright (c) 2017 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_CLBUFFERALLOCATOR_H__
+#define __ARM_COMPUTE_CLBUFFERALLOCATOR_H__
+
+#include "arm_compute/runtime/IAllocator.h"
+
+#include "arm_compute/core/CL/OpenCL.h"
+#include "arm_compute/runtime/CL/CLScheduler.h"
+
+#include <cstddef>
+
+namespace arm_compute
+{
+/** Default OpenCL cl buffer allocator implementation */
+class CLBufferAllocator : public IAllocator
+{
+public:
+    /** Default constructor */
+    explicit CLBufferAllocator(cl::Context context = CLScheduler::get().context());
+
+    // Inherited methods overridden:
+    void *allocate(size_t size, size_t alignment) override;
+    void free(void *ptr) override;
+
+private:
+    cl::Context _context;
+};
+} // arm_compute
+#endif /*__ARM_COMPUTE_CLBUFFERALLOCATOR_H__ */
diff --git a/arm_compute/runtime/CL/CLMemoryGroup.h b/arm_compute/runtime/CL/CLMemoryGroup.h
new file mode 100644
index 0000000..a6f3eb1
--- /dev/null
+++ b/arm_compute/runtime/CL/CLMemoryGroup.h
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2017 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_CLMEMORYGROUP_H__
+#define __ARM_COMPUTE_CLMEMORYGROUP_H__
+
+#include "arm_compute/runtime/MemoryGroupBase.h"
+
+#include "arm_compute/core/CL/OpenCL.h"
+#include "arm_compute/runtime/CL/CLTensor.h"
+
+namespace arm_compute
+{
+using CLMemoryGroup = MemoryGroupBase<CLTensor>;
+
+template <>
+inline void MemoryGroupBase<CLTensor>::associate_memory_group(CLTensor *obj)
+{
+    ARM_COMPUTE_ERROR_ON(obj == nullptr);
+    auto allocator = dynamic_cast<CLTensorAllocator *>(obj->allocator());
+    ARM_COMPUTE_ERROR_ON(allocator == nullptr);
+    allocator->set_associated_memory_group(this);
+}
+} // arm_compute
+#endif /*__ARM_COMPUTE_CLMEMORYGROUP_H__ */
diff --git a/arm_compute/runtime/CL/CLTensorAllocator.h b/arm_compute/runtime/CL/CLTensorAllocator.h
index ed371e0..682de17 100644
--- a/arm_compute/runtime/CL/CLTensorAllocator.h
+++ b/arm_compute/runtime/CL/CLTensorAllocator.h
@@ -24,19 +24,27 @@
 #ifndef __ARM_COMPUTE_CLTENSORALLOCATOR_H__
 #define __ARM_COMPUTE_CLTENSORALLOCATOR_H__
 
-#include "arm_compute/core/CL/OpenCL.h"
 #include "arm_compute/runtime/ITensorAllocator.h"
 
+#include "arm_compute/core/CL/OpenCL.h"
+
 #include <cstdint>
 
 namespace arm_compute
 {
+class CLTensor;
+template <typename>
+class MemoryGroupBase;
+using CLMemoryGroup = MemoryGroupBase<CLTensor>;
+
 /** Basic implementation of a CL memory tensor allocator. */
 class CLTensorAllocator : public ITensorAllocator
 {
 public:
     /** Default constructor. */
-    CLTensorAllocator();
+    CLTensorAllocator(CLTensor *owner = nullptr);
+    /** Default destructor */
+    ~CLTensorAllocator();
     /** Prevent instances of this class from being copied (As this class contains pointers). */
     CLTensorAllocator(const CLTensorAllocator &) = delete;
     /** Prevent instances of this class from being copy assigned (As this class contains pointers). */
@@ -45,8 +53,6 @@
     CLTensorAllocator(CLTensorAllocator &&) = default;
     /** Allow instances of this class to be moved */
     CLTensorAllocator &operator=(CLTensorAllocator &&) = default;
-    /** Default destructor */
-    ~CLTensorAllocator() = default;
 
     /** Interface to be implemented by the child class to return the pointer to the mapped data. */
     uint8_t *data();
@@ -85,6 +91,11 @@
      *
      */
     void free() override;
+    /** Associates the tensor with a memory group
+     *
+     * @param[in] associated_memory_group Memory group to associate the tensor with
+     */
+    void set_associated_memory_group(CLMemoryGroup *associated_memory_group);
 
 protected:
     /** Call map() on the OpenCL buffer.
@@ -96,8 +107,10 @@
     void unlock() override;
 
 private:
-    cl::Buffer _buffer;  /**< OpenCL buffer containing the tensor data. */
-    uint8_t   *_mapping; /**< Pointer to the CPU mapping of the OpenCL buffer. */
+    CLMemoryGroup *_associated_memory_group; /**< Registered memory manager */
+    cl::Buffer     _buffer;                  /**< OpenCL buffer containing the tensor data. */
+    uint8_t       *_mapping;                 /**< Pointer to the CPU mapping of the OpenCL buffer. */
+    CLTensor      *_owner;                   /**< Owner of the allocator */
 };
 }
 #endif /* __ARM_COMPUTE_CLTENSORALLOCATOR_H__ */
diff --git a/arm_compute/runtime/CL/functions/CLConvolutionLayer.h b/arm_compute/runtime/CL/functions/CLConvolutionLayer.h
index 2057b6f..cd1ea70 100644
--- a/arm_compute/runtime/CL/functions/CLConvolutionLayer.h
+++ b/arm_compute/runtime/CL/functions/CLConvolutionLayer.h
@@ -34,7 +34,11 @@
 #include "arm_compute/core/CL/kernels/CLIm2ColKernel.h"
 #include "arm_compute/core/CL/kernels/CLWeightsReshapeKernel.h"
 #include "arm_compute/core/Types.h"
+#include "arm_compute/runtime/CL/CLMemoryGroup.h"
 #include "arm_compute/runtime/CL/CLTensor.h"
+#include "arm_compute/runtime/IMemoryManager.h"
+
+#include <memory>
 
 namespace arm_compute
 {
@@ -48,7 +52,7 @@
 {
 public:
     /** Constructor */
-    CLConvolutionLayerReshapeWeights();
+    CLConvolutionLayerReshapeWeights(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
     /** Set the input and output tensors.
      *
      * @param[in]  weights      Weights tensor. Weights are 4D tensor with dimensions [kernel_x, kernel_y, IFM, OFM]. Data type supported: QS8/QS16/F16/F32.
@@ -62,6 +66,7 @@
     void run() override;
 
 private:
+    CLMemoryGroup            _memory_group;
     CLWeightsReshapeKernel   _weights_reshape_kernel;
     CLGEMMTranspose1xWKernel _weights_transposed_kernel;
     CLTensor                 _weights_reshaped;
@@ -81,7 +86,7 @@
 {
 public:
     /** Default constructor */
-    CLConvolutionLayer();
+    CLConvolutionLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
     /** Set the input and output tensors.
      *
      * @param[in]  input        Source tensor. 3 lower dimensions represent a single input [width, height, IFM],
@@ -101,6 +106,7 @@
     void run() override;
 
 private:
+    CLMemoryGroup                    _memory_group;
     CLConvolutionLayerReshapeWeights _reshape_weights;
     CLIm2ColKernel                   _input_im2col_kernel;
     CLGEMMInterleave4x4Kernel        _input_interleave_kernel;
diff --git a/arm_compute/runtime/CL/functions/CLFullyConnectedLayer.h b/arm_compute/runtime/CL/functions/CLFullyConnectedLayer.h
index e076f51..f71e2a3 100644
--- a/arm_compute/runtime/CL/functions/CLFullyConnectedLayer.h
+++ b/arm_compute/runtime/CL/functions/CLFullyConnectedLayer.h
@@ -30,6 +30,7 @@
 #include "arm_compute/core/CL/kernels/CLGEMMMatrixMultiplyKernel.h"
 #include "arm_compute/core/CL/kernels/CLIm2ColKernel.h"
 #include "arm_compute/core/CL/kernels/CLTransposeKernel.h"
+#include "arm_compute/runtime/CL/CLMemoryGroup.h"
 #include "arm_compute/runtime/CL/CLTensor.h"
 
 namespace arm_compute
@@ -64,7 +65,7 @@
 {
 public:
     /** Constructor */
-    CLFullyConnectedLayer();
+    CLFullyConnectedLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
     /** Set the input and output tensors.
      *
      * @param[in]  input                Source tensor. Data type supported: QS8/QS16/F16/F32.
@@ -83,6 +84,7 @@
     void configure_fc_fc(const ICLTensor *input, const ICLTensor *weights, ICLTensor *output);
     void configure_conv_fc(const ICLTensor *input, const ICLTensor *weights, ICLTensor *output);
 
+    CLMemoryGroup                       _memory_group;
     CLIm2ColKernel                      _im2col_kernel;
     CLFullyConnectedLayerReshapeWeights _reshape_weights_kernel;
     CLGEMMMatrixMultiplyKernel          _mm_kernel;
diff --git a/arm_compute/runtime/CL/functions/CLSoftmaxLayer.h b/arm_compute/runtime/CL/functions/CLSoftmaxLayer.h
index 18f7a02..70a265c 100644
--- a/arm_compute/runtime/CL/functions/CLSoftmaxLayer.h
+++ b/arm_compute/runtime/CL/functions/CLSoftmaxLayer.h
@@ -25,8 +25,12 @@
 #define __ARM_COMPUTE_CLSOFTMAXLAYER_H__
 
 #include "arm_compute/core/CL/kernels/CLSoftmaxLayerKernel.h"
+#include "arm_compute/runtime/CL/CLMemoryGroup.h"
 #include "arm_compute/runtime/CL/CLTensor.h"
 #include "arm_compute/runtime/IFunction.h"
+#include "arm_compute/runtime/IMemoryManager.h"
+
+#include <memory>
 
 namespace arm_compute
 {
@@ -46,7 +50,7 @@
 {
 public:
     /** Constructor */
-    CLSoftmaxLayer();
+    CLSoftmaxLayer(std::shared_ptr<IMemoryManager> memory_manager = nullptr);
     /** Set the input and output tensors.
      *
      * @param[in]  input  Source tensor. Data types supported: QS8/QS16/F16/F32
@@ -58,6 +62,7 @@
     void run() override;
 
 private:
+    CLMemoryGroup               _memory_group;
     CLLogits1DMaxKernel         _max_kernel;
     CLLogits1DShiftExpSumKernel _shift_exp_sum_kernel;
     CLLogits1DNormKernel        _norm_kernel;