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.9 kB

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