Publishing R3
[platform/upstream/dldt.git] / inference-engine / src / mkldnn_plugin / mkldnn / os / lin / lin_omp_manager.h
1 // Copyright (C) 2018 Intel Corporation
2 //
3 // SPDX-License-Identifier: Apache-2.0
4 //
5
6 #pragma once
7
8 #include <sched.h>
9 #include <cstdio>
10 #include <cstdlib>
11 #include <cstring>
12 #include <set>
13 #include <vector>
14
15 namespace MKLDNNPlugin {
16 namespace cpu {
17
18 #ifndef __APPLE__
19
20 struct Processor {
21     unsigned processor;
22     unsigned physicalId;
23     unsigned siblings;
24     unsigned coreId;
25     unsigned cpuCores;
26     unsigned speedMHz;
27
28     Processor();
29 };
30
31 class CpuInfoInterface {
32 public:
33     virtual ~CpuInfoInterface() {}
34
35     virtual const char *getFirstLine() = 0;
36
37     virtual const char *getNextLine() = 0;
38 };
39
40 class CpuInfo : public CpuInfoInterface {
41 public:
42     CpuInfo();
43
44     explicit CpuInfo(const char *content);
45
46     virtual ~CpuInfo();
47
48     virtual const char *getFirstLine();
49
50     virtual const char *getNextLine();
51
52 private:
53     const char *fileContentBegin;
54     const char *fileContentEnd;
55     const char *currentLine;
56
57     void loadContentFromFile(const char *fileName);
58
59     void loadContent(const char *content);
60
61     void parseLines(char *content);
62 };
63
64 class CollectionInterface {
65 public:
66     virtual ~CollectionInterface() {}
67
68     virtual unsigned getProcessorSpeedMHz() = 0;
69
70     virtual unsigned getTotalNumberOfSockets() = 0;
71
72     virtual unsigned getTotalNumberOfCpuCores() = 0;
73
74     virtual unsigned getNumberOfProcessors() = 0;
75
76     virtual const Processor &getProcessor(unsigned processorId) = 0;
77 };
78
79 class Collection : public CollectionInterface {
80 public:
81     explicit Collection(CpuInfoInterface *cpuInfo);
82
83     virtual unsigned getProcessorSpeedMHz();
84
85     virtual unsigned getTotalNumberOfSockets();
86
87     virtual unsigned getTotalNumberOfCpuCores();
88
89     virtual unsigned getNumberOfProcessors();
90
91     virtual const Processor &getProcessor(unsigned processorId);
92
93 private:
94     CpuInfoInterface &cpuInfo;
95     unsigned totalNumberOfSockets;
96     unsigned totalNumberOfCpuCores;
97     std::vector<Processor> processors;
98     Processor *currentProcessor;
99
100     Collection(const Collection &collection);
101
102     Collection &operator=(const Collection &collection);
103
104     void parseCpuInfo();
105
106     void parseCpuInfoLine(const char *cpuInfoLine);
107
108     void parseValue(const char *fieldName, const char *valueString);
109
110     void appendNewProcessor();
111
112     bool beginsWith(const char *lineBuffer, const char *text) const;
113
114     unsigned parseInteger(const char *text) const;
115
116     unsigned extractSpeedFromModelName(const char *text) const;
117
118     void collectBasicCpuInformation();
119
120     void updateCpuInformation(const Processor &processor,
121                               unsigned numberOfUniquePhysicalId);
122 };
123
124
125 class OpenMpManager {
126 public:
127     static void setGpuEnabled();
128
129     static void setGpuDisabled();
130
131     static void bindCurrentThreadToNonPrimaryCoreIfPossible();
132
133     static void bindOpenMpThreads(int env_cores = 0);
134
135     static int getOpenMpThreadNumber();
136
137     static void printVerboseInformation();
138
139     static bool isMajorThread(int currentThread);
140
141 private:
142     Collection &collection;
143
144     bool isGpuEnabled;
145     bool isAnyOpenMpEnvVarSpecified;
146     cpu_set_t currentCpuSet;
147     cpu_set_t currentCoreSet;
148
149     explicit OpenMpManager(Collection *collection);
150
151     OpenMpManager(const OpenMpManager &openMpManager);
152
153     OpenMpManager &operator=(const OpenMpManager &openMpManager);
154
155     static OpenMpManager &getInstance();
156
157     void getOpenMpEnvVars();
158
159     void getCurrentCpuSet();
160
161     int getCoreNumber();
162
163     void getDefaultCpuSet(cpu_set_t *defaultCpuSet);
164
165     void getCurrentCoreSet();
166
167     void selectAllCoreCpus(cpu_set_t *set, unsigned physicalCoreId);
168
169     unsigned getPhysicalCoreId(unsigned logicalCoreId);
170
171     bool isThreadsBindAllowed();
172
173     void setOpenMpThreadNumberLimit(int env_cores);
174
175     void bindCurrentThreadToLogicalCoreCpu(unsigned logicalCoreId);
176
177     void bindCurrentThreadToLogicalCoreCpus(unsigned logicalCoreId);
178 };
179
180 #endif  // #ifndef __APPLE__
181 }  // namespace cpu
182 }  // namespace MKLDNNPlugin