Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JAVA_API] Add as_long() to Tensor #935

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions modules/java_api/src/main/cpp/openvino_java.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ extern "C"
JNIEXPORT jintArray JNICALL Java_org_intel_openvino_Tensor_GetShape(JNIEnv *, jobject, jlong);
JNIEXPORT jfloatArray JNICALL Java_org_intel_openvino_Tensor_asFloat(JNIEnv *, jobject, jlong);
JNIEXPORT jintArray JNICALL Java_org_intel_openvino_Tensor_asInt(JNIEnv *, jobject, jlong);
JNIEXPORT jlongArray JNICALL Java_org_intel_openvino_Tensor_asLong(JNIEnv *, jobject, jlong);
JNIEXPORT void JNICALL Java_org_intel_openvino_Tensor_delete(JNIEnv *, jobject, jlong);

// ov::PrePostProcessor
Expand Down
23 changes: 23 additions & 0 deletions modules/java_api/src/main/cpp/tensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,29 @@ JNIEXPORT jintArray JNICALL Java_org_intel_openvino_Tensor_asInt(JNIEnv *env, jo
return 0;
}

JNIEXPORT jlongArray JNICALL Java_org_intel_openvino_Tensor_asLong(JNIEnv *env, jobject, jlong addr)
{
JNI_METHOD(
"asLong",
Tensor *ov_tensor = (Tensor *)addr;

size_t size = ov_tensor->get_size();
const long *data = ov_tensor->data<const long>();

jlongArray result = env->NewLongArray(size);
if (!result) {
throw std::runtime_error("Out of memory!");
} jlong *arr = env->GetLongArrayElements(result, nullptr);

for (size_t i = 0; i < size; ++i)
arr[i] = data[i];

env->ReleaseLongArrayElements(result, arr, 0);
return result;
)
return 0;
}

JNIEXPORT void JNICALL Java_org_intel_openvino_Tensor_delete(JNIEnv *, jobject, jlong addr)
{
Tensor *tensor = (Tensor *)addr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ public int[] as_int() {
return asInt(nativeObj);
}

/** Returns the tensor data as a long array. */
public long[] as_long() {
return asLong(nativeObj);
}

/*----------------------------------- native methods -----------------------------------*/
private static native long TensorCArray(int type, int[] shape, long cArray);

Expand All @@ -81,6 +86,8 @@ public int[] as_int() {

private static native int[] asInt(long addr);

private static native long[] asLong(long addr);

private static native int GetSize(long addr);

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public void testGetTensorFromLong() {
Tensor tensor = new Tensor(dimsArr, inputData);

assertArrayEquals(dimsArr, tensor.get_shape());
assertArrayEquals(inputData, tensor.as_long());
assertEquals(size, tensor.get_size());
}
}
Loading