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

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