Compute Library
 21.05
CPPTypes.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2021 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #ifndef ARM_COMPUTE_CPP_TYPES_H
25 #define ARM_COMPUTE_CPP_TYPES_H
26 
27 #include "arm_compute/core/Error.h"
28 
29 #include <array>
30 #include <string>
31 #include <vector>
32 
33 namespace arm_compute
34 {
35 /** CPU models - we only need to detect CPUs we have
36  * microarchitecture-specific code for.
37  *
38  * Architecture features are detected via HWCAPs.
39  */
40 enum class CPUModel
41 {
42  GENERIC,
45  A35,
46  A53,
47  A55r0,
48  A55r1,
49  KLEIN,
50  X1,
51  A73
52 };
53 
54 /** Global memory policy.
55  * The functions in the runtime will use different strategies based on the policy currently set.
56  *
57  * MINIMIZE will try to reduce the amount allocated by the functions at the expense of performance normally.
58  * NORMAL won't try to save any memory and will favor speed over memory consumption
59  *
60  */
61 enum class MemoryPolicy
62 {
63  MINIMIZE,
64  NORMAL
65 };
66 
67 /** Convert a cpumodel value to a string
68  *
69  * @param val CPUModel value to be converted
70  *
71  * @return String representing the corresponding CPUModel.
72  */
73 inline std::string cpu_model_to_string(CPUModel val)
74 {
75  switch(val)
76  {
77  case CPUModel::GENERIC:
78  {
79  return std::string("GENERIC");
80  }
81  case CPUModel::KLEIN:
82  {
83  return std::string("KLEIN");
84  }
86  {
87  return std::string("GENERIC_FP16");
88  }
90  {
91  return std::string("GENERIC_FP16_DOT");
92  }
93  case CPUModel::A53:
94  {
95  return std::string("A53");
96  }
97  case CPUModel::A55r0:
98  {
99  return std::string("A55r0");
100  }
101  case CPUModel::A55r1:
102  {
103  return std::string("A55r1");
104  }
105  case CPUModel::X1:
106  {
107  return std::string("X1");
108  }
109  case CPUModel::A73:
110  {
111  return std::string("A73");
112  }
113  default:
114  {
115  ARM_COMPUTE_ERROR("Invalid CPUModel.");
116  return std::string("GENERIC");
117  }
118  }
119 }
120 
121 class CPUInfo final
122 {
123 public:
124  /** Constructor */
125  CPUInfo();
126 
127  /** Disable copy constructor and assignment operator to avoid copying the vector of CPUs each time
128  * CPUInfo is initialized once in the IScheduler and ThreadInfo will get a pointer to it.
129  */
130  CPUInfo &operator=(const CPUInfo &cpuinfo) = delete;
131  CPUInfo(const CPUInfo &cpuinfo) = delete;
132  CPUInfo &operator=(CPUInfo &&cpuinfo) = default;
133  CPUInfo(CPUInfo &&cpuinfo) = default;
134 
135  /** Checks if the cpu model supports fp16.
136  *
137  * @return true of the cpu supports fp16, false otherwise
138  */
139  bool has_fp16() const;
140  /** Checks if the cpu model supports dot product.
141  *
142  * @return true of the cpu supports dot product, false otherwise
143  */
144  bool has_dotprod() const;
145  /** Checks if the cpu model supports sve.
146  *
147  * @return true of the cpu supports sve, false otherwise
148  */
149  bool has_sve() const;
150  /** Gets the cpu model for a given cpuid.
151  *
152  * @param[in] cpuid the id of the cpu core to be retrieved,
153  *
154  * @return the @ref CPUModel of the cpuid queiried.
155  */
156  CPUModel get_cpu_model(unsigned int cpuid) const;
157  /** Gets the current thread's cpu model
158  *
159  * @return Current thread's @ref CPUModel
160  */
161  CPUModel get_cpu_model() const;
162  /** Gets the L1 cache size
163  *
164  * @return the size of the L1 cache
165  */
166  unsigned int get_L1_cache_size() const;
167  /** Gets the L2 cache size
168  *
169  * @return the size of the L1 cache
170  */
171  unsigned int get_L2_cache_size() const;
172  /** Set the L1 cache size
173  *
174  * @param[in] size the new size to be set.
175  */
176  void set_L1_cache_size(unsigned int size);
177  /** Set the L2 cache size
178  *
179  * @param[in] size the new size to be set.
180  */
181  void set_L2_cache_size(unsigned int size);
182  /** Set fp16 support
183  *
184  * @param[in] fp16 whether the cpu supports fp16.
185  */
186  void set_fp16(const bool fp16);
187  /** Set dot product support
188  *
189  * @param[in] dotprod whether the cpu supports dot product.
190  */
191  void set_dotprod(const bool dotprod);
192  /** Set sve support
193  *
194  * @param[in] sve whether the cpu supports sve.
195  */
196  void set_sve(const bool sve);
197  /** Set the cpumodel for a given cpu core
198  *
199  * @param[in] cpuid the id of the core to be set.
200  * @param[in] model the @ref CPUModel to be set.
201  */
202  void set_cpu_model(unsigned int cpuid, CPUModel model);
203  /** Set max number of cpus
204  *
205  * @param[in] cpu_count the number of CPUs in the system.
206  */
207  void set_cpu_num(unsigned int cpu_count);
208 
209  /** Return the maximum number of CPUs present
210  *
211  * @return Number of CPUs
212  */
213  unsigned int get_cpu_num() const;
214 
215 private:
216  std::vector<CPUModel> _percpu = {};
217  bool _fp16 = false;
218  bool _dotprod = false;
219  bool _sve = false;
220  unsigned int _L1_cache_size = 32768;
221  unsigned int _L2_cache_size = 262144;
222 };
223 
224 class MEMInfo final
225 {
226 public:
227  MEMInfo();
228 
229  /** Return the total amount of RAM memory in the system expressed in KB.
230  *
231  * @return Total memory
232  */
233  size_t get_total_in_kb() const;
234 
235  static void set_policy(MemoryPolicy policy);
236  static MemoryPolicy get_policy();
237 
238  /** Common memory sizes expressed in Kb to avoid having them
239  * duplicated throughout the code.
240  */
241  static const size_t ONE_GB_IN_KB = { 1035842 };
242  static const size_t TWO_GB_IN_KB = { ONE_GB_IN_KB * 2 };
243 
244 private:
245  size_t _total;
246  size_t _free;
247  size_t _buffer;
248  static MemoryPolicy _policy;
249 };
250 
251 /** Information about executing thread and CPU. */
253 {
254  int thread_id{ 0 };
255  int num_threads{ 1 };
256  const CPUInfo *cpu_info{ nullptr };
257 };
258 } // namespace arm_compute
259 #endif /* ARM_COMPUTE_CPP_TYPES_H */
void set_L2_cache_size(unsigned int size)
Set the L2 cache size.
Definition: CPPTypes.cpp:103
bool has_sve() const
Checks if the cpu model supports sve.
Definition: CPPTypes.cpp:64
CPUInfo()
Constructor.
Definition: CPPTypes.cpp:113
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
bool has_fp16() const
Checks if the cpu model supports fp16.
Definition: CPPTypes.cpp:69
CPUModel
CPU models - we only need to detect CPUs we have microarchitecture-specific code for.
Definition: CPPTypes.h:40
Copyright (c) 2017-2021 Arm Limited.
std::string cpu_model_to_string(CPUModel val)
Convert a cpumodel value to a string.
Definition: CPPTypes.h:73
static const size_t TWO_GB_IN_KB
Definition: CPPTypes.h:242
static MemoryPolicy get_policy()
Definition: MEMUtils.cpp:86
bool has_dotprod() const
Checks if the cpu model supports dot product.
Definition: CPPTypes.cpp:74
void set_cpu_model(unsigned int cpuid, CPUModel model)
Set the cpumodel for a given cpu core.
Definition: CPPTypes.cpp:50
CPUInfo & operator=(const CPUInfo &cpuinfo)=delete
Disable copy constructor and assignment operator to avoid copying the vector of CPUs each time CPUInf...
unsigned int get_cpu_num() const
Return the maximum number of CPUs present.
Definition: CPPTypes.cpp:59
unsigned int get_L1_cache_size() const
Gets the L1 cache size.
Definition: CPPTypes.cpp:88
unsigned int get_L2_cache_size() const
Gets the L2 cache size.
Definition: CPPTypes.cpp:98
size_t get_total_in_kb() const
Return the total amount of RAM memory in the system expressed in KB.
Definition: MEMUtils.cpp:98
static void set_policy(MemoryPolicy policy)
Definition: MEMUtils.cpp:81
Information about executing thread and CPU.
Definition: CPPTypes.h:252
void set_sve(const bool sve)
Set sve support.
Definition: CPPTypes.cpp:45
CPUModel get_cpu_model() const
Gets the current thread's cpu model.
Definition: CPPTypes.cpp:122
void set_L1_cache_size(unsigned int size)
Set the L1 cache size.
Definition: CPPTypes.cpp:93
static const size_t ONE_GB_IN_KB
Common memory sizes expressed in Kb to avoid having them duplicated throughout the code.
Definition: CPPTypes.h:241
void set_dotprod(const bool dotprod)
Set dot product support.
Definition: CPPTypes.cpp:40
MemoryPolicy
Global memory policy.
Definition: CPPTypes.h:61
void set_fp16(const bool fp16)
Set fp16 support.
Definition: CPPTypes.cpp:35
const CPUInfo * cpu_info
Definition: CPPTypes.h:256
void set_cpu_num(unsigned int cpu_count)
Set max number of cpus.
Definition: CPPTypes.cpp:108