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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 "frontend/parallel/group_manager.h"
  17. #include <algorithm>
  18. #include <vector>
  19. #include "frontend/parallel/device_manager.h"
  20. #include "frontend/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. (void)CommManager::GetInstance().GetRankSize(world_group_, &world_size);
  64. if (devices.size() == world_size) {
  65. auto it = groups_.find(world_group_);
  66. if (it == groups_.end()) {
  67. (void)group->Init(world_group_, devices);
  68. groups_[world_group_] = *group;
  69. } else {
  70. *group = it->second;
  71. }
  72. MS_LOG(INFO) << "It is world group " << world_group_ << ", no need to create it.";
  73. return Status::SUCCESS;
  74. }
  75. auto it = groups_.find(group_name);
  76. // If there already exits a group with the desired 'name',
  77. // let the pointer point to the group.
  78. if (it != groups_.end()) {
  79. *group = it->second;
  80. return Status::SUCCESS;
  81. } else {
  82. (void)group->Init(group_name, devices);
  83. groups_[group_name] = *group;
  84. vector<uint32_t> ranks;
  85. (void)std::transform(std::begin(devices), std::end(devices), std::back_inserter(ranks),
  86. [](const Device dev) { return (uint32_t)dev.rank(); });
  87. // Create group through the CommManager interface
  88. bool ret = CommManager::GetInstance().CreateGroupSync(group_name, ranks);
  89. if (!ret) {
  90. MS_LOG(ERROR) << "Create group failed, group name is " << group_name;
  91. return Status::FAILED;
  92. }
  93. MS_LOG(INFO) << "Create group success, group name is " << group_name;
  94. return Status::SUCCESS;
  95. }
  96. }
  97. Status GroupManager::DestroyGroup(mindspore::parallel::Group *const group) {
  98. std::string name = (*group).name();
  99. auto it = groups_.find(name);
  100. if (it == groups_.end()) {
  101. MS_LOG(ERROR) << "Could not find group name :" << name;
  102. return Status::FAILED;
  103. }
  104. (void)groups_.erase(it);
  105. bool ret = CommManager::GetInstance().DestroyGroup(name);
  106. if (!ret) {
  107. return Status::FAILED;
  108. }
  109. return Status::SUCCESS;
  110. }
  111. Status GroupManager::DestroyAllGroups() {
  112. for (auto &it : groups_) {
  113. std::string name = it.first;
  114. bool ret = CommManager::GetInstance().DestroyGroup(name);
  115. if (!ret) {
  116. return Status::FAILED;
  117. }
  118. }
  119. groups_.clear();
  120. return Status::SUCCESS;
  121. }
  122. Status GroupManager::GetRankID(const std::string &name, unsigned int *const rank_id) {
  123. auto it = groups_.find(name);
  124. if (it == groups_.end()) {
  125. MS_LOG(ERROR) << "Could not find group name :" << name;
  126. return Status::FAILED;
  127. }
  128. bool ret = CommManager::GetInstance().GetRankID(name, rank_id);
  129. if (!ret) {
  130. return Status::FAILED;
  131. }
  132. return Status::SUCCESS;
  133. }
  134. Status GroupManager::GetRankSize(const std::string &name, unsigned int *const rank_size) {
  135. auto it = groups_.find(name);
  136. if (it == groups_.end()) {
  137. MS_LOG(ERROR) << "Could not find group name :" << name;
  138. return Status::FAILED;
  139. }
  140. bool ret = CommManager::GetInstance().GetRankSize(name, rank_size);
  141. if (!ret) {
  142. return Status::FAILED;
  143. }
  144. return Status::SUCCESS;
  145. }
  146. Status GroupManager::FindGroup(const std::string &name, mindspore::parallel::Group **group) {
  147. auto it = groups_.find(name);
  148. if (it == groups_.end()) {
  149. return Status::FAILED;
  150. }
  151. *group = &it->second;
  152. return Status::SUCCESS;
  153. }
  154. void GroupManager::Clear() { (void)DestroyAllGroups(); }
  155. } // namespace parallel
  156. } // namespace mindspore