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/train/train_session.h"
20 #include "include/train/train_cfg.h"
21 #include "include/errorcode.h"
22
Java_com_mindspore_lite_TrainSession_createTrainSession(JNIEnv * env,jobject thiz,jstring file_name,jlong ms_context_ptr,jboolean train_mode,jlong train_config_ptr)23 extern "C" JNIEXPORT jlong JNICALL Java_com_mindspore_lite_TrainSession_createTrainSession(JNIEnv *env, jobject thiz,
24 jstring file_name,
25 jlong ms_context_ptr,
26 jboolean train_mode,
27 jlong train_config_ptr) {
28 auto *pointer = reinterpret_cast<void *>(ms_context_ptr);
29 if (pointer == nullptr) {
30 MS_LOGE("Context pointer from java is nullptr");
31 return jlong(nullptr);
32 }
33 auto *lite_context_ptr = static_cast<mindspore::lite::Context *>(pointer);
34
35 auto session = mindspore::session::TrainSession::CreateTrainSession(env->GetStringUTFChars(file_name, JNI_FALSE),
36 lite_context_ptr, train_mode, nullptr);
37 if (session == nullptr) {
38 MS_LOGE("CreateTrainSession failed");
39 return jlong(nullptr);
40 }
41 return jlong(session);
42 }
43