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.

group_manager.h 3.1 kB

5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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_GROUP_MANAGER_H_
  17. #define MINDSPORE_CCSRC_FRONTEND_PARALLEL_GROUP_MANAGER_H_
  18. #include <cstdint>
  19. #include <map>
  20. #include <string>
  21. #include <vector>
  22. #include <utility>
  23. #include "frontend/parallel/device.h"
  24. #include "frontend/parallel/status.h"
  25. namespace mindspore {
  26. namespace parallel {
  27. constexpr char HCCL_WORLD_GROUP[] = "hccl_world_group";
  28. constexpr char NCCL_WORLD_GROUP[] = "nccl_world_group";
  29. constexpr char UNDEFINED_WORLD_GROUP[] = "undefined_world_group";
  30. // Devices that need communication should in the same group. These classes are used to
  31. // create and destroy group among devices.
  32. class Group {
  33. public:
  34. Group();
  35. ~Group() = default;
  36. Status Init(const std::string &name, const std::vector<Device> &devices);
  37. std::vector<Device> GetDevicesList() const;
  38. std::string name() const { return name_; }
  39. bool IsInThisGroup(int64_t device_rank);
  40. Status GetIndex(size_t *index);
  41. size_t GetDevNum() const { return devices_.size(); }
  42. private:
  43. std::string name_;
  44. std::vector<Device> devices_;
  45. };
  46. class GroupManager {
  47. public:
  48. GroupManager();
  49. ~GroupManager() = default;
  50. Status CreateGroup(const std::string &name, const std::vector<Device> &devices, Group *group);
  51. Status DestroyGroup(Group *group);
  52. Status DestroyAllGroups();
  53. Status GetRankID(const std::string &name, uint32_t *rank_id);
  54. Status GetRankSize(const std::string &name, uint32_t *rank_size);
  55. Status FindGroup(const std::string &name, Group **group);
  56. std::string world_group() const { return world_group_; }
  57. void set_world_group(const std::string &name) { world_group_ = name; }
  58. std::vector<std::pair<std::string, std::vector<uint32_t>>> group_info() const { return group_info_; }
  59. void Clear();
  60. private:
  61. bool CreateGroupByExecutor(const std::string &device_name, const std::string &group_name,
  62. const std::vector<uint32_t> ranks, int device_id);
  63. bool DestroyGroupByExecutor(const std::string &device_name, const std::string &group_name, int device_id);
  64. Status DestroyGroup(const std::string &group_name);
  65. // the key is group name (name_)
  66. std::map<std::string, Group> groups_;
  67. std::string world_group_;
  68. std::vector<std::pair<std::string, std::vector<uint32_t>>> group_info_;
  69. };
  70. Status CreateGroups(const std::vector<std::pair<std::string, std::vector<uint32_t>>> &group_info);
  71. } // namespace parallel
  72. } // namespace mindspore
  73. #endif // MINDSPORE_CCSRC_FRONTEND_PARALLEL_GROUP_MANAGER_H_