How to install CUDA 7.0 on Ubuntu 14.04 LTS for nVidia Quadro K4200 & GT 610

It is not an easy task to install nVidia graphic card driver and CUDA toolkit on Ubuntu Linux system. The CUDA package is shipped with its driver, which seems to cause lots of trouble after replacing the default Linux driver. I installed the CUDA toolkit 7.0 by following nVidia’s official website. However, it ended up being an infinite login loop error. I struggled for almost two days to figure out how to do it nicely and properly. There are multiple tutorials and guidelines available on the internet. Still, most of them didn’t work for my case, and it caused me lots of trouble rebooting and reinstalling various packages and kernels. Finally, here is one solution for me. ps: I have a GT 610 for display, Quadro K4200 for CUDA computing.

 

0) Download relevant CUDA.run file: mine was: cuda_7.0.28_linux.run (cuda_7.5.18_linux.run has a hard time finding my kernel source file, I don’t know why)
Also, run:

$sudo apt-get remove --purge nvidia-* && apt-get autoremove
$sudo apt-get install build-essential

1) Start with the regular GUI and Ubuntu working with no login problems.
2) No need to create a xorg.conf file. If you have one, remove it (assuming you have a fresh OS install).

$sudo rm /etc/X11/xorg.conf

3) Create the /etc/modprobe.d/blacklist-nouveau.conf file with :

blacklist nouveau options nouveau modeset=0 

Then

$sudo update-initramfs -u

4) Reboot the computer. Nothing should have changed in the loading-up menu. You should be taken to the login screen. Once there, type: Ctrl + Alt + F1 and log in your user.
5) Go to the directory where you have the CUDA driver, and run

$chmod +x cuda_7.0.28_linux.run

7) Now, run

$sudo service lightdm stop

The top line is a necessary step for installing the driver. It turns off the X window and prevents driver conflicts.
8) Run the CUDA driver run file. Notice to turn OpenGL flags off when install (IMPORTANT):

$sudo bash cuda-7.0.28_linux.run --no-opengl-libs

9) During the install:
Accept EULA conditions
Say YES to installing the NVIDIA driver
Say YES to installing CUDA Toolkit + Driver
Say YES to installing CUDA Samples

10) Installation should be complete. Now check if device nodes are present:
Check if /dev/nvidia* files exist. If they don’t, do :

$sudo modprobe nvidia

11) Set Environment path variables:

$export PATH=/usr/local/cuda-7.0/bin:$PATH
$export LD_LIBRARY_PATH=/usr/local/cuda-7.0/lib64:$LD_LIBRARY_PATH

12) Verify the driver version:

$cat /proc/driver/nvidia/version

13) Check CUDA driver version:

$nvcc -V

[Optional] At this point, you can switch the lightdm back on again by doing:

$sudo service lightdm start

You should be able to log in to your session through the GUI without any problems or login-loops.
14) Create CUDA Samples. Go to your NVIDIA_CUDA-7.0_Samples folder and type

$make

15) Go to NVIDIA_CUDA-7.0_Samples/bin/x86_64/linux/release/ for the demos, and do the two standard checks:

$./deviceQuery

to see your graphics card specs and

$./bandwidthTest

to check if it is operating correctly.

Both tests should ultimately output a ‘PASS’ in your terminal.

16) Reboot. Everything should be ok.

How to create a Mex project using Linux Eclipse

This method for creating Matlab Mex executable file has been tested using Matlab R2010b & Eclipse 3.7.1 & 64 bit Fedora 16 Linux system.

0. Compile Mex in Matlab command prompt

For your information, C/C++ Mex file can be compiled in Matlab command prompt using ‘mex’, for instance,

mex –g –v GNUMex.c

The OpenMP support can be turned on by adding compiling and linker flag ‘-fopenmp’, such as

mex –g -v GNUMexOpenMP.c CFLAGS="\$CFLAGS -fopenmp" LDFLAGS="\$LDFLAGS -fopenmp"

