diff options
author | Jan Eilers <jan.eilers@arm.com> | 2021-12-08 10:05:47 +0000 |
---|---|---|
committer | Keith Davis <keith.davis@arm.com> | 2022-06-29 10:04:20 +0100 |
commit | a96489a2fd459bd3d73297fa5fdaef5d13a57a4e (patch) | |
tree | 2299402be2854c1bd508f57a35edc5c9059961f6 /delegate/armnnDelegateJNI/src | |
parent | 764ac655bd301ffc8cde4c912e67499954f9ef8f (diff) | |
download | armnn-a96489a2fd459bd3d73297fa5fdaef5d13a57a4e.tar.gz |
Add jni interface for the ArmNN Delegate
* adds a version script to hide away all symbols that
are not required to use the interface
* the main purpose of the jni interface is to enable the
delegate to be used in android apps
* Add static building to patch - see patch 7 for JNI patch only
Signed-off-by: Jan Eilers <jan.eilers@arm.com>
Signed-off-by: Keith Davis <keith.davis@arm.com>
Change-Id: I9bb2d698b5fdb0d1b30cf79e6f19746310cd61b2
Diffstat (limited to 'delegate/armnnDelegateJNI/src')
-rw-r--r-- | delegate/armnnDelegateJNI/src/armnn_delegate_jni.cpp | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/delegate/armnnDelegateJNI/src/armnn_delegate_jni.cpp b/delegate/armnnDelegateJNI/src/armnn_delegate_jni.cpp new file mode 100644 index 0000000000..55cc066023 --- /dev/null +++ b/delegate/armnnDelegateJNI/src/armnn_delegate_jni.cpp @@ -0,0 +1,98 @@ +// +// Copyright © 2022 Arm Ltd and Contributors. All rights reserved. +// SPDX-License-Identifier: MIT +// + +#include <armnn_delegate.hpp> +#include <DelegateOptions.hpp> + +#if defined(ARMCOMPUTECL_ENABLED) +#include <arm_compute/core/CL/OpenCL.h> +#endif + +#include <jni.h> +#include <string> + +extern "C" { + +/// Creates an Arm NN Delegate object. +/// Options are passed in form of String arrays. For details about what options_keys and option_values +/// are supported please see: +// armnnDelegate::DelegateOptions::DelegateOptions(char const* const*, char const* const*,size_t,void (*)(const char*)) +JNIEXPORT jlong +JNICALL Java_com_arm_armnn_delegate_ArmnnDelegate_createDelegate(JNIEnv* env, + jclass clazz, + jobjectArray optionKeys, + jobjectArray optionValues) +{ + int numOptions = env->GetArrayLength(optionKeys); + const char* nativeOptionKeys[numOptions]; + const char* nativeOptionValues[numOptions]; + + jstring jKeyStrings[numOptions]; + jstring jValueStrings[numOptions]; + + // Convert java array of string into char so we can make use of it in cpp code + for (int i = 0; i < numOptions; i++) + { + jKeyStrings[i] = static_cast<jstring>(env->GetObjectArrayElement(optionKeys, i)); + jValueStrings[i] = static_cast<jstring>(env->GetObjectArrayElement(optionValues, i)); + + nativeOptionKeys[i] = env->GetStringUTFChars(jKeyStrings[i], 0); + nativeOptionValues[i] = env->GetStringUTFChars(jValueStrings[i], 0); + } + + armnnDelegate::DelegateOptions delegateOptions(nativeOptionKeys, + nativeOptionValues, + numOptions, + nullptr); + + // Release jni memory. After the delegate options are created there is no need to hold on to it anymore. + for (int i = 0; i < numOptions; i++) + { + env->ReleaseStringUTFChars(jKeyStrings[i], nativeOptionKeys[i]); + env->ReleaseStringUTFChars(jValueStrings[i], nativeOptionValues[i]); + } + + return reinterpret_cast<jlong>(armnnDelegate::TfLiteArmnnDelegateCreate(delegateOptions)); +} + +/// Destroys a given Arm NN Delegate object +JNIEXPORT void +JNICALL Java_com_arm_armnn_delegate_ArmnnDelegate_deleteDelegate(JNIEnv* env, jclass clazz, jlong delegate) +{ + armnnDelegate::TfLiteArmnnDelegateDelete(reinterpret_cast<TfLiteDelegate*>(delegate)); +} + +/// Returns true if a Arm Mali GPU is detected. +/// Can be used to ensure that GpuAcc is supported on a device. +JNIEXPORT jboolean +JNICALL Java_com_arm_armnn_delegate_ArmnnUtils_IsGpuAccSupported(JNIEnv* env, jclass clazz) +{ +#if defined(ARMCOMPUTECL_ENABLED) + cl::Device device = cl::Device::getDefault(); + char device_name[32]; + cl_int err = clGetDeviceInfo(device.get(), CL_DEVICE_NAME, sizeof(device_name), &device_name, NULL); + if (err != CL_SUCCESS) + { + return false; + } + // search for "Mali" in the devices name + if (strstr(device_name, "Mali")) + { + return true; + } +#endif + return false; +} + +/// Returns true if the current device supports Neon instructions. +/// Can be used to ensure the CpuAcc backend is supported. +JNIEXPORT jboolean +JNICALL Java_com_arm_armnn_delegate_ArmnnUtils_IsNeonDetected(JNIEnv* env, jclass clazz) +{ + return armnn::NeonDetected(); +} + +} + |