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