6

No matter how I reinstall the CUDA driver and toolkit, when typing gpuDevice(), it always show s:

CUDADevice with properties:

                      Name: 'Quadro K2000M'
                     Index: 1
         ComputeCapability: '3.0'
            SupportsDouble: 1
             DriverVersion: 6.5000
            ToolkitVersion: 5.5000
        MaxThreadsPerBlock: 1024
          MaxShmemPerBlock: 49152
        MaxThreadBlockSize: [1024 1024 64]
               MaxGridSize: [2.1475e+09 65535 65535]
                 SIMDWidth: 32
               TotalMemory: 2.1475e+09
                FreeMemory: 2.0431e+09
       MultiprocessorCount: 2
              ClockRateKHz: 745000
               ComputeMode: 'Default'
      GPUOverlapsTransfers: 1
    KernelExecutionTimeout: 0
          CanMapHostMemory: 1
           DeviceSupported: 1
            DeviceSelected: 1

which I don't understand. Why the toolkit version is always 5.5? Can I upgrade it to 6.5?

Ono
  • 1,357
  • 3
  • 16
  • 38
  • CUDA toolkit is a version the program was compiled against so if you have a program which was compiled against 5.5 then it's cuda toolkit version is 5.5. The only way to 'upgrade' is to ask MATLAB to use newer one/update matlab (note that it's similar to how msvcrt or many other libraries behaves - user cannot 'update' program from msvcrt80 to msvcrt90 but both runtimes can and do coexist on single system so you can run program using msvcrt80 alongside program using msvcrt90). – Maciej Piechotka Aug 05 '14 at 14:08
  • OK. So if I have 6.5 installed, and I want to compile mex file containig cuda code, would that be a problem? Because it seems that I have to have 5.5 installed to make it compile? – Ono Aug 05 '14 at 14:11
  • I don't think so. You can also take a look at [this](http://www.orangeowlsolutions.com/?s=mex) post. – Vitality Aug 05 '14 at 16:28
  • according to the mathworks [example](http://www.mathworks.com/help/distcomp/run-mex-functions-containing-cuda-code.html) "You must use the version of the NVIDIA compiler (nvcc) consistent with the ToolkitVersion property of the GPUDevice object." – Robert Crovella Aug 05 '14 at 20:18

2 Answers2

8

As @Robert mentioned, you have to use the same cuda version but not necessarily if you use simple trick (I'm using CUDA 6.0 and MATLAB CUDA version is 5.0). To make it work, you do not need the complicated procedure, nor the mex for compiling all .cu files and copying the xml file ( as in Link) to the directory to compile. Type simply the following two lines in the matlab command,

!nvcc -O3 -DNDEBUG -c mexGPUExample.cu -Xcompiler -fPIC -I/MATLAB_ROOT/extern/include -I/MATLAB_ROOT/toolbox/distcomp/gpu/extern/include;
mex mexGPUExample.o -L/usr/local/cuda-6.0/lib64 -L/MATLAB_ROOT/bin/glnxa64 -lcudart -lcufft -lmwgpu

Then it will magically work even if your ToolkitVersion mismatches. (Change /MATLAB_ROOT to your matlab root path)


Why MATLAB CUDA Toolkit version is different from System CUDA version?

Regarding your question, the installed CUDA version is not the same CUDA that MATLAB use.

If you go to

/matlabroot/bin/maci64  (OS X)
/matlabroot/bin/glnxa64 (unix variant)

depending on your os, you can see the [dynamic linking library, shared library]

libcudart.5.5.[dylib, so]
libcublas.5.5.[dylib, so]
libcufft.5.5.[dylib, so]

These are the libraries that MATLAB uses. To make matlab to use system libraries, follow the instructions below. (MAC only)

In sum,

  1. The installed cuda version is different from MATLAB cuda since they have their own library
  2. To trick it to load the new library, you might need to use install_name_tool to change the library link
  3. Anyway you don't need it to have same version.

EDIT : How to make MATLAB to use System CUDA Library (OS X)

Make MATLAB to use System CUDA library, The default MATLAB CUDA library version is 5.5 and if you want to use the up-to-date library, read the following

  1. Go to /Applications/MATLAB_R2014a.app/bin/maci64(MAC) or MATLAB_ROOT/bin/glxna64(LINUX)

  2. See the library dependencies of libmwgpu.[dylib, so] this is the entry library that is loaded when you use CUDA

    The result would look like

    dnab404675:maci64 user$ otool -L libmwgpu.dylib

libmwgpu.dylib: @rpath/libmwgpu.dylib (compatibility version 0.0.0, current version 0.0.0)

.... Some Libraries

@rpath/libcublas.5.5.dylib (compatibility version 5.5.0, current version 5.5.20) @rpath/libcudart.5.5.dylib (compatibility version 5.5.0, current version 5.5.20) @rpath/libcufft.5.5.dylib (compatibility version 5.5.0, current version 5.5.20)

... and more

Our goal is to modify the library dependency of `cublas`, `cudart`, `cufft` to 

>    /usr/local/cuda/lib/libcublas.dylib (compatibility version 5.5.0, current version 5.5.20)

/usr/local/cuda/lib/libcudart.dylib (compatibility version 5.5.0, current version 5.5.20) /usr/local/cuda/lib/libcufft.dylib (compatibility version 5.5.0, current version 5.5.20)

Note that if you type gpuDevice, it will still show it as toolkit version 5. But it loads the new version. So how we do that?
  1. Simply type

    sudo install_name_tool -change @rpath/libcufft.5.5.dylib /usr/local/cuda/lib/libcufft.dylib libmwgpu.dylib

    sudo install_name_tool -change @rpath/libcudart.5.5.dylib /usr/local/cuda/lib/libcudart.dylib libmwgpu.dylib

    sudo install_name_tool -change @rpath/libcublas.5.5.dylib /usr/local/cuda/lib/libcublas.dylib libmwgpu.dylib

I still don't know how to change the shared library path in Linux. Probably have to use hexadecimal editor such as HT From Stackoverflow Answer

miken32
  • 42,008
  • 16
  • 111
  • 154
VforVitamin
  • 946
  • 10
  • 12
  • Thank you so much for your explanation. It seems the best way is to also install CUDA 5.5 such that it will not further complicate the situation. – Ono Aug 08 '14 at 20:01
  • Sure, but currently I'm using cuda 6.0 and the MATLAB cuda version is 5.0 but it works perfectly. All you need to do is type in the matlab command, `!nvcc ...` and `mex ...` that I explained in the first part. – VforVitamin Aug 08 '14 at 22:16
  • On Linux, maybe the LD_PRELOAD trick may work, e.g. starting matlab with `LD_PRELOAD=libcufft.so:libcudart.so:libcublas.so matlab` (changing to correct paths of system CUDA libs) usually forces the linker to use system libraries with matlab. But it's a question whether Matlab's GPU support would work with newer CUDA... – Karel Lenc Dec 12 '14 at 17:10
0

You can also use CUDA 6.5 with matlab under Windows. The tricky part is, you need to compile the mex files under Visual Studio rather than inside matlab. There are numerous tutorials introducing how to compile mex under VS so there is no need to repeat here. You need only to create a NVIDIA cuda project with .cu as the source, and follow the standard procedures to compile mex.

Fontaine007
  • 577
  • 2
  • 8
  • 18