Adding CUDA to EXISTING VSS 2013 DLL project - c++

I have an existing Win32 DLL that compiles in VSS 2013.
I also have the correct CUDA tools installed, as I am successfully able to create a new CUDA 7.0 runtime project within VCC.
However, i am unable to figure out how to integrate the CUDA into my existing project so that I may add CUDA functions to my existing source.

To have CUDA working you would need to:
Have nvcc compile your .cu files - I think this happens as soon as the CUDA build rules are enabled in the project (but I can be wrong) because VS automatically uses them for all .cu files. This step gives you .o produced from your .cu files by nvcc.
Link against the needed CUDA libraries ('cudart` and friends) to resolve the driver and CUDA API calls in your code - this can be achieved by adding the needed CUDA libraries as linker inputs under the VS linker property page.
These instructions should cover enabling the CUDA build rules and adding the needed libraries as linker inputs.

Related

Understanding how to compile C++ project

I'm new to coding and Visual Studio. I know and understand how to create, save and compile a new C++ project in Visual Studio. I understand that a new VS project creates a solution file in the directory which then I can use to open the project, etc.
What I don't understand is how to compile a C++ project which doesn't have a VS solution file. I mean, do I have to manually create a project and import files and then build it?
Please take this GitHub project as an example. Under information, it clearly states the following:
Building
To build a static library (./lib/ompeval.a) on Unix systems, use make.
To enable -msse4.1 switch, use make SSE4=1. Run tests with ./test. For
Windows there's currently no build files, so you will have to compile
everything manually. The code has been tested with MSVC2013, TDM-GCC
5.1.0 and MinGW64 6.1, Clang 3.8.1 on Cygwin, and g++ 4.8 on Debian.
Now, what I don't understand is how do I build this project in my VS software so that I can successfully run it? Any help would be appreciated. Thank you.
As you said it, create a new project in Visual Studio and just import the source and header files. Looking at the makefile, there doesn't seem to be any pre or post build events so you can import the files and compile them right away (as long as the code is platform compatible ofc).
You may have to create multiple projects. One for the static library and one for the test files, since Visual Studio projects usually have one output type only. Mostly one of the presets: dynamic library (dll), static library (lib) or executable (exe).

How to add CUDA 7.0 acceleration to existing project?

According to http://docs.nvidia.com/ all you have to do to include cuda in your project is to:
Open the Visual Studio project, right click on the project name, and select Build Customizations..., then select the CUDA Toolkit version you would like to target.
I am following "Your First CUDA C Program" from CUDACast and I see in the video that VisualStudio still marks red all the instructions from CUDA, so I thought it was fine when I was writing in my project. But when I've built it I've seen errors in every line using their instructions.
Do I need to link some other libraries myself?
I've noticed two new sections when I open project's properties:
"CUDA C/C++" and "CUDA Linker"
Yes, if you are building a cuda runtime project, at a minimum you will need to link in cudart.lib the cuda runtime library. This involves specifying the path to that library as well as identify the library itself in "Additional Dependencies". If you don't do this, but use CUDA API functions such as cudaMalloc, you will get errors at link phase. (If you are getting errors prior to link phase, there is some other issue.)
I suggest you review a cuda sample code such as vectorAdd, to learn how a cuda Visual Studio project is structured.
You will also probably want to be sure you have followed all steps in the windows getting started guide properly, which includes building and running sample codes.
The reason that the library step is not shown (needed) in the video sequence is that your method of starting the project in Visual Studio (start an arbitrary project, then select CUDA build customizations) is different than that shown in the video (start a project by selecting the CUDA Runtime custom toolchain).

How to link opencv and other dll files to output exe of visual studio 2013

I am new with visual studio, opencv.
I am using visual studio 2013, opencv and c++ for my project.
I configured (copied path) the opencv and other library to my computer environment system.
After run the project in visual studio, normally, there will be an exe file in the project.
I can copy the exe file in the project folder and copy to other place in my computer and it will run normally.
This is because my computer environment systems are configured with opencv and other library.
I want to do the same thing with other computers BUT I do not want to manually configure each computer with opencv and other libraries.
Are there any ways that I can do to link everythings all in exe file after run the project in visual studio 2013 so that I can run the exe without depend on the path of libaries and opencv?
EDITED
I use opencv installer opencv2.4.7.exe
In the current VS2013 my project, i configured my project and opencv installer as this link
http://www.anlak.com/using-opencv-2-4-x-with-visual-studio-2010-tutorial/
question : Can i use the library in folder C:\opencv\build\x64\vc11\staticlib come from the opencv installer no need create my own library from source opencv?
question : In case i need to generate new library from opencv source (http://docs.opencv.org/doc/tutorials/introduction/windows_install/windows_install.html) or use lib in static folder of opencv installer, if i want to include it all to exe files, do i need to create new project and reconfigure?
Thank you.
First you need to rebuild openCV to generate static libraries instead of dynamically linked ones. This way all code that your application uses is thrown together in one single exe-file (which will probably be significantly bigger). This exe-file you can move to other computers and they should still work there, provided they have an architecture that is at least compatible with yours. So if you build it on an x86 perconal computer (32-bit), it should basically work on any other personal computer. If you build it on a x64 computer (AMD 64-bit), it will only run on other x64 machines. At least this is true assuming both systems use the same syscall API (Windows NT, POSIX...).
For openCV you do this by setting the BUILD_SHARED_LIBS build flag to false (see OpenCV as a static library (cmake options), the following line is taken from there):
cmake -DBUILD_SHARED_LIBS=OFF ..
Once you have done this, you will see that the openCV folder looks very similar to the one you have now, except that in your 'lib' folder there will now be .lib-files instead of .dll files (at least if you are working on Windows, which I assume you do since you are using Visual Studio).
Next step is to go to your project settings and set your linker to link with the static libraries instead of the dynamically ones. If you have used openCV's local method for linking, you can go to project settings -> linker -> input -> Addtional dependencies. There you change the extension of all the openCV libraries from .dll to .lib.
Now you should be able to rebuild your application and the resulting exe-file should have all dependent libraries contained in it.

Compiling V8 on Windows to Use with g++

I have gone through all the procedures on how to compile V8 and actually managed to compile it on Windows platform. However, the problem is that the compilation procedure on Windows forces you to compile with Visual Studio and therefore creates object files (.obj) which can be used in VS. I have managed to create a DLL file too but it only serves the purpose when there is an executable to run.
On the other hand, I am using Eclipse and g++ on Windows. To embed V8 into my C++ I will need to have a library file (.a extension so that the linker will work). Is there a way that this is doable?
When the DLL was created an import library should have been created as well. They typically have the same name as the DLL but with a .lib extension. Add this library to your project in Eclipse and it should be linked in at build time. you can do this by right clicking on the project and selecting Properties -> C/C++ Build then go Library and add it there.

How do I start a new CUDA project in Visual Studio 2008?

This is an incredibly basic question, but how do I start a new CUDA project in Visual Studio 2008? I have found tons and tons of documentation about CUDA related matters, but nothing about how to start a new project. I am working with Windows 7 x64 Visual Studio 2008 C++. I would really like to find some sort of really really basic Hello World app to just get a basic program compiling and running.
Edit:
I tried your steps Tom. I setup a console app. I then deleted the default .cpp it drops in and copied over the three files from the template project just to have something to compile. When I compile that, template_gold.cpp complained about not having stdafx.h included, so i included that. Now the build fails with this:
1>------ Build started: Project: CUDASandbox, Configuration: Debug x64 ------
1>Compiling...
1>template_gold.cpp
1>Linking...
1>LIBCMT.lib(crt0.obj) : error LNK2019: unresolved external symbol main referenced in function __tmainCRTStartup
1>D:\Stuff\Programming\Visual Studio 2008\Projects\CUDASandbox\x64\Debug\CUDASandbox.exe : fatal error LNK1120: 1 unresolved externals
1>Build log was saved at "file://d:\Stuff\Programming\Visual Studio 2008\Projects\CUDASandbox\CUDASandbox\x64\Debug\BuildLog.htm"
1>CUDASandbox - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
NOTE With the release of version 3.2 of the CUDA Toolkit, NVIDIA now includes the rules file with the Toolkit as opposed to the SDK. Therefore I've split this answer into two halves, use the correct instructions for your version of the Toolkit.
NOTE These instructions are valid for Visual Studio 2005 and 2008. For Visual Studio 2010 see this answer.
CUDA TOOLKIT 3.2 and later
I recommend using the NvCudaRuntimeApi.rules file (or NvCudaDriverApi.rules if using the driver API) provided by NVIDIA, this is released with the toolkit and supports the latest compiler flags in a friendly manner. Personally I would advise against using the VS wizard, but only because I really don't think you need it.
The rules file (installed into the Program Files\Microsoft Visual Studio 9.0\VC\VCProjectDefaults directory) "teaches" Visual Studio how to compile and link any .cu files in your project into your application.
Create a new project using the standard MS wizards (e.g. an empty console project)
Implement your host (serial) code in .c or .cpp files
Implement your wrappers and kernels in .cu files
Add the NvCudaRuntimeApi.rules (right click on the project, Custom Build Rules, tick the relevant box), see note 1
Add the CUDA runtime library (right click on the project and choose Properties, then in Linker -> General add $(CUDA_PATH)\lib\$(PlatformName) to the Additional Library Directories and in Linker -> Input add cudart.lib to the Additional Dependencies), see notes [2] and [3]
Optionally add the CUDA include files to the search path, required if you include any CUDA files in your .cpp files (as opposed to .cu files) (right click on the project and choose Properties, then in C/C++ -> General add $(CUDA_PATH)\include to the Additional Include Directories), see note [3]
Then just build your project and the .cu files will be compiled to .obj and added to the link automatically
Some other tips:
Change the code generation to use statically loaded C runtime to match the CUDA runtime; right click on the project and choose Properties, then in C/C++ -> Code Generation change the Runtime Library to /MT (or /MTd for debug, in which case you will need to mirror this in Runtime API -> Host -> Runtime Library), see note [4]
Enable syntax highlighting using the usertype.dat file included with the SDK, see the readme.txt in <sdk_install_dir>\C\doc\syntax_highlighting\visual_studio_8
I'd also recommend enabling Intellisense support with the following registry entry (replace 9.0 with 8.0 for VS2005 instead of VS2008):
[HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\9.0\Languages\Language Services\C/C++]
"NCB Default C/C++ Extensions"=".cpp;.cxx;.c;.cc;.h;.hh;.hxx;.hpp;.inl;.tlh;.tli;.cu;.cuh;.cl"
Incidentally I would advocate avoiding cutil if possible, instead roll your own checking. Cutil is not supported by NVIDIA, it's just used to try to keep the examples in the SDK focussed on the actual program and algorithm design and avoid repeating the same things in every example (e.g. command line parsing). If you write your own then you will have much better control and will know what is happening. For example, the cutilSafeCall wrapper calls exit() if the function fails - a real application (as opposed to a sample) should probably handle the failure more elegantly!
CUDA TOOLKIT 3.1 and earlier
I would use the Cuda.rules file provided by NVIDIA with the SDK, this is released alongside the toolkit and supports the latest compiler flags in a friendly manner. Personally I would advise against using the VS wizard, but only because I really don't think you need it.
The rules file (in the C\common directory of the SDK) "teaches" Visual Studio how to compile and link any .cu files in your project into your application.
Create a new project using the standard MS wizards (e.g. an empty console project)
Implement your host (serial) code in .c or .cpp files
Implement your wrappers and kernels in .cu files
Add the Cuda.rules (right click on the project, Custom Build Rules, browse for the rules file and ensure it is ticked)
Add the CUDA runtime library (right click on the project and choose Properties, then in Linker -> General add $(CUDA_LIB_PATH) to the Additional Library Directories and in Linker -> Input add cudart.lib to the Additional Dependencies), see note [2] below
Optionally add the CUDA include files to the search path, required if you include any CUDA files in your .cpp files (as opposed to .cu files) (right click on the project and choose Properties, then in C/C++ -> General add $(CUDA_INC_PATH) to the Additional Include Directories)
Then just build your project and the .cu files will be compiled to .obj and added to the link automatically
Some other tips:
Change the code generation to use statically loaded C runtime to match the CUDA runtime, right click on the project and choose Properties, then in C/C++ -> Code Generation change the Runtime Library to /MT (or /MTd for debug, in which case you will need to mirror this in CUDA Build Rule -> Hybrid CUDA/C++ Options), see note [4]
Enable syntax highlighting using the usertype.dat file included with the SDK, see the readme.txt in <sdk_install_dir>\C\doc\syntax_highlighting\visual_studio_8
I'd also recommend enabling Intellisense support with the following registry entry (replace 9.0 with 8.0 for VS2005 instead of VS2008):
[HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\9.0\Languages\Language Services\C/C++]
"NCB Default C/C++ Extensions"=".cpp;.cxx;.c;.cc;.h;.hh;.hxx;.hpp;.inl;.tlh;.tli;.cu;.cuh;.cl"
Incidentally I would advocate avoiding cutil if possible, instead roll your own checking. Cutil is not supported by NVIDIA, it's just used to try to keep the examples in the SDK focussed on the actual program and algorithm design and avoid repeating the same things in every example (e.g. command line parsing). If you write your own then you will have much better control and will know what is happening. For example, the cutilSafeCall wrapper calls exit() if the function fails - a real application (as opposed to a sample) should probably handle the failure more elegantly!
NOTE
You can also use a Toolkit-version-specific rules fule e.g. NvCudaRuntimeApi.v3.2.rules. This means that instead of looking for the CUDA Toolkit in %CUDA_PATH% it will look in %CUDA_PATH_V3_2%, which in turn means that you can have multiple versions of the CUDA Toolkit installed on your system and different projects can target different versions. See also note [3].
The rules file cannot modify the C/C++ compilation and linker settings, since it is simply adding compilation settings for the CUDA code. Therefore you need to do this step manually. Remember to do it for all configurations!
If you want to stabilise on a specific CUDA Toolkit version then you should replace CUDA_PATH with CUDA_PATH_V3_2. See also note 1.
Having mismatched version of the C runtime can cause a variety of problems; in particular if you have any errors regarding LIBCMT (e.g. LNK4098: defaultlib 'LIBCMT' conflicts with use of other libs) or multiply defined symbols for standard library functions, then this should be your first suspect.
What a great question!! For all the CUDA documentation out there, this is something that I've always thought was an obvious omission... In fact, I'm really glad I found this post, because after using CUDA for quite a while, I still hadn't found an official, correct way to get VS to produce a CUDA program from scratch.
When I've needed to start a new CUDA program, I've always just copied and modified the "template" example from the SDK directory. This may not be exactly what you're looking for, because it doesn't start fresh, but it is a quick way to get a CUDA-capable project working in VS with all the correct project/file names...
Make a copy of the "template" example from the SDK, and rename the directory -- the only necessary contents in the directory are source code and VS .sln and .vcproj files
Rename both .sln and .vcproj files
Open the .sln file in a text editor, and rename the Project variable and .vcproj filename in the 3rd line of the file
Open the .vcproj file in a text editor, and rename the Name and RootNamespace variables in the first few lines of the file
Open the project with VS, and open the Property Pages (right click on the project name in the solution explorer pane, select "Properties")
Change the Output File name in the Property Pages (under Configuration Properties -> Linker -> General) ... Before I change the filename, I select "All Configurations" from the Configuration pull-down and "x64" from the Platform pull-down, since I'm on a 64-bit system
Change the Program Database File name in the Property Pages (under Configuration Properties -> Linker -> Debugging) ... Before I change the filename, I select "Debug" and "x64" in the pull-downs.
Install CUDA VS wizard. It will setup VS and add CUDA Project to the "new project" menu.
Make sure that you have x64 compiler installed (must be checked during VS install).
Check if you have x64 libs, includes, nvcc dir and in the search path.
Create new project using CUDA template.
Change project type to x64 and CUDA setting to Native (if you have nv cuda-enabled card) or emulation otherwise.
The template will create custom build rules that compile .cu files with nvcc and other files with default compiler.
if, vs is trying to compile .cu files with C/C++ compiler, click on that file in solution explorer and disable compilation for that files (red dot on file's icon)
Additional info about installing CUDA wizard on VS2008 can be found here and here
[edit]
If you don't want to use wizard you have to setup CUDA lib/include/nvcc paths manually and add custom build rules to each new CUDA program. For additional info how to do it take a look at Tom's Answer.
You may want to take a look at this guide: http://www.programmerfish.com/how-to-run-cuda-on-visual-studio-2008-vs08/