blob: d62c1b6310d5c1c41f63dbf6afbde66b73a4fa28 [file] [log] [blame]
Georgios Pinitas8a5146f2021-01-12 15:51:07 +00001/*
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 Pinitas3f26ef42021-02-23 10:01:33 +000027#include "src/cpu/CpuTensor.h"
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000028#include "src/runtime/CPUUtils.h"
29
30#include <cstdlib>
Michalis Spyrou86ee2372021-03-09 19:33:12 +000031#include <malloc.h>
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000032
33namespace arm_compute
34{
35namespace cpu
36{
37namespace
38{
39void *default_allocate(void *user_data, size_t size)
40{
41 ARM_COMPUTE_UNUSED(user_data);
42 return ::operator new(size);
43}
44void default_free(void *user_data, void *ptr)
45{
46 ARM_COMPUTE_UNUSED(user_data);
47 ::operator delete(ptr);
48}
49void *default_aligned_allocate(void *user_data, size_t size, size_t alignment)
50{
51 ARM_COMPUTE_UNUSED(user_data);
52 void *ptr = nullptr;
53#if defined(BARE_METAL) || defined(__APPLE__)
54 size_t rem = size % alignment;
55 size_t real_size = (rem) ? (size + alignment - rem) : size;
Michalis Spyrou86ee2372021-03-09 19:33:12 +000056 ptr = memalign(alignment, real_size);
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000057#else /* defined(BARE_METAL) || defined(__APPLE__) */
Sang-Hoon Park57d73662021-03-04 11:06:48 +000058 if(posix_memalign(&ptr, alignment, size) != 0)
59 {
60 // posix_memalign returns non-zero on failures, the return values will be
61 // - EINVAL: wrong alignment
62 // - ENOMEM: insufficient memory
Michalis Spyrou86ee2372021-03-09 19:33:12 +000063 ARM_COMPUTE_LOG_ERROR_ACL("posix_memalign failed, the returned pointer will be invalid");
Sang-Hoon Park57d73662021-03-04 11:06:48 +000064 }
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000065#endif /* defined(BARE_METAL) || defined(__APPLE__) */
66 return ptr;
67}
68void default_aligned_free(void *user_data, void *ptr)
69{
70 ARM_COMPUTE_UNUSED(user_data);
71 free(ptr);
72}
73static AclAllocator default_allocator = { &default_allocate,
74 &default_free,
75 &default_aligned_allocate,
76 &default_aligned_free,
77 nullptr
78 };
79
80AllocatorWrapper populate_allocator(AclAllocator *external_allocator)
81{
82 bool is_valid = (external_allocator != nullptr);
83 if(is_valid)
84 {
85 is_valid = is_valid && (external_allocator->alloc != nullptr);
86 is_valid = is_valid && (external_allocator->free != nullptr);
87 is_valid = is_valid && (external_allocator->aligned_alloc != nullptr);
88 is_valid = is_valid && (external_allocator->aligned_free != nullptr);
89 }
90 return is_valid ? AllocatorWrapper(*external_allocator) : AllocatorWrapper(default_allocator);
91}
92
93CpuCapabilities populate_capabilities_legacy(const CPUInfo &cpu_info)
94{
95 CpuCapabilities caps;
96
97 // Extract SIMD extension
98 caps.neon = true;
99#ifdef SVE2
100 caps.sve2 = true;
101#endif /* SVE2 */
102 // Extract data-type support
103 caps.fp16 = cpu_info.has_fp16();
104#ifdef V8P6_BF
105 caps.bf16 = true;
106#endif /* V8P6_BF */
107
108 // Extract ISA extensions
109 caps.dot = cpu_info.has_dotprod();
110#ifdef MMLA_FP32
111 caps.mmla_fp = true;
112#endif /* MMLA_FP32 */
113#ifdef MMLA_INT8
114 caps.mmla_int8 = true;
115#endif /* MMLA_INT8 */
116
117 return caps;
118}
119
120CpuCapabilities populate_capabilities_flags(AclTargetCapabilities external_caps)
121{
122 CpuCapabilities caps;
123
124 // Extract SIMD extension
125 caps.neon = external_caps & AclCpuCapabilitiesNeon;
126 caps.sve = external_caps & AclCpuCapabilitiesSve;
127 caps.sve2 = external_caps & AclCpuCapabilitiesSve2;
128 // Extract data-type support
129 caps.fp16 = external_caps & AclCpuCapabilitiesFp16;
130 caps.bf16 = external_caps & AclCpuCapabilitiesBf16;
131 // Extract ISA extensions
132 caps.dot = external_caps & AclCpuCapabilitiesDot;
133 caps.mmla_fp = external_caps & AclCpuCapabilitiesMmlaFp;
134 caps.mmla_int8 = external_caps & AclCpuCapabilitiesMmlaInt8;
135
136 return caps;
137}
138
139CpuCapabilities populate_capabilities(AclTargetCapabilities external_caps,
140 int32_t max_threads)
141{
142 // Extract legacy structure
143 CPUInfo cpu_info;
144 arm_compute::utils::cpu::get_cpu_configuration(cpu_info);
145
146 CpuCapabilities caps;
147 if(external_caps != AclCpuCapabilitiesAuto)
148 {
149 caps = populate_capabilities_flags(external_caps);
150 }
151 else
152 {
153 caps = populate_capabilities_legacy(cpu_info);
154 }
155
156 // Set max number of threads
157#if defined(BARE_METAL)
158 ARM_COMPUTE_UNUSED(max_threads);
159 caps.max_threads = 1;
160#else /* defined(BARE_METAL) */
161 caps.max_threads = (max_threads > 0) ? max_threads : std::thread::hardware_concurrency();
162#endif /* defined(BARE_METAL) */
163
164 return caps;
165}
166} // namespace
167
168CpuContext::CpuContext(const AclContextOptions *options)
169 : IContext(Target::Cpu),
170 _allocator(default_allocator),
171 _caps(populate_capabilities(AclCpuCapabilitiesAuto, -1))
172{
173 if(options != nullptr)
174 {
175 _allocator = populate_allocator(options->allocator);
176 _caps = populate_capabilities(options->capabilities, options->max_compute_units);
177 }
178}
179
180const CpuCapabilities &CpuContext::capabilities() const
181{
182 return _caps;
183}
184
185AllocatorWrapper &CpuContext::allocator()
186{
187 return _allocator;
188}
Georgios Pinitas3f26ef42021-02-23 10:01:33 +0000189
190ITensorV2 *CpuContext::create_tensor(const AclTensorDescriptor &desc, bool allocate)
191{
192 CpuTensor *tensor = new CpuTensor(this, desc);
193 if(tensor != nullptr && allocate)
194 {
195 tensor->allocate();
196 }
197 return tensor;
198}
Georgios Pinitas8a5146f2021-01-12 15:51:07 +0000199} // namespace cpu
200} // namespace arm_compute