-
Notifications
You must be signed in to change notification settings - Fork 2
/
CMainInc.h
420 lines (407 loc) · 10 KB
/
CMainInc.h
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#pragma once
#include "Util/CUtilInc.h"
#include "DataUtil/CDataUtilInc.h"
#include "MrcUtil/CMrcUtilInc.h"
#include "TiffUtil/CTiffFileInc.h"
#include <Util/Util_Thread.h>
#include <stdio.h>
#include <cufft.h>
namespace MotionCor2
{
namespace DU = MotionCor2::DataUtil;
class CInput
{
public:
static CInput* GetInstance(void);
static void DeleteInstance(void);
~CInput(void);
void ShowTags(void);
void Parse(int argc, char* argv[]);
void GetBinnedSize(int* piImgSize, int* piBinnedSize);
bool IsInMrc(void);
bool IsInTiff(void);
bool IsInEer(void);
float GetFinalPixelSize(); // after binning & mag correction
float GetFinalPixelSize(float fPixelSize);
char m_acInMrcFile[256];
char m_acInTifFile[256];
char m_acInEerFile[256];
char m_acInSuffix[256];
char m_acInSkips[256];
char m_acGainMrc[256];
char m_acDarkMrc[256];
char m_acOutMrcFile[256];
char m_acArchiveFolder[256];
char m_acDefectFile[256];
char m_acInAlnFolder[256];
char m_acOutAlnFolder[256];
char m_acDefectMap[256];
char m_acFullSumMrc[256];
char m_acFmIntFile[256];
int m_aiNumPatches[3];
int m_iIterations;
float m_fTolerance;
float m_afBFactor[2];
int m_iPhaseOnly;
char m_acTmpFile[256];
char m_acLogDir[256];
int* m_piGpuIds;
int* m_piGpuMems;
int m_iNumGpus;
int m_iUseGpus;
int m_iStackZ;
float m_fFourierBin;
int m_iAlign;
float m_fInitDose;
float m_fFmDose;
float m_fPixelSize;
int m_iKv;
float m_fCs;
float m_fAmpCont;
float m_fExtPhase;
int m_aiThrow[2];
float m_afSumRange[2];
int m_aiGroup[2];
int m_iFmRef;
int m_iSerial;
int m_iRotGain;
int m_iFlipGain;
int m_iInvGain;
float m_afTilt[2];
int m_aiCropSize[2];
int m_aiOutStack[2];
float m_afMag[3];
int m_iInFmMotion;
float m_fGpuMemUsage;
int m_iSplitSum;
int m_iEerSampling;
int m_iOutStarFile;
int m_iTiffOrder;
int m_iCorrInterp;
//--------------------
char m_acInMrcTag[32];
char m_acInTifTag[32];
char m_acInEerTag[32];
char m_acInSuffixTag[32];
char m_acInSkipsTag[32];
char m_acGainMrcTag[32];
char m_acDarkMrcTag[32];
char m_acOutMrcTag[32];
char m_acArchiveTag[32];
char m_acDefectFileTag[32];
char m_acDefectMapTag[32];
char m_acFullSumMrcTag[32];
char m_acInAlnTag[32];
char m_acOutAlnTag[32];
char m_acPatchesTag[32];
char m_acIterTag[32];
char m_acTolTag[32];
char m_acBftTag[32];
char m_acPhaseOnlyTag[32];
char m_acTmpFileTag[32];
char m_acLogDirTag[32];
char m_acGpuIDTag[32];
char m_acStackZTag[32];
char m_acFourierBinTag[32];
char m_acAlignTag[32];
char m_acInitDoseTag[32];
char m_acFmDoseTag[32];
char m_acPixelSizeTag[32];
char m_acKvTag[32];
char m_acCsTag[32];
char m_acAmpContTag[32];
char m_acExtPhaseTag[32];
char m_acThrowTag[32];
char m_acTruncTag[32];
char m_acSumRangeTag[32];
char m_acGroupTag[32];
char m_acFmRefTag[32];
char m_acSerialTag[32];
char m_acTiltTag[32];
char m_acCropTag[32];
char m_acOutStackTag[32];
char m_acRotGainTag[32];
char m_acFlipGainTag[32];
char m_acInvGainTag[32];
char m_acMagTag[32];
char m_acInFmMotionTag[32];
char m_acGpuMemUsageTag[32];
char m_acUseGpusTag[32];
char m_acSplitSumTag[32];
char m_acFmIntFileTag[32];
char m_acEerSamplingTag[32];
char m_acOutStarTag[32];
char m_acTiffOrderTag[32];
char m_acCorrInterpTag[32];
private:
CInput(void);
void mPrint(void);
int m_argc;
char** m_argv;
static CInput* m_pInstance;
};
class CCheckFreeGpus
{
public:
static CCheckFreeGpus* GetInstance(void);
static void DeleteInstance(void);
//-----------------
~CCheckFreeGpus(void);
void SetAllGpus(int* piGpuIds, int iNumGpus);
int GetFreeGpus(int* piFreeGpus, int iNumFreeGpus);
void FreeGpus(void);
private:
CCheckFreeGpus(void);
void mClean(void);
int mOpenFile(void);
int mLockFile(int iFd);
int mUnlockFile(int iFd);
void mReadGpus(FILE* pFile);
void mWriteGpus(FILE* pFile);
bool mCheckActivePid(int iPid);
//-----------------
char m_acGpuFile[256];
int* m_piGpuIds;
int* m_piPids;
int m_iNumGpus;
int m_iPid;
//-----------------
static CCheckFreeGpus* m_pInstance;
};
class CGpuBuffer
{
public:
CGpuBuffer(void);
~CGpuBuffer(void);
void Clean(void);
void Create
( size_t tFrmBytes,
int iNumFrames,
int iGpuID
);
void AdjustBuffer(int iNumFrames);
void* GetFrame(int iFrame); // do not free
//----------------------------------------
size_t m_tFmBytes;
int m_iNumFrames;
int m_iNumGpuFrames;
int m_iGpuID;
private:
void mCalcGpuFrames(void);
void mCreateCpuBuf(int iNumFrms);
void mPrintAllocTimes(float* pfGBs, float* pfTimess);
//---------------------------------------------------
void* m_pvGpuFrames;
void** m_ppvCpuFrames;
int m_iMaxGpuFrms;
int m_iMaxCpuFrms;
};
class CStackBuffer
{
public:
CStackBuffer(void);
~CStackBuffer(void);
void Clean(void);
void Create
( int* piCmpSize,
int iNumFrames,
int* piGpuIDs,
int iNumGpus
);
void Adjust(int iNumFrames);
//-----------------
int GetStartFrame(int iNthGpu);// starting frame on GPU
int GetNumFrames(int iNthGpu); // frame on GPU
bool IsGpuFrame(int iNthGpu, int iFrame);
//---------------------------------------
// iFrame = 0 is the 1st frame on iNthGpu
//---------------------------------------
cufftComplex* GetFrame(int iNthGpu, int iFrame);
cufftComplex* GetFrame(int iAbsFrame);
//------------------------------------
int GetFrameGpu(int iFrame);
void SetDevice(int iNthGpu);
//--------------------------
int* m_piGpuIDs;
int m_iNumGpus;
int m_aiCmpSize[2];
int m_iNumFrames; // all stack frames
size_t m_tFmBytes;
private:
int* mCalcFramesPerGpu(int iNumFrms);
CGpuBuffer* m_pGpuBuffers;
};
enum EBuffer {tmp, sum, frm, xcf, pat};
enum EStkSize {img, cmp, pad};
class CBufferPool
{
public:
static CBufferPool* GetInstance(void);
static void DeleteInstance(void);
~CBufferPool(void);
void Clean(void);
void SetGpus(int* piGpuIDs, int iNumGpus);
void CreateBuffers(int* piStkSize);
void AdjustBuffer(int iNumFrames);
CStackBuffer* GetBuffer(EBuffer eBuf);
//-------------------------------------------------
// iFrame is relative the starting frame in nth GPU
//-------------------------------------------------
void* GetPinnedBuf(int iNthGpu);
//------------------------------
Util::CCufft2D* GetForwardFFT(int iNthGpu);
Util::CCufft2D* GetInverseFFT(int iNthGpu);
//-----------------------------------------
void SetDevice(int iNthGpu);
//--------------------------------------------------
int m_iNumSums;
int m_iNumGpus;
int* m_piGpuIDs;
float m_afXcfBin[2];
int m_aiStkSize[3];
private:
CBufferPool(void);
void mCreateSumBuffer(void);
void mCreateTmpBuffer(void);
void mCreateFrmBuffer(void);
void mCreateXcfBuffer(void);
void mCreatePatBuffer(void);
CStackBuffer* m_pTmpBuffer;
CStackBuffer* m_pSumBuffer;
CStackBuffer* m_pFrmBuffer;
CStackBuffer* m_pXcfBuffer;
CStackBuffer* m_pPatBuffer;
void* m_pvPinnedBuf;
Util::CCufft2D* m_pCufft2Ds;
static CBufferPool* m_pInstance;
};
class CLoadRefs
{
public:
static CLoadRefs* GetInstance(void);
static void DeleteInstance(void);
~CLoadRefs(void);
void CleanRefs(void);
bool LoadGain(char* pcGainFile);
bool LoadDark(char* pcDarkFile);
void PostProcess(int iRotFact, int iFlip, int iInverse);
bool AugmentRefs(int* piImgSize);
float* m_pfGain;
float* m_pfDark;
int m_aiRefSize[2];
private:
int mGetFileType(char* pcRefFile);
void mClearGain(void);
void mClearDark(void);
//-----------------
void mLoadGainMrc(char* pcMrcFile);
void mLoadGainTiff(char* pcTiffFile);
//-----------------
void mRotate(float* gfRef, int* piRefSize, int iRotFact);
void mFlip(float* gfRef, int* piRefSize, int iFlip);
void mInverse(float* gfRef, int* piRefSize, int iInverse);
//-----------------
float* mToFloat(void* pvRef, int iMode, int* piSize);
void mCheckDarkRef(void);
float* mAugmentRef(float* pfRef, int iFact);
CLoadRefs(void);
//-----------------
int m_aiDarkSize[2];
bool m_bAugmented;
//-----------------
static CLoadRefs* m_pInstance;
};
class CProcessThread : public Util_Thread
{
public:
CProcessThread(void);
~CProcessThread(void);
void DoIt(void* pvNewPackage);
void AsyncDoIt(void* pvDataPackage);
void Clear(void);
void ThreadMain(void);
private:
void mProcess(void);
bool mCheckGain(void);
void mApplyRefs(void);
bool mDetectBadPixels(void);
bool mCorrectBadPixels(void);
void mAlignStack(void);
void mFourierResize(void);
void mSaveAlnSums(void);
void* m_pvPackage;
std::queue<void*> m_aProcQueue;
};
class CSaveSerialCryoEM : public Util_Thread
{
public:
static CSaveSerialCryoEM* GetInstance(void);
static void DeleteInstance(void);
~CSaveSerialCryoEM(void);
void AsyncSave(DU::CDataPackage* pPackage);
void ThreadMain(void);
private:
CSaveSerialCryoEM(void);
void mInit(void);
void mClean(void);
void mSaveSums(void);
void mSaveStack(void);
void mSaveImage
( char* pcMrcFile, float* pfImg,
int* piImgSize, float fPixSize
);
void mSaveCtfStack(void);
void mSaveCtfFit(void);
DU::CDataPackage* m_pPackage;
float* m_gfImg;
void* m_pvGFindMinMax2D;
void* m_pvGCalcMoment2D;
std::queue<DU::CDataPackage*> m_aSaveQueue;
static CSaveSerialCryoEM* m_pInstance;
};
class CGenStarFile
{
public:
static CGenStarFile* GetInstance(void);
static void DeleteInstance(void);
~CGenStarFile(void);
void OpenFile(char* pcInFile);
void CloseFile(void);
void SetStackSize(int* piStkSize);
void SetHotPixels
( unsigned char* pucBadMap,
int* piMapSize, bool bPadded
);
void SetGlobalShifts(float* pfShifts, int iSize);
private:
CGenStarFile(void);
FILE* m_pFile;
char m_acInMain[256];
pthread_mutex_t m_aMutex;
static CGenStarFile* m_pInstance;
};
class CMain
{
public:
CMain(void);
~CMain(void);
bool DoIt(void);
private:
bool mLoadRefs(void);
bool mDoSingleCryoEM(void);
bool mDoSerialCryoEM(void);
void mDoSerialCryoEMMrc(void);
void mDoSerialCryoEMTiff(void);
void mDoSerialCryoEMEer(void);
int mAsyncLoad(int iCount);
void mOpenInAlnFile(void);
void mOpenOutAlnFile(void);
bool mCreateBufferPool(int* piStkSize, int iCount);
bool mWaitProcessThread(void);
bool mWaitSaveThread(void);
CProcessThread m_aProcessThread;
bool m_bHasGainFromFile;
bool m_bHasDarkFromFile;
char m_acLogFile[256];
}; //CMain
}