CompilationBuilder.cpp revision 0b9453e41a544f9c780eaa15ad65136ad4662ccb
1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "CompilationBuilder"
18
19#include "CompilationBuilder.h"
20
21#include "ExecutionBuilder.h"
22#include "ExecutionPlan.h"
23#include "ModelBuilder.h"
24#include "Utils.h"
25
26namespace android {
27namespace nn {
28
29CompilationBuilder::CompilationBuilder(const ModelBuilder* model) :
30    mModel(model) {
31    LOG(DEBUG) << "CompilationBuilder::CompilationBuilder";
32}
33
34int CompilationBuilder::finish() {
35    if (mFinished) {
36        LOG(ERROR) << "ANeuralNetworksCompilation_finish called more than once";
37        return ANEURALNETWORKS_BAD_STATE;
38    }
39
40    mFinished = true;
41
42#ifdef NN_DEBUGGABLE
43    if (getProp("debug.nn.partition.test")) {
44        ExecutionPlan plan;
45        mModel->partitionTheWork(mPreference, &plan);
46    }
47#endif  // NN_DEBUGGABLE
48
49    return ANEURALNETWORKS_NO_ERROR;
50}
51
52int CompilationBuilder::setPreference(int32_t preference) {
53    if (mFinished) {
54        LOG(ERROR) <<
55                "ANeuralNetworksCompilation_setPreference can't modify after compilation finished";
56        return ANEURALNETWORKS_BAD_STATE;
57    }
58    mPreference = preference;
59    return ANEURALNETWORKS_NO_ERROR;
60}
61
62int CompilationBuilder::createExecution(ExecutionBuilder **execution) {
63    if (!mFinished) {
64        LOG(ERROR) << "ANeuralNetworksExecution_create passed an unfinished compilation";
65        *execution = nullptr;
66        return ANEURALNETWORKS_BAD_STATE;
67    }
68    *execution = new ExecutionBuilder(this);
69    return (*execution ? ANEURALNETWORKS_NO_ERROR : ANEURALNETWORKS_OUT_OF_MEMORY);
70}
71
72}  // namespace nn
73}  // namespace android
74