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