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