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.

runtime_api.cc 2.3 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /**
  2. * Copyright 2019 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 "src/runtime/runtime_api.h"
  17. #include <mutex>
  18. #include <string>
  19. #include "src/runtime/workspace_pool.h"
  20. #include "src/runtime/thread_pool.h"
  21. #include "common/mslog.h"
  22. static std::mutex gWorkspaceMutex;
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. void LiteAPISetLastError(const char *msg) { MS_LOGE("The lite api set last error is %s.", msg); }
  27. void *LiteBackendAllocWorkspace(int deviceType, int deviceId, uint64_t size, int dtypeCode, int dtypeBits) {
  28. std::lock_guard<std::mutex> lock(gWorkspaceMutex);
  29. auto p = mindspore::predict::WorkspacePool::GetInstance();
  30. if (p == nullptr) {
  31. MS_LOGE("get ThreadPool install failed");
  32. return nullptr;
  33. }
  34. return p->AllocWorkSpaceMem(size);
  35. }
  36. int LiteBackendFreeWorkspace(int deviceType, int deviceId, void *ptr) {
  37. std::lock_guard<std::mutex> lock(gWorkspaceMutex);
  38. auto p = mindspore::predict::WorkspacePool::GetInstance();
  39. if (p == nullptr) {
  40. MS_LOGE("get ThreadPool install failed");
  41. return -1;
  42. }
  43. p->FreeWorkSpaceMem(ptr);
  44. return 0;
  45. }
  46. void ConfigThreadPool(int mode, int nthreads) {
  47. auto p = mindspore::predict::ThreadPool::GetInstance();
  48. if (p == nullptr) {
  49. MS_LOGE("get ThreadPool install failed");
  50. return;
  51. }
  52. p->ConfigThreadPool(mode, nthreads);
  53. }
  54. int LiteBackendParallelLaunch(FTVMParallelLambda flambda, void *cdata, int num_task) {
  55. auto p = mindspore::predict::ThreadPool::GetInstance();
  56. if (p == nullptr) {
  57. MS_LOGE("get ThreadPool install failed");
  58. return -1;
  59. }
  60. if (!p->LaunchThreadPoolTask()) {
  61. MS_LOGE("get ThreadPool or thread bind failed");
  62. return -1;
  63. }
  64. if (!p->AddTask(flambda, cdata, num_task)) {
  65. MS_LOGE("AddTask failed");
  66. return -1;
  67. }
  68. return 0;
  69. }
  70. #ifdef __cplusplus
  71. }
  72. #endif