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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 UpdateDeviceContextKey(const DeviceContextKey &old_key, const DeviceContextKey &new_key);
  38. void ClearDeviceContexts();
  39. void WaitTaskFinishOnDevice() const;
  40. private:
  41. DeviceContextManager() = default;
  42. ~DeviceContextManager() = default;
  43. DISABLE_COPY_AND_ASSIGN(DeviceContextManager);
  44. // The string converted from DeviceContextKey -> DeviceContextPtr.
  45. std::map<std::string, DeviceContextPtr> device_contexts_;
  46. // The name of device -> DeviceContextCreator.
  47. std::map<std::string, DeviceContextCreator> device_context_creators_;
  48. };
  49. class DeviceContextRegister {
  50. public:
  51. DeviceContextRegister(const std::string &device_name, DeviceContextCreator &&runtime_creator) {
  52. DeviceContextManager::GetInstance().Register(device_name, std::move(runtime_creator));
  53. }
  54. ~DeviceContextRegister() = default;
  55. };
  56. #define MS_REGISTER_DEVICE(DEVICE_NAME, DEVICE_CONTEXT_CLASS) \
  57. static const DeviceContextRegister g_device_##DEVICE_NAME##_reg( \
  58. DEVICE_NAME, [](const DeviceContextKey &device_context_key) { \
  59. return std::make_shared<DEVICE_CONTEXT_CLASS>(device_context_key); \
  60. });
  61. } // namespace device
  62. } // namespace mindspore
  63. #endif // MINDSPORE_CCSRC_RUNTIME_HARDWARE_DEVICE_CONTEXT_MANAGER_H_