Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding a cuda file to an existing c project in visual studio

I am trying to add a CUDA file to my existing C++ Visual Studio project. I have the CUDA 5.0 SDK installed, I have created a new .cu file and also set its Item Type to CUDA/C++ in the CUDA file properties. But it looks like it just does not compile giving errors that say that the compiler does not recognize the CUDA keywords. One of the errors I get is:

error C2065: 'threadIdx' : undeclared identifier

Any suggestions?

like image 249
shaveenk Avatar asked Jul 18 '13 03:07

shaveenk


People also ask

How add Cuda to existing Visual Studio project?

Open the Visual Studio project, right click on the project name, and select Build Dependencies->Build Customizations..., then select the CUDA Toolkit version you would like to target. Alternatively, you can configure your project always to build with the most recently installed version of the CUDA Toolkit.

How do I run a CUDA program in Visual Studio 2019?

[Step1] Download CUDA Toolkit 10.2 from https://developer.nvidia.com/cuda-downloads [Step 2] Directly edit . vcxproj file and replace 10.1 with 10.2. [Step 3] In the project properties tab of VS2109, change the platform tool set value to 'Visual Studio 2019 (v142)'. [Step 4] Build!

How do I add a CPP file to a project?

Add a new source file to the project, as follows. In Solution Explorer, right-click the Source Files folder, point to Add, and then click New Item. In the Code node, click C++ File (. cpp), type a name for the file, and then click Add.


2 Answers

I found that the best way to do this is to perform the following in the existing CPU project

1) Build Dependencies -> Build Customisations

click the Cuda checkbox

2) Create a new simple CUDA project using the wizard (you anyway probably want to test your CUDA project builds ok firstly), load both projects into the IDE and then compare settings between the two projects, you will need to add the following in project settings

$(CudaToolkitLibDir) to additional libraries settings (linker tab) $(CudaToolkitIncludeDir) to additional include directories (c++ tab)

cudart.lib to additional dependencies (linker tab)

Then compare the CUDA tabs

I found that 32 bit had been pre-selected for the target machine architecture for some reason so I changed that to 64 bit.

After this I added a define _CUDA_CODE_COMPILE_ to preprocessor definitions to switch between CUDA or CPU compilation.

#ifdef _CUDA_CODE_COMPILE_
    cudaCodeFunction();
#else
    cpuCodeFunction();
#endif

Not ideal but necessary since there seem to be no defines set to indicate that NVCC is installed (other than performing a shell command!)

like image 128
ejectamenta Avatar answered Nov 08 '22 09:11

ejectamenta


I can't go through it all at the moment but I think those steps are necessary:

  1. Right click on your Project in the Project Explorer Build...(customization?) [my Version is German. I can't tell the keyword exactly but it's something about "Build...". You need to check "CUDA 5.0" here.
  2. Set up the "Additional Include Directories" for Cuda in the "General" Tab of the Compiler options (Project Properties).
  3. Add the cuda libfile to "Additional Dependencies" in the "Input" Tab of the Linker.
  4. Mark the File as Cuda file (you've done that).
like image 36
Pixelchemist Avatar answered Nov 08 '22 08:11

Pixelchemist