-
Notifications
You must be signed in to change notification settings - Fork 2
/
CCheckFreeGpus.cpp
196 lines (180 loc) · 4.23 KB
/
CCheckFreeGpus.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
#include "CMainInc.h"
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <unistd.h>
#include <sys/types.h>
#include <signal.h>
#include <string.h>
using namespace MotionCor2;
CCheckFreeGpus* CCheckFreeGpus::m_pInstance = 0L;
CCheckFreeGpus* CCheckFreeGpus::GetInstance(void)
{
if(m_pInstance != 0L) return m_pInstance;
m_pInstance = new CCheckFreeGpus;
return m_pInstance;
}
void CCheckFreeGpus::DeleteInstance(void)
{
if(m_pInstance == 0L) return;
delete m_pInstance;
m_pInstance = 0L;
}
CCheckFreeGpus::CCheckFreeGpus(void)
{
strcpy(m_acGpuFile, "/tmp/MotionCor2_FreeGpus.txt");
m_piGpuIds = 0L;
m_piPids = 0L;
m_iNumGpus = 0;
}
CCheckFreeGpus::~CCheckFreeGpus(void)
{
mClean();
}
void CCheckFreeGpus::SetAllGpus(int* piGpuIds, int iNumGpus)
{
mClean();
m_iNumGpus = iNumGpus;
if(m_iNumGpus <= 0) m_iNumGpus = 0;
if(m_iNumGpus == 0) return;
//-----------------
m_piGpuIds = new int[m_iNumGpus];
m_piPids = new int[m_iNumGpus];
//-----------------
memcpy(m_piGpuIds, piGpuIds, sizeof(int) * m_iNumGpus);
memset(m_piPids, 0, sizeof(int) * m_iNumGpus);
m_iPid = getpid();
}
int CCheckFreeGpus::GetFreeGpus(int* piFreeGpus, int iNumFreeGpus)
{
if(m_iNumGpus == 0) return -1;
//-----------------
int iFd = mOpenFile();
if(iFd == -1) return -1;
//-----------------
int iRet = mLockFile(iFd);
if(iRet == -1)
{ close(iFd);
return -1;
}
//----------------
FILE* pFile = fdopen(iFd, "w+t");
mReadGpus(pFile);
//---------------
int iFreeGpus = 0;
for(int i=0; i<m_iNumGpus; i++)
{ if(mCheckActivePid(m_piPids[i])) continue;
piFreeGpus[iFreeGpus] = m_piGpuIds[i];
m_piPids[i] = m_iPid;
iFreeGpus++;
if(iFreeGpus == iNumFreeGpus) break;
}
if(iFreeGpus > 0) mWriteGpus(pFile);
//----------------------------------
mUnlockFile(iFd);
fclose(pFile);
return iFreeGpus;
}
void CCheckFreeGpus::FreeGpus(void)
{
if(m_iNumGpus <= 0) return;
if(m_piGpuIds == 0L) return;
if(m_piPids == 0L) return;
//-----------------
int iFd = mOpenFile();
if(iFd == -1) return;
//-----------------
int iRet = mLockFile(iFd);
if(iRet == -1)
{ close(iFd);
return;
}
//-------------
for(int i=0; i<m_iNumGpus; i++)
{ int iPid = m_piPids[i];
if(iPid == 0) continue;
if(iPid == m_iPid) m_piPids[i] = 0;
else if(!mCheckActivePid(iPid)) m_piPids[i] = 0;
}
FILE* pFile = fdopen(iFd, "w+t");
mWriteGpus(pFile);
//----------------
mUnlockFile(iFd);
fclose(pFile);
}
void CCheckFreeGpus::mClean(void)
{
if(m_piGpuIds != 0L) delete[] m_piGpuIds;
if(m_piPids != 0L) delete[] m_piPids;
m_piGpuIds = 0L;
m_piPids = 0L;
}
int CCheckFreeGpus::mOpenFile(void)
{
int iFd = open(m_acGpuFile, O_RDWR | O_CREAT, 0666);
chmod(m_acGpuFile, 0666);
if(iFd > -1) return iFd;
//----------------------
printf
( "Warning: Unable to open free GPU file: %s.\n"
" %s\n\n", m_acGpuFile, strerror(iFd)
);
return -1;
}
int CCheckFreeGpus::mLockFile(int iFd)
{
int iRet = flock(iFd, LOCK_EX);
if(iRet == 0) return iRet;
printf
( "Warning: Unable to lock GPU file in CCheckFreeGpus::mLockFile.\n\n"
);
return -1;
}
int CCheckFreeGpus::mUnlockFile(int iFd)
{
int iRet = flock(iFd, LOCK_UN);
if(iRet == 0) return iRet;
printf
( "Warning: Unable to unlock GPU file in CCheckFreeGpus::mLockFile.\n\n"
);
return -1;
}
void CCheckFreeGpus::mReadGpus(FILE* pFile)
{
char acLine[256] = {'\0'};
while(!feof(pFile))
{ char* pcRet = fgets(acLine, 256, pFile);
if(pcRet == 0L) continue;
int iGpuId = 0, iPid = 0;
int iEntries = sscanf(acLine, "%d %d", &iGpuId, &iPid);
if(iEntries != 2) continue;
//-------------------------
if(iPid == 0) continue;
else if(!mCheckActivePid(iPid)) continue;
//---------------------------------------
for(int i=0; i<m_iNumGpus; i++)
{ if(iGpuId != m_piGpuIds[i]) continue;
m_piPids[i] = iPid;
}
}
}
void CCheckFreeGpus::mWriteGpus(FILE* pFile)
{
fseek(pFile, 0, SEEK_SET);
char acEntry[64] = {'\0'};
for(int i=0; i<m_iNumGpus; i++)
{ sprintf(acEntry, "%d %d", m_piGpuIds[i], m_piPids[i]);
fprintf(pFile, "%-16s\n", acEntry);
}
}
bool CCheckFreeGpus::mCheckActivePid(int iPid)
{
if(iPid <= 0) return false;
int iRet = kill(iPid, 0);
if(iRet == 0) return true;
//------------------------
int iGroupID = getpgid(iPid);
if(iGroupID == -1) return false;
else return true;
}