MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
HostBuffer.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using System.Threading.Tasks;
6
7namespace MyCaffe.common
8{
13 public class HostBuffer<T> : IDisposable
14 {
15 CudaDnn<T> m_cuda;
16 long m_hBuffer;
17 long m_lCapacity;
18
24 {
25 m_cuda = cuda;
26 m_hBuffer = 0;
27 m_lCapacity = 0;
28 }
29
33 public void Dispose()
34 {
35 Free();
36 }
37
41 public long Handle
42 {
43 get { return m_hBuffer; }
44 }
45
49 public long Capacity
50 {
51 get { return m_lCapacity; }
52 }
53
57 public void Free()
58 {
59 if (m_hBuffer != 0)
60 {
61 m_cuda.FreeHostBuffer(m_hBuffer);
62 m_hBuffer = 0;
63 m_lCapacity = 0;
64 }
65 }
66
72 public void CopyFrom(Blob<T> b, bool bFromDiff = false)
73 {
74 CopyFromGpu(b.count(), (bFromDiff) ? b.gpu_diff : b.gpu_data);
75 }
76
82 public void CopyTo(Blob<T> b, bool bToDiff = false)
83 {
84 CopyToGpu(b.count(), (bToDiff) ? b.mutable_gpu_diff : b.mutable_gpu_data);
85 }
86
92 public void CopyFromGpu(int nCount, long hGpu)
93 {
94 if (nCount > m_lCapacity)
95 {
96 Free();
97 m_hBuffer = m_cuda.AllocHostBuffer(nCount);
98 m_lCapacity = nCount;
99 }
100
101 m_cuda.CopyDeviceToHost(nCount, hGpu, m_hBuffer);
102 }
103
109 public void CopyToGpu(int nCount, long hGpu)
110 {
111 m_cuda.CopyHostToDevice(nCount, m_hBuffer, hGpu);
112 }
113
118 public T[] GetHostData()
119 {
120 return m_cuda.GetHostMemory(m_hBuffer);
121 }
122
127 public double[] GetHostDataAsDouble()
128 {
129 return m_cuda.GetHostMemoryDouble(m_hBuffer);
130 }
131
136 public float[] GetHostDataAsFloat()
137 {
138 return m_cuda.GetHostMemoryFloat(m_hBuffer);
139 }
140
145 public void SetHostData(T[] rgSrc)
146 {
147 if (m_lCapacity < rgSrc.Length)
148 {
149 Free();
150 m_hBuffer = m_cuda.AllocHostBuffer(rgSrc.Length);
151 }
152
153 m_cuda.SetHostMemory(m_hBuffer, rgSrc);
154 }
155 }
156}
The Blob is the main holder of data that moves through the Layers of the Net.
Definition: Blob.cs:25
long mutable_gpu_diff
Returns the diff GPU handle used by the CudaDnn connection.
Definition: Blob.cs:1555
long mutable_gpu_data
Returns the data GPU handle used by the CudaDnn connection.
Definition: Blob.cs:1487
int count()
Returns the total number of items in the Blob.
Definition: Blob.cs:739
long gpu_diff
Returns the diff GPU handle used by the CudaDnn connection.
Definition: Blob.cs:1541
long gpu_data
Returns the data GPU handle used by the CudaDnn connection.
Definition: Blob.cs:1479
The CudaDnn object is the main interface to the Low-Level Cuda C++ DLL.
Definition: CudaDnn.cs:969
The HostBuffer helps manage host memory, often used when implementing CPU versions of a function or l...
Definition: HostBuffer.cs:14
void Free()
Free the host buffer.
Definition: HostBuffer.cs:57
double[] GetHostDataAsDouble()
Returns the host buffer data as an array of doubles.
Definition: HostBuffer.cs:127
long Capacity
Returns the capacity of the host buffer.
Definition: HostBuffer.cs:50
T[] GetHostData()
Returns the host buffer data as an array of the base type.
Definition: HostBuffer.cs:118
void SetHostData(T[] rgSrc)
Set the host buffer data, making to expand the capcity if needed.
Definition: HostBuffer.cs:145
HostBuffer(CudaDnn< T > cuda)
The constructor.
Definition: HostBuffer.cs:23
float[] GetHostDataAsFloat()
Returns the host buffer data as an array of floats.
Definition: HostBuffer.cs:136
void Dispose()
Release all resources used.
Definition: HostBuffer.cs:33
void CopyToGpu(int nCount, long hGpu)
Copy data from the host buffer into the GPU memory.
Definition: HostBuffer.cs:109
void CopyFrom(Blob< T > b, bool bFromDiff=false)
Copy the gpu data from the blob to the host buffer.
Definition: HostBuffer.cs:72
void CopyFromGpu(int nCount, long hGpu)
Copy data from the GPU into the host buffer making sure to grow the host buffer capacity if needed.
Definition: HostBuffer.cs:92
long Handle
Returns a handle to the host buffer.
Definition: HostBuffer.cs:42
void CopyTo(Blob< T > b, bool bToDiff=false)
Copy from the host buffer to the gpu data of the blob.
Definition: HostBuffer.cs:82
The MyCaffe.common namespace contains common MyCaffe classes.
Definition: BatchInput.cs:8