Is torch.cuda.Stream().cuda_stream equivalent to cudaStream_t


I’d like to obtain the underlying cudaStream_t pointer from torch.cuda.Stream().cuda_stream in python, then pass such obtained pointer into my cpp code which does not depend on torch. I’d like to use such pointer to call cudaStreamSynchronize(ptr).

Is this possible and safe to use like this? just reinterpret_cast<cudaStream_t>(torch.cuda.Stream().cuda_stream).

Yes, I believe you can create the pointer via:

cuda_stream = torch.cuda.Stream().cuda_stream
1 Like

Thanks for your reply.

As stream, I think the same equivalence is applied to torch.cuda.Event().cuda_event and cudaEvent_t as well. Right? just create pointer via cuda_event = torch.cuda.Event()._cuda_event ctypes.c_void_p(cuda_event).

Yes, this should also work for events.

1 Like

I want to pass torch.cuda.Stream() to my cpp code and use it as c10::cuda::CUDAStream but failed.
Whether there are some APIs that can do this? Hope for your reply.

Could you describe what in @Rhett_Ying’s suggestion is failing?