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.

context.cpp 2.6 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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_context_Context_createContext(JNIEnv *env, jobject thiz,
  20. jint device_type,
  21. jint thread_num,
  22. jint cpu_bind_mode) {
  23. auto *context = new (std::nothrow) mindspore::lite::Context();
  24. if (context == nullptr) {
  25. MS_LOGE("new Context fail!");
  26. return (jlong)context;
  27. }
  28. switch (device_type) {
  29. case 0:
  30. context->device_ctx_.type = mindspore::lite::DT_CPU;
  31. break;
  32. case 1:
  33. context->device_ctx_.type = mindspore::lite::DT_GPU;
  34. break;
  35. case 2:
  36. context->device_ctx_.type = mindspore::lite::DT_NPU;
  37. break;
  38. default:
  39. MS_LOGE("Invalid device_type : %d", device_type);
  40. return (jlong)context;
  41. }
  42. switch (cpu_bind_mode) {
  43. case -1:
  44. context->cpu_bind_mode_ = mindspore::lite::MID_CPU;
  45. break;
  46. case 0:
  47. context->cpu_bind_mode_ = mindspore::lite::NO_BIND;
  48. break;
  49. case 1:
  50. context->cpu_bind_mode_ = mindspore::lite::HIGHER_CPU;
  51. break;
  52. default:
  53. MS_LOGE("Invalid cpu_bind_mode : %d", cpu_bind_mode);
  54. return (jlong)context;
  55. }
  56. context->thread_num_ = thread_num;
  57. return (jlong)context;
  58. }
  59. extern "C" JNIEXPORT void JNICALL Java_com_mindspore_lite_context_Context_free(JNIEnv *env, jobject thiz,
  60. jlong context_ptr) {
  61. auto *pointer = reinterpret_cast<void *>(context_ptr);
  62. if (pointer == nullptr) {
  63. MS_LOGE("Context pointer from java is nullptr");
  64. return;
  65. }
  66. auto *lite_context_ptr = static_cast<mindspore::lite::Context *>(pointer);
  67. delete (lite_context_ptr);
  68. }