blob: d91f9179634fe2a51734062c4535d8e382cf0557 [file] [log] [blame]
Georgios Pinitas8a5146f2021-01-12 15:51:07 +00001/*
Pablo Tello4e66d702022-03-07 18:20:12 +00002 * Copyright (c) 2021-2022 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
40namespace arm_compute
41{
42namespace cpu
43{
44namespace
45{
46void *default_allocate(void *user_data, size_t size)
47{
48 ARM_COMPUTE_UNUSED(user_data);
49 return ::operator new(size);
50}
51void default_free(void *user_data, void *ptr)
52{
53 ARM_COMPUTE_UNUSED(user_data);
54 ::operator delete(ptr);
55}
56void *default_aligned_allocate(void *user_data, size_t size, size_t alignment)
57{
58 ARM_COMPUTE_UNUSED(user_data);
59 void *ptr = nullptr;
Pablo Marquez Tello48f26152021-11-18 10:15:23 +000060#if defined(BARE_METAL)
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000061 size_t rem = size % alignment;
62 size_t real_size = (rem) ? (size + alignment - rem) : size;
Michalis Spyrou86ee2372021-03-09 19:33:12 +000063 ptr = memalign(alignment, real_size);
Pablo Marquez Tello48f26152021-11-18 10:15:23 +000064#else /* defined(BARE_METAL) */
Sang-Hoon Park57d73662021-03-04 11:06:48 +000065 if(posix_memalign(&ptr, alignment, size) != 0)
66 {
67 // posix_memalign returns non-zero on failures, the return values will be
68 // - EINVAL: wrong alignment
69 // - ENOMEM: insufficient memory
Michalis Spyrou86ee2372021-03-09 19:33:12 +000070 ARM_COMPUTE_LOG_ERROR_ACL("posix_memalign failed, the returned pointer will be invalid");
Sang-Hoon Park57d73662021-03-04 11:06:48 +000071 }
Pablo Marquez Tello48f26152021-11-18 10:15:23 +000072#endif /* defined(BARE_METAL) */
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000073 return ptr;
74}
75void default_aligned_free(void *user_data, void *ptr)
76{
77 ARM_COMPUTE_UNUSED(user_data);
78 free(ptr);
79}
80static AclAllocator default_allocator = { &default_allocate,
81 &default_free,
82 &default_aligned_allocate,
83 &default_aligned_free,
84 nullptr
85 };
86
87AllocatorWrapper populate_allocator(AclAllocator *external_allocator)
88{
89 bool is_valid = (external_allocator != nullptr);
90 if(is_valid)
91 {
92 is_valid = is_valid && (external_allocator->alloc != nullptr);
93 is_valid = is_valid && (external_allocator->free != nullptr);
94 is_valid = is_valid && (external_allocator->aligned_alloc != nullptr);
95 is_valid = is_valid && (external_allocator->aligned_free != nullptr);
96 }
97 return is_valid ? AllocatorWrapper(*external_allocator) : AllocatorWrapper(default_allocator);
98}
99
Michalis Spyrou20fca522021-06-07 14:23:57 +0100100cpuinfo::CpuIsaInfo populate_capabilities_flags(AclTargetCapabilities external_caps)
Georgios Pinitas8a5146f2021-01-12 15:51:07 +0000101{
Michalis Spyrou20fca522021-06-07 14:23:57 +0100102 cpuinfo::CpuIsaInfo isa_caps;
Georgios Pinitas8a5146f2021-01-12 15:51:07 +0000103
104 // Extract SIMD extension
Michalis Spyrou20fca522021-06-07 14:23:57 +0100105 isa_caps.neon = external_caps & AclCpuCapabilitiesNeon;
106 isa_caps.sve = external_caps & AclCpuCapabilitiesSve;
107 isa_caps.sve2 = external_caps & AclCpuCapabilitiesSve2;
Georgios Pinitas8a5146f2021-01-12 15:51:07 +0000108
Michalis Spyrou20fca522021-06-07 14:23:57 +0100109 // Extract data-type support
Georgios Pinitas731fe662021-06-24 20:32:11 +0100110 isa_caps.fp16 = external_caps & AclCpuCapabilitiesFp16;
111 isa_caps.bf16 = external_caps & AclCpuCapabilitiesBf16;
112 isa_caps.svebf16 = isa_caps.bf16;
Michalis Spyrou20fca522021-06-07 14:23:57 +0100113
114 // Extract ISA extensions
115 isa_caps.dot = external_caps & AclCpuCapabilitiesDot;
116 isa_caps.i8mm = external_caps & AclCpuCapabilitiesMmlaInt8;
117 isa_caps.svef32mm = external_caps & AclCpuCapabilitiesMmlaFp;
118
119 return isa_caps;
Georgios Pinitas8a5146f2021-01-12 15:51:07 +0000120}
121
122CpuCapabilities populate_capabilities(AclTargetCapabilities external_caps,
123 int32_t max_threads)
124{
Georgios Pinitas8a5146f2021-01-12 15:51:07 +0000125 CpuCapabilities caps;
Michalis Spyrou20fca522021-06-07 14:23:57 +0100126
Georgios Pinitas731fe662021-06-24 20:32:11 +0100127 // Populate capabilities with system information
128 caps.cpu_info = cpuinfo::CpuInfo::build();
Georgios Pinitas8a5146f2021-01-12 15:51:07 +0000129 if(external_caps != AclCpuCapabilitiesAuto)
130 {
Georgios Pinitas731fe662021-06-24 20:32:11 +0100131 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 Pinitas8a5146f2021-01-12 15:51:07 +0000135 }
Georgios Pinitas8a5146f2021-01-12 15:51:07 +0000136
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
149CpuContext::CpuContext(const AclContextOptions *options)
150 : IContext(Target::Cpu),
151 _allocator(default_allocator),
152 _caps(populate_capabilities(AclCpuCapabilitiesAuto, -1))
153{
154 if(options != nullptr)
155 {
156 _allocator = populate_allocator(options->allocator);
157 _caps = populate_capabilities(options->capabilities, options->max_compute_units);
158 }
159}
160
161const CpuCapabilities &CpuContext::capabilities() const
162{
163 return _caps;
164}
165
166AllocatorWrapper &CpuContext::allocator()
167{
168 return _allocator;
169}
Georgios Pinitas3f26ef42021-02-23 10:01:33 +0000170
171ITensorV2 *CpuContext::create_tensor(const AclTensorDescriptor &desc, bool allocate)
172{
173 CpuTensor *tensor = new CpuTensor(this, desc);
174 if(tensor != nullptr && allocate)
175 {
176 tensor->allocate();
177 }
178 return tensor;
179}
Georgios Pinitasc3c352e2021-03-18 10:59:40 +0000180
181IQueue *CpuContext::create_queue(const AclQueueOptions *options)
182{
183 return new CpuQueue(this, options);
184}
Georgios Pinitas8a5146f2021-01-12 15:51:07 +0000185} // namespace cpu
186} // namespace arm_compute