CompilationBuilder.cpp revision 1f4381539b7e89c42336ee7cd1addb9a4c317b34
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 "Manager.h"
24#include "ModelBuilder.h"
25#include "Utils.h"
26
27namespace android {
28namespace nn {
29
30CompilationBuilder::CompilationBuilder(const ModelBuilder* model) :
31    mModel(model) {
32    LOG(DEBUG) << "CompilationBuilder::CompilationBuilder";
33}
34
35int CompilationBuilder::finish() {
36    if (mFinished) {
37        LOG(ERROR) << "ANeuralNetworksCompilation_finish called more than once";
38        return ANEURALNETWORKS_BAD_STATE;
39    }
40
41    mFinished = true;
42
43#ifdef NN_DEBUGGABLE
44    if (uint32_t p = DeviceManager::get()->getPartitioning()) {
45        int n = mModel->partitionTheWork(mPreference, &mPlan);
46        if ((p > 1) && (mPlan.getSimplePlan() == ANEURALNETWORKS_OP_FAILED)) {
47            nnAssert(n != ANEURALNETWORKS_NO_ERROR);
48            return n;
49        }
50    }
51#endif  // NN_DEBUGGABLE
52
53    return ANEURALNETWORKS_NO_ERROR;
54}
55
56int CompilationBuilder::setPreference(int32_t preference) {
57    if (mFinished) {
58        LOG(ERROR) <<
59                "ANeuralNetworksCompilation_setPreference can't modify after compilation finished";
60        return ANEURALNETWORKS_BAD_STATE;
61    }
62    mPreference = preference;
63    return ANEURALNETWORKS_NO_ERROR;
64}
65
66int CompilationBuilder::createExecution(ExecutionBuilder **execution) {
67    if (!mFinished) {
68        LOG(ERROR) << "ANeuralNetworksExecution_create passed an unfinished compilation";
69        *execution = nullptr;
70        return ANEURALNETWORKS_BAD_STATE;
71    }
72    *execution = new ExecutionBuilder(this);
73    return (*execution ? ANEURALNETWORKS_NO_ERROR : ANEURALNETWORKS_OUT_OF_MEMORY);
74}
75
76}  // namespace nn
77}  // namespace android
78