2using System.Collections.Generic;
16 Dictionary<int, MemoryInfo> m_rgItems =
new Dictionary<int, MemoryInfo>();
17 bool m_bEnableMemoryTrace =
false;
25 m_bEnableMemoryTrace = bEnableMemoryTrace;
37 public long AllocMemory(
long hKernel,
int nDeviceID,
long hMemory, ulong lSize,
bool bHalf)
39 MemoryInfo mi =
new MemoryInfo(hKernel, nDeviceID, hMemory, lSize, bHalf);
40 string strKey = mi.ToKey();
41 int nKeyHash = strKey.GetHashCode();
43 if (m_rgItems.ContainsKey(nKeyHash))
44 throw new Exception(
"Memory item '" + strKey +
"' already exists!");
46 m_rgItems.Add(nKeyHash, mi);
49 if (m_bEnableMemoryTrace)
62 public void FreeMemory(
long hKernel,
int nDeviceID,
long hMemory)
64 string strKey = MemoryInfo.ToKey(nDeviceID, hKernel, hMemory);
65 int nKeyHash = strKey.GetHashCode();
67 if (!m_rgItems.ContainsKey(nKeyHash))
68 throw new Exception(
"Memory item '" + strKey +
"' does not exist!");
70 m_rgItems.Remove(nKeyHash);
73 if (m_bEnableMemoryTrace)
83 get {
return m_bEnableMemoryTrace; }
84 set { m_bEnableMemoryTrace = value; }
96 foreach (KeyValuePair<int, MemoryInfo> kv
in m_rgItems)
98 ulong ulBase = (ulong)((typeof(T) == typeof(
float)) ? 4 : 8);
102 lMem += kv.Value.Size * ulBase;
140 public MemoryInfo(
long hKernel,
int nDeviceID,
long hMemory, ulong lSize,
bool bHalf)
143 m_nDeviceID = nDeviceID;
151 get {
return m_hKernel; }
156 get {
return m_nDeviceID; }
161 get {
return m_hMemory; }
166 get {
return m_lSize; }
171 get {
return m_bHalf; }
174 public string ToKey()
176 return ToKey(m_nDeviceID, m_hKernel, m_hMemory);
179 public static string ToKey(
int nDeviceID,
long hKernel,
long hMemory)
181 return nDeviceID.ToString() +
"_" + hKernel.ToString() +
"_" + hMemory.ToString();
184 public override string ToString()
186 return "ID:" + m_nDeviceID.ToString() +
" K:" + m_hKernel.ToString() +
" Mem:" + m_hMemory.ToString() +
" Size: " + m_lSize.ToString(
"N0");
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.