-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCGpuBuffer.cpp
executable file
·183 lines (173 loc) · 5.03 KB
/
CGpuBuffer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "CMainInc.h"
#include <Util/Util_Time.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <memory.h>
#include <sys/types.h>
#include <errno.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <nvToolsExt.h>
using namespace MotionCor2;
CGpuBuffer::CGpuBuffer(void)
{
m_pvGpuFrames = 0L;
m_ppvCpuFrames = 0L;
m_iNumFrames = 0; // <= m_iMaxAllFrms;
m_iNumGpuFrames = 0; // <= m_iMaxGpuFrms;
m_iGpuID = -1; // use pinned cpu memory
//-----------------
m_iMaxGpuFrms = 0;
m_iMaxCpuFrms = 0;
}
CGpuBuffer::~CGpuBuffer(void)
{
this->Clean();
}
void CGpuBuffer::Clean(void)
{
if(m_pvGpuFrames == 0L && m_ppvCpuFrames == 0L) return;
cudaSetDevice(m_iGpuID);
cudaDeviceSynchronize();
//----------------------
if(m_pvGpuFrames != 0L)
{ cudaFree(m_pvGpuFrames);
m_pvGpuFrames = 0L;
}
if(m_ppvCpuFrames != 0L)
{ for(int i=0; i<m_iMaxCpuFrms; i++)
{ cudaFreeHost(m_ppvCpuFrames[i]);
}
delete[] m_ppvCpuFrames;
m_ppvCpuFrames = 0L;
}
//-----------------
m_iNumGpuFrames = 0;
m_iNumFrames = 0;
m_iMaxGpuFrms = 0;
m_iMaxCpuFrms = 0;
}
void CGpuBuffer::Create(size_t tFrmBytes, int iNumFrames, int iGpuID)
{
this->Clean();
//------------
m_tFmBytes = tFrmBytes;
m_iNumFrames = iNumFrames;
m_iGpuID = iGpuID;
if(m_iNumFrames <= 0) return;
//---------------------------
cudaSetDevice(m_iGpuID);
mCalcGpuFrames();
//---------------
nvtxRangePushA("CGpuBuffer: allocate GPU memory");
Util_Time utilTime;
float afTimes[2] = {0.0f}, afGBs[2] = {0.0f};
size_t tBytes = m_iNumGpuFrames * m_tFmBytes;
afGBs[0] = (float)(tBytes / (1024.0 * 1024.0 * 1024.0));
if(tBytes > 0)
{ utilTime.Measure();
cudaMalloc(&m_pvGpuFrames, tBytes);
afTimes[0] = utilTime.GetElapsedSeconds();
}
nvtxRangePop();
if(m_iNumGpuFrames >= m_iNumFrames)
{ mPrintAllocTimes(afGBs, afTimes);
return;
}
//-----------------
nvtxRangePushA("CGpuBuffer: allocate pinned memory");
utilTime.Measure();
int iCpuFrms = m_iNumFrames - m_iNumGpuFrames;
mCreateCpuBuf(iCpuFrms);
afTimes[1] = utilTime.GetElapsedSeconds();
nvtxRangePop();
//-----------------
tBytes = m_iMaxCpuFrms * m_tFmBytes;
afGBs[1] = (float)(tBytes / (1024.0f * 1024.0 * 1024.0));
mPrintAllocTimes(afGBs, afTimes);
}
void CGpuBuffer::AdjustBuffer(int iNumFrames)
{
if(iNumFrames == m_iNumFrames) return;
//-----------------------------------------------------------
// If the new stack can be fit entirely int the existing
// GPU frame buffer, then simply update GPU frame numbers.
//-----------------------------------------------------------
if(iNumFrames <= m_iMaxGpuFrms)
{ m_iNumGpuFrames = iNumFrames;
m_iNumFrames = iNumFrames;
return;
}
//----------------------------------------------------------
// 1. In this case, use all GPU frame buffer. The left over
// is placed into the pinned frame buffer.
// 2. mCreateCpuBuf can create or expand the pinned buffer
// as needed.
//----------------------------------------------------------
m_iNumGpuFrames = m_iMaxGpuFrms;
int iCpuFrms = iNumFrames - m_iNumGpuFrames;
mCreateCpuBuf(iCpuFrms);
m_iNumFrames = iNumFrames;
}
void* CGpuBuffer::GetFrame(int iFrame)
{
if(m_pvGpuFrames == 0L && m_ppvCpuFrames == 0L) return 0L;
if(iFrame < 0 || iFrame >= m_iNumFrames) return 0L;
//-------------------------------------------------
if(iFrame < m_iNumGpuFrames)
{ char* gcFrames = reinterpret_cast<char*>(m_pvGpuFrames);
void* gvFrame = gcFrames + iFrame * m_tFmBytes;
return gvFrame;
}
else
{ return m_ppvCpuFrames[iFrame - m_iNumGpuFrames];
}
}
void CGpuBuffer::mCalcGpuFrames(void)
{
cudaSetDevice(m_iGpuID);
size_t tTotal = 0, tFree = 0;
cudaMemGetInfo(&tFree, &tTotal);
//------------------------------
CInput* pInput = CInput::GetInstance();
float fReserve = 1.0f - pInput->m_fGpuMemUsage;
if(fReserve < 0.1f) fReserve = 0.1f;
//----------------------------------
size_t t3GB = 1024 * 1024 * (size_t)(1024 * 3);
size_t tReserve = (size_t)(fReserve * tTotal);
if(tReserve < t3GB) tReserve = t3GB;
//----------------------------------
if(tFree <= tReserve) m_iNumGpuFrames = 0;
else m_iNumGpuFrames = (tFree - tReserve) / m_tFmBytes;
if(m_iNumGpuFrames > m_iNumFrames) m_iNumGpuFrames = m_iNumFrames;
//-----------------
m_iMaxGpuFrms = m_iNumGpuFrames;
}
void CGpuBuffer::mCreateCpuBuf(int iNumFrms)
{
if(iNumFrms <= 0) return;
if(iNumFrms <= m_iMaxCpuFrms) return;
//-----------------
void** ppvFrames = new void*[iNumFrms];
for(int i=0; i<m_iMaxCpuFrms; i++)
{ ppvFrames[i] = m_ppvCpuFrames[i];
m_ppvCpuFrames[i] = 0L;
}
//-----------------
for(int i=m_iMaxCpuFrms; i<iNumFrms; i++)
{ void* pvFrm = 0L;
cudaMallocHost(&pvFrm, m_tFmBytes);
ppvFrames[i] = pvFrm;
}
//-----------------
if(m_ppvCpuFrames != 0L) delete[] m_ppvCpuFrames;
m_ppvCpuFrames = ppvFrames;
m_iMaxCpuFrms = iNumFrms;
}
void CGpuBuffer::mPrintAllocTimes(float* pfGBs, float* pfTimes)
{
printf("GPU %d Allocation time: GPU (%6.2f GB) %6.2f s, "
"CPU (%6.2f GB) %6.2f s\n", m_iGpuID, pfGBs[0], pfTimes[0],
pfGBs[1], pfTimes[1]);
}