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

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