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.

kernel_runtime_manager.h 2.7 kB

5 years ago
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. #ifndef MINDSPORE_CCSRC_RUNTIME_DEVICE_KERNEL_RUNTIME_MANAGER_H_
  17. #define MINDSPORE_CCSRC_RUNTIME_DEVICE_KERNEL_RUNTIME_MANAGER_H_
  18. #include <map>
  19. #include <memory>
  20. #include <string>
  21. #include <functional>
  22. #include <utility>
  23. #include <mutex>
  24. #include <unordered_set>
  25. #include <vector>
  26. #include "utils/ms_utils.h"
  27. #include "runtime/device/kernel_runtime.h"
  28. #include "include/backend/visible.h"
  29. namespace mindspore {
  30. namespace device {
  31. using KernelRuntimeCreator = std::function<std::shared_ptr<KernelRuntime>()>;
  32. class BACKEND_EXPORT KernelRuntimeManager {
  33. public:
  34. static KernelRuntimeManager &Instance();
  35. void Register(const std::string &device_name, KernelRuntimeCreator &&runtime_creator);
  36. KernelRuntime *GetKernelRuntime(const std::string &device_name, uint32_t device_id);
  37. KernelRuntime *GetCurrentKernelRuntime();
  38. KernelRuntime *GetSingleKernelRuntime(const std::string &device_name, uint32_t device_id);
  39. void ReleaseKernelRuntime(const std::string &device_name, uint32_t device_id);
  40. void ClearRuntimeResource();
  41. void ClearGraphResource(uint32_t graph_id);
  42. void WaitTaskFinishOnDevice() const;
  43. private:
  44. KernelRuntimeManager() = default;
  45. ~KernelRuntimeManager() = default;
  46. DISABLE_COPY_AND_ASSIGN(KernelRuntimeManager);
  47. std::string GetDeviceKey(const std::string &device_name, uint32_t device_id);
  48. std::map<std::string, std::shared_ptr<KernelRuntime> > runtime_map_;
  49. std::map<std::string, KernelRuntimeCreator> runtime_creators_;
  50. std::mutex lock_;
  51. };
  52. class KernelRuntimeRegistrar {
  53. public:
  54. KernelRuntimeRegistrar(const std::string &device_name, KernelRuntimeCreator &&runtime_creator) {
  55. KernelRuntimeManager::Instance().Register(device_name, std::move(runtime_creator));
  56. }
  57. ~KernelRuntimeRegistrar() = default;
  58. };
  59. #define MS_REG_KERNEL_RUNTIME(DEVICE_NAME, RUNTIME_CLASS) \
  60. static const KernelRuntimeRegistrar g_kernel_runtime_##DEVICE_NAME##_reg( \
  61. DEVICE_NAME, []() { return std::make_shared<RUNTIME_CLASS>(); });
  62. } // namespace device
  63. } // namespace mindspore
  64. #endif // MINDSPORE_CCSRC_RUNTIME_DEVICE_KERNEL_RUNTIME_MANAGER_H_