cuda::experimental::places::get_device_from_stream#

inline int cuda::experimental::places::get_device_from_stream(
cudaStream_t stream
)#

Computes the CUDA device in which the stream was created.

The device is read from the stream’s context through the driver API: the context is looked up with cuStreamGetCtx, made current, and asked for its device with cuCtxGetDevice. This is the path stream_and_event::insert_dependency has always run on captured streams, and it is accepted during thread-local, relaxed and global capture without invalidating it (verified on drivers 580 and 595).

cudaStreamGetDevice / cuStreamGetDevice are deliberately not used: whether they are permitted during capture depends on the driver, not on the toolkit (rejected with cudaErrorStreamCaptureUnsupported by cudart 12.9 on driver 595 and by cudart 13.0 on driver 580, accepted on driver 615), and a rejection invalidates the capture, so there is no way to try them first.

The context lookup is cuda::stream_ref’s: cuStreamGetCtx_v2 on drivers 12.5 and newer, since the original cuStreamGetCtx returns CUDA_ERROR_NOT_SUPPORTED for green-context streams on CUDA 12 drivers, and the original query on older drivers, which have no such streams.

Answering from a guess (the current device) instead of a query is what put transient events in the wrong device’s context on multi-GPU systems (cudaErrorInvalidResourceHandle at event-record time).