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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. #ifndef NO_DLIB
  19. #include "hccl/hcom.h"
  20. #endif
  21. #if defined(ENABLE_GPU)
  22. #include "runtime/device/gpu/distribution/collective_init.h"
  23. using CollectiveInitializer = mindspore::device::gpu::CollectiveInitializer;
  24. using CreateCommGroupFunc = mindspore::device::gpu::CreateCommGroupFunc;
  25. using GetRankIDByGroupFunc = mindspore::device::gpu::GetRankIDByGroupFunc;
  26. using GetGroupSizeFunc = mindspore::device::gpu::GetGroupSizeFunc;
  27. using DestroyGroupFunc = mindspore::device::gpu::DestroyGroupFunc;
  28. #endif
  29. namespace mindspore {
  30. #ifndef NO_DLIB
  31. CommManager &CommManager::GetInstance() noexcept {
  32. static CommManager instance("hccl");
  33. return instance;
  34. }
  35. #define HCCL_RUN_CHECK(op_name, group, op) \
  36. do { \
  37. auto hccl_result = (op); \
  38. if (hccl_result != 0) { \
  39. MS_LOG(ERROR) << op_name << " failed: #" << group << "#"; \
  40. return false; \
  41. } \
  42. } while (0)
  43. #define HCCL_GROUP_CHECK_EMPTY(group) \
  44. do { \
  45. if (group.length() == 0) { \
  46. MS_LOG(ERROR) << "The length of group name should not be 0"; \
  47. return false; \
  48. } \
  49. } while (0)
  50. #define HCCL_GROUP_CHECK_IS_WORLD(group) \
  51. do { \
  52. if (group == "hccl_world_group") { \
  53. MS_LOG(ERROR) << "The group name should not be hccl_world_group"; \
  54. return false; \
  55. } \
  56. } while (0)
  57. bool CommManager::CreateGroupSync(const string &group, const vector<unsigned int> &rank_id_list) const {
  58. auto rank_size = rank_id_list.size();
  59. HCCL_GROUP_CHECK_EMPTY(group);
  60. HCCL_GROUP_CHECK_IS_WORLD(group);
  61. HCCL_RUN_CHECK(string("create communicate group"), group,
  62. HcomCreateGroup(group.c_str(), UlongToUint(rank_size), vector<unsigned int>(rank_id_list).data()));
  63. return true;
  64. }
  65. bool CommManager::GetRankID(const string &group, unsigned int *rank_id) const {
  66. HCCL_GROUP_CHECK_EMPTY(group);
  67. HCCL_RUN_CHECK(string("get rank_id"), group, HcomGetRankId(group.c_str(), rank_id));
  68. return true;
  69. }
  70. bool CommManager::GetRankSize(const string &group, unsigned int *rank_size) const {
  71. HCCL_GROUP_CHECK_EMPTY(group);
  72. HCCL_RUN_CHECK(string("get rank size"), group, HcomGetRankSize(group.c_str(), rank_size));
  73. return true;
  74. }
  75. bool CommManager::DestroyGroup(const string &group) const {
  76. HCCL_GROUP_CHECK_EMPTY(group);
  77. HCCL_GROUP_CHECK_IS_WORLD(group);
  78. HCCL_RUN_CHECK(string("destroy communicate group"), group, HcomDestroyGroup(group.c_str()));
  79. return true;
  80. }
  81. #elif defined(ENABLE_GPU)
  82. CommManager &CommManager::GetInstance() noexcept {
  83. static CommManager instance("nccl");
  84. return instance;
  85. }
  86. bool CommManager::CreateGroupSync(const string &group, const vector<unsigned int> &rank_id_list) const {
  87. const void *collective_handle_ = CollectiveInitializer::instance().collective_handle();
  88. if (!collective_handle_) {
  89. MS_LOG(EXCEPTION) << "GPU collective handle is not initialized.";
  90. }
  91. MS_LOG(INFO) << "Create communication group " << group << " by rank id list " << rank_id_list;
  92. auto create_comm_group_funcptr =
  93. reinterpret_cast<CreateCommGroupFunc>(dlsym(const_cast<void *>(collective_handle_), "CreateCommGroup"));
  94. MS_EXCEPTION_IF_NULL(create_comm_group_funcptr);
  95. bool ret = (*create_comm_group_funcptr)(group, rank_id_list);
  96. if (!ret) {
  97. MS_LOG(ERROR) << "Creating group " << group << "for rank id list" << rank_id_list << "failed.";
  98. return ret;
  99. }
  100. return ret;
  101. }
  102. bool CommManager::GetRankID(const string &group, unsigned int *rank_id) const {
  103. const void *collective_handle_ = CollectiveInitializer::instance().collective_handle();
  104. if (!collective_handle_) {
  105. MS_LOG(EXCEPTION) << "GPU collective handle is not initialized.";
  106. }
  107. auto get_rank_id_funcptr =
  108. reinterpret_cast<GetRankIDByGroupFunc>(dlsym(const_cast<void *>(collective_handle_), "GetRankIDByGroup"));
  109. MS_EXCEPTION_IF_NULL(get_rank_id_funcptr);
  110. int rank = (*get_rank_id_funcptr)(group);
  111. *rank_id = static_cast<unsigned int>(rank);
  112. MS_LOG(INFO) << "This process rank id is " << *rank_id << " in group " << group;
  113. return true;
  114. }
  115. bool CommManager::GetRankSize(const string &group, unsigned int *rank_size) const {
  116. const void *collective_handle_ = CollectiveInitializer::instance().collective_handle();
  117. if (!collective_handle_) {
  118. MS_LOG(EXCEPTION) << "GPU collective handle is not initialized.";
  119. }
  120. auto get_group_size_funcptr =
  121. reinterpret_cast<GetGroupSizeFunc>(dlsym(const_cast<void *>(collective_handle_), "GetGroupSize"));
  122. MS_EXCEPTION_IF_NULL(get_group_size_funcptr);
  123. int size = (*get_group_size_funcptr)(group);
  124. *rank_size = static_cast<unsigned int>(size);
  125. MS_LOG(INFO) << "Group " << group << " size is " << *rank_size;
  126. return true;
  127. }
  128. bool CommManager::DestroyGroup(const string &group) const {
  129. const void *collective_handle_ = CollectiveInitializer::instance().collective_handle();
  130. if (!collective_handle_) {
  131. MS_LOG(EXCEPTION) << "GPU collective handle is not initialized.";
  132. }
  133. auto destroy_group_funcptr =
  134. reinterpret_cast<DestroyGroupFunc>(dlsym(const_cast<void *>(collective_handle_), "DestroyGroup"));
  135. MS_EXCEPTION_IF_NULL(destroy_group_funcptr);
  136. bool ret = (*destroy_group_funcptr)(group);
  137. if (!ret) {
  138. MS_LOG(ERROR) << "Destroying group " << group << " failed.";
  139. return ret;
  140. }
  141. return ret;
  142. }
  143. #else
  144. CommManager &CommManager::GetInstance() noexcept {
  145. static CommManager instance("hccl");
  146. return instance;
  147. }
  148. bool CommManager::CreateGroupSync(const string &, const vector<unsigned int> &) const { return true; }
  149. bool CommManager::GetRankID(const string &group, unsigned int *rank_id) const { return true; }
  150. bool CommManager::GetRankSize(const string &group, unsigned int *rank_size) const {
  151. *rank_size = NO_COMM_DLIB_RANK_SIZE;
  152. return true;
  153. }
  154. bool CommManager::DestroyGroup(const string &group) const { return true; }
  155. #endif
  156. } // namespace mindspore