Snacks
Snacks

Reputation: 513

C++ CUDA project not compiling in VS2012, error on "<<<"

i have a little problem with compiling a "Hello world" CUDA program in VisualStudio2012. I have downloaded and example from Nvidia:

#include "stdafx.h"
#include "cuda.h"
#include "cuda_runtime.h"
#include "device_launch_parameters.h"

#include <stdio.h>

__device__ const char *STR = "HELLO WORLD!";
const char STR_LENGTH = 12;

__global__ void hello()
{
    printf("%c\n", STR[threadIdx.x % STR_LENGTH]);
}

int main(void)
{
    int num_threads = STR_LENGTH;
    int num_blocks = 1;
    hello<<<num_blocks,num_threads>>>();
    cudaDeviceSynchronize();

    return 0;
}

I changed the build configuration to cuda 6.0, added include and library paths in project properties. Still, it gave me one error in this line:

hello<<<num_blocks,num_threads>>>();

It reacts to <<<, telling me that there is an expression expected. Is there a way to fix this? It's driving me mad for the last few hours.

EDIT:

I changed the Item type of the file to "CUDA C/C++" in properties>general. Now it compiles, but with errors:

1>------ Build started: Project: CUDATest, Configuration: Debug Win32 ------
1>  Compiling CUDA source file CUDATest.cu...
1>  
1>  C:\Users\Rafał\Documents\Visual Studio 2012\Projects\CUDATest\CUDATest>"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v6.0\bin\nvcc.exe" -gencode=arch=compute_10,code=\"sm_10,compute_10\" --use-local-env --cl-version 2012 -ccbin "C:\Program Files\Microsoft Visual Studio 11.0\VC\bin"  -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v6.0\include" -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v6.0\include"  -G   --keep-dir Debug -maxrregcount=0  --machine 32 --compile -cudart static  -g   -DWIN32 -D_DEBUG -D_CONSOLE -D_UNICODE -DUNICODE -Xcompiler "/EHsc /W3 /nologo /Od /Zi /RTC1 /MDd  " -o Debug\CUDATest.cu.obj "C:\Users\Rafał\Documents\Visual Studio 2012\Projects\CUDATest\CUDATest\CUDATest.cu" 
1>nvcc : warning : The 'compute_10' and 'sm_10' architectures are deprecated, and may be removed in a future release.
1>C:/Users/Rafa│/Documents/Visual Studio 2012/Projects/CUDATest/CUDATest/CUDATest.cu(15): error : calling a __host__ function("printf") from a __global__ function("hello") is not allowed
1>  CUDATest.cu
1>C:\Program Files\MSBuild\Microsoft.Cpp\v4.0\V110\BuildCustomizations\CUDA 6.0.targets(597,9): error MSB3721: The command ""C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v6.0\bin\nvcc.exe" -gencode=arch=compute_10,code=\"sm_10,compute_10\" --use-local-env --cl-version 2012 -ccbin "C:\Program Files\Microsoft Visual Studio 11.0\VC\bin"  -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v6.0\include" -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v6.0\include"  -G   --keep-dir Debug -maxrregcount=0  --machine 32 --compile -cudart static  -g   -DWIN32 -D_DEBUG -D_CONSOLE -D_UNICODE -DUNICODE -Xcompiler "/EHsc /W3 /nologo /Od /Zi /RTC1 /MDd  " -o Debug\CUDATest.cu.obj "C:\Users\Rafał\Documents\Visual Studio 2012\Projects\CUDATest\CUDATest\CUDATest.cu"" exited with code 2.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

And if i comment the printf function, then i just get something like this:

1>------ Build started: Project: CUDATest, Configuration: Debug Win32 ------
1>  Compiling CUDA source file CUDATest.cu...
1>  
1>  C:\Users\Rafał\Documents\Visual Studio 2012\Projects\CUDATest\CUDATest>"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v6.0\bin\nvcc.exe" -gencode=arch=compute_10,code=\"sm_10,compute_10\" --use-local-env --cl-version 2012 -ccbin "C:\Program Files\Microsoft Visual Studio 11.0\VC\bin"  -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v6.0\include" -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v6.0\include"  -G   --keep-dir Debug -maxrregcount=0  --machine 32 --compile -cudart static  -g   -DWIN32 -D_DEBUG -D_CONSOLE -D_UNICODE -DUNICODE -Xcompiler "/EHsc /W3 /nologo /Od /Zi /RTC1 /MDd  " -o Debug\CUDATest.cu.obj "C:\Users\Rafał\Documents\Visual Studio 2012\Projects\CUDATest\CUDATest\CUDATest.cu" 
1>nvcc : warning : The 'compute_10' and 'sm_10' architectures are deprecated, and may be removed in a future release.
1>ptxas : fatal error : Ptx assembly aborted due to errors
1>  CUDATest.cu
1>C:\Program Files\MSBuild\Microsoft.Cpp\v4.0\V110\BuildCustomizations\CUDA 6.0.targets(597,9): error MSB3721: The command ""C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v6.0\bin\nvcc.exe" -gencode=arch=compute_10,code=\"sm_10,compute_10\" --use-local-env --cl-version 2012 -ccbin "C:\Program Files\Microsoft Visual Studio 11.0\VC\bin"  -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v6.0\include" -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v6.0\include"  -G   --keep-dir Debug -maxrregcount=0  --machine 32 --compile -cudart static  -g   -DWIN32 -D_DEBUG -D_CONSOLE -D_UNICODE -DUNICODE -Xcompiler "/EHsc /W3 /nologo /Od /Zi /RTC1 /MDd  " -o Debug\CUDATest.cu.obj "C:\Users\Rafał\Documents\Visual Studio 2012\Projects\CUDATest\CUDATest\CUDATest.cu"" exited with code -1.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Upvotes: 0

Views: 2530

Answers (1)

Geto
Geto

Reputation: 191

I've managed to run that example, however your GPU needs to have compute capability >=2.0.

Right click on project and choose "Properties".

Go to CUDA/C++ -> Device.

On "Code Generation" replace what you have with "compute_20,sm_20" (quotes for clarity).

EDIT (regarding Ptx assembly aborted due to errors ): If you remove the __device__ qualifier from const char *STR it should compile. (When you have commented out the printf function) - That was the reason for the ptxas error.

Upvotes: 2

Related Questions