pytorch/torch/csrc/cuda/Stream.cpp
Mike Guo 6ecc1a4c4f Make pytorch clang-tidy clean (#60649)
Summary:
This PR suppresses clang-tidy warnings in the codebase (for now) so that we can re-enable clang-tidy checks on master.

I ran this script to add the `NOLINTNEXTLINE` comments (on a devserver):
```bash
python3 setup.py develop

# Uses same script that's run on CI and adds the -j (parallel), -s (add comments), -k (continue if diagnostic errors are found) options
python3 tools/clang_tidy.py \
  -j \
  -s \
  -k \
  -v \
  --paths torch/csrc/ \
  -g"-torch/csrc/jit/passes/onnx/helper.cpp" \
  -g"-torch/csrc/jit/passes/onnx/shape_type_inference.cpp" \
  -g"-torch/csrc/jit/serialization/onnx.cpp" \
  -g"-torch/csrc/jit/serialization/export.cpp" \
  -g"-torch/csrc/jit/serialization/import.cpp" \
  -g"-torch/csrc/jit/serialization/import_legacy.cpp" \
  -g"-torch/csrc/onnx/init.cpp" \
  -g"-torch/csrc/cuda/nccl.*" \
  -g"-torch/csrc/cuda/python_nccl.cpp" \
  -g"-torch/csrc/autograd/FunctionsManual.cpp" \
  -g"-torch/csrc/generic/*.cpp" \
  -g"-torch/csrc/jit/codegen/cuda/runtime/*" \
  -g"-torch/csrc/deploy/interpreter/interpreter.cpp" \
  -g"-torch/csrc/deploy/interpreter/interpreter.h" \
  -g"-torch/csrc/deploy/interpreter/interpreter_impl.h" \
  -g"-torch/csrc/deploy/interpreter/test_main.cpp"
```

Pull Request resolved: https://github.com/pytorch/pytorch/pull/60649

Test Plan: Verified changes by re-running the script (without the `-s` option) and seeing no warnings/errors.

Reviewed By: walterddr, janeyx99

Differential Revision: D29504258

Pulled By: 1ntEgr8

fbshipit-source-id: 78310b30ee8213b73ddb4771ad874665323e7a4e
2021-07-01 12:21:07 -07:00

225 lines
8.1 KiB
C++

#include <pybind11/pybind11.h>
#include <torch/csrc/cuda/Stream.h>
#include <torch/csrc/cuda/Module.h>
#include <torch/csrc/utils/python_numbers.h>
#include <torch/csrc/Device.h>
#include <torch/csrc/THP.h>
#include <c10/cuda/CUDAGuard.h>
#include <structmember.h>
#include <cuda_runtime_api.h>
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
PyObject *THCPStreamClass = nullptr;
static PyObject * THCPStream_pynew(
PyTypeObject *type, PyObject *args, PyObject *kwargs) {
HANDLE_TH_ERRORS
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
int current_device;
THCudaCheck(cudaGetDevice(&current_device));
int priority = 0;
uint64_t cdata = 0;
uint64_t stream_ptr = 0;
// NOLINTNEXTLINE(modernize-avoid-c-arrays,cppcoreguidelines-avoid-c-arrays)
static char *kwlist[] = {"priority", "_cdata", "stream_ptr", nullptr};
if (!PyArg_ParseTupleAndKeywords(
args, kwargs, "|iKK", kwlist, &priority, &cdata, &stream_ptr)) {
return nullptr;
}
THPObjectPtr ptr(type->tp_alloc(type, 0));
if (!ptr) {
return nullptr;
}
if (stream_ptr) {
TORCH_CHECK(priority == 0, "Priority was explicitly set for a external stream")
}
at::cuda::CUDAStream stream =
cdata ?
at::cuda::CUDAStream::unpack(cdata) :
stream_ptr ?
at::cuda::getStreamFromExternal(reinterpret_cast<cudaStream_t>(stream_ptr), current_device) :
at::cuda::getStreamFromPool(
/* isHighPriority */ priority < 0 ? true : false);
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
THCPStream* self = (THCPStream *)ptr.get();
self->cdata = stream.pack();
new (&self->cuda_stream) at::cuda::CUDAStream(stream);
return (PyObject *)ptr.release();
END_HANDLE_TH_ERRORS
}
static void THCPStream_dealloc(THCPStream *self) {
self->cuda_stream.~CUDAStream();
Py_TYPE(self)->tp_free((PyObject*)self);
}
static PyObject * THCPStream_get_device(THCPStream *self, void *unused) {
HANDLE_TH_ERRORS
return THPDevice_New(self->cuda_stream.device());
END_HANDLE_TH_ERRORS
}
static PyObject * THCPStream_get_cuda_stream(THCPStream *self, void *unused) {
HANDLE_TH_ERRORS
return PyLong_FromVoidPtr(self->cuda_stream.stream());
END_HANDLE_TH_ERRORS
}
static PyObject * THCPStream_get_priority(THCPStream *self, void *unused) {
HANDLE_TH_ERRORS
return THPUtils_packInt64(self->cuda_stream.priority());
END_HANDLE_TH_ERRORS
}
static PyObject * THCPStream_priority_range(PyObject *_unused, PyObject* noargs) {
HANDLE_TH_ERRORS
// NOLINTNEXTLINE(cppcoreguidelines-init-variables)
int least_priority, greatest_priority;
std::tie(least_priority, greatest_priority) =
at::cuda::CUDAStream::priority_range();
return Py_BuildValue("(ii)", least_priority, greatest_priority);
END_HANDLE_TH_ERRORS
}
static PyObject * THCPStream_query(PyObject *_self, PyObject *noargs) {
HANDLE_TH_ERRORS
auto self = (THCPStream*)_self;
return PyBool_FromLong(self->cuda_stream.query());
END_HANDLE_TH_ERRORS
}
static PyObject * THCPStream_synchronize(PyObject *_self, PyObject *noargs) {
HANDLE_TH_ERRORS
{
pybind11::gil_scoped_release no_gil;
auto self = (THCPStream*)_self;
self->cuda_stream.synchronize();
}
Py_RETURN_NONE;
END_HANDLE_TH_ERRORS
}
static PyObject * THCPStream_eq(PyObject *_self, PyObject *_other) {
HANDLE_TH_ERRORS
auto self = (THCPStream*)_self;
auto other = (THCPStream*)_other;
return PyBool_FromLong(self->cuda_stream == other->cuda_stream);
END_HANDLE_TH_ERRORS
}
// NOLINTNEXTLINE(modernize-avoid-c-arrays, cppcoreguidelines-avoid-non-const-global-variables, cppcoreguidelines-avoid-c-arrays)
static struct PyMemberDef THCPStream_members[] = {
{nullptr}
};
// NOLINTNEXTLINE(modernize-avoid-c-arrays, cppcoreguidelines-avoid-non-const-global-variables, cppcoreguidelines-avoid-c-arrays)
static struct PyGetSetDef THCPStream_properties[] = {
{"cuda_stream",
(getter)THCPStream_get_cuda_stream, nullptr, nullptr, nullptr},
{"priority", (getter)THCPStream_get_priority, nullptr, nullptr, nullptr},
{nullptr}
};
// NOLINTNEXTLINE(modernize-avoid-c-arrays, cppcoreguidelines-avoid-non-const-global-variables, cppcoreguidelines-avoid-c-arrays)
static PyMethodDef THCPStream_methods[] = {
{(char*)"query", THCPStream_query, METH_NOARGS, nullptr},
{(char*)"synchronize",
THCPStream_synchronize, METH_NOARGS, nullptr},
{(char*)"priority_range",
THCPStream_priority_range, METH_STATIC | METH_NOARGS, nullptr},
{(char*)"__eq__", THCPStream_eq, METH_O, nullptr},
{nullptr}
};
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
PyTypeObject THCPStreamType = {
PyVarObject_HEAD_INIT(nullptr, 0)
"torch._C._CudaStreamBase", /* tp_name */
sizeof(THCPStream), /* tp_basicsize */
0, /* tp_itemsize */
(destructor)THCPStream_dealloc, /* tp_dealloc */
0, /* tp_vectorcall_offset */
// NOLINTNEXTLINE(modernize-use-nullptr)
0, /* tp_getattr */
// NOLINTNEXTLINE(modernize-use-nullptr)
0, /* tp_setattr */
// NOLINTNEXTLINE(modernize-use-nullptr)
0, /* tp_reserved */
// NOLINTNEXTLINE(modernize-use-nullptr)
0, /* tp_repr */
// NOLINTNEXTLINE(modernize-use-nullptr)
0, /* tp_as_number */
// NOLINTNEXTLINE(modernize-use-nullptr)
0, /* tp_as_sequence */
// NOLINTNEXTLINE(modernize-use-nullptr)
0, /* tp_as_mapping */
// NOLINTNEXTLINE(modernize-use-nullptr)
0, /* tp_hash */
// NOLINTNEXTLINE(modernize-use-nullptr)
0, /* tp_call */
// NOLINTNEXTLINE(modernize-use-nullptr)
0, /* tp_str */
// NOLINTNEXTLINE(modernize-use-nullptr)
0, /* tp_getattro */
// NOLINTNEXTLINE(modernize-use-nullptr)
0, /* tp_setattro */
// NOLINTNEXTLINE(modernize-use-nullptr)
0, /* tp_as_buffer */
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
nullptr, /* tp_doc */
// NOLINTNEXTLINE(modernize-use-nullptr)
0, /* tp_traverse */
// NOLINTNEXTLINE(modernize-use-nullptr)
0, /* tp_clear */
// NOLINTNEXTLINE(modernize-use-nullptr)
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
// NOLINTNEXTLINE(modernize-use-nullptr)
0, /* tp_iter */
// NOLINTNEXTLINE(modernize-use-nullptr)
0, /* tp_iternext */
THCPStream_methods, /* tp_methods */
THCPStream_members, /* tp_members */
THCPStream_properties, /* tp_getset */
// NOLINTNEXTLINE(modernize-use-nullptr)
0, /* tp_base */
// NOLINTNEXTLINE(modernize-use-nullptr)
0, /* tp_dict */
// NOLINTNEXTLINE(modernize-use-nullptr)
0, /* tp_descr_get */
// NOLINTNEXTLINE(modernize-use-nullptr)
0, /* tp_descr_set */
0, /* tp_dictoffset */
// NOLINTNEXTLINE(modernize-use-nullptr)
0, /* tp_init */
// NOLINTNEXTLINE(modernize-use-nullptr)
0, /* tp_alloc */
THCPStream_pynew, /* tp_new */
};
void THCPStream_init(PyObject *module)
{
Py_INCREF(THPStreamClass);
THCPStreamType.tp_base = THPStreamClass;
THCPStreamClass = (PyObject*)&THCPStreamType;
if (PyType_Ready(&THCPStreamType) < 0) {
throw python_error();
}
Py_INCREF(&THCPStreamType);
if (PyModule_AddObject(
module, "_CudaStreamBase", (PyObject *)&THCPStreamType) < 0) {
throw python_error();
}
}