• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 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 #include "run_tflite.h"
18 
19 #include <jni.h>
20 #include <string>
21 #include <iomanip>
22 #include <sstream>
23 #include <fcntl.h>
24 
25 #include <android/asset_manager_jni.h>
26 #include <android/log.h>
27 #include <android/sharedmem.h>
28 #include <sys/mman.h>
29 
30 extern "C"
31 JNIEXPORT jlong
32 JNICALL
Java_com_example_android_nn_benchmark_NNTestBase_initModel(JNIEnv * env,jobject,jstring _modelFileName)33 Java_com_example_android_nn_benchmark_NNTestBase_initModel(
34         JNIEnv *env,
35         jobject /* this */,
36         jstring _modelFileName) {
37     const char *modelFileName = env->GetStringUTFChars(_modelFileName, NULL);
38     void* handle = new BenchmarkModel(modelFileName);
39     env->ReleaseStringUTFChars(_modelFileName, modelFileName);
40 
41     return (jlong)(uintptr_t)handle;
42 }
43 
44 extern "C"
45 JNIEXPORT void
46 JNICALL
Java_com_example_android_nn_benchmark_NNTestBase_destroyModel(JNIEnv * env,jobject,jlong _modelHandle)47 Java_com_example_android_nn_benchmark_NNTestBase_destroyModel(
48         JNIEnv *env,
49         jobject /* this */,
50         jlong _modelHandle) {
51     BenchmarkModel* model = (BenchmarkModel *) _modelHandle;
52     delete(model);
53 }
54 
55 extern "C"
56 JNIEXPORT jboolean
57 JNICALL
Java_com_example_android_nn_benchmark_NNTestBase_resizeInputTensors(JNIEnv * env,jobject,jlong _modelHandle,jintArray _inputShape)58 Java_com_example_android_nn_benchmark_NNTestBase_resizeInputTensors(
59         JNIEnv *env,
60         jobject /* this */,
61         jlong _modelHandle,
62         jintArray _inputShape) {
63     BenchmarkModel* model = (BenchmarkModel *) _modelHandle;
64     jint* shapePtr = env->GetIntArrayElements(_inputShape, nullptr);
65     jsize shapeLen = env->GetArrayLength(_inputShape);
66 
67     std::vector<int> shape(shapePtr, shapePtr + shapeLen);
68     return model->resizeInputTensors(std::move(shape));
69 }
70 
71 extern "C"
72 JNIEXPORT jboolean
73 JNICALL
Java_com_example_android_nn_benchmark_NNTestBase_runBenchmark(JNIEnv * env,jobject,jlong _modelHandle,jboolean _useNNAPI)74 Java_com_example_android_nn_benchmark_NNTestBase_runBenchmark(
75         JNIEnv *env,
76         jobject /* this */,
77         jlong _modelHandle,
78         jboolean _useNNAPI) {
79     BenchmarkModel* model = (BenchmarkModel *) _modelHandle;
80     return model->runBenchmark(1, _useNNAPI);
81 }