@@ -96,13 +96,66 @@ PyObject* get_driver_version(PyObject *self, PyObject *Py_UNUSED(ignored)) {
9696 return Py_BuildValue (" (ii)" , major, minor);
9797}
9898
99+ // ========== Context helpers ==========
100+
101+ PyObject* synchronize_context (PyObject* self, PyObject* Py_UNUSED (ignored)) {
102+ Result<const DriverApi*> driver_result = get_driver_api ();
103+ if (!driver_result.is_ok ()) return NULL ;
104+ const DriverApi* d = *driver_result;
105+
106+ CUresult res = d->cuCtxSynchronize ();
107+ if (res != CUDA_SUCCESS ) {
108+ return PyErr_Format (PyExc_RuntimeError,
109+ " cuCtxSynchronize: %s" , get_cuda_error (d, res));
110+ }
111+ Py_RETURN_NONE;
112+ }
113+
114+ // ========== Stream helpers ==========
115+
116+ PyObject* create_stream (PyObject* self, PyObject* Py_UNUSED (ignored)) {
117+ Result<const DriverApi*> driver_result = get_driver_api ();
118+ if (!driver_result.is_ok ()) return NULL ;
119+ const DriverApi* d = *driver_result;
120+
121+ CUstream stream;
122+ CUresult res = d->cuStreamCreate (&stream, CU_STREAM_NON_BLOCKING );
123+ if (res != CUDA_SUCCESS ) {
124+ return PyErr_Format (PyExc_RuntimeError,
125+ " cuStreamCreate: %s" , get_cuda_error (d, res));
126+ }
127+ return PyLong_FromVoidPtr (stream);
128+ }
129+
130+ PyObject* destroy_stream (PyObject* self, PyObject* arg) {
131+ CUstream stream = static_cast <CUstream>(PyLong_AsVoidPtr (arg));
132+ if (PyErr_Occurred ()) return NULL ;
133+
134+ Result<const DriverApi*> driver_result = get_driver_api ();
135+ if (!driver_result.is_ok ()) return NULL ;
136+ const DriverApi* d = *driver_result;
137+
138+ CUresult res = d->cuStreamDestroy (stream);
139+ if (res != CUDA_SUCCESS ) {
140+ return PyErr_Format (PyExc_RuntimeError,
141+ " cuStreamDestroy: %s" , get_cuda_error (d, res));
142+ }
143+ Py_RETURN_NONE;
144+ }
145+
99146static PyMethodDef functions[] = {
100147 {" get_compute_capability" , get_compute_capability, METH_NOARGS ,
101148 " Get compute capability of the default CUDA device" },
102149 {" get_driver_version" , get_driver_version, METH_NOARGS ,
103150 " Get the cuda driver version" },
104151 {" _get_max_grid_size" , get_max_grid_size, METH_VARARGS ,
105152 " Get max grid size of a CUDA device, given device id" },
153+ {" _synchronize_context" , synchronize_context, METH_NOARGS ,
154+ " Synchronize the current CUDA context (drain all streams)." },
155+ {" _create_stream" , create_stream, METH_NOARGS ,
156+ " Create a non-blocking CUDA stream. Returns int handle." },
157+ {" _destroy_stream" , destroy_stream, METH_O ,
158+ " Destroy a CUDA stream given its int handle." },
106159 NULL
107160};
108161
0 commit comments