1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
/*
* Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "exec/Execution.h"
#include "util/logging.h"
namespace neurun
{
namespace exec
{
Execution::Execution(const std::shared_ptr<IExecutor> &executor) : _executor{executor}
{
_io_desc.inputs.resize(_executor->model().inputs.size());
_io_desc.outputs.resize(_executor->model().outputs.size());
}
void Execution::setInput(const model::IOIndex &index, const void *buffer, size_t length)
{
const auto input_index = model().inputs.at(index);
const auto info = model().operands.at(input_index).info();
if (length < info.total_size())
{
throw std::runtime_error{"Too small length"};
}
_io_desc.inputs.at(index.value()) = nnfw::cpp14::make_unique<InputDesc>(info, buffer, length);
}
void Execution::setInput(const model::IOIndex &index, const model::TypeInfo &type,
const model::Shape &shape, const void *buffer, size_t length)
{
const model::OperandInfo info{shape, type};
if (length < info.total_size())
{
throw std::runtime_error{"Too small length"};
}
_io_desc.inputs.at(index.value()) = nnfw::cpp14::make_unique<InputDesc>(info, buffer, length);
}
void Execution::setOutput(const model::IOIndex &index, void *buffer, size_t length)
{
const auto output_index = model().outputs.at(index);
const auto info = model().operands.at(output_index).info();
if (length < info.total_size())
{
throw std::runtime_error{"Too small length"};
}
_io_desc.outputs.at(index.value()) = nnfw::cpp14::make_unique<OutputDesc>(info, buffer, length);
}
void Execution::setOutput(const model::IOIndex &index, const model::TypeInfo &type,
const model::Shape &shape, void *buffer, size_t length)
{
const model::OperandInfo info{shape, type};
if (length < info.total_size())
{
throw std::runtime_error{"Too small length"};
}
_io_desc.outputs.at(index.value()) = nnfw::cpp14::make_unique<OutputDesc>(info, buffer, length);
}
void Execution::execute()
{
VERBOSE(Execution) << "Start execution" << std::endl;
_executor->execute(_io_desc);
VERBOSE(Execution) << "Execution finished" << std::endl;
}
void Execution::startExecute()
{
VERBOSE(Execution) << "Create asynchronous execution thread" << std::endl;
_exec_thread = nnfw::cpp14::make_unique<std::thread>(&Execution::execute, this);
}
void Execution::waitFinish()
{
VERBOSE(Execution) << "Wait to finish execution" << std::endl;
_exec_thread->join();
}
} // namespace exec
} // namespace neurun
|