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

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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, uint32_t device_id) {
  68. // The group operation thread must be same with nccl init thread in the GPU device.
  69. if (MsContext::GetInstance()->get_param<bool>(MS_CTX_ENABLE_MINDRT) ||
  70. (MsContext::GetInstance()->get_param<std::string>(MS_CTX_DEVICE_TARGET) == kGPUDevice)) {
  71. return CommManager::GetInstance().CreateGroupSync(group_name, ranks);
  72. } else {
  73. auto executor = session::ExecutorManager::Instance().GetExecutor(device_name, device_id);
  74. MS_EXCEPTION_IF_NULL(executor);
  75. return executor->CreateCommGroup(group_name, ranks);
  76. }
  77. }
  78. bool GroupManager::DestroyGroupByExecutor(const std::string &device_name, const std::string &group_name,
  79. uint32_t device_id) {
  80. // The group operation thread must be same with nccl init thread in the GPU device.
  81. if (MsContext::GetInstance()->get_param<bool>(MS_CTX_ENABLE_MINDRT) ||
  82. (MsContext::GetInstance()->get_param<std::string>(MS_CTX_DEVICE_TARGET) == kGPUDevice)) {
  83. return CommManager::GetInstance().DestroyGroup(group_name);
  84. } else {
  85. auto executor = session::ExecutorManager::Instance().GetExecutor(device_name, device_id);
  86. MS_EXCEPTION_IF_NULL(executor);
  87. return executor->DestroyCommGroup(group_name);
  88. }
  89. }
  90. Status CreateGroups(const std::vector<std::pair<std::string, std::vector<uint32_t>>> &group_info) {
  91. // Create group through the executor
  92. auto context_ptr = MsContext::GetInstance();
  93. MS_EXCEPTION_IF_NULL(context_ptr);
  94. std::string device_name = context_ptr->get_param<std::string>(MS_CTX_DEVICE_TARGET);
  95. uint32_t device_id = context_ptr->get_param<uint32_t>(MS_CTX_DEVICE_ID);
  96. auto executor = session::ExecutorManager::Instance().GetExecutor(device_name, device_id);
  97. MS_EXCEPTION_IF_NULL(executor);
  98. for (auto &group : group_info) {
  99. bool ret = true;
  100. // The group operation thread must be same with nccl init thread in the GPU device.
  101. if (context_ptr->get_param<bool>(MS_CTX_ENABLE_MINDRT) ||
  102. (context_ptr->get_param<std::string>(MS_CTX_DEVICE_TARGET) == kGPUDevice)) {
  103. ret = CommManager::GetInstance().CreateGroupSync(group.first, group.second);
  104. } else {
  105. ret = executor->CreateCommGroup(group.first, group.second);
  106. }
  107. if (!ret) {
  108. MS_LOG(ERROR) << "Create group failed, group name is " << group.first << ", ranks is " << group.second;
  109. return FAILED;
  110. }
  111. MS_LOG(INFO) << "Create group success, group name is " << group.first << ", ranks is " << group.second;
  112. }
  113. return SUCCESS;
  114. }
  115. #else
  116. bool GroupManager::CreateGroupByExecutor(const std::string &device_name, const std::string &group_name,
  117. const std::vector<uint32_t> ranks, uint32_t device_id) {
  118. MS_LOG(WARNING) << "Create group in stub";
  119. auto executor = parallel::ExecutorManager::Instance().GetExecutor(device_name, device_id);
  120. MS_EXCEPTION_IF_NULL(executor);
  121. return executor->CreateCommGroup(group_name, ranks);
  122. }
  123. bool GroupManager::DestroyGroupByExecutor(const std::string &device_name, const std::string &group_name,
  124. uint32_t device_id) {
  125. MS_LOG(WARNING) << "Destroy group in stub";
  126. auto executor = parallel::ExecutorManager::Instance().GetExecutor(device_name, device_id);
  127. MS_EXCEPTION_IF_NULL(executor);
  128. return executor->DestroyCommGroup(group_name);
  129. }
  130. Status CreateGroups(const std::vector<std::pair<std::string, std::vector<uint32_t>>> &group_info) {
  131. // Create group through the executor
  132. auto context_ptr = MsContext::GetInstance();
  133. MS_EXCEPTION_IF_NULL(context_ptr);
  134. std::string device_name = context_ptr->get_param<std::string>(MS_CTX_DEVICE_TARGET);
  135. uint32_t device_id = context_ptr->get_param<uint32_t>(MS_CTX_DEVICE_ID);
  136. auto executor = parallel::ExecutorManager::Instance().GetExecutor(device_name, device_id);
  137. MS_EXCEPTION_IF_NULL(executor);
  138. for (auto &group : group_info) {
  139. bool ret = executor->CreateCommGroup(group.first, group.second);
  140. if (!ret) {
  141. MS_LOG(ERROR) << "Create group failed, group name is " << group.first << ", ranks is " << group.second;
  142. return FAILED;
  143. }
  144. MS_LOG(INFO) << "Create group success, group name is " << group.first << ", ranks is " << group.second;
  145. }
  146. return SUCCESS;
  147. }
  148. #endif
  149. Status GroupManager::CreateGroup(const std::string &group_name, const std::vector<Device> &devices,
  150. mindspore::parallel::Group *const group) {
  151. // it is simple to use size to determine whether it is a world group
  152. uint32_t world_size = 0;
  153. (void)CommManager::GetInstance().GetRankSize(world_group_, &world_size);
  154. if (devices.size() == world_size) {
  155. auto iter = groups_.find(world_group_);
  156. if (iter == groups_.end()) {
  157. (void)group->Init(world_group_, devices);
  158. groups_[world_group_] = *group;
  159. } else {
  160. *group = iter->second;
  161. }
  162. MS_LOG(INFO) << "It is world group " << world_group_ << ", no need to create it.";
  163. return Status::SUCCESS;
  164. }
  165. auto it = groups_.find(group_name);
  166. // If there already exits a group with the desired 'name',
  167. // let the pointer point to the group.
  168. if (it != groups_.end()) {
  169. *group = it->second;
  170. return Status::SUCCESS;
  171. } else {
  172. (void)group->Init(group_name, devices);
  173. groups_[group_name] = *group;
  174. vector<uint32_t> ranks;
  175. (void)std::transform(std::begin(devices), std::end(devices), std::back_inserter(ranks),
  176. [](const Device dev) { return (uint32_t)dev.rank(); });
  177. // Create group through the executor
  178. auto context_ptr = MsContext::GetInstance();
  179. MS_EXCEPTION_IF_NULL(context_ptr);
  180. std::string device_name = context_ptr->get_param<std::string>(MS_CTX_DEVICE_TARGET);
  181. uint32_t device_id = context_ptr->get_param<uint32_t>(MS_CTX_DEVICE_ID);
  182. std::pair<std::string, std::vector<uint32_t>> group_info = std::make_pair(group_name, ranks);
  183. group_info_.push_back(group_info);
  184. bool ret = CreateGroupByExecutor(device_name, group_name, ranks, device_id);
  185. if (!ret) {
  186. MS_LOG(WARNING) << "Create group failed, group name is " << group_name;
  187. return Status::FAILED;
  188. }
  189. MS_LOG(INFO) << "Create group success, group name is " << group_name;
  190. return Status::SUCCESS;
  191. }
  192. }
  193. Status GroupManager::DestroyGroup(const std::string &group_name) {
  194. auto context_ptr = MsContext::GetInstance();
  195. MS_EXCEPTION_IF_NULL(context_ptr);
  196. std::string device_name = context_ptr->get_param<std::string>(MS_CTX_DEVICE_TARGET);
  197. uint32_t device_id = context_ptr->get_param<uint32_t>(MS_CTX_DEVICE_ID);
  198. bool ret = DestroyGroupByExecutor(device_name, group_name, device_id);
  199. if (!ret) {
  200. return Status::FAILED;
  201. }
  202. return Status::SUCCESS;
  203. }
  204. Status GroupManager::DestroyGroup(mindspore::parallel::Group *const group) {
  205. std::string name = (*group).name();
  206. auto it = groups_.find(name);
  207. if (it == groups_.end()) {
  208. MS_LOG(ERROR) << "Could not find group name :" << name;
  209. return Status::FAILED;
  210. }
  211. (void)groups_.erase(it);
  212. return DestroyGroup(name);
  213. }
  214. Status GroupManager::DestroyAllGroups() {
  215. for (auto &it : groups_) {
  216. std::string name = it.first;
  217. auto ret = DestroyGroup(name);
  218. if (ret != Status::SUCCESS) {
  219. return Status::FAILED;
  220. }
  221. }
  222. groups_.clear();
  223. return Status::SUCCESS;
  224. }
  225. Status GroupManager::GetRankID(const std::string &name, uint32_t *const rank_id) {
  226. auto it = groups_.find(name);
  227. if (it == groups_.end()) {
  228. MS_LOG(ERROR) << "Could not find group name :" << name;
  229. return Status::FAILED;
  230. }
  231. bool ret = CommManager::GetInstance().GetRankID(name, rank_id);
  232. if (!ret) {
  233. return Status::FAILED;
  234. }
  235. return Status::SUCCESS;
  236. }
  237. Status GroupManager::GetRankSize(const std::string &name, uint32_t *const rank_size) {
  238. auto it = groups_.find(name);
  239. if (it == groups_.end()) {
  240. MS_LOG(ERROR) << "Could not find group name :" << name;
  241. return Status::FAILED;
  242. }
  243. bool ret = CommManager::GetInstance().GetRankSize(name, rank_size);
  244. if (!ret) {
  245. return Status::FAILED;
  246. }
  247. return Status::SUCCESS;
  248. }
  249. Status GroupManager::FindGroup(const std::string &name, mindspore::parallel::Group **group) {
  250. auto it = groups_.find(name);
  251. if (it == groups_.end()) {
  252. return Status::FAILED;
  253. }
  254. *group = &it->second;
  255. return Status::SUCCESS;
  256. }
  257. void GroupManager::Clear() { (void)DestroyAllGroups(); }
  258. } // namespace parallel
  259. } // namespace mindspore