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_manager.h 4.5 kB

5 years ago
5 years ago
5 years ago
4 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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_FRONTEND_PARALLEL_DEVICE_MANAGER_H_
  17. #define MINDSPORE_CCSRC_FRONTEND_PARALLEL_DEVICE_MANAGER_H_
  18. #include <cstdint>
  19. #include <cstring>
  20. #include <map>
  21. #include <memory>
  22. #include <string>
  23. #include <utility>
  24. #include <vector>
  25. #include "frontend/parallel/device.h"
  26. #include "frontend/parallel/device_matrix.h"
  27. #include "frontend/parallel/group_manager.h"
  28. #include "frontend/parallel/status.h"
  29. #include "frontend/parallel/strategy.h"
  30. #include "utils/convert_utils.h"
  31. #include "utils/ms_utils.h"
  32. namespace mindspore {
  33. namespace parallel {
  34. #define MAX_DEVICE_NUM 4096
  35. constexpr char HCCL_BACKEND[] = "hccl";
  36. constexpr char NCCL_BACKEND[] = "nccl";
  37. constexpr char UNDEFINED_BACKEND[] = "undefined_backend";
  38. class DeviceManager;
  39. using DeviceManagerPtr = std::shared_ptr<DeviceManager>;
  40. // 'g_device_manager' is the globally unique manager to manage the devices.
  41. extern DeviceManagerPtr g_device_manager;
  42. // This method is used for initializing the global DeviceManager 'g_device_manager',
  43. // arguments including 'device_num' and 'global_rank'
  44. bool InitDevice(int64_t device_num, int64_t global_rank, const std::string &backend, const std::vector<int64_t> &stage);
  45. void CheckGlobalDeviceManager();
  46. std::string HashName(const std::string &rank_list_name);
  47. class DeviceManager {
  48. // This class is used to manage the abstract devices, including group-related and stage-related management.
  49. public:
  50. DeviceManager() { gm_ = GroupManager(); }
  51. ~DeviceManager() = default;
  52. Status Init(const RankList &devices, int64_t local_device, const RankList &stage_map, const std::string &backend);
  53. static DeviceManager &GetInstance();
  54. RankList GetDeviceListByStageId(int64_t stage_id) const;
  55. RankList GetDeviceListInThisStage() const;
  56. Device CreateNewDeviceByRank(int64_t rank) const;
  57. std::vector<Device> CreateDeviceListByRankList(RankList ranks);
  58. std::string GenerateGroupNameByRanks(RankList dev_ranks);
  59. Group CreateGroup(const std::string &group_name, const std::vector<Device> &devices);
  60. Group CreateGroup(const RankList &dev_ranks);
  61. size_t DeviceNum() const { return devices_.size(); }
  62. int64_t stage_num() const { return stage_num_; }
  63. int64_t stage_device_num() const { return stage_device_num_; }
  64. int64_t stage_id() const { return stage_id_; }
  65. int64_t rank_index_in_stage() const { return rank_index_in_stage_; }
  66. int64_t global_rank() const { return global_rank_; }
  67. std::string backend() const { return backend_; }
  68. GroupManager group_manager() const { return gm_; }
  69. void set_group_manager(const GroupManager &gm) { gm_ = gm; }
  70. std::vector<std::vector<int64_t>> stage_devices() const { return stage_devices_; }
  71. void Clear();
  72. std::string world_group() const { return gm_.world_group(); }
  73. std::vector<std::pair<std::string, std::vector<uint32_t>>> group_info() const { return gm_.group_info(); }
  74. std::string FindRankListNameByHashName(const std::string &hash_name);
  75. RankList FindRankListByHashName(const std::string &hash_name);
  76. private:
  77. std::vector<std::shared_ptr<Device>> devices_;
  78. // each stage has a list of devices
  79. std::vector<std::vector<int64_t>> stage_devices_;
  80. std::shared_ptr<Device> device_;
  81. GroupManager gm_;
  82. std::string backend_;
  83. // bimap:
  84. std::map<std::string, std::string> rank_to_group_; // the key is rank list, value is hash name
  85. std::map<std::string, std::string> group_to_rank_; // the key is hash name, value is rank list
  86. int64_t global_rank_ = 0; // the real rank in all devices
  87. int64_t stage_num_ = 1; // the stage num
  88. int64_t stage_id_ = 0; // the stage id of the global_rank_
  89. int64_t rank_index_in_stage_ = 0; // the index of this rank in it's stage
  90. int64_t stage_device_num_ = 0; // the device num of one stage
  91. };
  92. } // namespace parallel
  93. } // namespace mindspore
  94. #endif // MINDSPORE_CCSRC_FRONTEND_PARALLEL_DEVICE_MANAGER_H_