Moritz Pflanzer | d929b9c | 2017-06-28 10:15:48 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | 879d131 | 2019-09-30 13:25:53 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 ARM Limited. |
Moritz Pflanzer | d929b9c | 2017-06-28 10:15:48 +0100 | [diff] [blame] | 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 | |
Pablo Tello | 4e0ac6f | 2018-10-01 14:31:11 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/Error.h" |
| 28 | |
Pablo Tello | 0cf7798 | 2018-10-24 15:32:39 +0100 | [diff] [blame] | 29 | #include <array> |
Pablo Tello | 4e0ac6f | 2018-10-01 14:31:11 +0100 | [diff] [blame] | 30 | #include <string> |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 31 | #include <vector> |
| 32 | |
Moritz Pflanzer | d929b9c | 2017-06-28 10:15:48 +0100 | [diff] [blame] | 33 | namespace arm_compute |
| 34 | { |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 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 |
Moritz Pflanzer | d929b9c | 2017-06-28 10:15:48 +0100 | [diff] [blame] | 41 | { |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 42 | GENERIC, |
Anthony Barbier | 5a3ee4f | 2018-07-24 11:24:17 +0100 | [diff] [blame] | 43 | GENERIC_FP16, |
| 44 | GENERIC_FP16_DOT, |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 45 | A53, |
| 46 | A55r0, |
Anthony Barbier | 5a3ee4f | 2018-07-24 11:24:17 +0100 | [diff] [blame] | 47 | A55r1 |
Moritz Pflanzer | d929b9c | 2017-06-28 10:15:48 +0100 | [diff] [blame] | 48 | }; |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 49 | |
Pablo Tello | 0cf7798 | 2018-10-24 15:32:39 +0100 | [diff] [blame] | 50 | /** 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 | */ |
| 57 | enum class MemoryPolicy |
| 58 | { |
| 59 | MINIMIZE, |
| 60 | NORMAL |
| 61 | }; |
| 62 | |
Pablo Tello | 4e0ac6f | 2018-10-01 14:31:11 +0100 | [diff] [blame] | 63 | /** 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 | */ |
| 69 | inline 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 Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 105 | class CPUInfo final |
Moritz Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 106 | { |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 107 | public: |
| 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 Pinitas | 879d131 | 2019-09-30 13:25:53 +0100 | [diff] [blame] | 116 | CPUInfo &operator=(CPUInfo &&cpuinfo) = default; |
| 117 | CPUInfo(CPUInfo &&cpuinfo) = default; |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 118 | |
| 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 Barbier | 8914e32 | 2018-08-10 15:28:25 +0100 | [diff] [blame] | 183 | /** Return the maximum number of CPUs present |
| 184 | * |
| 185 | * @return Number of CPUs |
| 186 | */ |
| 187 | unsigned int get_cpu_num() const; |
| 188 | |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 189 | private: |
| 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 Pflanzer | beabe3b | 2017-08-31 14:56:32 +0100 | [diff] [blame] | 195 | }; |
| 196 | |
Pablo Tello | 0cf7798 | 2018-10-24 15:32:39 +0100 | [diff] [blame] | 197 | class MEMInfo final |
| 198 | { |
| 199 | public: |
| 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 | |
| 217 | private: |
| 218 | size_t _total; |
| 219 | size_t _free; |
| 220 | size_t _buffer; |
| 221 | static MemoryPolicy _policy; |
| 222 | }; |
| 223 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 224 | /** Information about executing thread and CPU. */ |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 225 | struct ThreadInfo |
| 226 | { |
Pablo Tello | 7fad9b1 | 2018-03-14 17:55:27 +0000 | [diff] [blame] | 227 | int thread_id{ 0 }; |
| 228 | int num_threads{ 1 }; |
| 229 | const CPUInfo *cpu_info{ nullptr }; |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 230 | }; |
Gian Marco Iodice | 356f643 | 2017-09-22 11:32:21 +0100 | [diff] [blame] | 231 | } // namespace arm_compute |
Moritz Pflanzer | d929b9c | 2017-06-28 10:15:48 +0100 | [diff] [blame] | 232 | #endif /* __ARM_COMPUTE_CPP_TYPES_H__ */ |