|
| 1 | +#include <inference_engine.hpp> |
| 2 | + |
| 3 | +#include "openvino_java.hpp" |
| 4 | +#include "jni_common.hpp" |
| 5 | + |
| 6 | +using namespace InferenceEngine; |
| 7 | + |
| 8 | +JNIEXPORT jlong JNICALL Java_org_intel_openvino_Blob_GetTensorDesc(JNIEnv *env, jobject obj, jlong addr) |
| 9 | +{ |
| 10 | + static const char method_name[] = "GetTensorDesc"; |
| 11 | + try |
| 12 | + { |
| 13 | + Blob::Ptr *output = reinterpret_cast<Blob::Ptr *>(addr); |
| 14 | + TensorDesc *tDesc = new TensorDesc((*output)->getTensorDesc()); |
| 15 | + |
| 16 | + return (jlong)tDesc; |
| 17 | + } |
| 18 | + catch (const std::exception &e) |
| 19 | + { |
| 20 | + throwJavaException(env, &e, method_name); |
| 21 | + } |
| 22 | + catch (...) |
| 23 | + { |
| 24 | + throwJavaException(env, 0, method_name); |
| 25 | + } |
| 26 | + |
| 27 | + return 0; |
| 28 | +} |
| 29 | + |
| 30 | +JNIEXPORT jlong JNICALL Java_org_intel_openvino_Blob_GetBlob(JNIEnv *env, jobject obj, jlong tensorDescAddr) |
| 31 | +{ |
| 32 | + static const char method_name[] = "GetBlob"; |
| 33 | + try |
| 34 | + { |
| 35 | + TensorDesc *tDesc = (TensorDesc *)tensorDescAddr; |
| 36 | + |
| 37 | + Blob::Ptr *blob = new Blob::Ptr(); |
| 38 | + *blob = make_shared_blob<uint8_t>(*tDesc); |
| 39 | + |
| 40 | + return (jlong)blob; |
| 41 | + } |
| 42 | + catch (const std::exception &e) |
| 43 | + { |
| 44 | + throwJavaException(env, &e, method_name); |
| 45 | + } |
| 46 | + catch (...) |
| 47 | + { |
| 48 | + throwJavaException(env, 0, method_name); |
| 49 | + } |
| 50 | + |
| 51 | + return 0; |
| 52 | +} |
| 53 | + |
| 54 | +JNIEXPORT jlong JNICALL Java_org_intel_openvino_Blob_BlobByte(JNIEnv *env, jobject obj, jlong tensorDescAddr, jbyteArray data) |
| 55 | +{ |
| 56 | + static const char method_name[] = "BlobByte"; |
| 57 | + try |
| 58 | + { |
| 59 | + TensorDesc *tDesc = (TensorDesc *)tensorDescAddr; |
| 60 | + |
| 61 | + Blob::Ptr *blob = new Blob::Ptr(); |
| 62 | + |
| 63 | + *blob = make_shared_blob<uint8_t>((*tDesc)); |
| 64 | + (*blob)->allocate(); |
| 65 | + env->GetByteArrayRegion(data, 0, (*blob)->size(), (*blob)->buffer()); |
| 66 | + |
| 67 | + return (jlong)blob; |
| 68 | + } |
| 69 | + catch (const std::exception &e) |
| 70 | + { |
| 71 | + throwJavaException(env, &e, method_name); |
| 72 | + } |
| 73 | + catch (...) |
| 74 | + { |
| 75 | + throwJavaException(env, 0, method_name); |
| 76 | + } |
| 77 | + |
| 78 | + return 0; |
| 79 | +} |
| 80 | + |
| 81 | +JNIEXPORT jlong JNICALL Java_org_intel_openvino_Blob_BlobFloat(JNIEnv *env, jobject obj, jlong tensorDescAddr, jfloatArray data) |
| 82 | +{ |
| 83 | + static const char method_name[] = "BlobFloat"; |
| 84 | + try |
| 85 | + { |
| 86 | + TensorDesc *tDesc = (TensorDesc *)tensorDescAddr; |
| 87 | + |
| 88 | + Blob::Ptr *blob = new Blob::Ptr(); |
| 89 | + |
| 90 | + *blob = make_shared_blob<float>((*tDesc)); |
| 91 | + (*blob)->allocate(); |
| 92 | + env->GetFloatArrayRegion(data, 0, (*blob)->size(), (*blob)->buffer()); |
| 93 | + |
| 94 | + return (jlong)blob; |
| 95 | + } |
| 96 | + catch (const std::exception &e) |
| 97 | + { |
| 98 | + throwJavaException(env, &e, method_name); |
| 99 | + } |
| 100 | + catch (...) |
| 101 | + { |
| 102 | + throwJavaException(env, 0, method_name); |
| 103 | + } |
| 104 | + |
| 105 | + return 0; |
| 106 | +} |
| 107 | + |
| 108 | +JNIEXPORT jlong JNICALL Java_org_intel_openvino_Blob_BlobCArray(JNIEnv *env, jobject obj, jlong tensorDescAddr, jlong matDataAddr) |
| 109 | +{ |
| 110 | + static const char method_name[] = "BlobCArray"; |
| 111 | + try |
| 112 | + { |
| 113 | + TensorDesc *tDesc = (TensorDesc *)tensorDescAddr; |
| 114 | + |
| 115 | + auto precision = tDesc->getPrecision(); |
| 116 | + |
| 117 | + std::vector<size_t> dims = tDesc->getDims(); |
| 118 | + Blob::Ptr *blob = new Blob::Ptr(); |
| 119 | + |
| 120 | + switch (precision) { |
| 121 | + case Precision::FP32: |
| 122 | + { |
| 123 | + float *data = (float *) matDataAddr; |
| 124 | + *blob = make_shared_blob<float>((*tDesc), data); |
| 125 | + break; |
| 126 | + } |
| 127 | + case Precision::Q78: |
| 128 | + case Precision::I16: |
| 129 | + case Precision::FP16: |
| 130 | + { |
| 131 | + short *data = (short *) matDataAddr; |
| 132 | + *blob = make_shared_blob<short>((*tDesc), data); |
| 133 | + break; |
| 134 | + } |
| 135 | + case Precision::U8: |
| 136 | + { |
| 137 | + uint8_t *data = (uint8_t *) matDataAddr; |
| 138 | + *blob = make_shared_blob<uint8_t>((*tDesc), data); |
| 139 | + break; |
| 140 | + } |
| 141 | + case Precision::I8: |
| 142 | + { |
| 143 | + int8_t *data = (int8_t *) matDataAddr; |
| 144 | + *blob = make_shared_blob<int8_t>((*tDesc), data); |
| 145 | + break; |
| 146 | + } |
| 147 | + case Precision::I32: |
| 148 | + { |
| 149 | + int32_t *data = (int32_t *) matDataAddr; |
| 150 | + *blob = make_shared_blob<int32_t>((*tDesc), data); |
| 151 | + break; |
| 152 | + } |
| 153 | + case Precision::BF16: |
| 154 | + { |
| 155 | + short *data = (short *) matDataAddr; |
| 156 | + *blob = make_shared_blob<short>((*tDesc), data); |
| 157 | + break; |
| 158 | + } |
| 159 | + default: |
| 160 | + throw std::runtime_error("Unsupported precision value!"); |
| 161 | + } |
| 162 | + |
| 163 | + return (jlong)blob; |
| 164 | + } |
| 165 | + catch (const std::exception &e) |
| 166 | + { |
| 167 | + throwJavaException(env, &e, method_name); |
| 168 | + } |
| 169 | + catch (...) |
| 170 | + { |
| 171 | + throwJavaException(env, 0, method_name); |
| 172 | + } |
| 173 | + |
| 174 | + return 0; |
| 175 | +} |
| 176 | + |
| 177 | +JNIEXPORT jint JNICALL Java_org_intel_openvino_Blob_size(JNIEnv *env, jobject obj, jlong addr) |
| 178 | +{ |
| 179 | + static const char method_name[] = "size"; |
| 180 | + try |
| 181 | + { |
| 182 | + Blob::Ptr *output = reinterpret_cast<Blob::Ptr *>(addr); |
| 183 | + int size = (*output)->size(); |
| 184 | + |
| 185 | + return size; |
| 186 | + } |
| 187 | + catch (const std::exception &e) |
| 188 | + { |
| 189 | + throwJavaException(env, &e, method_name); |
| 190 | + } |
| 191 | + catch (...) |
| 192 | + { |
| 193 | + throwJavaException(env, 0, method_name); |
| 194 | + } |
| 195 | + |
| 196 | + return 0; |
| 197 | +} |
| 198 | + |
| 199 | +JNIEXPORT jlong JNICALL Java_org_intel_openvino_Blob_rmap(JNIEnv *env, jobject obj, jlong addr) |
| 200 | +{ |
| 201 | + static const char method_name[] = "rmap"; |
| 202 | + try |
| 203 | + { |
| 204 | + Blob::Ptr *output = reinterpret_cast<Blob::Ptr *>(addr); |
| 205 | + |
| 206 | + if ((*output)->is<MemoryBlob>()) { |
| 207 | + LockedMemory<const void> *lmem = new LockedMemory<const void> (as<MemoryBlob>(*output)->rmap()); |
| 208 | + return (jlong)lmem; |
| 209 | + } else { |
| 210 | + throw std::runtime_error("Target Blob cannot be cast to the MemoryBlob!"); |
| 211 | + } |
| 212 | + } |
| 213 | + catch (const std::exception &e) |
| 214 | + { |
| 215 | + throwJavaException(env, &e, method_name); |
| 216 | + } |
| 217 | + catch (...) |
| 218 | + { |
| 219 | + throwJavaException(env, 0, method_name); |
| 220 | + } |
| 221 | + |
| 222 | + return 0; |
| 223 | +} |
| 224 | + |
| 225 | +JNIEXPORT void JNICALL Java_org_intel_openvino_Blob_delete(JNIEnv *, jobject, jlong addr) |
| 226 | +{ |
| 227 | + Blob::Ptr *output = reinterpret_cast<Blob::Ptr *>(addr); |
| 228 | + delete output; |
| 229 | +} |
0 commit comments