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

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