Skip to content

Commit 4a39554

Browse files
author
intelmm
committed
OpenMP manager modified for Unit Testing
1 parent be3d079 commit 4a39554

File tree

2 files changed

+16
-30
lines changed

2 files changed

+16
-30
lines changed

include/caffe/util/cpu_info.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,19 @@ class Collection {
5050
static const Processor &getProcessor(unsigned processorId);
5151

5252
private:
53+
CpuInfo &cpuInfo;
5354
unsigned totalNumberOfSockets;
5455
unsigned totalNumberOfCpuCores;
5556
std::vector<Processor> processors;
5657
Processor *currentProcessor;
5758

58-
Collection();
59+
Collection(CpuInfo &cpuInfo);
5960
Collection(const Collection &collection);
6061
Collection &operator =(const Collection &collection);
6162
static Collection &getSingleInstance();
6263

63-
void parseCpuFile(const char *fileName);
64-
void parseCpuFileContent(FILE *file);
65-
void parseCpuFileLine(const char *lineBuffer);
64+
void parseCpuInfo();
65+
void parseCpuInfoLine(const char *cpuInfoLine);
6666
void parseValue(const char *fieldName, const char *valueString);
6767
void appendNewProcessor();
6868
bool beginsWith(const char *lineBuffer, const char *text) const;

src/caffe/util/cpu_info.cpp

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,20 @@ const char *CpuInfo::getNextLine() {
7878
return savedCurrentLine;
7979
}
8080

81-
Collection::Collection() {
81+
Collection::Collection(CpuInfo &cpuInfo) : cpuInfo(cpuInfo) {
8282
totalNumberOfSockets = 0;
8383
totalNumberOfCpuCores = 0;
8484
currentProcessor = NULL;
8585

8686
processors.reserve(96);
8787

88-
parseCpuFile("/proc/cpuinfo");
88+
parseCpuInfo();
8989
collectBasicCpuInformation();
9090
}
9191

9292
Collection &Collection::getSingleInstance() {
93-
static Collection collection;
93+
static CpuInfo cpuInfo;
94+
static Collection collection(cpuInfo);
9495
return collection;
9596
}
9697

@@ -119,35 +120,20 @@ const Processor &Collection::getProcessor(unsigned processorId) {
119120
return collection.processors[processorId];
120121
}
121122

122-
void Collection::parseCpuFile(const char *fileName) {
123-
FILE *file = fopen(fileName, "rb");
124-
if (!file) {
125-
return;
126-
}
127-
128-
parseCpuFileContent(file);
129-
130-
fclose(file);
131-
}
132-
133-
void Collection::parseCpuFileContent(FILE *file) {
134-
while (!feof(file)) {
135-
char lineBuffer[1024];
136-
if (!fgets(lineBuffer, sizeof(lineBuffer), file)) {
137-
break;
138-
}
139-
140-
parseCpuFileLine(lineBuffer);
123+
void Collection::parseCpuInfo() {
124+
const char *cpuInfoLine = cpuInfo.getFirstLine();
125+
for(; cpuInfoLine; cpuInfoLine = cpuInfo.getNextLine()) {
126+
parseCpuInfoLine(cpuInfoLine);
141127
}
142128
}
143129

144-
void Collection::parseCpuFileLine(const char *lineBuffer) {
145-
int delimiterPosition = strcspn(lineBuffer, ":");
130+
void Collection::parseCpuInfoLine(const char *cpuInfoLine) {
131+
int delimiterPosition = strcspn(cpuInfoLine, ":");
146132

147-
if (lineBuffer[delimiterPosition] == '\0') {
133+
if (cpuInfoLine[delimiterPosition] == '\0') {
148134
currentProcessor = NULL;
149135
} else {
150-
parseValue(lineBuffer, &lineBuffer[delimiterPosition + 2]);
136+
parseValue(cpuInfoLine, &cpuInfoLine[delimiterPosition + 2]);
151137
}
152138
}
153139

0 commit comments

Comments
 (0)