2017-09-28 23:00:15 +00:00
|
|
|
/**
|
|
|
|
|
* Copyright (c) 2016-present, Facebook, Inc.
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2016-10-07 20:08:53 +00:00
|
|
|
#include <pybind11/pybind11.h>
|
|
|
|
|
#include <pybind11/stl.h>
|
|
|
|
|
|
2017-01-04 20:06:20 +00:00
|
|
|
#include "caffe2/mpi/mpi_common.h"
|
2016-10-07 20:08:53 +00:00
|
|
|
|
|
|
|
|
namespace caffe2 {
|
|
|
|
|
|
|
|
|
|
namespace py = pybind11;
|
|
|
|
|
|
2017-10-22 20:01:37 +00:00
|
|
|
PYBIND11_MODULE(mpi_utils, m) {
|
|
|
|
|
m.doc() = "MPI helper functions";
|
2016-10-07 20:08:53 +00:00
|
|
|
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();
|
|
|
|
|
});
|
2016-11-14 22:58:04 +00:00
|
|
|
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 = caffe2::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);
|
|
|
|
|
});
|
2016-10-07 20:08:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace caffe2
|