Monthly Archives: February 2012

cuSVM in Visual Studio 2010 with CUDA 4.0

cuSVM is a CUDA implementation of support vector classification and regression. Recently, I had some questions on a post regarding compiling cuSVM using Visual Studio. Out of curiosity, I downloaded cuSVM and went to work myself.

Now, if you just download cuSVM and open up the Visual Studio solution, you will be a bit dissapointed as it does not compile and is not contained in a CUDA project. In order to correct this, I took the steps listed below. While I encourage you to read the steps and complete this process yourself, you may also download the files used in this project by clicking here.

  1. Create a new CUDA 4.0 Project called "cuSVMPredict"
  2. Create a new CUDA 4.0 Project called "cuSVMTrain"
  3. Add the appropriate files from the original cuSVM project. This is done by copying each file into the correct location on the file system, right-clicking on the project and selecting "Add -> Existing Item...". The files needed include:
    • For cuSVMPredict: cuSVMPredict.cpp, cuSVMPredictKernel.cu, PredictModule.def
    • For cuSVMTrain: cuSVMTrain.cpp, cuSVMSolver.cu, TrainModule.def
    • Copy the "inc" directory into the root of the solution
  4. Open up the file "cuSVMSolver.cu" in the "cuSVMTrain" project
  5. Add "using namespace std;" on line 17, after the final include statement
  6. For each project, right-click on the project and select "Properties"
  7. Make the following changes:
    1. Under "Configuration Properties -> General":
      • Change "Target Extension" to ".mexw32"
      • Change "Configuration Type" to "Dynamic Library (.dll)"
    2. Under "Configuration Properties -> C/C++ -> General -> Additional Include Directories":
      • Add your Matlab include directory (MATLABDIR\extern\include)
      • Add "..\inc"
      • Add the CUDA GPU Computing SDK include directory. On Windows 7 this is "C:\ProgramData\NVIDIA Corporation\NVIDIA GPU Computing SDK 4.0\C\common\inc"
    3. Under "Configuration Properties -> CUDA C/C++ -> Common -> Additional Include Directories":
      • Add your Matlab include directory (MATLABDIR\extern\include)
      • Add the CUDA GPU Computing SDK include directory. On Windows 7 this is "C:\ProgramData\NVIDIA Corporation\NVIDIA GPU Computing SDK 4.0\C\common\inc"
    4. Under "Configuration Properties -> Linker -> General -> Additional Library Directories":
      • Add your Matlab include directory (MATLABDIR\extern\lib\win32\microsoft)
      • Add the CUDA GPU Computing SDK bin directory. On Windows 7 this is "C:\ProgramData\NVIDIA Corporation\NVIDIA GPU Computing SDK 4.0\C\common\bin"
    5. Under "Configuration Properties -> Linker -> Input -> Additional Dependencies":
      • Add "cuda.lib"
      • Add "cublas.lib"
      • Add "libmex.lib"
      • Add "libmat.lib"
      • Add "libmx.lib"
  8. Right-click on the solution and select "Build Solution"

Having completed these steps, you should be good to go.

Calling CUDA Kernels from C++

I had a need to call a CUDA kernel from inside a C++ class. When I went looking through the NVIDIA examples and the wealth of information on the internet, I could not find a clear solution to my problem. So, let me state plainly how to accomplish this:

  1. Create a ".cu" file for your kernels;
  2. For each kernel you would like to call from C++, create a wrapper that can be called from inside your class;
  3. Inside this wrapper, call your kernel function;
  4. Include the prototype for your wrapper in the header file of your class, but not inside the class definition.

An example of the ".cu" file and the class header are shown below. One thing you apparently do not need to do (particularly in Visual Studio 2010) is include the ".cu" file in your class. As long as it is part of your project, everything should work just fine.

#ifndef _PROJECT_KERNELS
#define _PROJECT_KERNELS

__global__ void kernel(double Param1, double Param2){
    // Do Kernel Stuff here
}
extern "C" void functionName(double Param1, double Param2){

    kernel<<<Nb, Nt>>kernel(Param1, Param2);
}
#endif
#ifndef MYCLASS_H_
#define MYCLASS_H_

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

extern "C" void functionName(double Param1, double Param2);

class myClass{
    myClass();
}
#endif