blob: e3ffd188a1074465edfed39967e2471c822e8fda [file] [log] [blame]
Laurent Carlier749294b2020-06-01 09:03:17 +01001//
telsoa01c577f2c2018-08-31 09:22:23 +01002// Copyright © 2017 Arm Ltd. All rights reserved.
David Beckecb56cd2018-09-05 12:52:57 +01003// SPDX-License-Identifier: MIT
telsoa01c577f2c2018-08-31 09:22:23 +01004//
5#pragma once
6
Matteo Martincighe5b8eb92019-11-28 15:45:42 +00007#include <armnn/backends/IMemoryManager.hpp>
Aron Virginas-Tarc9cc8042018-11-01 16:15:57 +00008#include <backendsCommon/WorkloadFactory.hpp>
telsoa01c577f2c2018-08-31 09:22:23 +01009
Narumol Prangnawarat680f9912019-10-01 11:32:10 +010010#if defined(ARMCOMPUTENEON_ENABLED) || defined(ARMCOMPUTECL_ENABLED)
David Beckac42efd2018-09-26 17:41:13 +010011#include <arm_compute/runtime/MemoryGroup.h>
telsoa01c577f2c2018-08-31 09:22:23 +010012#endif
13
telsoa01c577f2c2018-08-31 09:22:23 +010014#if defined(ARMCOMPUTENEON_ENABLED) || defined(ARMCOMPUTECL_ENABLED)
David Beckac42efd2018-09-26 17:41:13 +010015#include <arm_compute/runtime/IAllocator.h>
16#include <arm_compute/runtime/IMemoryGroup.h>
17#include <arm_compute/runtime/MemoryManagerOnDemand.h>
Jan Eilersc1c872f2021-07-22 13:17:04 +010018#include <arm_compute/runtime/CL/CLTensorAllocator.h>
telsoa01c577f2c2018-08-31 09:22:23 +010019#endif
20
21namespace armnn
22{
23
Aron Virginas-Tar56055192018-11-12 18:10:43 +000024class BaseMemoryManager : public IMemoryManager
telsoa01c577f2c2018-08-31 09:22:23 +010025{
26public:
27 enum class MemoryAffinity
28 {
29 Buffer,
30 Offset
31 };
32
33 BaseMemoryManager() { }
34 virtual ~BaseMemoryManager() { }
35
Aron Virginas-Tar56055192018-11-12 18:10:43 +000036 void Acquire() override;
37 void Release() override;
38
telsoa01c577f2c2018-08-31 09:22:23 +010039#if defined(ARMCOMPUTENEON_ENABLED) || defined(ARMCOMPUTECL_ENABLED)
Jan Eilersc1c872f2021-07-22 13:17:04 +010040 BaseMemoryManager(std::shared_ptr<arm_compute::IAllocator> alloc, MemoryAffinity memoryAffinity);
telsoa01c577f2c2018-08-31 09:22:23 +010041
42 std::shared_ptr<arm_compute::MemoryManagerOnDemand>& GetIntraLayerManager() { return m_IntraLayerMemoryMgr; }
43 std::shared_ptr<arm_compute::MemoryManagerOnDemand>& GetInterLayerManager() { return m_InterLayerMemoryMgr; }
44 std::shared_ptr<arm_compute::IMemoryGroup>& GetInterLayerMemoryGroup() { return m_InterLayerMemoryGroup; }
45
telsoa01c577f2c2018-08-31 09:22:23 +010046protected:
Jan Eilersc1c872f2021-07-22 13:17:04 +010047 std::shared_ptr<arm_compute::IAllocator> m_Allocator;
telsoa01c577f2c2018-08-31 09:22:23 +010048 std::shared_ptr<arm_compute::MemoryManagerOnDemand> m_IntraLayerMemoryMgr;
49 std::shared_ptr<arm_compute::MemoryManagerOnDemand> m_InterLayerMemoryMgr;
50 std::shared_ptr<arm_compute::IMemoryGroup> m_InterLayerMemoryGroup;
51
52 std::shared_ptr<arm_compute::MemoryManagerOnDemand> CreateArmComputeMemoryManager(MemoryAffinity memoryAffinity);
53
54 virtual std::shared_ptr<arm_compute::IMemoryGroup>
55 CreateMemoryGroup(const std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager) = 0;
telsoa01c577f2c2018-08-31 09:22:23 +010056#endif
57};
58
Matteo Martincighd95e9062019-01-31 15:35:59 +000059#if defined(ARMCOMPUTENEON_ENABLED)
telsoa01c577f2c2018-08-31 09:22:23 +010060class NeonMemoryManager : public BaseMemoryManager
61{
62public:
63 NeonMemoryManager() {}
64 virtual ~NeonMemoryManager() {}
65
telsoa01c577f2c2018-08-31 09:22:23 +010066 NeonMemoryManager(std::unique_ptr<arm_compute::IAllocator> alloc, MemoryAffinity memoryAffinity)
67 : BaseMemoryManager(std::move(alloc), memoryAffinity)
68 {
69 m_InterLayerMemoryGroup = CreateMemoryGroup(m_InterLayerMemoryMgr);
70 }
71
72protected:
Matteo Martincighd95e9062019-01-31 15:35:59 +000073 std::shared_ptr<arm_compute::IMemoryGroup>
telsoa01c577f2c2018-08-31 09:22:23 +010074 CreateMemoryGroup(const std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager) override;
telsoa01c577f2c2018-08-31 09:22:23 +010075};
Matteo Martincighd95e9062019-01-31 15:35:59 +000076#endif
telsoa01c577f2c2018-08-31 09:22:23 +010077
Matteo Martincighd95e9062019-01-31 15:35:59 +000078#if defined(ARMCOMPUTECL_ENABLED)
telsoa01c577f2c2018-08-31 09:22:23 +010079class ClMemoryManager : public BaseMemoryManager
80{
81public:
82 ClMemoryManager() {}
83 virtual ~ClMemoryManager() {}
84
Jan Eilersc1c872f2021-07-22 13:17:04 +010085 ClMemoryManager(std::shared_ptr<arm_compute::IAllocator> alloc)
telsoa01c577f2c2018-08-31 09:22:23 +010086 : BaseMemoryManager(std::move(alloc), MemoryAffinity::Buffer)
87 {
Jan Eilersc1c872f2021-07-22 13:17:04 +010088 arm_compute::CLTensorAllocator::set_global_allocator(alloc.get());
telsoa01c577f2c2018-08-31 09:22:23 +010089 m_InterLayerMemoryGroup = CreateMemoryGroup(m_InterLayerMemoryMgr);
90 }
91
92protected:
Matteo Martincighd95e9062019-01-31 15:35:59 +000093 std::shared_ptr<arm_compute::IMemoryGroup>
telsoa01c577f2c2018-08-31 09:22:23 +010094 CreateMemoryGroup(const std::shared_ptr<arm_compute::MemoryManagerOnDemand>& memoryManager) override;
telsoa01c577f2c2018-08-31 09:22:23 +010095};
Matteo Martincighd95e9062019-01-31 15:35:59 +000096#endif
telsoa01c577f2c2018-08-31 09:22:23 +010097
Aron Virginas-Tarf9aeef02018-10-12 15:18:03 +010098} //namespace armnn