blob: 1a971a6a16b31a272285dfdcf7052cca4eac689a [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>
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
Michalis Spyrou20fca522021-06-07 14:23:57 +010093cpuinfo::CpuIsaInfo populate_capabilities_flags(AclTargetCapabilities external_caps)
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000094{
Michalis Spyrou20fca522021-06-07 14:23:57 +010095 cpuinfo::CpuIsaInfo isa_caps;
Georgios Pinitas8a5146f2021-01-12 15:51:07 +000096
97 // Extract SIMD extension
Michalis Spyrou20fca522021-06-07 14:23:57 +010098 isa_caps.neon = external_caps & AclCpuCapabilitiesNeon;
99 isa_caps.sve = external_caps & AclCpuCapabilitiesSve;
100 isa_caps.sve2 = external_caps & AclCpuCapabilitiesSve2;
Georgios Pinitas8a5146f2021-01-12 15:51:07 +0000101
Michalis Spyrou20fca522021-06-07 14:23:57 +0100102 // Extract data-type support
Georgios Pinitas731fe662021-06-24 20:32:11 +0100103 isa_caps.fp16 = external_caps & AclCpuCapabilitiesFp16;
104 isa_caps.bf16 = external_caps & AclCpuCapabilitiesBf16;
105 isa_caps.svebf16 = isa_caps.bf16;
Michalis Spyrou20fca522021-06-07 14:23:57 +0100106
107 // Extract ISA extensions
108 isa_caps.dot = external_caps & AclCpuCapabilitiesDot;
109 isa_caps.i8mm = external_caps & AclCpuCapabilitiesMmlaInt8;
110 isa_caps.svef32mm = external_caps & AclCpuCapabilitiesMmlaFp;
111
112 return isa_caps;
Georgios Pinitas8a5146f2021-01-12 15:51:07 +0000113}
114
115CpuCapabilities populate_capabilities(AclTargetCapabilities external_caps,
116 int32_t max_threads)
117{
Georgios Pinitas8a5146f2021-01-12 15:51:07 +0000118 CpuCapabilities caps;
Michalis Spyrou20fca522021-06-07 14:23:57 +0100119
Georgios Pinitas731fe662021-06-24 20:32:11 +0100120 // Populate capabilities with system information
121 caps.cpu_info = cpuinfo::CpuInfo::build();
Georgios Pinitas8a5146f2021-01-12 15:51:07 +0000122 if(external_caps != AclCpuCapabilitiesAuto)
123 {
Georgios Pinitas731fe662021-06-24 20:32:11 +0100124 cpuinfo::CpuIsaInfo isa = populate_capabilities_flags(external_caps);
125 auto cpus = caps.cpu_info.cpus();
126
127 caps.cpu_info = cpuinfo::CpuInfo(isa, cpus);
Georgios Pinitas8a5146f2021-01-12 15:51:07 +0000128 }
Georgios Pinitas8a5146f2021-01-12 15:51:07 +0000129
130 // Set max number of threads
131#if defined(BARE_METAL)
132 ARM_COMPUTE_UNUSED(max_threads);
133 caps.max_threads = 1;
134#else /* defined(BARE_METAL) */
135 caps.max_threads = (max_threads > 0) ? max_threads : std::thread::hardware_concurrency();
136#endif /* defined(BARE_METAL) */
137
138 return caps;
139}
140} // namespace
141
142CpuContext::CpuContext(const AclContextOptions *options)
143 : IContext(Target::Cpu),
144 _allocator(default_allocator),
145 _caps(populate_capabilities(AclCpuCapabilitiesAuto, -1))
146{
147 if(options != nullptr)
148 {
149 _allocator = populate_allocator(options->allocator);
150 _caps = populate_capabilities(options->capabilities, options->max_compute_units);
151 }
152}
153
154const CpuCapabilities &CpuContext::capabilities() const
155{
156 return _caps;
157}
158
159AllocatorWrapper &CpuContext::allocator()
160{
161 return _allocator;
162}
Georgios Pinitas3f26ef42021-02-23 10:01:33 +0000163
164ITensorV2 *CpuContext::create_tensor(const AclTensorDescriptor &desc, bool allocate)
165{
166 CpuTensor *tensor = new CpuTensor(this, desc);
167 if(tensor != nullptr && allocate)
168 {
169 tensor->allocate();
170 }
171 return tensor;
172}
Georgios Pinitasc3c352e2021-03-18 10:59:40 +0000173
174IQueue *CpuContext::create_queue(const AclQueueOptions *options)
175{
176 return new CpuQueue(this, options);
177}
Georgios Pinitas8a5146f2021-01-12 15:51:07 +0000178} // namespace cpu
179} // namespace arm_compute