Matlab and CUDA

From CSclasswiki
Jump to: navigation, search

Back to GPU main page

Note for those with a version of Matlab R2010b or later: GPU computing capabilities are now available in MATLAB Release 2010b with Parallel Computing Toolbox. Learn more about GPU computing with MATLAB here

Installations needed for Windows

  • Matlab
  • CUDA driver and SDK (instructions for installation here)
  • MathWorks MATLAB® CUDA 1.1 Plug-in for Windows (the file link is at the bottom of this page)
  • Microsoft Visual Studio C++
    • You only need it for mex-file compilation, so you can download that for free here
    • Make sure to choose the right platform and the right version of Matlab and also make sure to check all the footnotes. For example, I downloaded Visual C++ 2010 Express version (the full version wasn't free) for 64-bit Windows which also required:


Set-up

Once everything is downloaded and installed, do the following:

  • put the nvmex.pl file from the Matlab plugin bin folder (Matlab_CUDA_1.1\bin) into the bin folder of the actual Matlab directory.
  • make sure to set the compiler to Visual Studio by typing "mex -setup" in the Matlab command line and choosing the Microsoft Visual C++ compiler option.

Compile and Test the CUFFT interface

(partly taken from the README.txt file in the Matlab plugin folder)

Notes:

  • any place that says C:\CUDA needs to be placed with the actual file path to your CUDA folder. For example, for my particular installation, I would replace the line:
    • >> nvmex fft2_cuda.c -IC:\CUDA\include -LC:\CUDA\lib -lcudart -lcufft
    • with
    • >> mex fft2_cuda.c -I"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v3.2\include" -L"C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v3.2\lib\x64" -lcudart -lcufft

with


>> dir

Nick's notes

I used Visual Studio Express 9.0, Visual C++ (free) Microsoft SDK 7 with .NET Framework 3.5 SP1 Details: [1] (Apparently nvcc doesn't like VS10; as of the time I write this NVidia has been promising VS10 support for 10 months or so without any results. Some people report fooling nvcc into running anyway, but I decided that using VS9 was the path of lesser resistance. it doesn't make any difference to me anyway since all I want to use is the compiler.)

NVidia drivers, CUDA toolkit, and GPU SDK from [2]

Add the following to your Windows PATH variable: C:\ProgramData\NVIDIA Corporation\NVIDIA GPU Computing SDK 3.2\C\common\bin;C:\Program Files\MATLAB\R2010b\bin\win64;C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin\amd64

For some reason, nvcc doesn't look in the right places when compiling for 64-bit architecture. Find this file:

C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin\vcvars64.bat

Make two copies of it, one in the same directory, and one in amd64 subdirectory:

C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin\vcvars64.bat
C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin\amd64\vcvars64.bat

Sources: [3] and [4]

Edit nvcc.profile (Located at C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v3.2\bin\nvcc.profile) to add some include directories that will be needed. The INCLUDES line should read as follows:

INCLUDES += "-I$(TOP)/include" "-I$(TOP)/include/cudart" "-IC:/Program Files (x86)/Microsoft Visual Studio 9.0/VC/include" $(_SPACE_)

Source: [5]

Now to compile a CUDA file for Matlab, first convert the CUDA to ordinary C++. [6] Run the command prompt as administrator, and compile as follows:

nvcc --cuda myprog.cu --output-file myprog.cpp

At last, you can compile the .cpp file using mex as usual, except that you have to link to the CUDA libraries:

mex -LC:\\Program Files\\NVIDIA GPU Computing Toolkit\\CUDA\\v3.2\\lib\\x64 -lcufft -lcudart myprog.cpp

Now you should have a .mex64 that runs on the GPU via CUDA!

Notes on reinstall Oct. 2011

  • Install CUDA
  • Install VC Express 2010
  • Install Windows SDK 7.1 -- make sure to check the box for x64 compiler
  • If necessary, install patch to replace x64 compiler [7]
  • Copy vcvars32.bat from VC/bin directory to VC/bin/amd64 and rename to vcvars64.bat

New install Aug. 2012

Big trouble getting microsoft windows SDK to install. Problem with the installer failing because of newer version of VC 2010 redistributable?

-- Resolved via MS bugfix

Install June 2014

Using CUDA 6 and VS 2012 Express. Keep getting the error "nvcc : fatal error : nvcc cannot find a supported version of Microsoft Visual Studio. Only the versions 2008, 2010, and 2012 are supported"

Not resolved.

Install July 2014

Installed VS 2012 Express first. CUDA installer detects whether VS is installed or not.

Now I can get CUDA to work, but Matlab R2014a doesn't like VS2012 Express; wants SDK 7.1, which is an earlier version of VS. So the linker fails.  :-(

Install August 2014

Fell back to CUDA 4.0 and VS 2010 Express, with SDK7.1. This is the combination that has worked in the past, and my code was written for it anyway. Seems to work; managed to compile on the new machine in 343. Installing the SDK on top of the ITS image required the Microsoft Fixit tool to remove the VS 2010 Redistributables (http://support.microsoft.com/mats/program_install_and_uninstall)

On the newer desktop machine (T7600) in my office I had some difficulty with nvcc asking for VS9 (i.e., 2008). I nstalled it, which got Matlab confused thinking it was dealing with legacy compilers, but mex-setup switched it back to the SDK compiler and everything now seems to work.

Install June 2015

Trying to set up for my GTX780 rig. CUDA 7.0 is out now; maybe that will help.