blob: fd6bfc3907b2114ca37254a5c9fd0537d48b981e [file] [log] [blame]
Moritz Pflanzerd929b9c2017-06-28 10:15:48 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 Arm Limited.
Moritz Pflanzerd929b9c2017-06-28 10:15:48 +01003 *
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 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_CPP_TYPES_H
25#define ARM_COMPUTE_CPP_TYPES_H
Moritz Pflanzerd929b9c2017-06-28 10:15:48 +010026
Pablo Tello4e0ac6f2018-10-01 14:31:11 +010027#include "arm_compute/core/Error.h"
28
Pablo Tello0cf77982018-10-24 15:32:39 +010029#include <array>
Pablo Tello4e0ac6f2018-10-01 14:31:11 +010030#include <string>
Pablo Tello7fad9b12018-03-14 17:55:27 +000031#include <vector>
32
Moritz Pflanzerd929b9c2017-06-28 10:15:48 +010033namespace arm_compute
34{
Pablo Tello7fad9b12018-03-14 17:55:27 +000035/** CPU models - we only need to detect CPUs we have
36 * microarchitecture-specific code for.
37 *
38 * Architecture features are detected via HWCAPs.
39 */
40enum class CPUModel
Moritz Pflanzerd929b9c2017-06-28 10:15:48 +010041{
Pablo Tello7fad9b12018-03-14 17:55:27 +000042 GENERIC,
Anthony Barbier5a3ee4f2018-07-24 11:24:17 +010043 GENERIC_FP16,
44 GENERIC_FP16_DOT,
Pablo Tello7fad9b12018-03-14 17:55:27 +000045 A53,
46 A55r0,
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +010047 A55r1,
David Mansell318c9f42020-07-08 13:28:45 +010048 X1,
49 A73
Moritz Pflanzerd929b9c2017-06-28 10:15:48 +010050};
Moritz Pflanzerc186b572017-09-07 09:48:04 +010051
Pablo Tello0cf77982018-10-24 15:32:39 +010052/** 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 */
59enum class MemoryPolicy
60{
61 MINIMIZE,
62 NORMAL
63};
64
Pablo Tello4e0ac6f2018-10-01 14:31:11 +010065/** 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 */
71inline 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 }
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +010099 case CPUModel::X1:
100 {
101 return std::string("X1");
102 }
David Mansell318c9f42020-07-08 13:28:45 +0100103 case CPUModel::A73:
104 {
105 return std::string("A73");
106 }
Pablo Tello4e0ac6f2018-10-01 14:31:11 +0100107 default:
108 {
109 ARM_COMPUTE_ERROR("Invalid CPUModel.");
110 return std::string("GENERIC");
111 }
112 }
113}
114
Pablo Tello7fad9b12018-03-14 17:55:27 +0000115class CPUInfo final
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100116{
Pablo Tello7fad9b12018-03-14 17:55:27 +0000117public:
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;
Georgios Pinitas879d1312019-09-30 13:25:53 +0100126 CPUInfo &operator=(CPUInfo &&cpuinfo) = default;
127 CPUInfo(CPUInfo &&cpuinfo) = default;
Pablo Tello7fad9b12018-03-14 17:55:27 +0000128
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
Anthony Barbier8914e322018-08-10 15:28:25 +0100193 /** Return the maximum number of CPUs present
194 *
195 * @return Number of CPUs
196 */
197 unsigned int get_cpu_num() const;
198
Pablo Tello7fad9b12018-03-14 17:55:27 +0000199private:
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;
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100205};
206
Pablo Tello0cf77982018-10-24 15:32:39 +0100207class MEMInfo final
208{
209public:
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
227private:
228 size_t _total;
229 size_t _free;
230 size_t _buffer;
231 static MemoryPolicy _policy;
232};
233
Alex Gildayc357c472018-03-21 13:54:09 +0000234/** Information about executing thread and CPU. */
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100235struct ThreadInfo
236{
Pablo Tello7fad9b12018-03-14 17:55:27 +0000237 int thread_id{ 0 };
238 int num_threads{ 1 };
239 const CPUInfo *cpu_info{ nullptr };
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100240};
Gian Marco Iodice356f6432017-09-22 11:32:21 +0100241} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000242#endif /* ARM_COMPUTE_CPP_TYPES_H */