How Big Is a cudaStream_t?
I Have Inherited Some Code That Basically Does Stuff Like This: Void *Stream; cudaStreamCreate((cudaStream_t *)&Stream); Looking at...
I have inherited some code that basically does stuff like this:
void *stream;
cudaStreamCreate((cudaStream_t *)&stream);
Looking at targets/x86_64-linux/driver_types.h for CUDA 8, I see:
typedef __device_builtin__ struct CUStream_st *cudaStream_t;
As far as I understand it, the cast will work, but I worry about how future-proof this may be, and also if it is safe when the code is ported to ARM. How dangerous is the above code? Does the __device_builtin__ affect anything?
(Note: I plan to talk to the developer directly and tell them to use cudaStream_t throughout and #include <cuda_runtime.h>, so I am hoping to clarify the technical issues here.)
1 Answer
How big is a
cudaStream_t?
Like you've observed,
typedef __device_builtin__ struct CUStream_st *cudaStream_t;
So it's a pointer, and has the size of a pointer, i.e. 64 bits on typical architectures today but different sizes on other architectures. But do you really need to utilize that information? I would guess not.
As far as I understand it, the cast will work, but I worry about how future-proof this may be
Then make it:
cudaStream_t stream;
cudaStreamCreate(&stream);
or use the C++'ish API wrappers, e.g.:
auto device = cuda::device::current::get();
auto stream = device.create_stream(cuda::stream::sync);
where that's abstracted away, and stream_t is a wrapper, not a pointer, anyway (caveat: I'm the author of the wrapper library.)
What I'd worry about is not incompatibility, but rather avoiding invalid assumptions. And, indeed, you should not assume cudaStream_t is a pointer - just treat it as something opaque.
and also if it is safe when the code is ported to ARM. How dangerous is the above code?
It's dangerous, but not because of the porting, but like I said, because of the invalid assumption. It would be less dangerous with, say,
static_assert(sizeof(void*) == sizeof(cudaStream_t),
"Unexpected size of cudaStream_t - not the same as void *");
but why are you insisting on the void *, really?