-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCMotionCor3.cpp
224 lines (213 loc) · 6.05 KB
/
CMotionCor3.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
#include "CMainInc.h"
#include "TiffUtil/CTiffFileInc.h"
#include <Mrcfile/CMrcFileInc.h>
#include <Util/Util_Time.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <stdio.h>
#include <string.h>
#include <nvToolsExt.h>
using namespace MotionCor2;
bool mCheckSame(void);
bool mCheckLoad(void);
bool mCheckSave(char* pcMrcFile);
bool mCheckFreeGpus(void);
bool mCheckGPUs(void);
void mCheckPeerAccess(void);
enum ERetCode
{ eSuccess = 0,
eNoFreeGPUs = 1,
eInputOutputSame = 2,
eFailLoad = 3,
eFailSave = 4,
eNoValidGPUs = 5,
eFailProcess = 6
};
int main(int argc, char* argv[])
{
nvtxRangePushA("MotionCor2");
CInput* pInput = CInput::GetInstance();
if(argc == 2)
{ if(strcasecmp(argv[1], "--version") == 0)
{ printf("MotionCor3 version 1.1.2\n"
"Built on Jun 11 2024\n");
return eSuccess;
}
if(strcasecmp(argv[1], "--help") == 0)
{ printf("\nUsage: MotionCor3 Tags\n");
pInput->ShowTags();
return eSuccess;
}
}
pInput->Parse(argc, argv);
//-----------------
bool bHasFreeGpus = mCheckFreeGpus();
if(!mCheckFreeGpus()) return eNoFreeGPUs;
//-----------------
cuInit(0);
if(mCheckSame()) return eInputOutputSame;
//-----------------
bool bLoad = mCheckLoad();
if(!bLoad) return eFailLoad;
//-----------------
bool bSave = mCheckSave(pInput->m_acOutMrcFile);
if(!bSave) return eFailSave;
//-----------------
bool bGpu = mCheckGPUs();
if(!bGpu) return eNoValidGPUs;
//-----------------
Util_Time aTimer;
aTimer.Measure();
CMain aMain;
bool bSuccess = aMain.DoIt();
//-----------------
CCheckFreeGpus* pCheckFreeGpus = CCheckFreeGpus::GetInstance();
pCheckFreeGpus->FreeGpus();
CCheckFreeGpus::DeleteInstance();
//-----------------
nvtxRangePop();
float fSecs = aTimer.GetElapsedSeconds();
printf("Total time: %f sec\n", fSecs);
if(!bSuccess) return eFailProcess;
else return eSuccess;
}
//--------------------------------------------------------------------
// 1. Do not check the free gpu file in /tmp folder if users do not
// activate -UseGpus option in command line.
// 2. Do not check when users specifies all the GPUs using -UseGpus
// in command line.
// 3. Check only when -UseGpus specifies less GPUs than the provided
// GPUs in the command line using -Gpu
// 4. This is added per request from Github users.
//--------------------------------------------------------------------
bool mCheckFreeGpus(void)
{
CInput* pInput = CInput::GetInstance();
if(pInput->m_iUseGpus >= pInput->m_iNumGpus) return true;
if(pInput->m_iUseGpus <= 0) return true;
//-----------------
CCheckFreeGpus* pCheckFreeGpus = CCheckFreeGpus::GetInstance();
pCheckFreeGpus->SetAllGpus(pInput->m_piGpuIds, pInput->m_iNumGpus);
int iFreeGpus = pCheckFreeGpus->GetFreeGpus(
pInput->m_piGpuIds, pInput->m_iUseGpus);
//-----------------
if(iFreeGpus <= 0)
{ fprintf(stderr, "Error: All GPUs are in use, quit.\n\n");
return false;
}
//-----------------
pInput->m_iNumGpus = iFreeGpus;
return true;
}
bool mCheckSame(void)
{
CInput* pInput = CInput::GetInstance();
if(pInput->m_iSerial == 1) return false;
//--------------------------------------
int iRet = strcasecmp(pInput->m_acInMrcFile,
pInput->m_acOutMrcFile);
if(iRet != 0) return false;
//-------------------------
fprintf
( stderr, "mCheckSame: %s\n Input: %s\n Output: %s\n\n",
"input and output files are the same.",
pInput->m_acInMrcFile,
pInput->m_acOutMrcFile
);
return true;
}
bool mCheckLoad(void)
{
CInput* pInput = CInput::GetInstance();
if(pInput->m_iSerial == 1) return true;
//-------------------------------------
if(pInput->IsInTiff()) return true;
else if(pInput->IsInMrc()) return true;
else if(pInput->IsInEer()) return true;
//-------------------------------------
fprintf(stderr, "Error: no valid input file name.\n"
" mCheckLoad: \n\n");
return false;
}
bool mCheckSave(char* pcMrcFile)
{
CInput* pInput = CInput::GetInstance();
if(pInput->m_iSerial == 1) return true;
//-------------------------------------
Mrc::CSaveMrc aSaveMrc;
bool bSave = aSaveMrc.OpenFile(pcMrcFile);
if(bSave)
{ remove(pcMrcFile);
return true;
}
//------------------
fprintf(stderr, "Error:cannot open output MRC file.\n"
" mCheckSave: %s\n\n", pcMrcFile);
return false;
}
bool mCheckGPUs(void)
{
CInput* pInput = CInput::GetInstance();
int* piGpuIds = new int[pInput->m_iNumGpus];
int* piGpuMems = new int[pInput->m_iNumGpus];
//-------------------------------------------
int iCount = 0;
cudaDeviceProp aDeviceProp;
//-------------------------
for(int i=0; i<pInput->m_iNumGpus; i++)
{ int iGpuId = pInput->m_piGpuIds[i];
cudaError_t tErr = cudaSetDevice(iGpuId);
if(tErr != cudaSuccess)
{ printf
( "Info: skip device %d, %s\n",
pInput->m_piGpuIds[i],
cudaGetErrorString(tErr)
);
continue;
}
piGpuIds[iCount] = iGpuId;
cudaGetDeviceProperties(&aDeviceProp, iGpuId);
piGpuMems[iCount] = (int)(aDeviceProp.totalGlobalMem
/ (1024 * 1024));
iCount++;
}
//---------------
for(int i=0; i<iCount; i++)
{ pInput->m_piGpuIds[i] = piGpuIds[i];
pInput->m_piGpuMems[i] = piGpuMems[i];
printf
( "GPU %d memory: %d MB\n",
pInput->m_piGpuIds[i],
pInput->m_piGpuMems[i]
);
}
printf("\n");
pInput->m_iNumGpus = iCount;
if(piGpuIds != 0L) delete[] piGpuIds;
if(piGpuMems != 0L) delete[] piGpuMems;
if(iCount > 0) return true;
//-------------------------
fprintf(stderr, "mCheckGPUs: no valid device detected.\n\n");
return false;
}
void mCheckPeerAccess(void)
{
cudaError_t cuErr;
CInput* pInput = CInput::GetInstance();
if(pInput->m_iNumGpus == 1) return;
//---------------------------------
for(int i=0; i<pInput->m_iNumGpus; i++)
{ cudaSetDevice(pInput->m_piGpuIds[i]);
for(int j=0; j<pInput->m_iNumGpus; j++)
{ if(j == i) continue;
cuErr = cudaDeviceEnablePeerAccess
( pInput->m_piGpuIds[j], 0 );
if(cuErr == cudaSuccess) continue;
printf("mCheckGPUs: GPU %d cannot access %d memory\n"
" %s.\n\n", pInput->m_piGpuIds[i],
pInput->m_piGpuIds[j],
cudaGetErrorString(cuErr));
}
}
cudaSetDevice(pInput->m_piGpuIds[0]);
}