-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCBufferPool.cpp
executable file
·271 lines (250 loc) · 7.74 KB
/
CBufferPool.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include "CMainInc.h"
#include "Align/CAlignInc.h"
#include "DataUtil/CDataUtilInc.h"
#include "Util/CUtilInc.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <memory.h>
#include <sys/types.h>
#include <errno.h>
#include <Util/Util_Time.h>
using namespace MotionCor2;
namespace DU = MotionCor2::DataUtil;
CBufferPool* CBufferPool::m_pInstance = 0L;
CBufferPool* CBufferPool::GetInstance(void)
{
if(m_pInstance != 0L) return m_pInstance;
m_pInstance = new CBufferPool;
return m_pInstance;
}
void CBufferPool::DeleteInstance(void)
{
if(m_pInstance == 0L) return;
delete m_pInstance;
m_pInstance = 0L;
}
CBufferPool::CBufferPool(void)
{
m_pTmpBuffer = 0L;
m_pSumBuffer = 0L;
m_pFrmBuffer = 0L;
m_pXcfBuffer = 0L;
m_pPatBuffer = 0L;
m_pvPinnedBuf = 0L;
m_pCufft2Ds = 0L;
//---------------
m_piGpuIDs = 0L;
m_iNumGpus = 0;
memset(m_aiStkSize, 0, sizeof(m_aiStkSize));
}
CBufferPool::~CBufferPool(void)
{
this->Clean();
}
void CBufferPool::Clean(void)
{
if(m_pTmpBuffer != 0L) delete m_pTmpBuffer;
if(m_pSumBuffer != 0L) delete m_pSumBuffer;
if(m_pFrmBuffer != 0L) delete m_pFrmBuffer;
if(m_pXcfBuffer != 0L) delete m_pXcfBuffer;
if(m_pPatBuffer != 0L) delete m_pPatBuffer;
if(m_pvPinnedBuf != 0L) cudaFreeHost(m_pvPinnedBuf);
if(m_pCufft2Ds != 0L)
{ for(int i=0; i<m_iNumGpus; i++)
{ this->SetDevice(i);
m_pCufft2Ds[2*i].DestroyPlan();
m_pCufft2Ds[2*i+1].DestroyPlan();
}
delete[] m_pCufft2Ds;
}
//---------------------------
m_pTmpBuffer = 0L;
m_pSumBuffer = 0L;
m_pFrmBuffer = 0L;
m_pXcfBuffer = 0L;
m_pPatBuffer = 0L;
m_pvPinnedBuf = 0L;
m_pCufft2Ds = 0L;
memset(m_aiStkSize, 0, sizeof(m_aiStkSize));
}
void CBufferPool::SetGpus(int* piGpuIDs, int iNumGpus)
{
this->Clean();
//------------
if(m_piGpuIDs != 0L) delete[] m_piGpuIDs;
m_piGpuIDs = new int[iNumGpus];
memcpy(m_piGpuIDs, piGpuIDs, sizeof(int) * iNumGpus);
m_iNumGpus = iNumGpus;
}
//------------------------------------------------------------------------------
// 1. This is used for the first-time creation of the buffer. It should be
// called only once throughout the lifetime once the program is started.
// 2. The frame size cannot be changed.
// 3. The number of frames in a movie can be changed using Adjust(...);
//------------------------------------------------------------------------------
void CBufferPool::CreateBuffers(int* piStkSize)
{
this->Clean();
memcpy(m_aiStkSize, piStkSize, sizeof(m_aiStkSize));
m_pCufft2Ds = new Util::CCufft2D[2 * m_iNumGpus];
//-----------------------------------------------
Util_Time aTimer;
aTimer.Measure();
//---------------------------------------------------------------
// If the proc queue does not have the package, this is because
// the loading is not done and the package has not been placed
// from the load queue to the proc queue. This happens in the
// single processing or the first movie of the batch processing.
//---------------------------------------------------------------
mCreateSumBuffer();
mCreateTmpBuffer();
mCreateXcfBuffer();
mCreatePatBuffer();
mCreateFrmBuffer();
//-----------------
Util::CheckRUsage("Create buffers");
float fTime = aTimer.GetElapsedSeconds();
printf("Create buffers: %.2f seconds\n\n", fTime);
}
void CBufferPool::AdjustBuffer(int iNumFrames)
{
if(iNumFrames == m_aiStkSize[2]) return;
else m_aiStkSize[2] = iNumFrames;
//-----------------
m_pFrmBuffer->Adjust(iNumFrames);
m_pXcfBuffer->Adjust(iNumFrames);
//-----------------------------------------------
// 1) m_pPatBuffer will be NULL if pat align
// is not specified. Check NULL first
//-----------------------------------------------
if(m_pPatBuffer != 0L) m_pPatBuffer->Adjust(iNumFrames);
}
CStackBuffer* CBufferPool::GetBuffer(EBuffer eBuf)
{
if(eBuf == EBuffer::tmp)
{ if(m_pTmpBuffer == 0L) mCreateTmpBuffer();
return m_pTmpBuffer;
}
if(eBuf == EBuffer::sum)
{ if(m_pSumBuffer == 0L) mCreateSumBuffer();
return m_pSumBuffer;
}
if(eBuf == EBuffer::frm)
{ if(m_pFrmBuffer == 0L) mCreateFrmBuffer();
return m_pFrmBuffer;
}
if(eBuf == EBuffer::xcf)
{ if(m_pXcfBuffer == 0L) mCreateXcfBuffer();
return m_pXcfBuffer;
}
if(eBuf == EBuffer::pat)
{ if(m_pPatBuffer == 0L) mCreatePatBuffer();
return m_pPatBuffer;
}
return 0L;
}
void* CBufferPool::GetPinnedBuf(int iNthGpu)
{
char* pcPinnedBuf = reinterpret_cast<char*>(m_pvPinnedBuf);
size_t tOffset = m_pTmpBuffer->m_tFmBytes * iNthGpu;
char* pcBuf = pcPinnedBuf + tOffset;
return pcBuf;
}
Util::CCufft2D* CBufferPool::GetForwardFFT(int iNthGpu)
{
if(m_pCufft2Ds == 0L) return 0L;
else return &m_pCufft2Ds[2 * iNthGpu];
}
Util::CCufft2D* CBufferPool::GetInverseFFT(int iNthGpu)
{
if(m_pCufft2Ds == 0L) return 0L;
else return &m_pCufft2Ds[2*iNthGpu+1];
}
void CBufferPool::SetDevice(int iNthGpu)
{
if(iNthGpu >= m_iNumGpus) return;
cudaSetDevice(m_piGpuIDs[iNthGpu]);
}
void CBufferPool::mCreateSumBuffer(void)
{
m_iNumSums = 1;
//-----------------
Align::CAlignParam* pAlnParam = Align::CAlignParam::GetInstance();
if(pAlnParam->SplitSum()) m_iNumSums += 2;
//-----------------
bool bSimpleSum = pAlnParam->SimpleSum();
if(DU::CFmIntParam::bDoseWeight() && !bSimpleSum)
{ m_iNumSums += 1;
if(DU::CFmIntParam::bDWSelectedSum()) m_iNumSums += 1;
}
//-----------------
int aiCmpSize[] = {m_aiStkSize[0] / 2 + 1, m_aiStkSize[1]};
m_pSumBuffer = new CStackBuffer;
int iNumFrames = m_iNumGpus * m_iNumSums;
m_pSumBuffer->Create(aiCmpSize, iNumFrames, m_piGpuIDs, m_iNumGpus);
}
void CBufferPool::mCreateTmpBuffer(void)
{
int iNumFrames = 4;
iNumFrames *= m_iNumGpus;
//-----------------------
int iSizeX = (m_aiStkSize[0] > 4096) ? m_aiStkSize[0] : 4096;
int iSizeY = (m_aiStkSize[1] > 4096) ? m_aiStkSize[1] : 4096;
int aiCmpSize[] = {iSizeX / 2 + 1, iSizeY};
//-----------------------------------------
m_pTmpBuffer = new CStackBuffer;
m_pTmpBuffer->Create(aiCmpSize, iNumFrames, m_piGpuIDs, m_iNumGpus);
//------------------------------------------------------------------
size_t tBytes = m_pTmpBuffer->m_tFmBytes * m_iNumGpus;
cudaMallocHost(&m_pvPinnedBuf, tBytes);
}
void CBufferPool::mCreateXcfBuffer(void)
{
CInput* pInput = CInput::GetInstance();
if(pInput->m_iAlign == 0) return;
//-------------------------------
float fBinX = m_aiStkSize[0] / 2048.0f;
float fBinY = m_aiStkSize[1] / 2048.0f;
float fBin = fmin(fBinX, fBinY);
if(fBin < 1) fBin = 1.0f;
//-----------------------
int iXcfX = (int)(m_aiStkSize[0] / fBin + 0.5f);
int iXcfY = (int)(m_aiStkSize[1] / fBin + 0.5f);
//---------------------------------------
int aiXcfCmpSize[] = {iXcfX / 2 + 1, iXcfY / 2 * 2};
m_afXcfBin[0] = (m_pSumBuffer->m_aiCmpSize[0] - 1.0f)
/ (aiXcfCmpSize[0] - 1.0f);
m_afXcfBin[1] = m_pSumBuffer->m_aiCmpSize[1]
* 1.0f / aiXcfCmpSize[1];
//----------------------------------------------------
m_pXcfBuffer = new CStackBuffer;
m_pXcfBuffer->Create(aiXcfCmpSize, m_aiStkSize[2],
m_piGpuIDs, m_iNumGpus);
}
void CBufferPool::mCreatePatBuffer(void)
{
CInput* pInput = CInput::GetInstance();
if(pInput->m_aiNumPatches[0] <= 1) return;
if(pInput->m_aiNumPatches[1] <= 1) return;
if(m_pXcfBuffer == 0L) return;
//----------------------------
int iXcfX = (m_pXcfBuffer->m_aiCmpSize[0] - 1) * 2;
int iXcfY = m_pXcfBuffer->m_aiCmpSize[1];
int iPatX = iXcfX / pInput->m_aiNumPatches[0];
int iPatY = iXcfY / pInput->m_aiNumPatches[1];
if(iPatX > (iXcfX / 2)) return;
if(iPatY > (iXcfY / 2)) return;
//-----------------------------
int aiPatCmpSize[2] = {iPatX / 2 + 1, iPatY / 2 * 2};
m_pPatBuffer = new CStackBuffer;
m_pPatBuffer->Create(aiPatCmpSize, m_pXcfBuffer->m_iNumFrames,
m_piGpuIDs, m_iNumGpus);
}
void CBufferPool::mCreateFrmBuffer(void)
{
m_pFrmBuffer = new CStackBuffer;
int aiCmpSize[] = {m_aiStkSize[0] / 2 + 1, m_aiStkSize[1]};
m_pFrmBuffer->Create(aiCmpSize, m_aiStkSize[2],
m_piGpuIDs, m_iNumGpus);
}