I have declared the following template to make code shorter:
template <typename T>
void allocateGPUSpace(T* ptr, int size){
cudaMalloc((void**)&ptr,size * sizeof(T));
}
Moreover, I use the template as follows:
float* alphaWiMinusOne;
allocateGPUSpace<float>( alphaWiMinusOne,numUnigrams);
However, when i compile the code, VS 2008 gives the warning
warning: variable "alphaWiMinusOne" is used before its value is set
and
uninitialized local variable 'alphaWiMinusOne' used
Does cuda not understand templates in C++? Gosh, that will be a MUST do for nvidia
Firstly, that warning doesn’t come from CUDA, it comes from the host compiler (so Microsoft’s C++ compiler or GCC depending on your platform), and it is a perfectly valid warning. You have made the same mistake you made here, and this code won’t work as you are hoping, because you are passing the pointer to operate on by value, not by reference. Your code should be like this:
and the call like this:
or perhaps
and then
Using either will eliminate the compiler warnings and the code will work. As a note of style, it would be a rather short sighted helper function design that didn’t include any error checking…….