blob: 917bf68a0c4b30572c0b34c625998547cdde22fa [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 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>
Pablo Marquez Tello48f26152021-11-18 10:15:23 +000031#if !defined(__APPLE__)
Michalis Spyrou86ee2372021-03-09 19:33:12 +000032#include <malloc.h>
Pablo Marquez Tello48f26152021-11-18 10:15:23 +000033#endif // !defined(__APPLE__)
34
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000035
36namespace arm_compute
37{
38namespace cpu
39{
40namespace
41{
42void *default_allocate(void *user_data, size_t size)
43{
44 ARM_COMPUTE_UNUSED(user_data);
45 return ::operator new(size);
46}
47void default_free(void *user_data, void *ptr)
48{
49 ARM_COMPUTE_UNUSED(user_data);
50 ::operator delete(ptr);
51}
52void *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 Tello48f26152021-11-18 10:15:23 +000056#if defined(BARE_METAL)
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000057 size_t rem = size % alignment;
58 size_t real_size = (rem) ? (size + alignment - rem) : size;
Michalis Spyrou86ee2372021-03-09 19:33:12 +000059 ptr = memalign(alignment, real_size);
Pablo Marquez Tello48f26152021-11-18 10:15:23 +000060#else /* defined(BARE_METAL) */
Sang-Hoon Park57d73662021-03-04 11:06:48 +000061 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 Spyrou86ee2372021-03-09 19:33:12 +000066 ARM_COMPUTE_LOG_ERROR_ACL("posix_memalign failed, the returned pointer will be invalid");
Sang-Hoon Park57d73662021-03-04 11:06:48 +000067 }
Pablo Marquez Tello48f26152021-11-18 10:15:23 +000068#endif /* defined(BARE_METAL) */
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000069 return ptr;
70}
71void default_aligned_free(void *user_data, void *ptr)
72{
73 ARM_COMPUTE_UNUSED(user_data);
74 free(ptr);
75}
76static AclAllocator default_allocator = { &default_allocate,
77 &default_free,
78 &default_aligned_allocate,
79 &default_aligned_free,
80 nullptr
81 };
82
83AllocatorWrapper 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 Spyrou20fca522021-06-07 14:23:57 +010096cpuinfo::CpuIsaInfo populate_capabilities_flags(AclTargetCapabilities external_caps)
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000097{
Michalis Spyrou20fca522021-06-07 14:23:57 +010098 cpuinfo::CpuIsaInfo isa_caps;
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000099
100 // Extract SIMD extension
Michalis Spyrou20fca522021-06-07 14:23:57 +0100101 isa_caps.neon = external_caps & AclCpuCapabilitiesNeon;
102 isa_caps.sve = external_caps & AclCpuCapabilitiesSve;
103 isa_caps.sve2 = external_caps & AclCpuCapabilitiesSve2;
Georgios Pinitas8a5146f2021-01-12 15:51:07 +0000104
Michalis Spyrou20fca522021-06-07 14:23:57 +0100105 // Extract data-type support
Georgios Pinitas731fe662021-06-24 20:32:11 +0100106 isa_caps.fp16 = external_caps & AclCpuCapabilitiesFp16;
107 isa_caps.bf16 = external_caps & AclCpuCapabilitiesBf16;
108 isa_caps.svebf16 = isa_caps.bf16;
Michalis Spyrou20fca522021-06-07 14:23:57 +0100109
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 Pinitas8a5146f2021-01-12 15:51:07 +0000116}
117
118CpuCapabilities populate_capabilities(AclTargetCapabilities external_caps,
119 int32_t max_threads)
120{
Georgios Pinitas8a5146f2021-01-12 15:51:07 +0000121 CpuCapabilities caps;
Michalis Spyrou20fca522021-06-07 14:23:57 +0100122
Georgios Pinitas731fe662021-06-24 20:32:11 +0100123 // Populate capabilities with system information
124 caps.cpu_info = cpuinfo::CpuInfo::build();
Georgios Pinitas8a5146f2021-01-12 15:51:07 +0000125 if(external_caps != AclCpuCapabilitiesAuto)
126 {
Georgios Pinitas731fe662021-06-24 20:32:11 +0100127 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 Pinitas8a5146f2021-01-12 15:51:07 +0000131 }
Georgios Pinitas8a5146f2021-01-12 15:51:07 +0000132
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
145CpuContext::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
157const CpuCapabilities &CpuContext::capabilities() const
158{
159 return _caps;
160}
161
162AllocatorWrapper &CpuContext::allocator()
163{
164 return _allocator;
165}
Georgios Pinitas3f26ef42021-02-23 10:01:33 +0000166
167ITensorV2 *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 Pinitasc3c352e2021-03-18 10:59:40 +0000176
177IQueue *CpuContext::create_queue(const AclQueueOptions *options)
178{
179 return new CpuQueue(this, options);
180}
Georgios Pinitas8a5146f2021-01-12 15:51:07 +0000181} // namespace cpu
182} // namespace arm_compute