Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2019-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 <queue> |
| 22 | #include <stdlib.h> |
| 23 | #include <string> |
Kristofer Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 24 | #include <vector> |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 25 | |
| 26 | namespace InferenceProcess { |
| 27 | struct DataPtr { |
| 28 | void *data; |
| 29 | size_t size; |
| 30 | |
| 31 | DataPtr(void *data = nullptr, size_t size = 0); |
Kristofer Jonsson | 34e2496 | 2020-11-23 16:22:10 +0100 | [diff] [blame] | 32 | |
| 33 | void invalidate(); |
| 34 | void clean(); |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | struct InferenceJob { |
| 38 | std::string name; |
| 39 | DataPtr networkModel; |
Kristofer Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 40 | std::vector<DataPtr> input; |
| 41 | std::vector<DataPtr> output; |
| 42 | std::vector<DataPtr> expectedOutput; |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 43 | size_t numBytesToPrint; |
| 44 | |
| 45 | InferenceJob(); |
| 46 | InferenceJob(const std::string &name, |
| 47 | const DataPtr &networkModel, |
Kristofer Jonsson | 72fa50b | 2020-09-10 13:26:41 +0200 | [diff] [blame] | 48 | const std::vector<DataPtr> &input, |
| 49 | const std::vector<DataPtr> &output, |
| 50 | const std::vector<DataPtr> &expectedOutput, |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 51 | size_t numBytesToPrint); |
Kristofer Jonsson | 34e2496 | 2020-11-23 16:22:10 +0100 | [diff] [blame] | 52 | |
| 53 | void invalidate(); |
| 54 | void clean(); |
Kristofer Jonsson | 641c091 | 2020-08-31 11:34:14 +0200 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | class InferenceProcess { |
| 58 | public: |
| 59 | InferenceProcess(); |
| 60 | |
| 61 | bool push(const InferenceJob &job); |
| 62 | bool runJob(InferenceJob &job); |
| 63 | bool run(bool exitOnEmpty = true); |
| 64 | |
| 65 | private: |
| 66 | volatile uint32_t lock; |
| 67 | std::queue<InferenceJob> inferenceJobQueue; |
| 68 | |
| 69 | void getLock(); |
| 70 | void freeLock(); |
| 71 | }; |
| 72 | } // namespace InferenceProcess |