blob: 55cc066023f9dd27cc53babc7bc9031fd7482399 [file] [log] [blame]
Jan Eilersa96489a2021-12-08 10:05:47 +00001//
2// Copyright © 2022 Arm Ltd and Contributors. All rights reserved.
3// SPDX-License-Identifier: MIT
4//
5
6#include <armnn_delegate.hpp>
7#include <DelegateOptions.hpp>
8
9#if defined(ARMCOMPUTECL_ENABLED)
10#include <arm_compute/core/CL/OpenCL.h>
11#endif
12
13#include <jni.h>
14#include <string>
15
16extern "C" {
17
18/// Creates an Arm NN Delegate object.
19/// Options are passed in form of String arrays. For details about what options_keys and option_values
20/// are supported please see:
21// armnnDelegate::DelegateOptions::DelegateOptions(char const* const*, char const* const*,size_t,void (*)(const char*))
22JNIEXPORT jlong
23JNICALL Java_com_arm_armnn_delegate_ArmnnDelegate_createDelegate(JNIEnv* env,
24 jclass clazz,
25 jobjectArray optionKeys,
26 jobjectArray optionValues)
27{
28 int numOptions = env->GetArrayLength(optionKeys);
29 const char* nativeOptionKeys[numOptions];
30 const char* nativeOptionValues[numOptions];
31
32 jstring jKeyStrings[numOptions];
33 jstring jValueStrings[numOptions];
34
35 // Convert java array of string into char so we can make use of it in cpp code
36 for (int i = 0; i < numOptions; i++)
37 {
38 jKeyStrings[i] = static_cast<jstring>(env->GetObjectArrayElement(optionKeys, i));
39 jValueStrings[i] = static_cast<jstring>(env->GetObjectArrayElement(optionValues, i));
40
41 nativeOptionKeys[i] = env->GetStringUTFChars(jKeyStrings[i], 0);
42 nativeOptionValues[i] = env->GetStringUTFChars(jValueStrings[i], 0);
43 }
44
45 armnnDelegate::DelegateOptions delegateOptions(nativeOptionKeys,
46 nativeOptionValues,
47 numOptions,
48 nullptr);
49
50 // Release jni memory. After the delegate options are created there is no need to hold on to it anymore.
51 for (int i = 0; i < numOptions; i++)
52 {
53 env->ReleaseStringUTFChars(jKeyStrings[i], nativeOptionKeys[i]);
54 env->ReleaseStringUTFChars(jValueStrings[i], nativeOptionValues[i]);
55 }
56
57 return reinterpret_cast<jlong>(armnnDelegate::TfLiteArmnnDelegateCreate(delegateOptions));
58}
59
60/// Destroys a given Arm NN Delegate object
61JNIEXPORT void
62JNICALL Java_com_arm_armnn_delegate_ArmnnDelegate_deleteDelegate(JNIEnv* env, jclass clazz, jlong delegate)
63{
64 armnnDelegate::TfLiteArmnnDelegateDelete(reinterpret_cast<TfLiteDelegate*>(delegate));
65}
66
67/// Returns true if a Arm Mali GPU is detected.
68/// Can be used to ensure that GpuAcc is supported on a device.
69JNIEXPORT jboolean
70JNICALL Java_com_arm_armnn_delegate_ArmnnUtils_IsGpuAccSupported(JNIEnv* env, jclass clazz)
71{
72#if defined(ARMCOMPUTECL_ENABLED)
73 cl::Device device = cl::Device::getDefault();
74 char device_name[32];
75 cl_int err = clGetDeviceInfo(device.get(), CL_DEVICE_NAME, sizeof(device_name), &device_name, NULL);
76 if (err != CL_SUCCESS)
77 {
78 return false;
79 }
80 // search for "Mali" in the devices name
81 if (strstr(device_name, "Mali"))
82 {
83 return true;
84 }
85#endif
86 return false;
87}
88
89/// Returns true if the current device supports Neon instructions.
90/// Can be used to ensure the CpuAcc backend is supported.
91JNIEXPORT jboolean
92JNICALL Java_com_arm_armnn_delegate_ArmnnUtils_IsNeonDetected(JNIEnv* env, jclass clazz)
93{
94 return armnn::NeonDetected();
95}
96
97}
98