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