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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. #include "parallel/group_manager.h"
  17. #include <algorithm>
  18. #include <vector>
  19. #include "parallel/device_manager.h"
  20. #include "parallel/ops_info/ops_utils.h"
  21. #include "utils/comm_manager.h"
  22. namespace mindspore {
  23. namespace parallel {
  24. Group::Group() {
  25. name_.clear();
  26. devices_.clear();
  27. }
  28. Status Group::Init(const std::string &name, const std::vector<Device> &devices) {
  29. this->name_ = name;
  30. this->devices_ = devices;
  31. return Status::SUCCESS;
  32. }
  33. std::vector<Device> Group::GetDevicesList() const { return devices_; }
  34. bool Group::IsInThisGroup(int32_t device_rank) {
  35. for (auto &device : devices_) {
  36. if (device.rank() == device_rank) {
  37. return true;
  38. }
  39. }
  40. return false;
  41. }
  42. // Get the position of the device in the group
  43. Status Group::GetIndex(size_t *index) {
  44. size_t pos = 0;
  45. CheckGlobalDeviceManager();
  46. int32_t rank = g_device_manager->global_rank();
  47. for (auto &device : devices_) {
  48. if (device.rank() == rank) {
  49. *index = pos;
  50. return Status::SUCCESS;
  51. } else {
  52. pos++;
  53. }
  54. }
  55. MS_LOG(ERROR) << "Could not find device rank " << rank << "in this group!";
  56. return Status::FAILED;
  57. }
  58. GroupManager::GroupManager() { groups_.clear(); }
  59. Status GroupManager::CreateGroup(const std::string &group_name, const std::vector<Device> &devices,
  60. mindspore::parallel::Group *const group) {
  61. // it is simple to use size to determine whether it is a world group
  62. uint32_t world_size = 0;
  63. if (world_group_ != NCCL_WORLD_GROUP) {
  64. (void)CommManager::GetInstance().GetRankSize(world_group_, &world_size);
  65. }
  66. if ((world_group_ == NCCL_WORLD_GROUP) || (devices.size() == world_size)) {
  67. auto it = groups_.find(world_group_);
  68. if (it == groups_.end()) {
  69. (void)group->Init(world_group_, devices);
  70. groups_[world_group_] = *group;
  71. } else {
  72. *group = it->second;
  73. }
  74. MS_LOG(INFO) << "It is world group " << world_group_ << ", no need to create it.";
  75. return Status::SUCCESS;
  76. }
  77. auto it = groups_.find(group_name);
  78. // If there already exits a group with the desired 'name',
  79. // let the pointer point to the group.
  80. if (it != groups_.end()) {
  81. *group = it->second;
  82. return Status::SUCCESS;
  83. } else {
  84. (void)group->Init(group_name, devices);
  85. groups_[group_name] = *group;
  86. vector<uint32_t> ranks;
  87. (void)std::transform(std::begin(devices), std::end(devices), std::back_inserter(ranks),
  88. [](const Device dev) { return (uint32_t)dev.rank(); });
  89. // Create group through the CommManager interface
  90. bool ret = CommManager::GetInstance().CreateGroupSync(group_name, ranks);
  91. if (!ret) {
  92. MS_LOG(ERROR) << "Create group failed, group name is " << group_name;
  93. return Status::FAILED;
  94. }
  95. MS_LOG(INFO) << "Create group success, group name is " << group_name;
  96. return Status::SUCCESS;
  97. }
  98. }
  99. Status GroupManager::DestroyGroup(mindspore::parallel::Group *const group) {
  100. std::string name = (*group).name();
  101. auto it = groups_.find(name);
  102. if (it == groups_.end()) {
  103. MS_LOG(ERROR) << "Could not find group name :" << name;
  104. return Status::FAILED;
  105. }
  106. (void)groups_.erase(it);
  107. bool ret = CommManager::GetInstance().DestroyGroup(name);
  108. if (!ret) {
  109. return Status::FAILED;
  110. }
  111. return Status::SUCCESS;
  112. }
  113. Status GroupManager::DestroyAllGroups() {
  114. for (auto &it : groups_) {
  115. std::string name = it.first;
  116. bool ret = CommManager::GetInstance().DestroyGroup(name);
  117. if (!ret) {
  118. return Status::FAILED;
  119. }
  120. }
  121. groups_.clear();
  122. return Status::SUCCESS;
  123. }
  124. Status GroupManager::GetRankID(const std::string &name, unsigned int *const rank_id) {
  125. auto it = groups_.find(name);
  126. if (it == groups_.end()) {
  127. MS_LOG(ERROR) << "Could not find group name :" << name;
  128. return Status::FAILED;
  129. }
  130. bool ret = CommManager::GetInstance().GetRankID(name, rank_id);
  131. if (!ret) {
  132. return Status::FAILED;
  133. }
  134. return Status::SUCCESS;
  135. }
  136. Status GroupManager::GetRankSize(const std::string &name, unsigned int *const rank_size) {
  137. auto it = groups_.find(name);
  138. if (it == groups_.end()) {
  139. MS_LOG(ERROR) << "Could not find group name :" << name;
  140. return Status::FAILED;
  141. }
  142. bool ret = CommManager::GetInstance().GetRankSize(name, rank_size);
  143. if (!ret) {
  144. return Status::FAILED;
  145. }
  146. return Status::SUCCESS;
  147. }
  148. Status GroupManager::FindGroup(const std::string &name, mindspore::parallel::Group **group) {
  149. auto it = groups_.find(name);
  150. if (it == groups_.end()) {
  151. return Status::FAILED;
  152. }
  153. *group = &it->second;
  154. return Status::SUCCESS;
  155. }
  156. void GroupManager::Clear() { (void)DestroyAllGroups(); }
  157. } // namespace parallel
  158. } // namespace mindspore