Skip to content

Commit f1a529b

Browse files
author
intelmm
committed
OpenMP manager modified for Unit Testing
1 parent a658b7a commit f1a529b

File tree

3 files changed

+51
-40
lines changed

3 files changed

+51
-40
lines changed

include/caffe/util/cpu_info.hpp

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,25 @@ class CpuInfo : public CpuInfoInterface {
4848
void parseLines(char *content);
4949
};
5050

51-
class Collection {
51+
class CollectionInterface {
5252
public:
53-
static unsigned getProcessorSpeedMHz();
54-
static unsigned getTotalNumberOfSockets();
55-
static unsigned getTotalNumberOfCpuCores();
56-
static unsigned getNumberOfProcessors();
57-
static const Processor &getProcessor(unsigned processorId);
53+
virtual ~CollectionInterface() {}
54+
virtual unsigned getProcessorSpeedMHz() = 0;
55+
virtual unsigned getTotalNumberOfSockets() = 0;
56+
virtual unsigned getTotalNumberOfCpuCores() = 0;
57+
virtual unsigned getNumberOfProcessors() = 0;
58+
virtual const Processor &getProcessor(unsigned processorId) = 0;
59+
};
60+
61+
class Collection : public CollectionInterface {
62+
public:
63+
explicit Collection(CpuInfoInterface *cpuInfo);
64+
65+
virtual unsigned getProcessorSpeedMHz();
66+
virtual unsigned getTotalNumberOfSockets();
67+
virtual unsigned getTotalNumberOfCpuCores();
68+
virtual unsigned getNumberOfProcessors();
69+
virtual const Processor &getProcessor(unsigned processorId);
5870

5971
private:
6072
CpuInfoInterface &cpuInfo;
@@ -63,11 +75,10 @@ class Collection {
6375
std::vector<Processor> processors;
6476
Processor *currentProcessor;
6577

66-
explicit Collection(CpuInfoInterface *cpuInfo);
6778
Collection(const Collection &collection);
6879
Collection &operator =(const Collection &collection);
69-
static Collection &getSingleInstance();
7080

81+
void initialize();
7182
void parseCpuInfo();
7283
void parseCpuInfoLine(const char *cpuInfoLine);
7384
void parseValue(const char *fieldName, const char *valueString);
@@ -93,13 +104,17 @@ class OpenMpManager {
93104
static void bindOpenMpThreads();
94105
static void printVerboseInformation();
95106

107+
static unsigned getProcessorSpeedMHz();
108+
96109
private:
110+
Collection &collection;
111+
97112
bool isGpuEnabled;
98113
bool isAnyOpenMpEnvVarSpecified;
99114
cpu_set_t currentCpuSet;
100115
cpu_set_t currentCoreSet;
101116

102-
OpenMpManager();
117+
explicit OpenMpManager(Collection *collection);
103118
OpenMpManager(const OpenMpManager &openMpManager);
104119
OpenMpManager &operator =(const OpenMpManager &openMpManager);
105120
static OpenMpManager &getInstance();

src/caffe/util/cpu_info.cpp

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -92,35 +92,24 @@ Collection::Collection(CpuInfoInterface *cpuInfo) : cpuInfo(*cpuInfo) {
9292
collectBasicCpuInformation();
9393
}
9494

95-
Collection &Collection::getSingleInstance() {
96-
static CpuInfo cpuInfo;
97-
static Collection collection(&cpuInfo);
98-
return collection;
99-
}
100-
10195
unsigned Collection::getProcessorSpeedMHz() {
102-
Collection &collection = getSingleInstance();
103-
return collection.processors.size() ? collection.processors[0].speedMHz : 0;
96+
return processors.size() ? processors[0].speedMHz : 0;
10497
}
10598

10699
unsigned Collection::getTotalNumberOfSockets() {
107-
Collection &collection = getSingleInstance();
108-
return collection.totalNumberOfSockets;
100+
return totalNumberOfSockets;
109101
}
110102

111103
unsigned Collection::getTotalNumberOfCpuCores() {
112-
Collection &collection = getSingleInstance();
113-
return collection.totalNumberOfCpuCores;
104+
return totalNumberOfCpuCores;
114105
}
115106

116107
unsigned Collection::getNumberOfProcessors() {
117-
Collection &collection = getSingleInstance();
118-
return collection.processors.size();
108+
return processors.size();
119109
}
120110

121111
const Processor &Collection::getProcessor(unsigned processorId) {
122-
Collection &collection = getSingleInstance();
123-
return collection.processors[processorId];
112+
return processors[processorId];
124113
}
125114

126115
void Collection::parseCpuInfo() {
@@ -262,14 +251,16 @@ static const char *openMpEnvVars[] = {
262251
static const unsigned numberOfOpenMpEnvVars =
263252
sizeof(openMpEnvVars) / sizeof(openMpEnvVars[0]);
264253

265-
OpenMpManager::OpenMpManager() {
254+
OpenMpManager::OpenMpManager(Collection *collection) : collection(*collection) {
266255
getOpenMpEnvVars();
267256
getCurrentCpuSet();
268257
getCurrentCoreSet();
269258
}
270259

271260
OpenMpManager &OpenMpManager::getInstance() {
272-
static OpenMpManager openMpManager;
261+
static CpuInfo cpuInfo;
262+
static Collection collection(&cpuInfo);
263+
static OpenMpManager openMpManager(&collection);
273264
return openMpManager;
274265
}
275266

@@ -325,7 +316,7 @@ void OpenMpManager::getCurrentCpuSet() {
325316

326317
void OpenMpManager::getDefaultCpuSet(cpu_set_t *defaultCpuSet) {
327318
CPU_ZERO(defaultCpuSet);
328-
unsigned numberOfProcessors = Collection::getNumberOfProcessors();
319+
unsigned numberOfProcessors = collection.getNumberOfProcessors();
329320
for (int processorId = 0; processorId < numberOfProcessors; processorId++) {
330321
CPU_SET(processorId, defaultCpuSet);
331322
}
@@ -337,8 +328,8 @@ void OpenMpManager::getDefaultCpuSet(cpu_set_t *defaultCpuSet) {
337328
available. */
338329

339330
void OpenMpManager::getCurrentCoreSet() {
340-
unsigned numberOfProcessors = Collection::getNumberOfProcessors();
341-
unsigned totalNumberOfCpuCores = Collection::getTotalNumberOfCpuCores();
331+
unsigned numberOfProcessors = collection.getNumberOfProcessors();
332+
unsigned totalNumberOfCpuCores = collection.getTotalNumberOfCpuCores();
342333

343334
cpu_set_t usedCoreSet;
344335
CPU_ZERO(&usedCoreSet);
@@ -356,8 +347,8 @@ void OpenMpManager::getCurrentCoreSet() {
356347
}
357348

358349
void OpenMpManager::selectAllCoreCpus(cpu_set_t *set, unsigned physicalCoreId) {
359-
unsigned numberOfProcessors = Collection::getNumberOfProcessors();
360-
unsigned totalNumberOfCpuCores = Collection::getTotalNumberOfCpuCores();
350+
unsigned numberOfProcessors = collection.getNumberOfProcessors();
351+
unsigned totalNumberOfCpuCores = collection.getTotalNumberOfCpuCores();
361352

362353
int processorId = physicalCoreId % totalNumberOfCpuCores;
363354
while (processorId < numberOfProcessors) {
@@ -370,7 +361,7 @@ void OpenMpManager::selectAllCoreCpus(cpu_set_t *set, unsigned physicalCoreId) {
370361
}
371362

372363
unsigned OpenMpManager::getPhysicalCoreId(unsigned logicalCoreId) {
373-
unsigned numberOfProcessors = Collection::getNumberOfProcessors();
364+
unsigned numberOfProcessors = collection.getNumberOfProcessors();
374365

375366
for (int processorId = 0; processorId < numberOfProcessors; processorId++) {
376367
if (CPU_ISSET(processorId, &currentCoreSet)) {
@@ -415,16 +406,16 @@ void OpenMpManager::printVerboseInformation() {
415406
OpenMpManager &openMpManager = getInstance();
416407

417408
LOG(INFO) << "Processor speed [MHz]: "
418-
<< Collection::getProcessorSpeedMHz();
409+
<< openMpManager.collection.getProcessorSpeedMHz();
419410

420411
LOG(INFO) << "Total number of sockets: "
421-
<< Collection::getTotalNumberOfSockets();
412+
<< openMpManager.collection.getTotalNumberOfSockets();
422413

423414
LOG(INFO) << "Total number of CPU cores: "
424-
<< Collection::getTotalNumberOfCpuCores();
415+
<< openMpManager.collection.getTotalNumberOfCpuCores();
425416

426417
LOG(INFO) << "Total number of processors: "
427-
<< Collection::getNumberOfProcessors();
418+
<< openMpManager.collection.getNumberOfProcessors();
428419

429420
LOG(INFO) << "GPU is used: "
430421
<< (openMpManager.isGpuEnabled ? "yes" : "no");
@@ -439,6 +430,11 @@ void OpenMpManager::printVerboseInformation() {
439430
<< omp_get_max_threads();
440431
}
441432

433+
unsigned OpenMpManager::getProcessorSpeedMHz() {
434+
OpenMpManager &openMpManager = getInstance();
435+
return openMpManager.collection.getProcessorSpeedMHz();
436+
}
437+
442438
#endif // _OPENMP
443439

444440
} // namespace cpu

src/caffe/util/math_functions.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ void caffe_set(const int N, const Dtype alpha, Dtype* Y) {
7272
#ifdef _OPENMP
7373

7474
int nthr = omp_get_max_threads();
75-
int threshold = nthr * caffe::cpu::Collection::getProcessorSpeedMHz() / 3;
75+
int threshold = nthr * caffe::cpu::OpenMpManager::getProcessorSpeedMHz() / 3;
7676
bool run_parallel = true;
7777

7878
// Note: we Assume GPU's CPU path is single threaded
@@ -131,7 +131,7 @@ void caffe_cpu_copy(const int N, const Dtype* X, Dtype* Y) {
131131
#ifdef _OPENMP
132132

133133
int nthr = omp_get_max_threads();
134-
int threshold = nthr * caffe::cpu::Collection::getProcessorSpeedMHz() / 3;
134+
int threshold = nthr * caffe::cpu::OpenMpManager::getProcessorSpeedMHz() / 3;
135135
const bool run_parallel =
136136
(Caffe::mode() != Caffe::GPU) &&
137137
(omp_in_parallel() == 0) &&
@@ -389,7 +389,7 @@ static void bernoulli_generate(int n, double p, int* r) {
389389

390390
#ifdef _OPENMP
391391
int nthr = omp_get_max_threads();
392-
int threshold = nthr * caffe::cpu::Collection::getProcessorSpeedMHz() / 3;
392+
int threshold = nthr * caffe::cpu::OpenMpManager::getProcessorSpeedMHz() / 3;
393393
bool run_parallel =
394394
(Caffe::mode() != Caffe::GPU) &&
395395
(omp_in_parallel() == 0) &&

0 commit comments

Comments
 (0)