1 /**
2 * Copyright 2020 Huawei Technologies Co., Ltd
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <jni.h>
18 #include "common/ms_log.h"
19 #include "include/context.h"
20
Java_com_mindspore_lite_config_MSConfig_createMSConfig(JNIEnv * env,jobject thiz,jint device_type,jint thread_num,jint cpu_bind_mode,jboolean enable_float16)21 extern "C" JNIEXPORT jlong JNICALL Java_com_mindspore_lite_config_MSConfig_createMSConfig(
22 JNIEnv *env, jobject thiz, jint device_type, jint thread_num, jint cpu_bind_mode, jboolean enable_float16) {
23 auto *context = new (std::nothrow) mindspore::lite::Context();
24 if (context == nullptr) {
25 MS_LOGE("new Context fail!");
26 return (jlong) nullptr;
27 }
28
29 auto &cpu_device_ctx = context->device_list_[0];
30 switch (cpu_bind_mode) {
31 case 0:
32 cpu_device_ctx.device_info_.cpu_device_info_.cpu_bind_mode_ = mindspore::lite::NO_BIND;
33 break;
34 case 1:
35 cpu_device_ctx.device_info_.cpu_device_info_.cpu_bind_mode_ = mindspore::lite::HIGHER_CPU;
36 break;
37 case 2:
38 cpu_device_ctx.device_info_.cpu_device_info_.cpu_bind_mode_ = mindspore::lite::MID_CPU;
39 break;
40 default:
41 MS_LOGE("Invalid cpu_bind_mode : %d", cpu_bind_mode);
42 delete context;
43 return (jlong) nullptr;
44 }
45 if (enable_float16) {
46 cpu_device_ctx.device_info_.cpu_device_info_.enable_float16_ = true;
47 }
48 switch (device_type) {
49 case 0:
50 context->device_list_[0].device_type_ = mindspore::lite::DT_CPU;
51 break;
52 case 1: // DT_GPU
53 {
54 mindspore::lite::DeviceContext gpu_device_ctx{mindspore::lite::DT_GPU, {false}};
55 if (enable_float16) {
56 gpu_device_ctx.device_info_.gpu_device_info_.enable_float16_ = true;
57 }
58 context->device_list_.push_back(gpu_device_ctx);
59 break;
60 }
61 case 2: // DT_NPU
62 MS_LOGE("We only support CPU and GPU now.");
63 delete context;
64 return (jlong) nullptr;
65 default:
66 MS_LOGE("Invalid device_type : %d", device_type);
67 delete context;
68 return (jlong) nullptr;
69 }
70 context->thread_num_ = thread_num;
71 return (jlong)context;
72 }
73
Java_com_mindspore_lite_config_MSConfig_free(JNIEnv * env,jobject thiz,jlong context_ptr)74 extern "C" JNIEXPORT void JNICALL Java_com_mindspore_lite_config_MSConfig_free(JNIEnv *env, jobject thiz,
75 jlong context_ptr) {
76 auto *pointer = reinterpret_cast<void *>(context_ptr);
77 if (pointer == nullptr) {
78 MS_LOGE("Context pointer from java is nullptr");
79 return;
80 }
81 auto *lite_context_ptr = static_cast<mindspore::lite::Context *>(pointer);
82 delete (lite_context_ptr);
83 }
84