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

5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 <utility>
  20. #include "backend/session/executor_manager.h"
  21. #include "frontend/parallel/device_manager.h"
  22. #include "utils/comm_manager.h"
  23. #include "utils/ms_context.h"
  24. namespace mindspore {
  25. namespace parallel {
  26. Group::Group() {
  27. name_.clear();
  28. devices_.clear();
  29. }
  30. Status Group::Init(const std::string &name, const std::vector<Device> &devices) {
  31. this->name_ = name;
  32. this->devices_ = devices;
  33. return Status::SUCCESS;
  34. }
  35. std::vector<Device> Group::GetDevicesList() const { return devices_; }
  36. bool Group::IsInThisGroup(int64_t device_rank) {
  37. for (auto &device : devices_) {
  38. if (device.rank() == device_rank) {
  39. return true;
  40. }
  41. }
  42. return false;
  43. }
  44. // Get the position of the device in the group
  45. Status Group::GetIndex(size_t *index) {
  46. size_t pos = 0;
  47. CheckGlobalDeviceManager();
  48. int64_t rank = g_device_manager->global_rank();
  49. for (auto &device : devices_) {
  50. if (device.rank() == rank) {
  51. *index = pos;
  52. return Status::SUCCESS;
  53. } else {
  54. pos++;
  55. }
  56. }
  57. MS_LOG(ERROR) << "Could not find device rank " << rank << "in this group!";
  58. return Status::FAILED;
  59. }
  60. GroupManager::GroupManager() { groups_.clear(); }
  61. Status GroupManager::CreateGroup(const std::string &group_name, const std::vector<Device> &devices,
  62. mindspore::parallel::Group *const group) {
  63. // it is simple to use size to determine whether it is a world group
  64. uint32_t world_size = 0;
  65. (void)CommManager::GetInstance().GetRankSize(world_group_, &world_size);
  66. if (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 executor
  90. auto context_ptr = MsContext::GetInstance();
  91. MS_EXCEPTION_IF_NULL(context_ptr);
  92. std::string device_name = context_ptr->get_param<std::string>(MS_CTX_DEVICE_TARGET);
  93. uint32_t device_id = context_ptr->get_param<uint32_t>(MS_CTX_DEVICE_ID);
  94. auto executor = session::ExecutorManager::Instance().GetExecutor(device_name, device_id);
  95. MS_EXCEPTION_IF_NULL(executor);
  96. bool ret = executor->CreateCommGroup(group_name, ranks);
  97. if (!ret) {
  98. MS_LOG(ERROR) << "Create group failed, group name is " << group_name;
  99. return Status::FAILED;
  100. }
  101. std::pair<std::string, std::vector<uint32_t>> group_info = std::make_pair(group_name, ranks);
  102. group_info_.push_back(group_info);
  103. MS_LOG(INFO) << "Create group success, group name is " << group_name;
  104. return Status::SUCCESS;
  105. }
  106. }
  107. Status GroupManager::DestroyGroup(const std::string &group_name) {
  108. auto context_ptr = MsContext::GetInstance();
  109. MS_EXCEPTION_IF_NULL(context_ptr);
  110. std::string device_name = context_ptr->get_param<std::string>(MS_CTX_DEVICE_TARGET);
  111. uint32_t device_id = context_ptr->get_param<uint32_t>(MS_CTX_DEVICE_ID);
  112. auto executor = session::ExecutorManager::Instance().GetExecutor(device_name, device_id);
  113. MS_EXCEPTION_IF_NULL(executor);
  114. bool ret = executor->DestroyCommGroup(group_name);
  115. if (!ret) {
  116. return Status::FAILED;
  117. }
  118. return Status::SUCCESS;
  119. }
  120. Status GroupManager::DestroyGroup(mindspore::parallel::Group *const group) {
  121. std::string name = (*group).name();
  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. (void)groups_.erase(it);
  128. return DestroyGroup(name);
  129. }
  130. Status GroupManager::DestroyAllGroups() {
  131. for (auto &it : groups_) {
  132. std::string name = it.first;
  133. auto ret = DestroyGroup(name);
  134. if (ret != Status::SUCCESS) {
  135. return Status::FAILED;
  136. }
  137. }
  138. groups_.clear();
  139. return Status::SUCCESS;
  140. }
  141. Status GroupManager::GetRankID(const std::string &name, uint32_t *const rank_id) {
  142. auto it = groups_.find(name);
  143. if (it == groups_.end()) {
  144. MS_LOG(ERROR) << "Could not find group name :" << name;
  145. return Status::FAILED;
  146. }
  147. bool ret = CommManager::GetInstance().GetRankID(name, rank_id);
  148. if (!ret) {
  149. return Status::FAILED;
  150. }
  151. return Status::SUCCESS;
  152. }
  153. Status GroupManager::GetRankSize(const std::string &name, uint32_t *const rank_size) {
  154. auto it = groups_.find(name);
  155. if (it == groups_.end()) {
  156. MS_LOG(ERROR) << "Could not find group name :" << name;
  157. return Status::FAILED;
  158. }
  159. bool ret = CommManager::GetInstance().GetRankSize(name, rank_size);
  160. if (!ret) {
  161. return Status::FAILED;
  162. }
  163. return Status::SUCCESS;
  164. }
  165. Status GroupManager::FindGroup(const std::string &name, mindspore::parallel::Group **group) {
  166. auto it = groups_.find(name);
  167. if (it == groups_.end()) {
  168. return Status::FAILED;
  169. }
  170. *group = &it->second;
  171. return Status::SUCCESS;
  172. }
  173. void GroupManager::Clear() { (void)DestroyAllGroups(); }
  174. Status CreateGroups(const std::vector<std::pair<std::string, std::vector<uint32_t>>> &group_info) {
  175. // Create group through the executor
  176. auto context_ptr = MsContext::GetInstance();
  177. MS_EXCEPTION_IF_NULL(context_ptr);
  178. std::string device_name = context_ptr->get_param<std::string>(MS_CTX_DEVICE_TARGET);
  179. uint32_t device_id = context_ptr->get_param<uint32_t>(MS_CTX_DEVICE_ID);
  180. auto executor = session::ExecutorManager::Instance().GetExecutor(device_name, device_id);
  181. MS_EXCEPTION_IF_NULL(executor);
  182. for (auto &group : group_info) {
  183. bool ret = executor->CreateCommGroup(group.first, group.second);
  184. if (!ret) {
  185. MS_LOG(ERROR) << "Create group failed, group name is " << group.first << ", ranks is " << group.second;
  186. return FAILED;
  187. }
  188. MS_LOG(INFO) << "Create group success, group name is " << group.first << ", ranks is " << group.second;
  189. }
  190. return SUCCESS;
  191. }
  192. } // namespace parallel
  193. } // namespace mindspore