Skip to content

Commit

Permalink
[JAVA_API] Add as_long() to Tensor
Browse files Browse the repository at this point in the history
  • Loading branch information
ClaudioPaul0 committed Oct 15, 2024
1 parent 4272f47 commit d740fdd
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
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
7 changes: 7 additions & 0 deletions modules/java_api/src/main/java/org/intel/openvino/Tensor.java
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());
}
}

0 comments on commit d740fdd

Please sign in to comment.