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.

comm_manager.cc 10 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 "utils/comm_manager.h"
  17. #include "utils/convert_utils.h"
  18. #include "utils/ms_context.h"
  19. #include "frontend/parallel/context.h"
  20. #include "frontend/parallel/group_manager.h"
  21. #ifndef NO_DLIB
  22. #include "runtime/hccl_adapter/hccl_adapter.h"
  23. #include "hccl/hcom.h"
  24. #include "runtime/device/ascend/distribute/ascend_collective.h"
  25. using HcclCollectiveGroup = mindspore::device::ascend::collective::HcclCollectiveGroup;
  26. #endif
  27. #if defined(ENABLE_GPU)
  28. #include "runtime/device/gpu/distribution/collective_init.h"
  29. using CollectiveInitializer = mindspore::device::gpu::CollectiveInitializer;
  30. using CreateCommGroupFunc = mindspore::device::gpu::CreateCommGroupFunc;
  31. using GetRankIDByGroupFunc = mindspore::device::gpu::GetRankIDByGroupFunc;
  32. using GetGroupSizeFunc = mindspore::device::gpu::GetGroupSizeFunc;
  33. using DestroyGroupFunc = mindspore::device::gpu::DestroyGroupFunc;
  34. #endif
  35. namespace mindspore {
  36. #ifndef NO_DLIB
  37. CommManager &CommManager::GetInstance() noexcept {
  38. static CommManager instance("hccl");
  39. return instance;
  40. }
  41. #define HCCL_RUN_CHECK(op_name, group, op) \
  42. do { \
  43. auto hccl_result = (op); \
  44. if (hccl_result != 0) { \
  45. MS_LOG(ERROR) << op_name << " failed: #" << group << "#"; \
  46. return false; \
  47. } \
  48. } while (0)
  49. #define HCCL_GROUP_CHECK_EMPTY(group) \
  50. do { \
  51. if (group.length() == 0) { \
  52. MS_LOG(ERROR) << "The length of group name should not be 0"; \
  53. return false; \
  54. } \
  55. } while (0)
  56. #define HCCL_GROUP_CHECK_IS_WORLD(group) \
  57. do { \
  58. if (group == "hccl_world_group") { \
  59. MS_LOG(ERROR) << "The group name should not be hccl_world_group"; \
  60. return false; \
  61. } \
  62. } while (0)
  63. bool CommManager::CreateGroupSync(const string &group, const vector<unsigned int> &rank_id_list) const {
  64. auto rank_size = rank_id_list.size();
  65. HCCL_GROUP_CHECK_EMPTY(group);
  66. HCCL_GROUP_CHECK_IS_WORLD(group);
  67. auto context_ptr = MsContext::GetInstance();
  68. MS_EXCEPTION_IF_NULL(context_ptr);
  69. bool is_task_sink = context_ptr->get_param<bool>(MS_CTX_ENABLE_TASK_SINK);
  70. auto mode = context_ptr->get_param<int>(MS_CTX_EXECUTION_MODE);
  71. if (!is_task_sink && mode == kGraphMode) {
  72. HcclCollectiveGroup::instance().CreateCommGroup(group, rank_id_list);
  73. } else {
  74. HCCL_RUN_CHECK(string("create communicate group"), group,
  75. hccl::HcclAdapter::GetInstance().HcclCreateGroup(group, UlongToUint(rank_size),
  76. vector<unsigned int>(rank_id_list).data()));
  77. }
  78. return true;
  79. }
  80. bool CommManager::GetRankID(const string &group, unsigned int *rank_id) const {
  81. HCCL_GROUP_CHECK_EMPTY(group);
  82. auto context = MsContext::GetInstance();
  83. MS_EXCEPTION_IF_NULL(context);
  84. if (context->get_param<int>(MS_CTX_EXECUTION_MODE) == kGraphMode) {
  85. if (!context->get_param<bool>(MS_CTX_ENABLE_TASK_SINK)) {
  86. *rank_id = static_cast<unsigned int>(HcclCollectiveGroup::instance().GetRankId(group));
  87. } else {
  88. HCCL_RUN_CHECK(string("get rank_id"), group, hccl::HcclAdapter::GetInstance().HcclGetRankId(group, rank_id));
  89. }
  90. } else {
  91. HCCL_RUN_CHECK(string("get rank_id"), group, hccl::HcclAdapter::GetInstance().HcclGetRankId(rank_id));
  92. }
  93. return true;
  94. }
  95. bool CommManager::GetRankSize(const string &group, unsigned int *rank_size) const {
  96. HCCL_GROUP_CHECK_EMPTY(group);
  97. auto context = MsContext::GetInstance();
  98. MS_EXCEPTION_IF_NULL(context);
  99. if (context->get_param<int>(MS_CTX_EXECUTION_MODE) == kGraphMode) {
  100. if (!context->get_param<bool>(MS_CTX_ENABLE_TASK_SINK)) {
  101. *rank_size = static_cast<unsigned int>(HcclCollectiveGroup::instance().GetRankSize(group));
  102. } else {
  103. HCCL_RUN_CHECK(string("get rank size"), group,
  104. hccl::HcclAdapter::GetInstance().HcclGetRankSize(group, rank_size));
  105. }
  106. } else {
  107. HCCL_RUN_CHECK(string("get rank size"), group, hccl::HcclAdapter::GetInstance().HcclGetRankSize(rank_size));
  108. }
  109. return true;
  110. }
  111. bool CommManager::DestroyGroup(const string &group) const {
  112. HCCL_GROUP_CHECK_EMPTY(group);
  113. HCCL_GROUP_CHECK_IS_WORLD(group);
  114. HCCL_RUN_CHECK(string("destroy communicate group"), group, hccl::HcclAdapter::GetInstance().HcclDestroyGroup(group));
  115. return true;
  116. }
  117. #elif defined(ENABLE_GPU)
  118. CommManager &CommManager::GetInstance() noexcept {
  119. static CommManager instance("nccl");
  120. return instance;
  121. }
  122. bool CommManager::CreateGroupSync(const string &group, const vector<unsigned int> &rank_id_list) const {
  123. const void *collective_handle_ = CollectiveInitializer::instance().collective_handle();
  124. if (!collective_handle_) {
  125. MS_LOG(EXCEPTION) << "GPU collective handle is not initialized.";
  126. }
  127. MS_LOG(INFO) << "Create communication group " << group << " by rank id list " << rank_id_list;
  128. auto create_comm_group_funcptr =
  129. reinterpret_cast<CreateCommGroupFunc>(dlsym(const_cast<void *>(collective_handle_), "CreateCommGroup"));
  130. MS_EXCEPTION_IF_NULL(create_comm_group_funcptr);
  131. bool ret = (*create_comm_group_funcptr)(group, rank_id_list);
  132. if (!ret) {
  133. MS_LOG(ERROR) << "Creating group " << group << "for rank id list" << rank_id_list << "failed.";
  134. return ret;
  135. }
  136. return ret;
  137. }
  138. bool CommManager::GetRankID(const string &group, unsigned int *rank_id) const {
  139. const void *collective_handle_ = CollectiveInitializer::instance().collective_handle();
  140. if (!collective_handle_) {
  141. MS_LOG(EXCEPTION) << "GPU collective handle is not initialized.";
  142. }
  143. auto get_rank_id_funcptr =
  144. reinterpret_cast<GetRankIDByGroupFunc>(dlsym(const_cast<void *>(collective_handle_), "GetRankIDByGroup"));
  145. MS_EXCEPTION_IF_NULL(get_rank_id_funcptr);
  146. int rank = (*get_rank_id_funcptr)(group);
  147. *rank_id = static_cast<unsigned int>(rank);
  148. MS_LOG(INFO) << "This process rank id is " << *rank_id << " in group " << group;
  149. return true;
  150. }
  151. bool CommManager::GetRankSize(const string &group, unsigned int *rank_size) const {
  152. const void *collective_handle_ = CollectiveInitializer::instance().collective_handle();
  153. if (!collective_handle_) {
  154. MS_LOG(EXCEPTION) << "GPU collective handle is not initialized.";
  155. }
  156. auto get_group_size_funcptr =
  157. reinterpret_cast<GetGroupSizeFunc>(dlsym(const_cast<void *>(collective_handle_), "GetGroupSize"));
  158. MS_EXCEPTION_IF_NULL(get_group_size_funcptr);
  159. int size = (*get_group_size_funcptr)(group);
  160. *rank_size = static_cast<unsigned int>(size);
  161. MS_LOG(INFO) << "Group " << group << " size is " << *rank_size;
  162. return true;
  163. }
  164. bool CommManager::DestroyGroup(const string &group) const {
  165. const void *collective_handle_ = CollectiveInitializer::instance().collective_handle();
  166. if (!collective_handle_) {
  167. MS_LOG(EXCEPTION) << "GPU collective handle is not initialized.";
  168. }
  169. auto destroy_group_funcptr =
  170. reinterpret_cast<DestroyGroupFunc>(dlsym(const_cast<void *>(collective_handle_), "DestroyGroup"));
  171. MS_EXCEPTION_IF_NULL(destroy_group_funcptr);
  172. bool ret = (*destroy_group_funcptr)(group);
  173. if (!ret) {
  174. MS_LOG(ERROR) << "Destroying group " << group << " failed.";
  175. return ret;
  176. }
  177. return ret;
  178. }
  179. #else
  180. CommManager &CommManager::GetInstance() noexcept {
  181. static CommManager instance("hccl");
  182. return instance;
  183. }
  184. bool CommManager::CreateGroupSync(const string &, const vector<unsigned int> &) const { return true; }
  185. bool CommManager::GetRankID(const string &group, unsigned int *rank_id) const { return true; }
  186. bool CommManager::GetRankSize(const string &group, unsigned int *rank_size) const {
  187. *rank_size = NO_COMM_DLIB_RANK_SIZE;
  188. return true;
  189. }
  190. bool CommManager::DestroyGroup(const string &group) const { return true; }
  191. #endif
  192. uint32_t GetRank() {
  193. uint32_t rank_id = 0;
  194. auto ms_context = MsContext::GetInstance();
  195. MS_EXCEPTION_IF_NULL(ms_context);
  196. std::string world_group;
  197. std::string backend = ms_context->get_param<std::string>(MS_CTX_DEVICE_TARGET);
  198. if (backend == kAscendDevice) {
  199. world_group = parallel::HCCL_WORLD_GROUP;
  200. } else if (backend == kGPUDevice) {
  201. world_group = parallel::NCCL_WORLD_GROUP;
  202. } else {
  203. // Other backends like CPU not support parallel, return rank_id with default 0.
  204. return rank_id;
  205. }
  206. auto parallel_context = parallel::ParallelContext::GetInstance();
  207. MS_EXCEPTION_IF_NULL(parallel_context);
  208. if (parallel_context->parallel_mode() != parallel::STAND_ALONE) {
  209. if (!CommManager::GetInstance().GetRankID(world_group, &rank_id)) {
  210. MS_LOG(EXCEPTION) << "Get rank id failed.";
  211. }
  212. }
  213. return rank_id;
  214. }
  215. bool IsStandAlone() {
  216. auto parallel_context = parallel::ParallelContext::GetInstance();
  217. MS_EXCEPTION_IF_NULL(parallel_context);
  218. return parallel_context->parallel_mode() == parallel::STAND_ALONE;
  219. }
  220. } // namespace mindspore