Files
pytorch/caffe2/python/mpi_python.cc
Sebastian Messmer 643ca5def2 Replace c10::guts::stuff with std::stuff (#30915)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/30915

Since we now have C++14, we don't need these c10::guts helpers anymore
ghstack-source-id: 95777609

Test Plan: waitforsandcastle

Differential Revision: D18869639

fbshipit-source-id: 97716f932297c64c6e814410ac47b444c33d4e2e
2019-12-16 13:57:19 -08:00

48 lines
1.2 KiB
C++

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include "caffe2/mpi/mpi_common.h"
namespace caffe2 {
namespace py = pybind11;
PYBIND11_MODULE(mpi_utils, m) {
m.doc() = "MPI helper functions";
m.def(
"SetupPeers",
&MPISetupPeers,
py::arg("replicas"),
py::arg("role"),
py::arg("job_path"));
m.def("CommSize", [] {
auto comm = GlobalMPIComm();
return MPICommSize(comm);
});
m.def("CommRank", [] {
auto comm = GlobalMPIComm();
return MPICommRank(comm);
});
m.def("Finalize", [] {
// NOTE(pietern): Doesn't seem to work when calling it
// from Python. It ends up calling pthread_join on a
// thread that doesn't exit. For now, running mpirun
// with `-quiet` and skipping the finalize call.
MPI_Finalize();
});
m.def("Broadcast", [](py::bytes in) -> py::bytes {
std::string str = in;
auto comm = GlobalMPIComm();
auto length = str.length();
MPI_Bcast(&length, sizeof(length), MPI_CHAR, 0, comm);
auto ptr = std::make_unique<char[]>(length);
if (MPICommRank(comm) == 0) {
memcpy(ptr.get(), str.data(), str.length());
}
MPI_Bcast(ptr.get(), length, MPI_CHAR, 0, comm);
return std::string(ptr.get(), length);
});
}
} // namespace caffe2