HIP is the AMD GPU programming model corresponding to the NVIDIA's CUDA. I have a code snippet from HIP source code that I can't fully understand. As a reminder, the understanding of the following code snippnet doesn't require any background knowledge of HIP, but more of a question in C++ template/function pointer.
typedef int hipLaunchParm;
template <typename... Args, typename F = void (*)(hipLaunchParm, Args...)>
inline void hipLaunchKernel(F&& kernel, const dim3& numBlocks, const dim3& dimBlocks,
std::uint32_t groupMemBytes, hipStream_t stream, Args... args)
{
hipLaunchKernelGGL(kernel, numBlocks, dimBlocks, groupMemBytes, stream,
hipLaunchParm{}, std::move(args)...);
}
I'm confused about the following:
typename... Args
useful?If F is a function pointer, why does it need to be double referenced in the argument?
F isn't a function pointer necessarily. That is just the default type. You can pass any† callable as long as it is invokable with the given arguments, and you want to avoid copying stateful function objects when that's not necessary. Some might not even be copyable. That may be the reason why they use a reference here.
† as far as C++ is concerned. I don't know about restrictions that HIP / CUDA might have.
How is the first template argument
typename... Args
useful?
It allows passing a variable number of arguments into the delegated function.
hipLaunchParm is just a alias for integer, but what is the meaning of {} when it is called in the argument?
T{}
is a syntax for value initialisation of a temporary. In case of integer, this means that zero is passed as an argument.