1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
|
#include <pybind11/pybind11.h>
#include <torch/csrc/cuda/Event.h>
#include <torch/csrc/cuda/Module.h>
#include <torch/csrc/cuda/Stream.h>
#include <torch/csrc/Device.h>
#include <torch/csrc/THP.h>
#include <torch/csrc/utils/python_arg_parser.h>
#include <c10/cuda/CUDAGuard.h>
#include <structmember.h>
#include <cuda_runtime_api.h>
PyObject *THCPEventClass = nullptr;
static PyObject * THCPEvent_pynew(
PyTypeObject *type, PyObject *args, PyObject *kwargs) {
HANDLE_TH_ERRORS
unsigned char enable_timing = 0;
unsigned char blocking = 0;
unsigned char interprocess = 0;
static char *kwlist[] =
{"enable_timing", "blocking", "interprocess", nullptr};
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|bbb", kwlist,
&enable_timing, &blocking, &interprocess)) {
return nullptr;
}
THPObjectPtr ptr(type->tp_alloc(type, 0));
if (!ptr) {
return nullptr;
}
THCPEvent* self = (THCPEvent *)ptr.get();
unsigned int flags =
(blocking ? cudaEventBlockingSync : cudaEventDefault) |
(enable_timing ? cudaEventDefault : cudaEventDisableTiming) |
(interprocess ? cudaEventInterprocess : cudaEventDefault);
new (&self->cuda_event) at::cuda::CUDAEvent(flags);
return (PyObject *)ptr.release();
END_HANDLE_TH_ERRORS
}
static PyObject * THCPEvent_from_ipc_handle(
PyTypeObject *type, PyObject *args, PyObject *kwargs) {
HANDLE_TH_ERRORS
static torch::PythonArgParser parser({
"from_ipc_handle(Device device, std::string ipc_handle)",
});
torch::ParsedArgs<2> parsed_args;
auto r = parser.parse(args, kwargs, parsed_args);
at::Device device = r.device(0);
std::string handle_string = r.string(1);
TORCH_CHECK(handle_string.size() == sizeof(cudaIpcEventHandle_t),
"cudaIpcEventHandle_t expects byte-like object of size ",
sizeof(cudaIpcEventHandle_t), ", but got ", handle_string.size());
TORCH_CHECK(device.type() == at::kCUDA, "Event can only be created on "
"CUDA devices, but got device type ", device.type())
THPObjectPtr ptr(type->tp_alloc(type, 0));
if (!ptr) {
return nullptr;
}
THCPEvent* self = (THCPEvent *)ptr.get();
cudaIpcEventHandle_t handle;
std::memcpy(&handle, handle_string.c_str(), handle_string.size());
new (&self->cuda_event) at::cuda::CUDAEvent(device.index(), &handle);
return (PyObject *)ptr.release();
END_HANDLE_TH_ERRORS
}
static void THCPEvent_dealloc(THCPEvent *self) {
self->cuda_event.~CUDAEvent();
Py_TYPE(self)->tp_free((PyObject*)self);
}
static PyObject * THCPEvent_get_cuda_event(THCPEvent *self, void *unused) {
HANDLE_TH_ERRORS
return PyLong_FromVoidPtr(self->cuda_event.event());
END_HANDLE_TH_ERRORS
}
static PyObject * THCPEvent_get_device(THCPEvent *self, void *unused) {
HANDLE_TH_ERRORS
at::optional<at::Device> device = self->cuda_event.device();
if (!device) {
Py_RETURN_NONE;
}
return THPDevice_New(device.value());
END_HANDLE_TH_ERRORS
}
static PyObject * THCPEvent_record(THCPEvent *self, THCPStream *stream) {
HANDLE_TH_ERRORS
self->cuda_event.record(stream->cuda_stream);
Py_RETURN_NONE;
END_HANDLE_TH_ERRORS
}
static PyObject * THCPEvent_wait(THCPEvent *self, THCPStream *stream) {
HANDLE_TH_ERRORS
{
pybind11::gil_scoped_release no_gil;
self->cuda_event.block(stream->cuda_stream);
}
Py_RETURN_NONE;
END_HANDLE_TH_ERRORS
}
static PyObject * THCPEvent_query(THCPEvent *self, PyObject *noargs) {
HANDLE_TH_ERRORS
return PyBool_FromLong(self->cuda_event.query());
END_HANDLE_TH_ERRORS
}
static PyObject * THCPEvent_elapsed_time(THCPEvent *self, THCPEvent *other) {
HANDLE_TH_ERRORS
return PyFloat_FromDouble(self->cuda_event.elapsed_time(other->cuda_event));
END_HANDLE_TH_ERRORS
}
static PyObject * THCPEvent_synchronize(THCPEvent *self, PyObject *noargs) {
HANDLE_TH_ERRORS
{
pybind11::gil_scoped_release no_gil;
self->cuda_event.synchronize();
}
Py_RETURN_NONE;
END_HANDLE_TH_ERRORS
}
static PyObject * THCPEvent_ipc_handle(THCPEvent *self, PyObject *noargs) {
HANDLE_TH_ERRORS
cudaIpcEventHandle_t handle;
self->cuda_event.ipc_handle(&handle);
return PyBytes_FromStringAndSize((const char *)&handle, sizeof(handle));
END_HANDLE_TH_ERRORS
}
static struct PyGetSetDef THCPEvent_properties[] = {
{"device", (getter)THCPEvent_get_device, nullptr, nullptr, nullptr},
{"cuda_event", (getter)THCPEvent_get_cuda_event, nullptr, nullptr, nullptr},
{nullptr}
};
static PyMethodDef THCPEvent_methods[] = {
{(char*)"from_ipc_handle", (PyCFunction)(void(*)(void))THCPEvent_from_ipc_handle,
METH_CLASS | METH_VARARGS | METH_KEYWORDS, nullptr},
{(char*)"record", (PyCFunction)THCPEvent_record, METH_O, nullptr},
{(char*)"wait", (PyCFunction)THCPEvent_wait, METH_O, nullptr},
{(char*)"query", (PyCFunction)THCPEvent_query, METH_NOARGS, nullptr},
{(char*)"elapsed_time", (PyCFunction)THCPEvent_elapsed_time, METH_O, nullptr},
{(char*)"synchronize", (PyCFunction)THCPEvent_synchronize,
METH_NOARGS, nullptr},
{(char*)"ipc_handle", (PyCFunction)THCPEvent_ipc_handle,
METH_NOARGS, nullptr},
{nullptr}
};
PyTypeObject THCPEventType = {
PyVarObject_HEAD_INIT(nullptr, 0)
"torch._C._CudaEventBase", /* tp_name */
sizeof(THCPEvent), /* tp_basicsize */
0, /* tp_itemsize */
(destructor)THCPEvent_dealloc, /* tp_dealloc */
0, /* tp_vectorcall_offset */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_reserved */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
nullptr, /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
THCPEvent_methods, /* tp_methods */
0, /* tp_members */
THCPEvent_properties, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_init */
0, /* tp_alloc */
THCPEvent_pynew, /* tp_new */
};
void THCPEvent_init(PyObject *module) {
THCPEventClass = (PyObject*)&THCPEventType;
if (PyType_Ready(&THCPEventType) < 0) {
throw python_error();
}
Py_INCREF(&THCPEventType);
if (PyModule_AddObject(
module, "_CudaEventBase", (PyObject *)&THCPEventType) < 0) {
throw python_error();
}
}
|