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.

mpi_initializer.cc 1.9 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 "device/gpu/mpi/mpi_initializer.h"
  17. #include <mpi.h>
  18. #include <pybind11/operators.h>
  19. #include <iostream>
  20. namespace mindspore {
  21. namespace device {
  22. namespace gpu {
  23. MPIInitializer::MPIInitializer() {
  24. int init_flag = 0;
  25. if (MPI_Initialized(&init_flag) != MPI_SUCCESS) {
  26. return;
  27. }
  28. if (init_flag == 0) {
  29. auto ret = MPI_Init(nullptr, nullptr);
  30. if (ret != MPI_SUCCESS) {
  31. return;
  32. }
  33. }
  34. MPI_Comm_rank(MPI_COMM_WORLD, &rank_id_);
  35. MPI_Comm_size(MPI_COMM_WORLD, &rank_size_);
  36. }
  37. MPIInitializer::~MPIInitializer() {
  38. int finalized_flag = 0;
  39. (void)MPI_Finalized(&finalized_flag);
  40. if (finalized_flag == 0) {
  41. (void)MPI_Finalize();
  42. }
  43. }
  44. MPIInitializer &MPIInitializer::GetInstance() {
  45. static MPIInitializer instance;
  46. return instance;
  47. }
  48. int MPIInitializer::get_rank_id() { return MPIInitializer::GetInstance().rank_id_; }
  49. int MPIInitializer::get_rank_size() { return MPIInitializer::GetInstance().rank_size_; }
  50. PYBIND11_MODULE(_ms_mpi, mpi_initializer) {
  51. mpi_initializer.doc() = "mindspore mpi python wrapper";
  52. mpi_initializer.def("get_rank_id", &MPIInitializer::get_rank_id, "get rank id");
  53. mpi_initializer.def("get_rank_size", &MPIInitializer::get_rank_size, "get rank size");
  54. }
  55. } // namespace gpu
  56. } // namespace device
  57. } // namespace mindspore