Rapidinha: Compilando um programa C++ com CUDA

No Gravatar

Em meus primeiros passos no aprendizado de programação em CUDA, tive um problema muito básico, mas que foi difícil encontrar a solução do mesmo: como compilar um programa escrito em C++ for CUDA ???

Vejamos um exemplo de código, o arquivo teste.cu

//**********************************************************************************************

cudaArray* cu_array;
texture<float, 2> tex;

// Allocate array
cudaMallocArray(&cu_array, cudaCreateChannelDesc<float>(), width, height);

// Copy image data to array
cudaMemcpy(cu_array, image, width*height, cudaMemcpyHostToDevice);

// Bind the array to the texture
cudaBindTexture(tex, cu_array);

// Run kernel
dim3 blockDim(16, 16, 1);
dim3 gridDim(width / blockDim.x, height / blockDim.y, 1);
kernel<<< gridDim, blockDim, 0 >>>(d_odata, width, height);
cudaUnbindTexture(tex);

__global__ void kernel(float* odata, int height, int width)
{
unsigned int x = blockIdx.x*blockDim.x + threadIdx.x;
unsigned int y = blockIdx.y*blockDim.y + threadIdx.y;
float c = texfetch(tex, x, y);
odata[y*width+x] = c;
}
//**********************************************************************************************

Para compilar este programa, primeiramente é necessário ter o compilador nvcc instalado, por meio das bibliotecas fornecidas pela NVidia.

Temos então a seguinte sequência de comandos:

nvcc -cuda teste.cu

Este comando irá gerar um código C++ a partir do arquivo teste.cu, guardando-o no arquivo teste.cu.cpp. Após isso, faz-se:

nvcc teste.cu.cpp -o teste.exe

E assim, teremos o executável tão desejado!

Espero que ajude!

See ya!

  • Share/Bookmark

Tags: , , ,

1 Comentário sobre "Rapidinha: Compilando um programa C++ com CUDA"

  • Wagner says:
Deixe um comentário

View in: Mobile | Standard