blob: 0da30c3bcf977ead3af1b774f4198ff2cc960a41 [file] [log] [blame]
Kristofer Jonsson116a6352020-08-20 17:25:23 +02001/*
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +01002 * Copyright (c) 2020-2022 Arm Limited.
Kristofer Jonsson116a6352020-08-20 17:25:23 +02003 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Licensed under the Apache License, Version 2.0 (the License); you may
7 * not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
14 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19#include <ethosu.hpp>
20#include <uapi/ethosu.h>
21
22#include <algorithm>
23#include <exception>
Kristofer Jonsson35de9e62022-03-08 13:25:45 +010024#include <fstream>
Kristofer Jonsson116a6352020-08-20 17:25:23 +020025#include <iostream>
26
Kristofer Jonsson116a6352020-08-20 17:25:23 +020027#include <fcntl.h>
28#include <poll.h>
Davide Grohmanne446b422021-10-19 15:33:23 +020029#include <signal.h>
Kristofer Jonsson79689c52020-10-16 14:42:19 +020030#include <sys/ioctl.h>
31#include <sys/mman.h>
Kristofer Jonsson116a6352020-08-20 17:25:23 +020032#include <unistd.h>
33
34using namespace std;
35
Per Åstrandec3f2b02021-06-09 10:43:38 +020036namespace EthosU {
37__attribute__((weak)) int eioctl(int fd, unsigned long cmd, void *data = nullptr) {
Kristofer Jonsson116a6352020-08-20 17:25:23 +020038 int ret = ::ioctl(fd, cmd, data);
Kristofer Jonsson79689c52020-10-16 14:42:19 +020039 if (ret < 0) {
Kristofer Jonsson116a6352020-08-20 17:25:23 +020040 throw EthosU::Exception("IOCTL failed");
41 }
42
43 return ret;
44}
Kristofer Jonssonb74492c2020-09-10 13:26:01 +020045
Per Åstrandec3f2b02021-06-09 10:43:38 +020046__attribute__((weak)) int eopen(const char *pathname, int flags) {
47 int fd = ::open(pathname, flags);
Per Åstrandec3f2b02021-06-09 10:43:38 +020048 if (fd < 0) {
49 throw Exception("Failed to open device");
50 }
51
52 return fd;
53}
54
Davide Grohmanne446b422021-10-19 15:33:23 +020055__attribute__((weak)) int
56eppoll(struct pollfd *fds, nfds_t nfds, const struct timespec *tmo_p, const sigset_t *sigmask) {
57 int result = ::ppoll(fds, nfds, tmo_p, sigmask);
Davide Grohmannc90bfab2021-10-19 13:59:27 +020058 if (result < 0) {
Davide Grohmanne446b422021-10-19 15:33:23 +020059 throw Exception("Failed to wait for ppoll event or signal");
Davide Grohmannc90bfab2021-10-19 13:59:27 +020060 }
61
62 return result;
Per Åstrandec3f2b02021-06-09 10:43:38 +020063}
64
65__attribute__((weak)) int eclose(int fd) {
Davide Grohmannc90bfab2021-10-19 13:59:27 +020066 int result = ::close(fd);
67 if (result < 0) {
68 throw Exception("Failed to close file");
69 }
70
71 return result;
Per Åstrandec3f2b02021-06-09 10:43:38 +020072}
73__attribute((weak)) void *emmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset) {
Davide Grohmannc90bfab2021-10-19 13:59:27 +020074 void *ptr = ::mmap(addr, length, prot, flags, fd, offset);
75 if (ptr == MAP_FAILED) {
76 throw Exception("Failed to mmap file");
77 }
78
79 return ptr;
Per Åstrandec3f2b02021-06-09 10:43:38 +020080}
81
82__attribute__((weak)) int emunmap(void *addr, size_t length) {
Davide Grohmannc90bfab2021-10-19 13:59:27 +020083 int result = ::munmap(addr, length);
84 if (result < 0) {
85 throw Exception("Failed to munmap file");
86 }
87
88 return result;
Per Åstrandec3f2b02021-06-09 10:43:38 +020089}
90
91} // namespace EthosU
92
Kristofer Jonsson79689c52020-10-16 14:42:19 +020093namespace EthosU {
Kristofer Jonsson116a6352020-08-20 17:25:23 +020094
Kristofer Jonssonb74492c2020-09-10 13:26:01 +020095/****************************************************************************
96 * Exception
97 ****************************************************************************/
98
Kristofer Jonsson79689c52020-10-16 14:42:19 +020099Exception::Exception(const char *msg) : msg(msg) {}
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200100
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200101Exception::~Exception() throw() {}
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200102
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200103const char *Exception::what() const throw() {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200104 return msg.c_str();
105}
106
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200107/****************************************************************************
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200108 * Semantic Version
109 ****************************************************************************/
110
111bool SemanticVersion::operator==(const SemanticVersion &other) {
112 return other.major == major && other.minor == minor && other.patch == patch;
113}
114
115bool SemanticVersion::operator<(const SemanticVersion &other) {
116 if (other.major > major)
117 return true;
118 if (other.minor > minor)
119 return true;
120 return other.patch > patch;
121}
122
123bool SemanticVersion::operator<=(const SemanticVersion &other) {
124 return *this < other || *this == other;
125}
126
127bool SemanticVersion::operator!=(const SemanticVersion &other) {
128 return !(*this == other);
129}
130
131bool SemanticVersion::operator>(const SemanticVersion &other) {
132 return !(*this <= other);
133}
134
135bool SemanticVersion::operator>=(const SemanticVersion &other) {
136 return !(*this < other);
137}
138
139ostream &operator<<(ostream &out, const SemanticVersion &v) {
140 return out << "{ major=" << unsigned(v.major) << ", minor=" << unsigned(v.minor) << ", patch=" << unsigned(v.patch)
141 << " }";
142}
143
144/****************************************************************************
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200145 * Device
146 ****************************************************************************/
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200147Device::Device(const char *device) {
Per Åstrandec3f2b02021-06-09 10:43:38 +0200148 fd = eopen(device, O_RDWR | O_NONBLOCK);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200149}
150
Davide Grohmannfc495592022-04-25 15:27:52 +0200151Device::~Device() noexcept(false) {
Per Åstrandec3f2b02021-06-09 10:43:38 +0200152 eclose(fd);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200153}
154
Per Åstrandafb399f2021-11-26 12:50:35 +0100155int Device::ioctl(unsigned long cmd, void *data) const {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200156 return eioctl(fd, cmd, data);
157}
158
Per Åstrandafb399f2021-11-26 12:50:35 +0100159Capabilities Device::capabilities() const {
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200160 ethosu_uapi_device_capabilities uapi;
161 (void)eioctl(fd, ETHOSU_IOCTL_CAPABILITIES_REQ, static_cast<void *>(&uapi));
162
163 Capabilities capabilities(
164 HardwareId(uapi.hw_id.version_status,
165 SemanticVersion(uapi.hw_id.version_major, uapi.hw_id.version_minor),
166 SemanticVersion(uapi.hw_id.product_major),
167 SemanticVersion(uapi.hw_id.arch_major_rev, uapi.hw_id.arch_minor_rev, uapi.hw_id.arch_patch_rev)),
Davide Grohmann40e23d12021-06-17 09:59:40 +0200168 HardwareConfiguration(uapi.hw_cfg.macs_per_cc, uapi.hw_cfg.cmd_stream_version, bool(uapi.hw_cfg.custom_dma)),
Davide Grohmann35ce6c82021-06-01 15:03:51 +0200169 SemanticVersion(uapi.driver_major_rev, uapi.driver_minor_rev, uapi.driver_patch_rev));
170 return capabilities;
171}
172
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200173/****************************************************************************
174 * Buffer
175 ****************************************************************************/
176
Per Åstrandafb399f2021-11-26 12:50:35 +0100177Buffer::Buffer(const Device &device, const size_t capacity) : fd(-1), dataPtr(nullptr), dataCapacity(capacity) {
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200178 ethosu_uapi_buffer_create uapi = {static_cast<uint32_t>(dataCapacity)};
179 fd = device.ioctl(ETHOSU_IOCTL_BUFFER_CREATE, static_cast<void *>(&uapi));
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200180
Davide Grohmann2ba3c1d2021-11-08 15:08:48 +0100181 void *d;
182 try {
183 d = emmap(nullptr, dataCapacity, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
184 } catch (std::exception &e) {
185 try {
186 eclose(fd);
187 } catch (...) { std::throw_with_nested(e); }
Davide Grohmannfc495592022-04-25 15:27:52 +0200188 throw;
Davide Grohmann2ba3c1d2021-11-08 15:08:48 +0100189 }
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200190 dataPtr = reinterpret_cast<char *>(d);
191}
192
Davide Grohmannfc495592022-04-25 15:27:52 +0200193Buffer::~Buffer() noexcept(false) {
Davide Grohmann80f8ddf2022-04-07 14:50:50 +0200194 try {
195 emunmap(dataPtr, dataCapacity);
196 } catch (std::exception &e) {
197 try {
198 eclose(fd);
199 } catch (...) { std::throw_with_nested(e); }
Davide Grohmannfc495592022-04-25 15:27:52 +0200200 throw;
Davide Grohmann80f8ddf2022-04-07 14:50:50 +0200201 }
Davide Grohmannfc495592022-04-25 15:27:52 +0200202 eclose(fd);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200203}
204
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200205size_t Buffer::capacity() const {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200206 return dataCapacity;
207}
208
Per Åstrandafb399f2021-11-26 12:50:35 +0100209void Buffer::clear() const {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200210 resize(0, 0);
211}
212
Per Åstrandafb399f2021-11-26 12:50:35 +0100213char *Buffer::data() const {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200214 return dataPtr + offset();
215}
216
Per Åstrandafb399f2021-11-26 12:50:35 +0100217void Buffer::resize(size_t size, size_t offset) const {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200218 ethosu_uapi_buffer uapi;
219 uapi.offset = offset;
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200220 uapi.size = size;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200221 eioctl(fd, ETHOSU_IOCTL_BUFFER_SET, static_cast<void *>(&uapi));
222}
223
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200224size_t Buffer::offset() const {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200225 ethosu_uapi_buffer uapi;
226 eioctl(fd, ETHOSU_IOCTL_BUFFER_GET, static_cast<void *>(&uapi));
227 return uapi.offset;
228}
229
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200230size_t Buffer::size() const {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200231 ethosu_uapi_buffer uapi;
232 eioctl(fd, ETHOSU_IOCTL_BUFFER_GET, static_cast<void *>(&uapi));
233 return uapi.size;
234}
235
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200236int Buffer::getFd() const {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200237 return fd;
238}
239
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200240/****************************************************************************
241 * Network
242 ****************************************************************************/
243
Per Åstrandafb399f2021-11-26 12:50:35 +0100244Network::Network(const Device &device, shared_ptr<Buffer> &buffer) : fd(-1), buffer(buffer) {
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200245 // Create buffer handle
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200246 ethosu_uapi_network_create uapi;
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100247 uapi.type = ETHOSU_UAPI_NETWORK_BUFFER;
248 uapi.fd = buffer->getFd();
249 fd = device.ioctl(ETHOSU_IOCTL_NETWORK_CREATE, static_cast<void *>(&uapi));
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100250 try {
Davide Grohmann80f8ddf2022-04-07 14:50:50 +0200251 collectNetworkInfo();
252 } catch (std::exception &e) {
253 try {
254 eclose(fd);
255 } catch (...) { std::throw_with_nested(e); }
Davide Grohmannfc495592022-04-25 15:27:52 +0200256 throw;
Jonny Svärd2c017132021-04-12 15:56:44 +0200257 }
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100258}
Jonny Svärd2c017132021-04-12 15:56:44 +0200259
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100260Network::Network(const Device &device, const unsigned index) : fd(-1) {
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100261 // Create buffer handle
262 ethosu_uapi_network_create uapi;
263 uapi.type = ETHOSU_UAPI_NETWORK_INDEX;
264 uapi.index = index;
265 fd = device.ioctl(ETHOSU_IOCTL_NETWORK_CREATE, static_cast<void *>(&uapi));
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100266 try {
Davide Grohmann80f8ddf2022-04-07 14:50:50 +0200267 collectNetworkInfo();
268 } catch (std::exception &e) {
269 try {
270 eclose(fd);
271 } catch (...) { std::throw_with_nested(e); }
Davide Grohmannfc495592022-04-25 15:27:52 +0200272 throw;
Davide Grohmann80f8ddf2022-04-07 14:50:50 +0200273 }
274}
Kristofer Jonsson3c6a2602022-03-10 11:17:29 +0100275
Davide Grohmann80f8ddf2022-04-07 14:50:50 +0200276void Network::collectNetworkInfo() {
277 ethosu_uapi_network_info info;
278 ioctl(ETHOSU_IOCTL_NETWORK_INFO, static_cast<void *>(&info));
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100279
Davide Grohmann80f8ddf2022-04-07 14:50:50 +0200280 for (uint32_t i = 0; i < info.ifm_count; i++) {
281 ifmDims.push_back(info.ifm_size[i]);
282 }
283
284 for (uint32_t i = 0; i < info.ofm_count; i++) {
285 ofmDims.push_back(info.ofm_size[i]);
Kristofer Jonsson35de9e62022-03-08 13:25:45 +0100286 }
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200287}
288
Davide Grohmannfc495592022-04-25 15:27:52 +0200289Network::~Network() noexcept(false) {
Per Åstrandec3f2b02021-06-09 10:43:38 +0200290 eclose(fd);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200291}
292
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200293int Network::ioctl(unsigned long cmd, void *data) {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200294 return eioctl(fd, cmd, data);
295}
296
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200297shared_ptr<Buffer> Network::getBuffer() {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200298 return buffer;
299}
300
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200301const std::vector<size_t> &Network::getIfmDims() const {
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200302 return ifmDims;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200303}
304
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200305size_t Network::getIfmSize() const {
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200306 size_t size = 0;
307
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200308 for (auto s : ifmDims) {
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200309 size += s;
310 }
311
312 return size;
313}
314
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200315const std::vector<size_t> &Network::getOfmDims() const {
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200316 return ofmDims;
317}
318
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200319size_t Network::getOfmSize() const {
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200320 size_t size = 0;
321
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200322 for (auto s : ofmDims) {
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200323 size += s;
324 }
325
326 return size;
327}
328
329/****************************************************************************
330 * Inference
331 ****************************************************************************/
332
Davide Grohmann82d22582022-04-25 12:52:38 +0200333ostream &operator<<(ostream &out, const InferenceStatus &status) {
334 switch (status) {
335 case InferenceStatus::OK:
336 return out << "ok";
337 case InferenceStatus::ERROR:
338 return out << "error";
339 case InferenceStatus::RUNNING:
340 return out << "running";
341 case InferenceStatus::REJECTED:
342 return out << "rejected";
343 }
344 throw Exception("Unknown inference status");
345}
346
Davide Grohmannfc495592022-04-25 15:27:52 +0200347Inference::~Inference() noexcept(false) {
Per Åstrandec3f2b02021-06-09 10:43:38 +0200348 eclose(fd);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200349}
350
Per Åstrand2354d3e2020-10-24 20:17:10 +0200351void Inference::create(std::vector<uint32_t> &counterConfigs, bool cycleCounterEnable = false) {
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200352 ethosu_uapi_inference_create uapi;
353
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200354 if (ifmBuffers.size() > ETHOSU_FD_MAX) {
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200355 throw Exception("IFM buffer overflow");
356 }
357
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200358 if (ofmBuffers.size() > ETHOSU_FD_MAX) {
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200359 throw Exception("OFM buffer overflow");
360 }
361
Per Åstrand2354d3e2020-10-24 20:17:10 +0200362 if (counterConfigs.size() != ETHOSU_PMU_EVENT_MAX) {
363 throw Exception("Wrong size of counter configurations");
364 }
365
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200366 uapi.ifm_count = 0;
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200367 for (auto it : ifmBuffers) {
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200368 uapi.ifm_fd[uapi.ifm_count++] = it->getFd();
369 }
370
371 uapi.ofm_count = 0;
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200372 for (auto it : ofmBuffers) {
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200373 uapi.ofm_fd[uapi.ofm_count++] = it->getFd();
374 }
375
Per Åstrand2354d3e2020-10-24 20:17:10 +0200376 for (int i = 0; i < ETHOSU_PMU_EVENT_MAX; i++) {
377 uapi.pmu_config.events[i] = counterConfigs[i];
378 }
379
380 uapi.pmu_config.cycle_count = cycleCounterEnable;
381
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200382 fd = network->ioctl(ETHOSU_IOCTL_INFERENCE_CREATE, static_cast<void *>(&uapi));
383}
384
Per Åstrand2354d3e2020-10-24 20:17:10 +0200385std::vector<uint32_t> Inference::initializeCounterConfig() {
386 return std::vector<uint32_t>(ETHOSU_PMU_EVENT_MAX, 0);
387}
388
389uint32_t Inference::getMaxPmuEventCounters() {
390 return ETHOSU_PMU_EVENT_MAX;
391}
392
Per Åstrandafb399f2021-11-26 12:50:35 +0100393int Inference::wait(int64_t timeoutNanos) const {
Davide Grohmanne446b422021-10-19 15:33:23 +0200394 struct pollfd pfd;
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200395 pfd.fd = fd;
396 pfd.events = POLLIN | POLLERR;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200397 pfd.revents = 0;
398
Davide Grohmanne446b422021-10-19 15:33:23 +0200399 // if timeout negative wait forever
400 if (timeoutNanos < 0) {
401 return eppoll(&pfd, 1, NULL, NULL);
402 }
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200403
Davide Grohmanne446b422021-10-19 15:33:23 +0200404 struct timespec tmo_p;
405 int64_t nanosec = 1000000000;
406 tmo_p.tv_sec = timeoutNanos / nanosec;
407 tmo_p.tv_nsec = timeoutNanos % nanosec;
Per Åstrand2354d3e2020-10-24 20:17:10 +0200408
Davide Grohmanne446b422021-10-19 15:33:23 +0200409 return eppoll(&pfd, 1, &tmo_p, NULL);
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200410}
411
Davide Grohmann82d22582022-04-25 12:52:38 +0200412InferenceStatus Inference::status() const {
Per Åstrand2354d3e2020-10-24 20:17:10 +0200413 ethosu_uapi_result_status uapi;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200414
Per Åstrand2354d3e2020-10-24 20:17:10 +0200415 eioctl(fd, ETHOSU_IOCTL_INFERENCE_STATUS, static_cast<void *>(&uapi));
416
Davide Grohmann82d22582022-04-25 12:52:38 +0200417 switch (uapi.status) {
418 case ETHOSU_UAPI_STATUS_OK:
419 return InferenceStatus::OK;
420 case ETHOSU_UAPI_STATUS_ERROR:
421 return InferenceStatus::ERROR;
422 case ETHOSU_UAPI_STATUS_RUNNING:
423 return InferenceStatus::RUNNING;
424 case ETHOSU_UAPI_STATUS_REJECTED:
425 return InferenceStatus::REJECTED;
426 }
427
428 throw Exception("Unknown inference status");
Per Åstrand2354d3e2020-10-24 20:17:10 +0200429}
430
Per Åstrandafb399f2021-11-26 12:50:35 +0100431const std::vector<uint32_t> Inference::getPmuCounters() const {
Per Åstrand2354d3e2020-10-24 20:17:10 +0200432 ethosu_uapi_result_status uapi;
433 std::vector<uint32_t> counterValues = std::vector<uint32_t>(ETHOSU_PMU_EVENT_MAX, 0);
434
435 eioctl(fd, ETHOSU_IOCTL_INFERENCE_STATUS, static_cast<void *>(&uapi));
436
437 for (int i = 0; i < ETHOSU_PMU_EVENT_MAX; i++) {
438 if (uapi.pmu_config.events[i]) {
439 counterValues.at(i) = uapi.pmu_count.events[i];
440 }
441 }
442
443 return counterValues;
444}
445
Per Åstrandafb399f2021-11-26 12:50:35 +0100446uint64_t Inference::getCycleCounter() const {
Per Åstrand2354d3e2020-10-24 20:17:10 +0200447 ethosu_uapi_result_status uapi;
448
449 eioctl(fd, ETHOSU_IOCTL_INFERENCE_STATUS, static_cast<void *>(&uapi));
450
451 return uapi.pmu_count.cycle_count;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200452}
453
Per Åstrandafb399f2021-11-26 12:50:35 +0100454int Inference::getFd() const {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200455 return fd;
456}
457
Per Åstrandafb399f2021-11-26 12:50:35 +0100458const shared_ptr<Network> Inference::getNetwork() const {
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200459 return network;
460}
461
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200462vector<shared_ptr<Buffer>> &Inference::getIfmBuffers() {
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200463 return ifmBuffers;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200464}
465
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200466vector<shared_ptr<Buffer>> &Inference::getOfmBuffers() {
Kristofer Jonssonb74492c2020-09-10 13:26:01 +0200467 return ofmBuffers;
Kristofer Jonsson116a6352020-08-20 17:25:23 +0200468}
469
Kristofer Jonsson79689c52020-10-16 14:42:19 +0200470} // namespace EthosU