MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
CudaDnnMemoryTracker.cs
1using System;
2using System.Collections.Generic;
3using System.Diagnostics;
4using System.Linq;
5using System.Text;
6
7namespace MyCaffe.common
8{
14 public class CudaDnnMemoryTracker<T>
15 {
16 Dictionary<int, MemoryInfo> m_rgItems = new Dictionary<int, MemoryInfo>();
17 bool m_bEnableMemoryTrace = false;
18
23 public CudaDnnMemoryTracker(bool bEnableMemoryTrace = false)
24 {
25 m_bEnableMemoryTrace = bEnableMemoryTrace;
26 }
27
37 public long AllocMemory(long hKernel, int nDeviceID, long hMemory, ulong lSize, bool bHalf)
38 {
39 MemoryInfo mi = new MemoryInfo(hKernel, nDeviceID, hMemory, lSize, bHalf);
40 string strKey = mi.ToKey();
41 int nKeyHash = strKey.GetHashCode();
42
43 if (m_rgItems.ContainsKey(nKeyHash))
44 throw new Exception("Memory item '" + strKey + "' already exists!");
45
46 m_rgItems.Add(nKeyHash, mi);
47
48#if DEBUG
49 if (m_bEnableMemoryTrace)
50 Trace.WriteLine("Memory Used: " + TotalMemoryUsedText);
51#endif
52
53 return hMemory;
54 }
55
62 public void FreeMemory(long hKernel, int nDeviceID, long hMemory)
63 {
64 string strKey = MemoryInfo.ToKey(nDeviceID, hKernel, hMemory);
65 int nKeyHash = strKey.GetHashCode();
66
67 if (!m_rgItems.ContainsKey(nKeyHash))
68 throw new Exception("Memory item '" + strKey + "' does not exist!");
69
70 m_rgItems.Remove(nKeyHash);
71
72#if DEBUG
73 if (m_bEnableMemoryTrace)
74 Trace.WriteLine("Memory Used: " + TotalMemoryUsedText);
75#endif
76 }
77
82 {
83 get { return m_bEnableMemoryTrace; }
84 set { m_bEnableMemoryTrace = value; }
85 }
86
91 {
92 get
93 {
94 ulong lMem = 0;
95
96 foreach (KeyValuePair<int, MemoryInfo> kv in m_rgItems)
97 {
98 ulong ulBase = (ulong)((typeof(T) == typeof(float)) ? 4 : 8);
99 if (kv.Value.Half)
100 ulBase = 2;
101
102 lMem += kv.Value.Size * ulBase;
103 }
104
105 return lMem;
106 }
107 }
108
112 public ulong TotalMemoryUsed
113 {
114 get
115 {
116 return TotalItemsAllocated;
117 }
118 }
119
124 {
125 get
126 {
127 return (TotalMemoryUsed / 1000000).ToString("N0") + " MB";
128 }
129 }
130 }
131
132 class MemoryInfo
133 {
134 long m_hKernel;
135 int m_nDeviceID;
136 long m_hMemory;
137 ulong m_lSize;
138 bool m_bHalf;
139
140 public MemoryInfo(long hKernel, int nDeviceID, long hMemory, ulong lSize, bool bHalf)
141 {
142 m_hKernel = hKernel;
143 m_nDeviceID = nDeviceID;
144 m_hMemory = hMemory;
145 m_lSize = lSize;
146 m_bHalf = bHalf;
147 }
148
149 public long Kernel
150 {
151 get { return m_hKernel; }
152 }
153
154 public int DeviceID
155 {
156 get { return m_nDeviceID; }
157 }
158
159 public long Memory
160 {
161 get { return m_hMemory; }
162 }
163
164 public ulong Size
165 {
166 get { return m_lSize; }
167 }
168
169 public bool Half
170 {
171 get { return m_bHalf; }
172 }
173
174 public string ToKey()
175 {
176 return ToKey(m_nDeviceID, m_hKernel, m_hMemory);
177 }
178
179 public static string ToKey(int nDeviceID, long hKernel, long hMemory)
180 {
181 return nDeviceID.ToString() + "_" + hKernel.ToString() + "_" + hMemory.ToString();
182 }
183
184 public override string ToString()
185 {
186 return "ID:" + m_nDeviceID.ToString() + " K:" + m_hKernel.ToString() + " Mem:" + m_hMemory.ToString() + " Size: " + m_lSize.ToString("N0");
187 }
188 }
189}
The CudaDnnMemoryTracker is used for diagnostics in that it helps estimate the amount of memory that ...
void FreeMemory(long hKernel, int nDeviceID, long hMemory)
Simulate a memory free.
CudaDnnMemoryTracker(bool bEnableMemoryTrace=false)
The CudaDnnMemoryTracker constructor.
ulong? TotalItemsAllocated
Returns the total number of items allocated.
bool EnableMemoryTrace
Enable/disable the memory trace - this feature is only available in debug builds.
string TotalMemoryUsedText
Returns a text string describing the total amount of memory used (in bytes).
ulong TotalMemoryUsed
Returns the total amount of memory used (in bytes).
long AllocMemory(long hKernel, int nDeviceID, long hMemory, ulong lSize, bool bHalf)
Simulate a memory allocation.
The MyCaffe.common namespace contains common MyCaffe classes.
Definition: BatchInput.cs:8