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

5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. #if !defined(NO_DLIB) || defined(ENABLE_GPU)
  21. #include "backend/session/executor_manager.h"
  22. #else
  23. #include "frontend/parallel/parallel_stub/executor_manager_stub.h"
  24. #endif
  25. #include "frontend/parallel/device_manager.h"
  26. #include "utils/comm_manager.h"
  27. #include "utils/ms_context.h"
  28. namespace mindspore {
  29. namespace parallel {
  30. Group::Group() {
  31. name_.clear();
  32. devices_.clear();
  33. }
  34. Status Group::Init(const std::string &name, const std::vector<Device> &devices) {
  35. this->name_ = name;
  36. this->devices_ = devices;
  37. return Status::SUCCESS;
  38. }
  39. std::vector<Device> Group::GetDevicesList() const { return devices_; }
  40. bool Group::IsInThisGroup(int64_t device_rank) {
  41. for (auto &device : devices_) {
  42. if (device.rank() == device_rank) {
  43. return true;
  44. }
  45. }
  46. return false;
  47. }
  48. // Get the position of the device in the group
  49. Status Group::GetIndex(size_t *index) {
  50. size_t pos = 0;
  51. CheckGlobalDeviceManager();
  52. int64_t rank = g_device_manager->global_rank();
  53. for (auto &device : devices_) {
  54. if (device.rank() == rank) {
  55. *index = pos;
  56. return Status::SUCCESS;
  57. } else {
  58. pos++;
  59. }
  60. }
  61. MS_LOG(ERROR) << "Could not find device rank " << rank << "in this group!";
  62. return Status::FAILED;
  63. }
  64. GroupManager::GroupManager() { groups_.clear(); }
  65. #if !defined(NO_DLIB) || defined(ENABLE_GPU)
  66. bool GroupManager::CreateGroupByExecutor(const std::string &device_name, const std::string &group_name,
  67. const std::vector<uint32_t> ranks, int device_id) {
  68. auto executor = session::ExecutorManager::Instance().GetExecutor(device_name, device_id);
  69. MS_EXCEPTION_IF_NULL(executor);
  70. bool ret = executor->CreateCommGroup(group_name, ranks);
  71. return ret;
  72. }
  73. bool GroupManager::DestroyGroupByExecutor(const std::string &device_name, const std::string &group_name,
  74. int device_id) {
  75. auto executor = session::ExecutorManager::Instance().GetExecutor(device_name, device_id);
  76. MS_EXCEPTION_IF_NULL(executor);
  77. bool ret = executor->DestroyCommGroup(group_name);
  78. return ret;
  79. }
  80. Status CreateGroups(const std::vector<std::pair<std::string, std::vector<uint32_t>>> &group_info) {
  81. // Create group through the executor
  82. auto context_ptr = MsContext::GetInstance();
  83. MS_EXCEPTION_IF_NULL(context_ptr);
  84. std::string device_name = context_ptr->get_param<std::string>(MS_CTX_DEVICE_TARGET);
  85. uint32_t device_id = context_ptr->get_param<uint32_t>(MS_CTX_DEVICE_ID);
  86. auto executor = session::ExecutorManager::Instance().GetExecutor(device_name, device_id);
  87. MS_EXCEPTION_IF_NULL(executor);
  88. for (auto &group : group_info) {
  89. bool ret = executor->CreateCommGroup(group.first, group.second);
  90. if (!ret) {
  91. MS_LOG(ERROR) << "Create group failed, group name is " << group.first << ", ranks is " << group.second;
  92. return FAILED;
  93. }
  94. MS_LOG(INFO) << "Create group success, group name is " << group.first << ", ranks is " << group.second;
  95. }
  96. return SUCCESS;
  97. }
  98. #else
  99. bool GroupManager::CreateGroupByExecutor(const std::string &device_name, const std::string &group_name,
  100. const std::vector<uint32_t> ranks, int device_id) {
  101. MS_LOG(WARNING) << "Create group in stub";
  102. auto executor = parallel::ExecutorManager::Instance().GetExecutor(device_name, device_id);
  103. MS_EXCEPTION_IF_NULL(executor);
  104. return executor->CreateCommGroup(group_name, ranks);
  105. }
  106. bool GroupManager::DestroyGroupByExecutor(const std::string &device_name, const std::string &group_name,
  107. int device_id) {
  108. MS_LOG(WARNING) << "Destroy group in stub";
  109. auto executor = parallel::ExecutorManager::Instance().GetExecutor(device_name, device_id);
  110. MS_EXCEPTION_IF_NULL(executor);
  111. return executor->DestroyCommGroup(group_name);
  112. }
  113. Status CreateGroups(const std::vector<std::pair<std::string, std::vector<uint32_t>>> &group_info) {
  114. // Create group through the executor
  115. auto context_ptr = MsContext::GetInstance();
  116. MS_EXCEPTION_IF_NULL(context_ptr);
  117. std::string device_name = context_ptr->get_param<std::string>(MS_CTX_DEVICE_TARGET);
  118. uint32_t device_id = context_ptr->get_param<uint32_t>(MS_CTX_DEVICE_ID);
  119. auto executor = parallel::ExecutorManager::Instance().GetExecutor(device_name, device_id);
  120. MS_EXCEPTION_IF_NULL(executor);
  121. for (auto &group : group_info) {
  122. bool ret = executor->CreateCommGroup(group.first, group.second);
  123. if (!ret) {
  124. MS_LOG(ERROR) << "Create group failed, group name is " << group.first << ", ranks is " << group.second;
  125. return FAILED;
  126. }
  127. MS_LOG(INFO) << "Create group success, group name is " << group.first << ", ranks is " << group.second;
  128. }
  129. return SUCCESS;
  130. }
  131. #endif
  132. Status GroupManager::CreateGroup(const std::string &group_name, const std::vector<Device> &devices,
  133. mindspore::parallel::Group *const group) {
  134. // it is simple to use size to determine whether it is a world group
  135. uint32_t world_size = 0;
  136. (void)CommManager::GetInstance().GetRankSize(world_group_, &world_size);
  137. if (devices.size() == world_size) {
  138. auto it = groups_.find(world_group_);
  139. if (it == groups_.end()) {
  140. (void)group->Init(world_group_, devices);
  141. groups_[world_group_] = *group;
  142. } else {
  143. *group = it->second;
  144. }
  145. MS_LOG(INFO) << "It is world group " << world_group_ << ", no need to create it.";
  146. return Status::SUCCESS;
  147. }
  148. auto it = groups_.find(group_name);
  149. // If there already exits a group with the desired 'name',
  150. // let the pointer point to the group.
  151. if (it != groups_.end()) {
  152. *group = it->second;
  153. return Status::SUCCESS;
  154. } else {
  155. (void)group->Init(group_name, devices);
  156. groups_[group_name] = *group;
  157. vector<uint32_t> ranks;
  158. (void)std::transform(std::begin(devices), std::end(devices), std::back_inserter(ranks),
  159. [](const Device dev) { return (uint32_t)dev.rank(); });
  160. // Create group through the executor
  161. auto context_ptr = MsContext::GetInstance();
  162. MS_EXCEPTION_IF_NULL(context_ptr);
  163. std::string device_name = context_ptr->get_param<std::string>(MS_CTX_DEVICE_TARGET);
  164. uint32_t device_id = context_ptr->get_param<uint32_t>(MS_CTX_DEVICE_ID);
  165. bool ret = CreateGroupByExecutor(device_name, group_name, ranks, device_id);
  166. if (!ret) {
  167. MS_LOG(ERROR) << "Create group failed, group name is " << group_name;
  168. return Status::FAILED;
  169. }
  170. std::pair<std::string, std::vector<uint32_t>> group_info = std::make_pair(group_name, ranks);
  171. group_info_.push_back(group_info);
  172. MS_LOG(INFO) << "Create group success, group name is " << group_name;
  173. return Status::SUCCESS;
  174. }
  175. }
  176. Status GroupManager::DestroyGroup(const std::string &group_name) {
  177. auto context_ptr = MsContext::GetInstance();
  178. MS_EXCEPTION_IF_NULL(context_ptr);
  179. std::string device_name = context_ptr->get_param<std::string>(MS_CTX_DEVICE_TARGET);
  180. uint32_t device_id = context_ptr->get_param<uint32_t>(MS_CTX_DEVICE_ID);
  181. bool ret = DestroyGroupByExecutor(device_name, group_name, device_id);
  182. if (!ret) {
  183. return Status::FAILED;
  184. }
  185. return Status::SUCCESS;
  186. }
  187. Status GroupManager::DestroyGroup(mindspore::parallel::Group *const group) {
  188. std::string name = (*group).name();
  189. auto it = groups_.find(name);
  190. if (it == groups_.end()) {
  191. MS_LOG(ERROR) << "Could not find group name :" << name;
  192. return Status::FAILED;
  193. }
  194. (void)groups_.erase(it);
  195. return DestroyGroup(name);
  196. }
  197. Status GroupManager::DestroyAllGroups() {
  198. for (auto &it : groups_) {
  199. std::string name = it.first;
  200. auto ret = DestroyGroup(name);
  201. if (ret != Status::SUCCESS) {
  202. return Status::FAILED;
  203. }
  204. }
  205. groups_.clear();
  206. return Status::SUCCESS;
  207. }
  208. Status GroupManager::GetRankID(const std::string &name, uint32_t *const rank_id) {
  209. auto it = groups_.find(name);
  210. if (it == groups_.end()) {
  211. MS_LOG(ERROR) << "Could not find group name :" << name;
  212. return Status::FAILED;
  213. }
  214. bool ret = CommManager::GetInstance().GetRankID(name, rank_id);
  215. if (!ret) {
  216. return Status::FAILED;
  217. }
  218. return Status::SUCCESS;
  219. }
  220. Status GroupManager::GetRankSize(const std::string &name, uint32_t *const rank_size) {
  221. auto it = groups_.find(name);
  222. if (it == groups_.end()) {
  223. MS_LOG(ERROR) << "Could not find group name :" << name;
  224. return Status::FAILED;
  225. }
  226. bool ret = CommManager::GetInstance().GetRankSize(name, rank_size);
  227. if (!ret) {
  228. return Status::FAILED;
  229. }
  230. return Status::SUCCESS;
  231. }
  232. Status GroupManager::FindGroup(const std::string &name, mindspore::parallel::Group **group) {
  233. auto it = groups_.find(name);
  234. if (it == groups_.end()) {
  235. return Status::FAILED;
  236. }
  237. *group = &it->second;
  238. return Status::SUCCESS;
  239. }
  240. void GroupManager::Clear() { (void)DestroyAllGroups(); }
  241. } // namespace parallel
  242. } // namespace mindspore