or

mex –g -v GNUMexOpenMP.cpp CXXFLAGS="\$CXXFLAGS -fopenmp" LDCXXFLAGS="\$LDCXXFLAGS -fopenmp"

 1. Creation of an empty project

  • Create a simple C or C++ project under Eclipse (File / New/ C Project), name it as you wish

  • Choose Empty Project under Shared Library, follow project wizard until an empty C or C++ project is created

2. Configuration of Compiler

Now we need to define some important properties of the project to ensure that the output file is fully compatible with Matlab. Open project properties (Right click on the project name / Properties). Start with the “ C/C++ Build” tab

  • Under “Settings/Tool Settings/Cross GCC Compiler/Symbols,” add ‘MATLAB_MEX_FILE’ as a defined symbols

  • Under “Settings/Tool Settings/Cross GCC Compiler/Includes” add Matlab include folder which is usually /home/Username/MATLAB/R2010b/extern/include

  • Under “Settings/Tool Settings/Cross GCC Linker/Libraries” add Matlab library folder which is usually /home/Username/MATLAB/R2010b/bin/glnxa64 for 64bit Linux system, this path may vary according to your system, however, do not use /home/Username/MATLAB/R2010b/extern/lib

  • Under “Settings/Tool Settings/Cross GCC Linker/Libraries” also add Matlab library file libmat.so, libmx.so and libmex.so into the Libraries section since many default Mex functions are linked to these libraries. Note here mat, mx, and mex are added, this will be fine otherwise, a missing function error will appear.

  • We also need to configure the output file, since the default output file extension is not compatible with Matlab. Under “Settings/Build Artifact” change Artifact extension as ‘mexa64’ for 64bit Linux or ‘mexa32’ for 32bit, Output prefix can be left as blank.

3. Configuration for OpenMP

If OpenMP is used in the source code, this step is necessary, otherwise ignore this part.

  • Open features for OpenMP by adding ‘-fopenmp’ under ‘Command’ boxes for both ‘Cross GCC Compiler’ and ‘Cross GCC Linker’ (not shown here).

  • Also, check the ‘-fPIC’ flag for successful compiling, this option is under ‘Cross GCC Compiler/Miscellaneous’.

4. mexFunction

Add a main C/C++ file that contains the body of “mexFunction” with the following code snippet ( do not forget to include the “mex.h” header file to give access to the prototype of the mexFunction). Of course, any needed header files have to be put at the front of the source file, such as

#include "mex.h"
#include <stdio.h>
#include <omp.h>
void mexFunction(int nlhs,mxArray* plhs[], int nrhs, const mxArray* prhs[])
{
   int count;
   int th_id;
   #pragma omp parallel for private(count, th_id) num_threads(2)
   for( count = 0; count < 60; count++ )
   {
      th_id = omp_get_thread_num();
      printf("Hello World from thread %d, %d\n", th_id, count);
   }
   printf( "Finished\n" );
}

5. Building

Now, if you build the project (Right click / “Build”), you will get a mexw32 file (or mexw64) which can be executed in Matlab.

6. Conclusion

After all the steps, you will be able to compile your Mex file in Eclipse, as I did. Some online tutorials require adding a module-definition file (.def) and ‘mexversion.rc’. However, Matlab removed ‘mexversion.rc’ in recent releases (?). I have tested compiling mex file under Visual Studio 2008 without this file, and it turned out just fine. Unsurprisingly it also turned out fine for compiling Mex without this file under linux.  I also tried to add .def file, as mentioned online, But I always got the error “unrecognized option –Wl, --output-def = XXX”. After trying solutions for hours, I finally decided to drop this file, and it worked just fine. In conclusion, I would recommend compiling using the default Matlab Mex script for a simple Mex project. So take your own risk to compile this way and enjoy the power of Mex.

Also, check "How to create a Mex project using Visual Studio 2008" for compiling Mex in Windows.