forked from projectceladon/nn_gpu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prepare_model.cpp
executable file
·72 lines (61 loc) · 1.65 KB
/
prepare_model.cpp
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
#include <algorithm>
#include <memory.h>
#include <string.h>
#include <hidl/LegacySupport.h>
#include <thread>
#include "prepare_model.h"
#include "executor_manager.h"
#include "validate.h"
NAME_SPACE_BEGIN
PreparedModel::PreparedModel(const Model& model)
: // Make a copy of the model, as we need to preserve it.
mModel(model)
{
NN_GPU_CALL();
exec = ExecutorManager::createExecutor(mModel);
}
bool PreparedModel::initialize()
{
NN_GPU_CALL();
exec->initPerModel();
return true;
}
void PreparedModel::asyncExecute(const Request& request,
const sp<IExecutionCallback>& callback)
{
NN_GPU_CALL();
exec->initPerExecThread();
bool succ = exec->run(request);
exec->deinitPerExecThread();
if (succ)
{
callback->notify(ErrorStatus::NONE);
}
else
{
callback->notify(ErrorStatus::GENERAL_FAILURE);
}
}
Return<ErrorStatus> PreparedModel::execute(const Request& request,
const sp<IExecutionCallback>& callback)
{
NN_GPU_CALL();
if (callback.get() == nullptr)
{
LOGE("invalid callback passed to execute");
return ErrorStatus::INVALID_ARGUMENT;
}
if (!validateRequest(request, mModel)) {
callback->notify(ErrorStatus::INVALID_ARGUMENT);
return ErrorStatus::INVALID_ARGUMENT;
}
execThreads.push_back(std::thread([this, request, callback]{ asyncExecute(request, callback); }));
return ErrorStatus::NONE;
}
PreparedModel::~PreparedModel()
{
NN_GPU_CALL();
for (auto& th : execThreads) th.join();
exec->deinitPerModel();
}
NAME_SPACE_STOP