Georgios Pinitas | 8a5146f | 2021-01-12 15:51:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2021 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 | #include "src/cpu/CpuContext.h" |
| 25 | |
| 26 | #include "arm_compute/core/CPP/CPPTypes.h" |
Georgios Pinitas | 3f26ef4 | 2021-02-23 10:01:33 +0000 | [diff] [blame] | 27 | #include "src/cpu/CpuTensor.h" |
Georgios Pinitas | 8a5146f | 2021-01-12 15:51:07 +0000 | [diff] [blame] | 28 | #include "src/runtime/CPUUtils.h" |
| 29 | |
| 30 | #include <cstdlib> |
Michalis Spyrou | 86ee237 | 2021-03-09 19:33:12 +0000 | [diff] [blame] | 31 | #include <malloc.h> |
Georgios Pinitas | 8a5146f | 2021-01-12 15:51:07 +0000 | [diff] [blame] | 32 | |
| 33 | namespace arm_compute |
| 34 | { |
| 35 | namespace cpu |
| 36 | { |
| 37 | namespace |
| 38 | { |
| 39 | void *default_allocate(void *user_data, size_t size) |
| 40 | { |
| 41 | ARM_COMPUTE_UNUSED(user_data); |
| 42 | return ::operator new(size); |
| 43 | } |
| 44 | void default_free(void *user_data, void *ptr) |
| 45 | { |
| 46 | ARM_COMPUTE_UNUSED(user_data); |
| 47 | ::operator delete(ptr); |
| 48 | } |
| 49 | void *default_aligned_allocate(void *user_data, size_t size, size_t alignment) |
| 50 | { |
| 51 | ARM_COMPUTE_UNUSED(user_data); |
| 52 | void *ptr = nullptr; |
| 53 | #if defined(BARE_METAL) || defined(__APPLE__) |
| 54 | size_t rem = size % alignment; |
| 55 | size_t real_size = (rem) ? (size + alignment - rem) : size; |
Michalis Spyrou | 86ee237 | 2021-03-09 19:33:12 +0000 | [diff] [blame] | 56 | ptr = memalign(alignment, real_size); |
Georgios Pinitas | 8a5146f | 2021-01-12 15:51:07 +0000 | [diff] [blame] | 57 | #else /* defined(BARE_METAL) || defined(__APPLE__) */ |
Sang-Hoon Park | 57d7366 | 2021-03-04 11:06:48 +0000 | [diff] [blame] | 58 | if(posix_memalign(&ptr, alignment, size) != 0) |
| 59 | { |
| 60 | // posix_memalign returns non-zero on failures, the return values will be |
| 61 | // - EINVAL: wrong alignment |
| 62 | // - ENOMEM: insufficient memory |
Michalis Spyrou | 86ee237 | 2021-03-09 19:33:12 +0000 | [diff] [blame] | 63 | ARM_COMPUTE_LOG_ERROR_ACL("posix_memalign failed, the returned pointer will be invalid"); |
Sang-Hoon Park | 57d7366 | 2021-03-04 11:06:48 +0000 | [diff] [blame] | 64 | } |
Georgios Pinitas | 8a5146f | 2021-01-12 15:51:07 +0000 | [diff] [blame] | 65 | #endif /* defined(BARE_METAL) || defined(__APPLE__) */ |
| 66 | return ptr; |
| 67 | } |
| 68 | void default_aligned_free(void *user_data, void *ptr) |
| 69 | { |
| 70 | ARM_COMPUTE_UNUSED(user_data); |
| 71 | free(ptr); |
| 72 | } |
| 73 | static AclAllocator default_allocator = { &default_allocate, |
| 74 | &default_free, |
| 75 | &default_aligned_allocate, |
| 76 | &default_aligned_free, |
| 77 | nullptr |
| 78 | }; |
| 79 | |
| 80 | AllocatorWrapper populate_allocator(AclAllocator *external_allocator) |
| 81 | { |
| 82 | bool is_valid = (external_allocator != nullptr); |
| 83 | if(is_valid) |
| 84 | { |
| 85 | is_valid = is_valid && (external_allocator->alloc != nullptr); |
| 86 | is_valid = is_valid && (external_allocator->free != nullptr); |
| 87 | is_valid = is_valid && (external_allocator->aligned_alloc != nullptr); |
| 88 | is_valid = is_valid && (external_allocator->aligned_free != nullptr); |
| 89 | } |
| 90 | return is_valid ? AllocatorWrapper(*external_allocator) : AllocatorWrapper(default_allocator); |
| 91 | } |
| 92 | |
| 93 | CpuCapabilities populate_capabilities_legacy(const CPUInfo &cpu_info) |
| 94 | { |
| 95 | CpuCapabilities caps; |
| 96 | |
| 97 | // Extract SIMD extension |
| 98 | caps.neon = true; |
| 99 | #ifdef SVE2 |
| 100 | caps.sve2 = true; |
| 101 | #endif /* SVE2 */ |
| 102 | // Extract data-type support |
| 103 | caps.fp16 = cpu_info.has_fp16(); |
| 104 | #ifdef V8P6_BF |
| 105 | caps.bf16 = true; |
| 106 | #endif /* V8P6_BF */ |
| 107 | |
| 108 | // Extract ISA extensions |
| 109 | caps.dot = cpu_info.has_dotprod(); |
| 110 | #ifdef MMLA_FP32 |
| 111 | caps.mmla_fp = true; |
| 112 | #endif /* MMLA_FP32 */ |
| 113 | #ifdef MMLA_INT8 |
| 114 | caps.mmla_int8 = true; |
| 115 | #endif /* MMLA_INT8 */ |
| 116 | |
| 117 | return caps; |
| 118 | } |
| 119 | |
| 120 | CpuCapabilities populate_capabilities_flags(AclTargetCapabilities external_caps) |
| 121 | { |
| 122 | CpuCapabilities caps; |
| 123 | |
| 124 | // Extract SIMD extension |
| 125 | caps.neon = external_caps & AclCpuCapabilitiesNeon; |
| 126 | caps.sve = external_caps & AclCpuCapabilitiesSve; |
| 127 | caps.sve2 = external_caps & AclCpuCapabilitiesSve2; |
| 128 | // Extract data-type support |
| 129 | caps.fp16 = external_caps & AclCpuCapabilitiesFp16; |
| 130 | caps.bf16 = external_caps & AclCpuCapabilitiesBf16; |
| 131 | // Extract ISA extensions |
| 132 | caps.dot = external_caps & AclCpuCapabilitiesDot; |
| 133 | caps.mmla_fp = external_caps & AclCpuCapabilitiesMmlaFp; |
| 134 | caps.mmla_int8 = external_caps & AclCpuCapabilitiesMmlaInt8; |
| 135 | |
| 136 | return caps; |
| 137 | } |
| 138 | |
| 139 | CpuCapabilities populate_capabilities(AclTargetCapabilities external_caps, |
| 140 | int32_t max_threads) |
| 141 | { |
| 142 | // Extract legacy structure |
| 143 | CPUInfo cpu_info; |
| 144 | arm_compute::utils::cpu::get_cpu_configuration(cpu_info); |
| 145 | |
| 146 | CpuCapabilities caps; |
| 147 | if(external_caps != AclCpuCapabilitiesAuto) |
| 148 | { |
| 149 | caps = populate_capabilities_flags(external_caps); |
| 150 | } |
| 151 | else |
| 152 | { |
| 153 | caps = populate_capabilities_legacy(cpu_info); |
| 154 | } |
| 155 | |
| 156 | // Set max number of threads |
| 157 | #if defined(BARE_METAL) |
| 158 | ARM_COMPUTE_UNUSED(max_threads); |
| 159 | caps.max_threads = 1; |
| 160 | #else /* defined(BARE_METAL) */ |
| 161 | caps.max_threads = (max_threads > 0) ? max_threads : std::thread::hardware_concurrency(); |
| 162 | #endif /* defined(BARE_METAL) */ |
| 163 | |
| 164 | return caps; |
| 165 | } |
| 166 | } // namespace |
| 167 | |
| 168 | CpuContext::CpuContext(const AclContextOptions *options) |
| 169 | : IContext(Target::Cpu), |
| 170 | _allocator(default_allocator), |
| 171 | _caps(populate_capabilities(AclCpuCapabilitiesAuto, -1)) |
| 172 | { |
| 173 | if(options != nullptr) |
| 174 | { |
| 175 | _allocator = populate_allocator(options->allocator); |
| 176 | _caps = populate_capabilities(options->capabilities, options->max_compute_units); |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | const CpuCapabilities &CpuContext::capabilities() const |
| 181 | { |
| 182 | return _caps; |
| 183 | } |
| 184 | |
| 185 | AllocatorWrapper &CpuContext::allocator() |
| 186 | { |
| 187 | return _allocator; |
| 188 | } |
Georgios Pinitas | 3f26ef4 | 2021-02-23 10:01:33 +0000 | [diff] [blame] | 189 | |
| 190 | ITensorV2 *CpuContext::create_tensor(const AclTensorDescriptor &desc, bool allocate) |
| 191 | { |
| 192 | CpuTensor *tensor = new CpuTensor(this, desc); |
| 193 | if(tensor != nullptr && allocate) |
| 194 | { |
| 195 | tensor->allocate(); |
| 196 | } |
| 197 | return tensor; |
| 198 | } |
Georgios Pinitas | 8a5146f | 2021-01-12 15:51:07 +0000 | [diff] [blame] | 199 | } // namespace cpu |
| 200 | } // namespace arm_compute |