You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ms_config.cpp 2.8 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. #include <jni.h>
  17. #include "common/ms_log.h"
  18. #include "include/context.h"
  19. extern "C" JNIEXPORT jlong JNICALL Java_com_mindspore_lite_config_MSConfig_createMSConfig(
  20. JNIEnv *env, jobject thiz, jint device_type, jint thread_num, jint cpu_bind_mode, jboolean enable_float16) {
  21. auto *context = new (std::nothrow) mindspore::lite::Context();
  22. if (context == nullptr) {
  23. MS_LOGE("new Context fail!");
  24. return (jlong)context;
  25. }
  26. auto &cpu_device_ctx = context->device_list_[0];
  27. switch (device_type) {
  28. case 0:
  29. context->device_list_[0].device_type_ = mindspore::lite::DT_CPU;
  30. break;
  31. case 1: // DT_GPU
  32. {
  33. mindspore::lite::DeviceContext gpu_device_ctx{mindspore::lite::DT_GPU, {false}};
  34. gpu_device_ctx.device_info_.gpu_device_info_.enable_float16_ = enable_float16;
  35. context->device_list_.push_back(gpu_device_ctx);
  36. break;
  37. }
  38. case 2: // DT_NPU
  39. MS_LOGE("We only support CPU and GPU now.");
  40. return (jlong)context;
  41. break;
  42. default:
  43. MS_LOGE("Invalid device_type : %d", device_type);
  44. return (jlong)context;
  45. }
  46. switch (cpu_bind_mode) {
  47. case 0:
  48. cpu_device_ctx.device_info_.cpu_device_info_.cpu_bind_mode_ = mindspore::lite::NO_BIND;
  49. break;
  50. case 1:
  51. cpu_device_ctx.device_info_.cpu_device_info_.cpu_bind_mode_ = mindspore::lite::HIGHER_CPU;
  52. break;
  53. case 2:
  54. cpu_device_ctx.device_info_.cpu_device_info_.cpu_bind_mode_ = mindspore::lite::MID_CPU;
  55. break;
  56. default:
  57. MS_LOGE("Invalid cpu_bind_mode : %d", cpu_bind_mode);
  58. return (jlong)context;
  59. }
  60. cpu_device_ctx.device_info_.cpu_device_info_.enable_float16_ = enable_float16;
  61. context->thread_num_ = thread_num;
  62. return (jlong)context;
  63. }
  64. extern "C" JNIEXPORT void JNICALL Java_com_mindspore_lite_config_MSConfig_free(JNIEnv *env, jobject thiz,
  65. jlong context_ptr) {
  66. auto *pointer = reinterpret_cast<void *>(context_ptr);
  67. if (pointer == nullptr) {
  68. MS_LOGE("Context pointer from java is nullptr");
  69. return;
  70. }
  71. auto *lite_context_ptr = static_cast<mindspore::lite::Context *>(pointer);
  72. delete (lite_context_ptr);
  73. }