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.

device_context_manager.h 2.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /**
  2. * Copyright 2021 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_HARDWARE_DEVICE_CONTEXT_MANAGER_H_
  17. #define MINDSPORE_CCSRC_RUNTIME_HARDWARE_DEVICE_CONTEXT_MANAGER_H_
  18. #include <map>
  19. #include <string>
  20. #include <memory>
  21. #include <utility>
  22. #include <functional>
  23. #include <mutex>
  24. #include <vector>
  25. #include "runtime/hardware/device_context.h"
  26. namespace mindspore {
  27. namespace device {
  28. using DeviceContextCreator = std::function<std::shared_ptr<DeviceContext>(const DeviceContextKey &)>;
  29. class DeviceContextManager {
  30. public:
  31. static DeviceContextManager &GetInstance() {
  32. static DeviceContextManager instance;
  33. return instance;
  34. }
  35. void Register(const std::string &device_name, DeviceContextCreator &&device_context_creator);
  36. DeviceContext *GetOrCreateDeviceContext(const DeviceContextKey &device_context_key);
  37. void ClearDeviceContexts();
  38. private:
  39. DeviceContextManager() = default;
  40. ~DeviceContextManager() = default;
  41. DISABLE_COPY_AND_ASSIGN(DeviceContextManager);
  42. // The string converted from DeviceContextKey -> DeviceContextPtr.
  43. std::map<std::string, DeviceContextPtr> device_contexts_;
  44. // The name of device -> DeviceContextCreator.
  45. std::map<std::string, DeviceContextCreator> device_context_creators_;
  46. std::mutex lock_;
  47. };
  48. class DeviceContextRegister {
  49. public:
  50. DeviceContextRegister(const std::string &device_name, DeviceContextCreator &&runtime_creator) {
  51. DeviceContextManager::GetInstance().Register(device_name, std::move(runtime_creator));
  52. }
  53. ~DeviceContextRegister() = default;
  54. };
  55. #define MS_REGISTER_DEVICE(DEVICE_NAME, DEVICE_CONTEXT_CLASS) \
  56. static const DeviceContextRegister g_device_##DEVICE_NAME##_reg( \
  57. DEVICE_NAME, [](const DeviceContextKey &device_context_key) { \
  58. return std::make_shared<DEVICE_CONTEXT_CLASS>(device_context_key); \
  59. });
  60. } // namespace device
  61. } // namespace mindspore
  62. #endif // MINDSPORE_CCSRC_RUNTIME_HARDWARE_DEVICE_CONTEXT_MANAGER_H_