1 /*
2 * Copyright (c) 2017-2020 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,
43 GENERIC_FP16,
44 GENERIC_FP16_DOT,
45 A53,
46 A55r0,
47 A55r1,
48 X1,
49 A73
50 };
51
52 /** Global memory policy.
53 * The functions in the runtime will use different strategies based on the policy currently set.
54 *
55 * MINIMIZE will try to reduce the amount allocated by the functions at the expense of performance normally.
56 * NORMAL won't try to save any memory and will favor speed over memory consumption
57 *
58 */
59 enum class MemoryPolicy
60 {
61 MINIMIZE,
62 NORMAL
63 };
64
65 /** Convert a cpumodel value to a string
66 *
67 * @param val CPUModel value to be converted
68 *
69 * @return String representing the corresponding CPUModel.
70 */
cpu_model_to_string(CPUModel val)71 inline std::string cpu_model_to_string(CPUModel val)
72 {
73 switch(val)
74 {
75 case CPUModel::GENERIC:
76 {
77 return std::string("GENERIC");
78 }
79 case CPUModel::GENERIC_FP16:
80 {
81 return std::string("GENERIC_FP16");
82 }
83 case CPUModel::GENERIC_FP16_DOT:
84 {
85 return std::string("GENERIC_FP16_DOT");
86 }
87 case CPUModel::A53:
88 {
89 return std::string("A53");
90 }
91 case CPUModel::A55r0:
92 {
93 return std::string("A55r0");
94 }
95 case CPUModel::A55r1:
96 {
97 return std::string("A55r1");
98 }
99 case CPUModel::X1:
100 {
101 return std::string("X1");
102 }
103 case CPUModel::A73:
104 {
105 return std::string("A73");
106 }
107 default:
108 {
109 ARM_COMPUTE_ERROR("Invalid CPUModel.");
110 return std::string("GENERIC");
111 }
112 }
113 }
114
115 class CPUInfo final
116 {
117 public:
118 /** Constructor */
119 CPUInfo();
120
121 /** Disable copy constructor and assignment operator to avoid copying the vector of CPUs each time
122 * CPUInfo is initialized once in the IScheduler and ThreadInfo will get a pointer to it.
123 */
124 CPUInfo &operator=(const CPUInfo &cpuinfo) = delete;
125 CPUInfo(const CPUInfo &cpuinfo) = delete;
126 CPUInfo &operator=(CPUInfo &&cpuinfo) = default;
127 CPUInfo(CPUInfo &&cpuinfo) = default;
128
129 /** Checks if the cpu model supports fp16.
130 *
131 * @return true of the cpu supports fp16, false otherwise
132 */
133 bool has_fp16() const;
134 /** Checks if the cpu model supports dot product.
135 *
136 * @return true of the cpu supports dot product, false otherwise
137 */
138 bool has_dotprod() const;
139 /** Gets the cpu model for a given cpuid.
140 *
141 * @param[in] cpuid the id of the cpu core to be retrieved,
142 *
143 * @return the @ref CPUModel of the cpuid queiried.
144 */
145 CPUModel get_cpu_model(unsigned int cpuid) const;
146 /** Gets the current thread's cpu model
147 *
148 * @return Current thread's @ref CPUModel
149 */
150 CPUModel get_cpu_model() const;
151 /** Gets the L1 cache size
152 *
153 * @return the size of the L1 cache
154 */
155 unsigned int get_L1_cache_size() const;
156 /** Gets the L2 cache size
157 *
158 * @return the size of the L1 cache
159 */
160 unsigned int get_L2_cache_size() const;
161 /** Set the L1 cache size
162 *
163 * @param[in] size the new size to be set.
164 */
165 void set_L1_cache_size(unsigned int size);
166 /** Set the L2 cache size
167 *
168 * @param[in] size the new size to be set.
169 */
170 void set_L2_cache_size(unsigned int size);
171 /** Set fp16 support
172 *
173 * @param[in] fp16 whether the cpu supports fp16.
174 */
175 void set_fp16(const bool fp16);
176 /** Set dot product support
177 *
178 * @param[in] dotprod whether the cpu supports dot product.
179 */
180 void set_dotprod(const bool dotprod);
181 /** Set the cpumodel for a given cpu core
182 *
183 * @param[in] cpuid the id of the core to be set.
184 * @param[in] model the @ref CPUModel to be set.
185 */
186 void set_cpu_model(unsigned int cpuid, CPUModel model);
187 /** Set max number of cpus
188 *
189 * @param[in] cpu_count the number of CPUs in the system.
190 */
191 void set_cpu_num(unsigned int cpu_count);
192
193 /** Return the maximum number of CPUs present
194 *
195 * @return Number of CPUs
196 */
197 unsigned int get_cpu_num() const;
198
199 private:
200 std::vector<CPUModel> _percpu = {};
201 bool _fp16 = false;
202 bool _dotprod = false;
203 unsigned int _L1_cache_size = 32768;
204 unsigned int _L2_cache_size = 262144;
205 };
206
207 class MEMInfo final
208 {
209 public:
210 MEMInfo();
211
212 /** Return the total amount of RAM memory in the system expressed in KB.
213 *
214 * @return Total memory
215 */
216 size_t get_total_in_kb() const;
217
218 static void set_policy(MemoryPolicy policy);
219 static MemoryPolicy get_policy();
220
221 /** Common memory sizes expressed in Kb to avoid having them
222 * duplicated throughout the code.
223 */
224 static const size_t ONE_GB_IN_KB = { 1035842 };
225 static const size_t TWO_GB_IN_KB = { ONE_GB_IN_KB * 2 };
226
227 private:
228 size_t _total;
229 size_t _free;
230 size_t _buffer;
231 static MemoryPolicy _policy;
232 };
233
234 /** Information about executing thread and CPU. */
235 struct ThreadInfo
236 {
237 int thread_id{ 0 };
238 int num_threads{ 1 };
239 const CPUInfo *cpu_info{ nullptr };
240 };
241 } // namespace arm_compute
242 #endif /* ARM_COMPUTE_CPP_TYPES_H */
243