1 // Copyright (C) 2018 Intel Corporation
3 // SPDX-License-Identifier: Apache-2.0
15 namespace MKLDNNPlugin {
31 class CpuInfoInterface {
33 virtual ~CpuInfoInterface() {}
35 virtual const char *getFirstLine() = 0;
37 virtual const char *getNextLine() = 0;
40 class CpuInfo : public CpuInfoInterface {
44 explicit CpuInfo(const char *content);
48 virtual const char *getFirstLine();
50 virtual const char *getNextLine();
53 const char *fileContentBegin;
54 const char *fileContentEnd;
55 const char *currentLine;
57 void loadContentFromFile(const char *fileName);
59 void loadContent(const char *content);
61 void parseLines(char *content);
64 class CollectionInterface {
66 virtual ~CollectionInterface() {}
68 virtual unsigned getProcessorSpeedMHz() = 0;
70 virtual unsigned getTotalNumberOfSockets() = 0;
72 virtual unsigned getTotalNumberOfCpuCores() = 0;
74 virtual unsigned getNumberOfProcessors() = 0;
76 virtual const Processor &getProcessor(unsigned processorId) = 0;
79 class Collection : public CollectionInterface {
81 explicit Collection(CpuInfoInterface *cpuInfo);
83 virtual unsigned getProcessorSpeedMHz();
85 virtual unsigned getTotalNumberOfSockets();
87 virtual unsigned getTotalNumberOfCpuCores();
89 virtual unsigned getNumberOfProcessors();
91 virtual const Processor &getProcessor(unsigned processorId);
94 CpuInfoInterface &cpuInfo;
95 unsigned totalNumberOfSockets;
96 unsigned totalNumberOfCpuCores;
97 std::vector<Processor> processors;
98 Processor *currentProcessor;
100 Collection(const Collection &collection);
102 Collection &operator=(const Collection &collection);
106 void parseCpuInfoLine(const char *cpuInfoLine);
108 void parseValue(const char *fieldName, const char *valueString);
110 void appendNewProcessor();
112 bool beginsWith(const char *lineBuffer, const char *text) const;
114 unsigned parseInteger(const char *text) const;
116 unsigned extractSpeedFromModelName(const char *text) const;
118 void collectBasicCpuInformation();
120 void updateCpuInformation(const Processor &processor,
121 unsigned numberOfUniquePhysicalId);
125 class OpenMpManager {
127 static void setGpuEnabled();
129 static void setGpuDisabled();
131 static void bindCurrentThreadToNonPrimaryCoreIfPossible();
133 static void bindOpenMpThreads(int env_cores = 0);
135 static int getOpenMpThreadNumber();
137 static void printVerboseInformation();
139 static bool isMajorThread(int currentThread);
142 Collection &collection;
145 bool isAnyOpenMpEnvVarSpecified;
146 cpu_set_t currentCpuSet;
147 cpu_set_t currentCoreSet;
149 explicit OpenMpManager(Collection *collection);
151 OpenMpManager(const OpenMpManager &openMpManager);
153 OpenMpManager &operator=(const OpenMpManager &openMpManager);
155 static OpenMpManager &getInstance();
157 void getOpenMpEnvVars();
159 void getCurrentCpuSet();
163 void getDefaultCpuSet(cpu_set_t *defaultCpuSet);
165 void getCurrentCoreSet();
167 void selectAllCoreCpus(cpu_set_t *set, unsigned physicalCoreId);
169 unsigned getPhysicalCoreId(unsigned logicalCoreId);
171 bool isThreadsBindAllowed();
173 void setOpenMpThreadNumberLimit(int env_cores);
175 void bindCurrentThreadToLogicalCoreCpu(unsigned logicalCoreId);
177 void bindCurrentThreadToLogicalCoreCpus(unsigned logicalCoreId);
180 #endif // #ifndef __APPLE__
182 } // namespace MKLDNNPlugin