diff --git a/examples/android/android-simple/app/build.gradle b/examples/android/android-simple/app/build.gradle index 0ecee901b..8529509b2 100644 --- a/examples/android/android-simple/app/build.gradle +++ b/examples/android/android-simple/app/build.gradle @@ -2,17 +2,17 @@ apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' -Properties properties = new Properties() -properties.load(project.rootProject.file('local.properties').newDataInputStream()) -def ndkDir = properties.getProperty('ndk.dir') -def valLayerBinDir = "${ndkDir}/sources/third_party/vulkan/src/build-android/jniLibs" +//Properties properties = new Properties() +//properties.load(project.rootProject.file('local.properties').newDataInputStream()) +//def ndkDir = properties.getProperty('ndk.dir') +//def valLayerBinDir = "${ndkDir}/sources/third_party/vulkan/src/build-android/jniLibs" android { compileSdkVersion 29 ndkVersion '21.2.6472646' defaultConfig { - applicationId "com.ethicalml.kompute.examples.android" + applicationId "com.ethicalml.kompute" minSdkVersion 26 targetSdkVersion 29 versionCode = 1 @@ -46,14 +46,14 @@ android { } } - sourceSets { - main { - jniLibs { - // Must have ndk-r12 or better which including layer binaries - srcDirs = ["${valLayerBinDir}"] - } - } - } +// sourceSets { +// main { +// jniLibs { +// // Must have ndk-r12 or better which including layer binaries +// srcDirs = ["${valLayerBinDir}"] +// } +// } +// } flavorDimensions 'cpuArch' productFlavors { @@ -81,5 +81,5 @@ dependencies { implementation 'androidx.appcompat:appcompat:1.1.0' implementation 'androidx.constraintlayout:constraintlayout:1.1.3' implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" - implementation fileTree(dir: 'libs', include: ['*.jar']) +// implementation fileTree(dir: 'libs', include: ['*.jar']) } diff --git a/examples/android/android-simple/app/src/main/AndroidManifest.xml b/examples/android/android-simple/app/src/main/AndroidManifest.xml index 58826d222..ea3001a09 100755 --- a/examples/android/android-simple/app/src/main/AndroidManifest.xml +++ b/examples/android/android-simple/app/src/main/AndroidManifest.xml @@ -1,9 +1,7 @@ + package="com.ethicalml.komputejni"> diff --git a/examples/android/android-simple/app/src/main/cpp/CMakeLists.txt b/examples/android/android-simple/app/src/main/cpp/CMakeLists.txt index 0b3948088..3ba634942 100644 --- a/examples/android/android-simple/app/src/main/cpp/CMakeLists.txt +++ b/examples/android/android-simple/app/src/main/cpp/CMakeLists.txt @@ -12,8 +12,8 @@ set(VK_ANDROID_INCLUDE_DIR ${ANDROID_NDK}/sources/third_party/vulkan/src/include # ${ANDROID_APP_GLUE_DIR}/android_native_app_glue.c) # build vulkan app -add_library(kompute_android SHARED - KomputeJniNative.cpp) +add_library(kompute-jni SHARED + kompute-jni.cpp) include_directories( ${VK_ANDROID_COMMON_DIR} @@ -24,10 +24,10 @@ include_directories( set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 \ -DVK_USE_PLATFORM_ANDROID_KHR=1 -DKOMPUTE_DISABLE_VK_DEBUG_LAYERS=1") -set(CMAKE_SHARED_LINKER_FLAGS - "${CMAKE_SHARED_LINKER_FLAGS} -u ANativeActivity_onCreate") +#set(CMAKE_SHARED_LINKER_FLAGS +# "${CMAKE_SHARED_LINKER_FLAGS} -u ANativeActivity_onCreate") -target_link_libraries(kompute_android +target_link_libraries(kompute-jni kompute # app-glue log diff --git a/examples/android/android-simple/app/src/main/cpp/KomputeJniNative.cpp b/examples/android/android-simple/app/src/main/cpp/kompute-jni.cpp similarity index 50% rename from examples/android/android-simple/app/src/main/cpp/KomputeJniNative.cpp rename to examples/android/android-simple/app/src/main/cpp/kompute-jni.cpp index 85a780fb3..d8a6e2368 100644 --- a/examples/android/android-simple/app/src/main/cpp/KomputeJniNative.cpp +++ b/examples/android/android-simple/app/src/main/cpp/kompute-jni.cpp @@ -12,30 +12,30 @@ // See the License for the specific language governing permissions and // limitations under the License. -//#undef DEBUG -//#define RELEASE 1 +#undef DEBUG +#define RELEASE 1 #include //#include //#include //#include //#include -//#include +#include #include #include -//#include "kompute/Kompute.hpp" +#include "kompute/Kompute.hpp" // Functions interacting with Android native activity -void android_main(struct android_app* state); -void terminate(void); -void handle_cmd(android_app* app, int32_t cmd); +//void android_main(struct android_app* state); +//void terminate(void); +//void handle_cmd(android_app* app, int32_t cmd); // Android log function wrappers -static const char* kTAG = "Vulkan-Tutorial01"; +static const char* kTAG = "KomputeJni"; #define LOGI(...) \ ((void)__android_log_print(ANDROID_LOG_INFO, kTAG, __VA_ARGS__)) #define LOGW(...) \ @@ -43,27 +43,27 @@ static const char* kTAG = "Vulkan-Tutorial01"; #define LOGE(...) \ ((void)__android_log_print(ANDROID_LOG_ERROR, kTAG, __VA_ARGS__)) - +extern "C" { JNIEXPORT jstring JNICALL -Java_com_ethicalml_kompute_examples_android_KomputeJni_stringFromJNI(JNIEnv *env, jobject thiz) { +Java_com_ethicalml_komputejni_KomputeJni_stringFromJNI(JNIEnv *env, jobject thiz) { #if defined(__arm__) - #if defined(__ARM_ARCH_7A__) - #if defined(__ARM_NEON__) - #if defined(__ARM_PCS_VFP) - #define ABI "armeabi-v7a/NEON (hard-float)" - #else - #define ABI "armeabi-v7a/NEON" - #endif - #else - #if defined(__ARM_PCS_VFP) - #define ABI "armeabi-v7a (hard-float)" - #else - #define ABI "armeabi-v7a" - #endif - #endif - #else - #define ABI "armeabi" - #endif +#if defined(__ARM_ARCH_7A__) +#if defined(__ARM_NEON__) +#if defined(__ARM_PCS_VFP) +#define ABI "armeabi-v7a/NEON (hard-float)" +#else +#define ABI "armeabi-v7a/NEON" +#endif +#else +#if defined(__ARM_PCS_VFP) +#define ABI "armeabi-v7a (hard-float)" +#else +#define ABI "armeabi-v7a" +#endif +#endif +#else +#define ABI "armeabi" +#endif #elif defined(__i386__) #define ABI "x86" #elif defined(__x86_64__) @@ -80,32 +80,36 @@ Java_com_ethicalml_kompute_examples_android_KomputeJni_stringFromJNI(JNIEnv *env LOGI("Initialising vulkan"); -// if (!InitVulkan()) { -// LOGE("Vulkan is unavailable, install vulkan and re-start"); -// return false; -// } -// -// LOGI("Creating manager"); -// -// kp::Manager mgr; -// -// auto tensorA = mgr.buildTensor({0,1,2}); -// auto tensorB = mgr.buildTensor({0,1,2}); -// auto tensorC = mgr.buildTensor({1,2,3}); -// -// LOGI("Result before:"); -// for(const float & i : tensorC->data()) { -// LOGI("%f ", i); -// } -// -// mgr.evalOpDefault>({tensorA, tensorB, tensorC}); -// mgr.evalOpDefault({tensorC}); -// -// LOGI("Result after:"); -// for(const float & i : tensorC->data()) { -// LOGI("%f ", i); -// } + // TODO: This optionally to be initialized from Kompute + if(!InitVulkan()) { + sleep(1); + if (!InitVulkan()) { + LOGE("Vulkan is unavailable, install vulkan and re-start"); + return (*env).NewStringUTF("ERROR"); + } + } - return (*env).NewStringUTF("Hello from JNI ! Compiled with ABI "); + LOGI("Creating manager"); + + kp::Manager mgr; + + auto tensorA = mgr.buildTensor({0,1,2}); + auto tensorB = mgr.buildTensor({0,1,2}); + auto tensorC = mgr.buildTensor({1,2,3}); + + LOGI("Result before:"); + for(const float & i : tensorC->data()) { + LOGI("%f ", i); + } + + mgr.evalOpDefault>({tensorA, tensorB, tensorC}); + mgr.evalOpDefault({tensorC}); + + LOGI("Result after:"); + for(const float & i : tensorC->data()) { + LOGI("%f ", i); + } + + return (*env).NewStringUTF("SUCCESS"); +} } - diff --git a/examples/android/android-simple/app/src/main/java/com/ethicalml/kompute/examples/android/KomputeJni.kt b/examples/android/android-simple/app/src/main/java/com/ethicalml/komputejni/KomputeJni.kt similarity index 84% rename from examples/android/android-simple/app/src/main/java/com/ethicalml/kompute/examples/android/KomputeJni.kt rename to examples/android/android-simple/app/src/main/java/com/ethicalml/komputejni/KomputeJni.kt index d3b1261aa..8c7c9996e 100755 --- a/examples/android/android-simple/app/src/main/java/com/ethicalml/kompute/examples/android/KomputeJni.kt +++ b/examples/android/android-simple/app/src/main/java/com/ethicalml/komputejni/KomputeJni.kt @@ -13,11 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.ethicalml.kompute.examples.android +package com.ethicalml.komputejni import android.os.Bundle import androidx.appcompat.app.AppCompatActivity -import com.ethicalml.kompute.examples.android.databinding.ActivityHelloJniBinding +import com.ethicalml.komputejni.databinding.ActivityKomputeJniBinding class KomputeJni : AppCompatActivity() { @@ -28,9 +28,9 @@ class KomputeJni : AppCompatActivity() { * the text is retrieved by calling a native * function. */ - val binding = ActivityHelloJniBinding.inflate(layoutInflater) + val binding = ActivityKomputeJniBinding.inflate(layoutInflater) setContentView(binding.root) - binding.helloTextview.text = stringFromJNI() + binding.komputeJniTextview.text = stringFromJNI() } /* @@ -61,7 +61,7 @@ class KomputeJni : AppCompatActivity() { * at the installation time by the package manager. */ init { - System.loadLibrary("kompute_android") + System.loadLibrary("kompute-jni") } } } diff --git a/examples/android/android-simple/app/src/main/res/layout/activity_hello_jni.xml b/examples/android/android-simple/app/src/main/res/layout/activity_kompute_jni.xml similarity index 60% rename from examples/android/android-simple/app/src/main/res/layout/activity_hello_jni.xml rename to examples/android/android-simple/app/src/main/res/layout/activity_kompute_jni.xml index 18840726a..8196c4b61 100644 --- a/examples/android/android-simple/app/src/main/res/layout/activity_hello_jni.xml +++ b/examples/android/android-simple/app/src/main/res/layout/activity_kompute_jni.xml @@ -2,19 +2,19 @@ + app:layout_constraintBottom_toBottomOf="@+id/activity_kompute_jni" + app:layout_constraintLeft_toLeftOf="@+id/activity_kompute_jni" + app:layout_constraintRight_toRightOf="@+id/activity_kompute_jni" + app:layout_constraintTop_toTopOf="@+id/activity_kompute_jni" />