blob: 7c14891ef801ffc86dca8d7acb38ea8f232d7e71 [file] [log] [blame]
Georgios Pinitas8a5146f2021-01-12 15:51:07 +00001/*
Matthew Bentham1d062042023-07-06 13:13:59 +00002 * Copyright (c) 2021-2023 Arm Limited.
Georgios Pinitas8a5146f2021-01-12 15:51:07 +00003 *
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 Pinitasc3c352e2021-03-18 10:59:40 +000027#include "src/cpu/CpuQueue.h"
Georgios Pinitas3f26ef42021-02-23 10:01:33 +000028#include "src/cpu/CpuTensor.h"
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000029
30#include <cstdlib>
Kevin Lo7195f712022-01-07 15:46:02 +080031#if !defined(__APPLE__) && !defined(__OpenBSD__)
Michalis Spyrou86ee2372021-03-09 19:33:12 +000032#include <malloc.h>
Pablo Marquez Tello48f26152021-11-18 10:15:23 +000033
Pablo Tello4e66d702022-03-07 18:20:12 +000034#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 Pinitas8a5146f2021-01-12 15:51:07 +000039
Matthew Bentham1d062042023-07-06 13:13:59 +000040#ifndef BARE_METAL
41#include <thread>
42#endif /* BARE_METAL */
43
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000044namespace arm_compute
45{
46namespace cpu
47{
48namespace
49{
50void *default_allocate(void *user_data, size_t size)
51{
52 ARM_COMPUTE_UNUSED(user_data);
53 return ::operator new(size);
54}
55void default_free(void *user_data, void *ptr)
56{
57 ARM_COMPUTE_UNUSED(user_data);
58 ::operator delete(ptr);
59}
60void *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 Tello48f26152021-11-18 10:15:23 +000064#if defined(BARE_METAL)
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000065 size_t rem = size % alignment;
66 size_t real_size = (rem) ? (size + alignment - rem) : size;
Michalis Spyrou86ee2372021-03-09 19:33:12 +000067 ptr = memalign(alignment, real_size);
Pablo Marquez Tello48f26152021-11-18 10:15:23 +000068#else /* defined(BARE_METAL) */
Sang-Hoon Park57d73662021-03-04 11:06:48 +000069 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 Spyrou86ee2372021-03-09 19:33:12 +000074 ARM_COMPUTE_LOG_ERROR_ACL("posix_memalign failed, the returned pointer will be invalid");
Sang-Hoon Park57d73662021-03-04 11:06:48 +000075 }
Pablo Marquez Tello48f26152021-11-18 10:15:23 +000076#endif /* defined(BARE_METAL) */
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000077 return ptr;
78}
79void default_aligned_free(void *user_data, void *ptr)
80{
81 ARM_COMPUTE_UNUSED(user_data);
82 free(ptr);
83}
84static AclAllocator default_allocator = { &default_allocate,
85 &default_free,
86 &default_aligned_allocate,
87 &default_aligned_free,
88 nullptr
89 };
90
91AllocatorWrapper 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 Spyrou20fca522021-06-07 14:23:57 +0100104cpuinfo::CpuIsaInfo populate_capabilities_flags(AclTargetCapabilities external_caps)
Georgios Pinitas8a5146f2021-01-12 15:51:07 +0000105{
Michalis Spyrou20fca522021-06-07 14:23:57 +0100106 cpuinfo::CpuIsaInfo isa_caps;
Georgios Pinitas8a5146f2021-01-12 15:51:07 +0000107
108 // Extract SIMD extension
Michalis Spyrou20fca522021-06-07 14:23:57 +0100109 isa_caps.neon = external_caps & AclCpuCapabilitiesNeon;
110 isa_caps.sve = external_caps & AclCpuCapabilitiesSve;
111 isa_caps.sve2 = external_caps & AclCpuCapabilitiesSve2;
Georgios Pinitas8a5146f2021-01-12 15:51:07 +0000112
Michalis Spyrou20fca522021-06-07 14:23:57 +0100113 // Extract data-type support
Georgios Pinitas731fe662021-06-24 20:32:11 +0100114 isa_caps.fp16 = external_caps & AclCpuCapabilitiesFp16;
115 isa_caps.bf16 = external_caps & AclCpuCapabilitiesBf16;
116 isa_caps.svebf16 = isa_caps.bf16;
Michalis Spyrou20fca522021-06-07 14:23:57 +0100117
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 Pinitas8a5146f2021-01-12 15:51:07 +0000124}
125
126CpuCapabilities populate_capabilities(AclTargetCapabilities external_caps,
127 int32_t max_threads)
128{
Georgios Pinitas8a5146f2021-01-12 15:51:07 +0000129 CpuCapabilities caps;
Michalis Spyrou20fca522021-06-07 14:23:57 +0100130
Georgios Pinitas731fe662021-06-24 20:32:11 +0100131 // Populate capabilities with system information
132 caps.cpu_info = cpuinfo::CpuInfo::build();
Georgios Pinitas8a5146f2021-01-12 15:51:07 +0000133 if(external_caps != AclCpuCapabilitiesAuto)
134 {
Georgios Pinitas731fe662021-06-24 20:32:11 +0100135 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 Pinitas8a5146f2021-01-12 15:51:07 +0000139 }
Georgios Pinitas8a5146f2021-01-12 15:51:07 +0000140
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
153CpuContext::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
165const CpuCapabilities &CpuContext::capabilities() const
166{
167 return _caps;
168}
169
170AllocatorWrapper &CpuContext::allocator()
171{
172 return _allocator;
173}
Georgios Pinitas3f26ef42021-02-23 10:01:33 +0000174
175ITensorV2 *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 Pinitasc3c352e2021-03-18 10:59:40 +0000184
185IQueue *CpuContext::create_queue(const AclQueueOptions *options)
186{
187 return new CpuQueue(this, options);
188}
Georgios Pinitas8a5146f2021-01-12 15:51:07 +0000189} // namespace cpu
190} // namespace arm_compute