Fully functioning android ui and passing of float arrays
This commit is contained in:
parent
a1e25fc792
commit
9698ba7f42
3 changed files with 136 additions and 13 deletions
|
|
@ -40,6 +40,22 @@ static const char* kTAG = "KomputeJni";
|
|||
#define LOGE(...) \
|
||||
((void)__android_log_print(ANDROID_LOG_ERROR, kTAG, __VA_ARGS__))
|
||||
|
||||
static std::vector<float> jfloatArrayToVector(JNIEnv *env, const jfloatArray & fromArray) {
|
||||
float *inCArray = env->GetFloatArrayElements(fromArray, NULL);
|
||||
if (NULL == inCArray) return std::vector<float>();
|
||||
int32_t length = env->GetArrayLength(fromArray);
|
||||
|
||||
std::vector<float> outVector(inCArray, inCArray + length);
|
||||
return outVector;
|
||||
}
|
||||
|
||||
static jfloatArray vectorToJFloatArray(JNIEnv *env, const std::vector<float> & fromVector) {
|
||||
jfloatArray ret = env->NewFloatArray(fromVector.size());
|
||||
if (NULL == ret) return NULL;
|
||||
env->SetFloatArrayRegion(ret, 0, fromVector.size(), fromVector.data());
|
||||
return ret;
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
|
||||
JNIEXPORT jboolean JNICALL
|
||||
|
|
@ -60,16 +76,25 @@ Java_com_ethicalml_kompute_KomputeJni_initVulkan(JNIEnv *env, jobject thiz) {
|
|||
}
|
||||
|
||||
|
||||
JNIEXPORT jstring JNICALL
|
||||
Java_com_ethicalml_kompute_KomputeJni_stringFromJNI(JNIEnv *env, jobject thiz) {
|
||||
JNIEXPORT jfloatArray JNICALL
|
||||
Java_com_ethicalml_kompute_KomputeJni_kompute(
|
||||
JNIEnv *env,
|
||||
jobject thiz,
|
||||
jfloatArray xiJFloatArr,
|
||||
jfloatArray xjJFloatArr,
|
||||
jfloatArray yJFloatArr) {
|
||||
|
||||
LOGI("Creating manager");
|
||||
|
||||
std::vector<float> xiVector = jfloatArrayToVector(env, xiJFloatArr);
|
||||
std::vector<float> xjVector = jfloatArrayToVector(env, xjJFloatArr);
|
||||
std::vector<float> yVector = jfloatArrayToVector(env, yJFloatArr);
|
||||
|
||||
kp::Manager mgr;
|
||||
|
||||
auto tensorA = mgr.buildTensor({0,1,2});
|
||||
auto tensorB = mgr.buildTensor({0,1,2});
|
||||
auto tensorC = mgr.buildTensor({1,2,3});
|
||||
auto tensorA = mgr.buildTensor(xiVector);
|
||||
auto tensorB = mgr.buildTensor(xjVector);
|
||||
auto tensorC = mgr.buildTensor(yVector);
|
||||
|
||||
LOGI("Result before:");
|
||||
for(const float & i : tensorC->data()) {
|
||||
|
|
@ -84,6 +109,6 @@ Java_com_ethicalml_kompute_KomputeJni_stringFromJNI(JNIEnv *env, jobject thiz) {
|
|||
LOGI("%f ", i);
|
||||
}
|
||||
|
||||
return env->NewStringUTF("Result is: ");
|
||||
return vectorToJFloatArray(env, tensorC->data());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,6 +18,10 @@ package com.ethicalml.kompute
|
|||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import android.widget.Toast
|
||||
import android.view.View
|
||||
import android.widget.EditText
|
||||
import android.widget.TextView
|
||||
import com.ethicalml.kompute.databinding.ActivityKomputeJniBinding
|
||||
|
||||
class KomputeJni : AppCompatActivity() {
|
||||
|
|
@ -34,14 +38,42 @@ class KomputeJni : AppCompatActivity() {
|
|||
binding.komputeGifView.getSettings().setLoadWithOverviewMode(true)
|
||||
|
||||
val successVulkanInit = initVulkan()
|
||||
if (successVulkanInit) {
|
||||
Toast.makeText(applicationContext, "Vulkan Loaded SUCCESS", Toast.LENGTH_SHORT).show()
|
||||
} else {
|
||||
binding.KomputeButton.isEnabled = false
|
||||
Toast.makeText(applicationContext, "Vulkan Load FAILED", Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
Log.i("KomputeJni", "Vulkan Result: " + successVulkanInit)
|
||||
|
||||
binding.komputeJniTextview.text = stringFromJNI()
|
||||
binding.komputeJniTextview.text = "N/A"
|
||||
}
|
||||
|
||||
fun KomputeButtonOnClick(v: View) {
|
||||
|
||||
// val binding = ActivityKomputeJniBinding.inflate(layoutInflater)
|
||||
// setContentView(binding.root)
|
||||
|
||||
val xiEditText = findViewById<EditText>(R.id.XIEditText)
|
||||
val xjEditText = findViewById<EditText>(R.id.XJEditText)
|
||||
val yEditText = findViewById<EditText>(R.id.YEditText)
|
||||
val komputeJniTextview = findViewById<TextView>(R.id.kompute_jni_textview)
|
||||
|
||||
val xi = xiEditText.text.removeSurrounding("[", "]").split(",").map { it.toFloat() }.toFloatArray()
|
||||
val xj = xjEditText.text.removeSurrounding("[", "]").split(",").map { it.toFloat() }.toFloatArray()
|
||||
val y = yEditText.text.removeSurrounding("[", "]").split(",").map { it.toFloat() }.toFloatArray()
|
||||
|
||||
val out = kompute(xi, xj, y)
|
||||
|
||||
Log.i("KomputeJni", "RESULT:")
|
||||
Log.i("KomputeJni", out.contentToString())
|
||||
|
||||
komputeJniTextview.text = out.contentToString()
|
||||
}
|
||||
|
||||
external fun initVulkan(): Boolean
|
||||
|
||||
external fun stringFromJNI(): String
|
||||
external fun kompute(xi: FloatArray, xj: FloatArray, y: FloatArray): FloatArray
|
||||
|
||||
companion object {
|
||||
init {
|
||||
|
|
|
|||
|
|
@ -10,10 +10,11 @@
|
|||
|
||||
<LinearLayout
|
||||
android:layout_width="409dp"
|
||||
android:layout_height="354dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
tools:layout_editor_absoluteX="1dp"
|
||||
tools:layout_editor_absoluteY="376dp">
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
tools:layout_editor_absoluteX="1dp">
|
||||
|
||||
<Space
|
||||
android:layout_width="match_parent"
|
||||
|
|
@ -60,15 +61,80 @@
|
|||
android:textSize="24sp" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/editTextTextPersonName"
|
||||
android:id="@+id/XIEditText"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:ems="10"
|
||||
android:inputType="textPersonName"
|
||||
android:text="Name" />
|
||||
android:text="[ 1, 2, 3 ]" />
|
||||
</LinearLayout>
|
||||
|
||||
<Space
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="20dp" />
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="50dp"
|
||||
android:orientation="horizontal">
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:text="Input Xj"
|
||||
android:textSize="24sp" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/XJEditText"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:ems="10"
|
||||
android:inputType="textPersonName"
|
||||
android:text="[ 1, 2, 3 ]" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<Space
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="20dp" />
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="50dp"
|
||||
android:orientation="horizontal">
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:text="Input Y"
|
||||
android:textSize="24sp" />
|
||||
|
||||
<EditText
|
||||
android:id="@+id/YEditText"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:ems="10"
|
||||
android:inputType="textPersonName"
|
||||
android:text="[ 1, 2, 3 ]" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<Space
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="20dp" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/KomputeButton"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:onClick="KomputeButtonOnClick"
|
||||
android:text="Kompute" />
|
||||
|
||||
<Space
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="20dp" />
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue