Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2020 Arm Limited. All rights reserved. |
| 3 | * |
| 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 | #pragma once |
| 20 | |
| 21 | #include <uapi/ethosu.h> |
| 22 | |
| 23 | #include <memory> |
| 24 | #include <string> |
| 25 | |
| 26 | namespace EthosU |
| 27 | { |
| 28 | |
| 29 | class Exception : |
| 30 | public std::exception |
| 31 | { |
| 32 | public: |
| 33 | Exception(const char *msg); |
| 34 | virtual ~Exception() throw(); |
| 35 | virtual const char *what() const throw(); |
| 36 | |
| 37 | private: |
| 38 | std::string msg; |
| 39 | }; |
| 40 | |
| 41 | class Device |
| 42 | { |
| 43 | public: |
| 44 | Device(const char *device = "/dev/ethosu0"); |
| 45 | virtual ~Device(); |
| 46 | |
| 47 | int ioctl(unsigned long cmd, void *data = nullptr); |
| 48 | |
| 49 | private: |
| 50 | int fd; |
| 51 | }; |
| 52 | |
| 53 | class Buffer |
| 54 | { |
| 55 | public: |
| 56 | Buffer(Device &device, const size_t capacity); |
| 57 | virtual ~Buffer(); |
| 58 | |
| 59 | size_t capacity() const; |
| 60 | void clear(); |
| 61 | char *data(); |
| 62 | void resize(size_t size, size_t offset = 0); |
| 63 | size_t offset() const; |
| 64 | size_t size() const; |
| 65 | |
| 66 | int getFd() const; |
| 67 | |
| 68 | private: |
| 69 | int fd; |
| 70 | char *dataPtr; |
| 71 | const size_t dataCapacity; |
| 72 | }; |
| 73 | |
| 74 | class Network |
| 75 | { |
| 76 | public: |
| 77 | Network(Device &device, std::shared_ptr<Buffer> &buffer); |
| 78 | virtual ~Network(); |
| 79 | |
| 80 | int ioctl(unsigned long cmd, void *data = nullptr); |
| 81 | std::shared_ptr<Buffer> getBuffer(); |
| 82 | |
| 83 | private: |
| 84 | int fd; |
| 85 | std::shared_ptr<Buffer> buffer; |
| 86 | }; |
| 87 | |
| 88 | class Inference |
| 89 | { |
| 90 | public: |
| 91 | Inference(std::shared_ptr<Network> &network, std::shared_ptr<Buffer> &ifm, std::shared_ptr<Buffer> &ofm); |
| 92 | virtual ~Inference(); |
| 93 | |
| 94 | void wait(int timeoutSec = -1); |
| 95 | bool failed(); |
| 96 | int getFd(); |
| 97 | std::shared_ptr<Network> getNetwork(); |
| 98 | std::shared_ptr<Buffer> getIfmBuffer(); |
| 99 | std::shared_ptr<Buffer> getOfmBuffer(); |
| 100 | |
| 101 | private: |
| 102 | int fd; |
| 103 | std::shared_ptr<Network> network; |
| 104 | std::shared_ptr<Buffer> ifmBuffer; |
| 105 | std::shared_ptr<Buffer> ofmBuffer; |
| 106 | }; |
| 107 | |
| 108 | } |