#include <c10/util/error.h>
#include <c10/util/thread_name.h>
#include <torch/csrc/Exceptions.h>
#include <torch/csrc/multiprocessing/init.h>
#include <torch/csrc/python_headers.h>
#include <torch/csrc/utils/object_ptr.h>
#include <torch/csrc/utils/pybind.h>
#include <torch/csrc/utils/python_strings.h>
#include <initializer_list>
#if defined(__linux__)
#include <sys/prctl.h>
#endif
#define SYSASSERT(rv, ...) \
TORCH_CHECK((rv) >= 0, ##__VA_ARGS__, ": ", c10::utils::str_error(errno))
namespace torch::multiprocessing {
namespace {
PyObject* multiprocessing_init(PyObject* , PyObject* ) {
auto multiprocessing_module =
THPObjectPtr(PyImport_ImportModule("torch.multiprocessing"));
TORCH_CHECK_PYTHON(multiprocessing_module);
auto module = py::handle(multiprocessing_module).cast<py::module>();
module.def("_prctl_pr_set_pdeathsig", [](int signal) {
#if defined(__linux__)
auto rv = prctl(PR_SET_PDEATHSIG, signal);
SYSASSERT(rv, "prctl");
#endif
});
Py_RETURN_TRUE;
}
PyObject* set_thread_name(PyObject* , PyObject* arg) {
TORCH_CHECK(THPUtils_checkString(arg), "invalid argument to setDevice");
auto name = THPUtils_unpackString(arg);
c10::setThreadName(name);
Py_RETURN_TRUE;
}
PyObject* get_thread_name(PyObject* , PyObject* ) {
return THPUtils_packString(c10::getThreadName());
}
}
static std::initializer_list<PyMethodDef> methods = {
{
"_multiprocessing_init",
multiprocessing_init,
METH_NOARGS,
nullptr,
},
{
"_set_thread_name",
set_thread_name,
METH_O,
nullptr,
},
{
"_get_thread_name",
get_thread_name,
METH_NOARGS,
nullptr,
},
{nullptr, nullptr, 0, nullptr},
};
const PyMethodDef* python_functions() {
return std::data(methods);
}
}