Template Functions on CUDA template<typename T> void allocateGPU(T* array, uint size)

Hi,

I would like to know how I can do the following code work?

[codebox]

//--------------------------------//

// cudautils.h

//--------------------------------//

template

void allocateGPU(T* array, unsigned int size);

template

void freeGPU(T* array);

//-------------------------------//

// cudautils.cu

//-------------------------------//

template

void allocateGPU(T* array, unsigned int size)

{

   cudaMalloc((void**)&array, sizeof(T) * size);

}

template

void allocateGPU(T* array, unsigned int size)

{

   cudaFree(array);

}

//-------------------------------//

// main.cpp

//-------------------------------//

#include

using namespace std;

#include “cudautils.h”

int main()

{

   float* data = 0;

   unsigned int N = 10;

allocateGPU(data, 10);

   freeGPU(data);

return 0;

}

[/codebox]

[b]template
void allocateGPU(T* array, unsigned int size)
{
cudaMalloc((void**)&array, sizeof(T) * size);
}

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.

Why not use Thrust’s memory management facilities:

[1] http://thrust.googlecode.com/svn/trunk/doc…_functions.html

Yes, sure. But at the moment I want to learn this low level development in CUDA.