template
void allocateGPU(T* array, unsigned int size)
{
cudaFree(array);
}[/b]
Nothing wrong at all, It may works fine.
I usually using template function for allocation
[b]template inline T* AllocateMemory(size_t size)
{
T* data;
cudaError_t result = cudaMalloc((void**)&data, size);
if (result != cudaSuccess) {
data = NULL;
}
return data;
}
template void FreeData(T* data)
{
if (data != NULL) {
cudaFree(data);
}
}[/b]
Hope it helpful for you.
When I add cuda codes as cudaMalloc and cudaFree in a template file, it gives me error. I think that this happens because of the c++ compilation does not recognize them.
I did the following using void pointers to simulate template:
[b]
//-----------------------------------------//
// cudautils.h
//-----------------------------------------//
void* allocateGPU(size_t size);
void freeGPU(void* gpuPtr);
//-----------------------------------------//
// cudautils.cu
//-----------------------------------------//
void* allocateGPU(size_t size)
{
void* gpuPtr = 0;
cudaMalloc((void**)&gpuPtr, size);
return gpuPtr;
}
void freeGPU(void* gpuPtr)
{
cudaFree(gpuPtr);
}
//-----------------------------------------//
// main.cpp
//-----------------------------------------//
int main()
{
int N = 10;
float* data = static_cast<float*>( allocateGPU(sizeof(float) * N) );
freeGPU(data);
return 0;
}
[/b]
Now I can use the header cudautils.h in my c++ code. Note that there is no cuda code in the header file.