1

I am trying to create a CUDA + C++ project. Basically a .cpp project that calls for some CUDA kernel. So I simply followed the example here, which basically adds two vectors. The kernel does the summation job: http://blog.norture.com/2012/10/gpu-parallel-programming-in-vs2012-with-nvidia-cuda/

Here is the code,

#include <iostream>
#include "cuda_runtime.h"
#include "cuda.h"
#include "device_launch_parameters.h"

using namespace std;

__global__ void saxpy(int n, float a, float *x, float *y)
{
  int i = blockIdx.x*blockDim.x + threadIdx.x;
  if (i < n) y[i] = a*x[i] + y[i];
}

int main(void)
{
  int N = 1<<20;
  float *x, *y, *d_x, *d_y;
  x = (float*)malloc(N*sizeof(float));
  y = (float*)malloc(N*sizeof(float));

  cudaMalloc(&d_x, N*sizeof(float));
  cudaMalloc(&d_y, N*sizeof(float));

  for (int i = 0; i < N; i++) {
    x[i] = 1.0f;
    y[i] = 2.0f;
  }

  cudaMemcpy(d_x, x, N*sizeof(float), cudaMemcpyHostToDevice);
  cudaMemcpy(d_y, y, N*sizeof(float), cudaMemcpyHostToDevice);

  // Perform SAXPY on 1M elements
  saxpy<<<(N+255)/256, 256>>>(N, 2.0, d_x, d_y);

  cudaMemcpy(y, d_y, N*sizeof(float), cudaMemcpyDeviceToHost);

  float maxError = 0.0f;
  for (int i = 0; i < N; i++)
    maxError = max(maxError, abs(y[i]-4.0f));
  cout << "Max error: " << maxError;
}

When I built I got this error:

1>------ Rebuild All started: Project: CUDATest001, Configuration: Debug x64 ------
1>  CUDATestZeroZeroOne.cpp
1>CUDATestZeroZeroOne.obj : error LNK2001: unresolved external symbol threadIdx
1>CUDATestZeroZeroOne.obj : error LNK2001: unresolved external symbol blockIdx
1>CUDATestZeroZeroOne.obj : error LNK2001: unresolved external symbol blockDim
1>D:\Projects\CUDATest001\x64\Debug\CUDATest001.exe : fatal error LNK1120: 3 unresolved externals
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

If the line saxpy<<<(N+255)/256, 256>>>(N, 2.0, d_x, d_y); is commented out, then this error appeared:

1>------ Rebuild All started: Project: CUDATest001, Configuration: Debug x64 ------
1>  CUDATestZeroZeroOne.cpp
1>CUDATestZeroZeroOne.obj : error LNK2001: unresolved external symbol threadIdx
1>CUDATestZeroZeroOne.obj : error LNK2001: unresolved external symbol blockIdx
1>CUDATestZeroZeroOne.obj : error LNK2001: unresolved external symbol blockDim
1>D:\Projects\CUDATest001\x64\Debug\CUDATest001.exe : fatal error LNK1120: 3 unresolved externals
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========

I am using vs2012 + CUDA 5.5. I started with a empty C++ win32 console project, added a .cpp file which includes all the code above. I am not even sure at this point should it be a .cu or a .cpp file?

Anyone has any idea how to make this work? Thanks.

Nick X Tsui
  • 2,737
  • 6
  • 39
  • 73
  • Have you tried changing the type of file to `.cu`? – sgarizvi Nov 18 '13 at 19:56
  • @sgar91: Yea it does not change anything. Even though I changed the extension to cu it still shows cpp file in the solution window. And the compile gives same error. – Nick X Tsui Nov 18 '13 at 20:07

1 Answers1

5

In the context menu for your project, click Build Customizations. Turn on the CUDA 5.5 target.

In the context menu for your .cpp file, click Rename and rename it to .cu.

In the context menu for your .cu file (that you just renamed), select Properties. Then go to General and make sure Item Type is set to CUDA C/C++.

Rebuild.

When you start a new CUDA project, you can select Templates > NVIDIA > CUDA 5.5 > CUDA 5.5 Runtime to get a project that should compile without any modifications.

Roger Dahl
  • 15,132
  • 8
  • 62
  • 82
  • 2
    Yea, the third advice makes a difference. So many threads, but none of them mentioned using Item Type CUDA C/C++. Thanks a lot!! – Nick X Tsui Nov 18 '13 at 21:12