Reputation: 3171
I'm writing a cuda library where some functions would have a parameter accepting a cudaStream_t from outside(user side) if the user give one, otherwise will use an internal stream.
So this function would like to have a cudaStream_t as the last param with default val, but I didn't find an appropriate value for cudaStream_t, and I'm not sure pass-by-value or pass-by-ref/pointer
One approach I can think of is pass-by-pointer and nullptr
as default value
void cudaFunc1(....., cudaStream_t * stream_ptr=nullptr);
but I see many pass-by-value usage in others code....not sure what's the best practice
Upvotes: 1
Views: 734
Reputation: 151879
Without any additional information, I would recommend use of pass-by-value with cudaStreamLegacy
as the default option. See here.
If you know that you intend your application to be compiled with the per thread default stream option, then a better choice would be cudaStreamPerThread
.
These choices make no assumptions that streams have already been created, or will be created, as part of your function call usage.
If you want to use whatever is the default stream associated with how the application was compiled, then simply using NULL for the stream parameter default will do that.
Either pass-by-value or pass-by-pointer should be workable. This mostly has to do with the usual semantics of those choices, not anything specific or unique to CUDA. If you intended your function to create the stream if not already created, and you wanted this created stream to be used external to the function, then either pass-by-pointer, or else return the created stream as the function return value, would seem to be the obvious choices. You can also use C++ pass-by-reference.
Upvotes: 2