blob: be6dfacb3856654824b5ff4d8aa5f73da304a36d [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,
48 X1
Moritz Pflanzerd929b9c2017-06-28 10:15:48 +010049};
Moritz Pflanzerc186b572017-09-07 09:48:04 +010050
Pablo Tello0cf77982018-10-24 15:32:39 +010051/** Global memory policy.
52 * The functions in the runtime will use different strategies based on the policy currently set.
53 *
54 * MINIMIZE will try to reduce the amount allocated by the functions at the expense of performance normally.
55 * NORMAL won't try to save any memory and will favor speed over memory consumption
56 *
57 */
58enum class MemoryPolicy
59{
60 MINIMIZE,
61 NORMAL
62};
63
Pablo Tello4e0ac6f2018-10-01 14:31:11 +010064/** Convert a cpumodel value to a string
65 *
66 * @param val CPUModel value to be converted
67 *
68 * @return String representing the corresponding CPUModel.
69 */
70inline std::string cpu_model_to_string(CPUModel val)
71{
72 switch(val)
73 {
74 case CPUModel::GENERIC:
75 {
76 return std::string("GENERIC");
77 }
78 case CPUModel::GENERIC_FP16:
79 {
80 return std::string("GENERIC_FP16");
81 }
82 case CPUModel::GENERIC_FP16_DOT:
83 {
84 return std::string("GENERIC_FP16_DOT");
85 }
86 case CPUModel::A53:
87 {
88 return std::string("A53");
89 }
90 case CPUModel::A55r0:
91 {
92 return std::string("A55r0");
93 }
94 case CPUModel::A55r1:
95 {
96 return std::string("A55r1");
97 }
Georgios Pinitas5aa1a0b2020-07-02 20:02:20 +010098 case CPUModel::X1:
99 {
100 return std::string("X1");
101 }
Pablo Tello4e0ac6f2018-10-01 14:31:11 +0100102 default:
103 {
104 ARM_COMPUTE_ERROR("Invalid CPUModel.");
105 return std::string("GENERIC");
106 }
107 }
108}
109
Pablo Tello7fad9b12018-03-14 17:55:27 +0000110class CPUInfo final
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100111{
Pablo Tello7fad9b12018-03-14 17:55:27 +0000112public:
113 /** Constructor */
114 CPUInfo();
115
116 /** Disable copy constructor and assignment operator to avoid copying the vector of CPUs each time
117 * CPUInfo is initialized once in the IScheduler and ThreadInfo will get a pointer to it.
118 */
119 CPUInfo &operator=(const CPUInfo &cpuinfo) = delete;
120 CPUInfo(const CPUInfo &cpuinfo) = delete;
Georgios Pinitas879d1312019-09-30 13:25:53 +0100121 CPUInfo &operator=(CPUInfo &&cpuinfo) = default;
122 CPUInfo(CPUInfo &&cpuinfo) = default;
Pablo Tello7fad9b12018-03-14 17:55:27 +0000123
124 /** Checks if the cpu model supports fp16.
125 *
126 * @return true of the cpu supports fp16, false otherwise
127 */
128 bool has_fp16() const;
129 /** Checks if the cpu model supports dot product.
130 *
131 * @return true of the cpu supports dot product, false otherwise
132 */
133 bool has_dotprod() const;
134 /** Gets the cpu model for a given cpuid.
135 *
136 * @param[in] cpuid the id of the cpu core to be retrieved,
137 *
138 * @return the @ref CPUModel of the cpuid queiried.
139 */
140 CPUModel get_cpu_model(unsigned int cpuid) const;
141 /** Gets the current thread's cpu model
142 *
143 * @return Current thread's @ref CPUModel
144 */
145 CPUModel get_cpu_model() const;
146 /** Gets the L1 cache size
147 *
148 * @return the size of the L1 cache
149 */
150 unsigned int get_L1_cache_size() const;
151 /** Gets the L2 cache size
152 *
153 * @return the size of the L1 cache
154 */
155 unsigned int get_L2_cache_size() const;
156 /** Set the L1 cache size
157 *
158 * @param[in] size the new size to be set.
159 */
160 void set_L1_cache_size(unsigned int size);
161 /** Set the L2 cache size
162 *
163 * @param[in] size the new size to be set.
164 */
165 void set_L2_cache_size(unsigned int size);
166 /** Set fp16 support
167 *
168 * @param[in] fp16 whether the cpu supports fp16.
169 */
170 void set_fp16(const bool fp16);
171 /** Set dot product support
172 *
173 * @param[in] dotprod whether the cpu supports dot product.
174 */
175 void set_dotprod(const bool dotprod);
176 /** Set the cpumodel for a given cpu core
177 *
178 * @param[in] cpuid the id of the core to be set.
179 * @param[in] model the @ref CPUModel to be set.
180 */
181 void set_cpu_model(unsigned int cpuid, CPUModel model);
182 /** Set max number of cpus
183 *
184 * @param[in] cpu_count the number of CPUs in the system.
185 */
186 void set_cpu_num(unsigned int cpu_count);
187
Anthony Barbier8914e322018-08-10 15:28:25 +0100188 /** Return the maximum number of CPUs present
189 *
190 * @return Number of CPUs
191 */
192 unsigned int get_cpu_num() const;
193
Pablo Tello7fad9b12018-03-14 17:55:27 +0000194private:
195 std::vector<CPUModel> _percpu = {};
196 bool _fp16 = false;
197 bool _dotprod = false;
198 unsigned int _L1_cache_size = 32768;
199 unsigned int _L2_cache_size = 262144;
Moritz Pflanzerbeabe3b2017-08-31 14:56:32 +0100200};
201
Pablo Tello0cf77982018-10-24 15:32:39 +0100202class MEMInfo final
203{
204public:
205 MEMInfo();
206
207 /** Return the total amount of RAM memory in the system expressed in KB.
208 *
209 * @return Total memory
210 */
211 size_t get_total_in_kb() const;
212
213 static void set_policy(MemoryPolicy policy);
214 static MemoryPolicy get_policy();
215
216 /** Common memory sizes expressed in Kb to avoid having them
217 * duplicated throughout the code.
218 */
219 static const size_t ONE_GB_IN_KB = { 1035842 };
220 static const size_t TWO_GB_IN_KB = { ONE_GB_IN_KB * 2 };
221
222private:
223 size_t _total;
224 size_t _free;
225 size_t _buffer;
226 static MemoryPolicy _policy;
227};
228
Alex Gildayc357c472018-03-21 13:54:09 +0000229/** Information about executing thread and CPU. */
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100230struct ThreadInfo
231{
Pablo Tello7fad9b12018-03-14 17:55:27 +0000232 int thread_id{ 0 };
233 int num_threads{ 1 };
234 const CPUInfo *cpu_info{ nullptr };
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100235};
Gian Marco Iodice356f6432017-09-22 11:32:21 +0100236} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +0000237#endif /* ARM_COMPUTE_CPP_TYPES_H */