ArmNN
 21.08
WorkingMemHandle.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2021 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #pragma once
7 
8 #include "Layer.hpp"
9 #include "Network.hpp"
10 #include "WorkingMemDescriptor.hpp"
11 
13 #include <armnn/Tensor.hpp>
14 
15 #include <unordered_map>
16 #include <mutex>
17 
18 namespace armnn
19 {
20 
21 namespace experimental
22 {
23 
24 class WorkingMemHandle final : public IWorkingMemHandle
25 {
26 
27 public:
28  WorkingMemHandle(NetworkId networkId,
29  std::vector<WorkingMemDescriptor> workingMemDescriptors,
30  std::unordered_map<LayerGuid, WorkingMemDescriptor> workingMemDescriptorMap,
31  std::vector<std::shared_ptr<IMemoryManager>> memoryManagers,
32  std::unordered_map<LayerGuid, std::vector<std::unique_ptr<ITensorHandle> > > ownedTensorHandles);
33 
35  { Free(); }
36 
38  {
39  return m_NetworkId;
40  }
41 
42 
43 
44  /// Allocate the backing memory required for execution. If this is not called, then allocation will be
45  /// deferred to execution time. The mutex must be locked.
46  void Allocate() override;
47 
48  /// Free the backing memory required for execution. The mutex must be locked.
49  void Free() override;
50 
51  /// IsAllocated returns true if the backing memory is currently allocated. The mutex must be locked.
52  bool IsAllocated() override
53  {
54  return m_IsAllocated;
55  }
56 
57  /// Get a mutex which can be used for synchronizing access to the WorkingMemHandle object.
58  std::mutex& GetMutex() override
59  {
60  return m_Mutex;
61  }
62 
63  /// Get the WorkingMemDescriptor for a Layer. The mutex must be locked.
65  {
66  auto result = m_WorkingMemDescriptorMap.find(id);
67  ARMNN_ASSERT(result != m_WorkingMemDescriptorMap.end());
68  return result->second;
69  }
70 
71  /// Get the WorkingMemDescriptor at an index. The WorkingMemDescriptors are stored in the same order as
72  /// the Workloads in a topologically sorted graph. The mutex must be locked.
74  {
75  return m_WorkingMemDescriptors[id];
76  }
77 
78 private:
79  NetworkId m_NetworkId;
80  std::shared_ptr<ProfilerImpl> m_Profiler;
81 
82  std::vector<WorkingMemDescriptor> m_WorkingMemDescriptors;
83  std::unordered_map<LayerGuid, WorkingMemDescriptor> m_WorkingMemDescriptorMap;
84 
85  // Vector of IMemoryManagers that manage the WorkingMemHandle's memory
86  std::vector<std::shared_ptr<IMemoryManager>> m_MemoryManagers;
87  // TensorHandles owned by this WorkingMemHandle
88  // constant tensor's can be shared by multiple WorkingMemHandles and so will not be stored here
89  std::unordered_map<LayerGuid, std::vector<std::unique_ptr<ITensorHandle> > > m_OwnedTensorHandles;
90 
91  bool m_IsAllocated;
92  std::mutex m_Mutex;
93 };
94 
95 } // end experimental namespace
96 
97 } // end armnn namespace
WorkingMemDescriptor & GetWorkingMemDescriptorAt(unsigned int id) override
Get the WorkingMemDescriptor at an index.
Copyright (c) 2021 ARM Limited and Contributors.
NetworkId GetNetworkId() override
Returns the NetworkId of the Network that this IWorkingMemHandle works with.
WorkingMemDescriptor & GetWorkingMemDescriptor(LayerGuid id) override
Get the WorkingMemDescriptor for a Layer. The mutex must be locked.
int NetworkId
Definition: IRuntime.hpp:24
#define ARMNN_ASSERT(COND)
Definition: Assert.hpp:14
void Allocate() override
Allocate the backing memory required for execution.
std::mutex & GetMutex() override
Get a mutex which can be used for synchronizing access to the WorkingMemHandle object.
bool IsAllocated() override
IsAllocated returns true if the backing memory is currently allocated. The mutex must be locked...
profiling::ProfilingGuid LayerGuid
Define LayerGuid type.
Definition: Types.hpp:313
WorkingMemHandle(NetworkId networkId, std::vector< WorkingMemDescriptor > workingMemDescriptors, std::unordered_map< LayerGuid, WorkingMemDescriptor > workingMemDescriptorMap, std::vector< std::shared_ptr< IMemoryManager >> memoryManagers, std::unordered_map< LayerGuid, std::vector< std::unique_ptr< ITensorHandle > > > ownedTensorHandles)
void Free() override
Free the backing memory required for execution. The mutex must be locked.