06.02.06

Improve, Optimize and Speed up Matlab

Posted in Matlab at 9:04 pm by keith

If you run a copy of MATLAB, MathWorks Inc. has a set of optimised BLAS (Basic Linear Algebra Subprograms) instructions to be used in the software.

MATLAB uses Basic Linear Algebra Subprograms for its vector inner product, matrix-vector product, matrix-matrix product, and triangular solvers involving “\” commands. MATLAB also uses BLAS behind its core numerical linear algebra routines from Linear Algebra Package (LAPACK), which are used in functions like chol, lu, qr, and within the linear system solver with “\”.


I have been testing out several methods myself by changing the BLAS files so that it is best optimised for the machine which I am using. One of the most popular BLAS provider for MATLAB 6.5 is programmed by Kevin Sheppard. He has provided a very useful set of instructions to create your own ATLAS DLL file for MATLAB in Windows platform.

So far, I have tried creating my personalised ATLAS DLL file for BLAS using the scripts provided by Kevin. With the flexibility and availability of the source codes, things could not have been any easier. Unfortunately, the outcome from the optimised BLAS isn’t really up to performance. In fact, by comparing the DLL file generated by myself using the script, with the Precompiled Binaries ATLAS DLL, the latter is much better. Well, the only conclusion I can draw from this observation is that it varies from system to system. From then, I would prefer to use the Precompiled Binaries provided instead of compiling it myself, without actually understanding the script. Furthermore, it is a bit of work to compile yourself. However, if you do have the time, do give it a try.

Unfortunately, Kevin stopped releasing DLL binaries for MATLAB, the last one at MATLAB 6.5.1. One of the main reasons is that the next generation of MATLAB, after MATLAB 6.x, is MATLAB 7 (R14) which comes with BLAS file consisting of the Intel Math Kernel Library. Intel Math Kernel Library has been known to perform really fast for LAPACK and BLAS, particularly linear algebra operations. Realising that ability, Mathworks Inc. must have harnassed their Kernel Library to create a faster MATLAB application.

Intel Math Kernel Library for Windows is not free, however Linux users can smile this time, while able to obtain a non-commercial version of Intel Math Kernel Library for Linux (non-commercial). However, Windows users need to be too unhappy about it as long as you can get an evaluation copy of Intel Math Kernel Library. I will explain why later.

When I used Intel Math Kernel Library in MATLAB 6.x and MATLAB 7 (R14), I personally feel that it performed better than the provided default ATLAS file. On the other hand, the latest release of MATLAB 2006a has not much of a difference. In some way, Mathworks Inc. must have incorporated the Math Kernel Library well optimised and embedded into the MATLAB application. So, if you are using MATLAB 7 (R14) and older, Intel Math Kernel Library would be pretty useful to help speed up your linear algebra operations.

To utilise the ATLAS file from the Intel Math Kernel Library, requires only a directory containing some files from the entire package. In fact, users need not actually install Intel Math Kernel Library on their system, the correct files can be extracted (indirectly) from the zipped package. There are two ways to obtain the BLAS files from Intel installer, which I will describe below:

Method 1: Quick and Easy
This method requires you to install the Intel Math Kernel Library first. Though the package is not free, users are allowed to download the trial package to test the file.

  1. Run the (trial) Installer and install Intel Math Kernel Library (MKL) in your computer.
  2. Depending where you place your default directory for MKL, open a Windows Explorer and navigate to the directory, let’s assume C:\mkl60
  3. Copy either one of the following folder:
    • If you are using 32-bit computing machine, copy the following folder: C:\mkl60\ia32\bin.
    • If you are using 64-bit system, copy the following folder: C:\mkl60\ia64\bin
  4. From above, copy the contents inside the folder into the MATLAB\bin directory, i.e. C:\MATLAB6p5p1\bin\win32 (for windows)
  5. Next, override the default BLAS Library by setting the environment variable BLAS_VERSION to the name of the MLK Library, mkl*.dll.
    • Click the Start button, go to the Settings menu, and select Control Panel.
    • On the Control Panel menu, select System.
    • In the System Properties dialog box, click the Advanced tab.
    • On the Advanced panel, click the Environment Variables button.
    • In the Environment Variables dialog box, click the New button in the User variables section.
    • In the New User Variable dialog box, enter the name of the variable as BLAS_VERSION and set the value of the variable to the name of the MKL library: mkl.dll.
  6. Set the environment variable LAPACK_VERBOSITY to 1.
  7. Once done, start your MATLAB, you should be running on your Intel Math Kernel Library for your BLAS.

Method 2: Tedious but Clean
This will require you to obtain a copy of WinRaR to open and extract the Intel Math Kernel Library installer file.

  1. Download a copy of WinRaR, following the link above.
  2. Use WinRar and extract all the files inside MKL installer.
  3. You will notice a .cab file after the extraction. Use WinRar again to extract this file to a temporary directory, say C:\mkl_temp
  4. There will be over 1,000 of files inside the directory, depending on your machine architecture, you will only require a couple of essential files.
    • For i32 machines: extract the following files:
      - mkl_lapack32.dll
      - mkl_lapack64.dll
      - mkl_def.dll
      - mkl_p3.dll
      - mkl_p4.dll
      - mkl_vml_def.dll
      - mkl_vml_p3.dll
      - mkl_vml_p4.dll
      - libguide40.dll
    • For 64-bit machines: extract the following files:
      - mkl_lapack32.dll
      - mkl_lapack64.dll
      - mkl_itp.dll
      - mkl_vml_itp.dll
      - mkl_i2p.dll
      - mkl_vml_i2p.dll
      - libguide40.dll
  5. From above, copy the files into the MATLAB\bin directory, i.e. C:\MATLAB6p5p1\bin\win32 (for windows)
  6. Next, override the default BLAS Library by setting the environment variable BLAS_VERSION to the name of the MLK Library, mkl.dll.
    • Click the Start button, go to the Settings menu, and select Control Panel.
    • On the Control Panel menu, select System.
    • In the System Properties dialog box, click the Advanced tab.
    • On the Advanced panel, click the Environment Variables button.
    • In the Environment Variables dialog box, click the New button in the User variables section.
    • In the New User Variable dialog box, enter the name of the variable as BLAS_VERSION and set the value of the variable to the name of the MKL library: mkl*.dll.
  7. Set the environment variable LAPACK_VERBOSITY to 1.
  8. Once done, start your MATLAB, you should be running on your Intel Math Kernel Library for your BLAS.

While choosing your correct DLL file in your environment variable, the following directory list may be helpful for you.

mkl60\ia32: Contains all libraries for 32-bit applications
mkl60\ia32\bin: Contains DLLs for 32-bit applications

mkl_lapack32.dll: LAPACK routines and drivers, single precision data types
mkl_lapack64.dll: LAPACK routines and drivers, double precision data types
mkl_def.dll: default kernel (Intel® Pentium®, Pentium® Pro, and Pentium® II processors)
mkl_p3.dll: Intel® Pentium® III processor kernel
mkl_p4.dll: Pentium 4 processor kernel
mkl_vml_def.dll: VML part of default kernel (Pentium, Pentium Pro, Pentium II processors)
mkl_vml_p3.dll: VML part of Pentium III processor kernel
mkl_vml_p4.dll: VML part of Pentium 4 processor kernel
libguide40.dll: Threading library

mkl60\ia32\lib: Contains static libraries and static interfaces to DLLs for 32-bit applications

mkl_c.lib: cdecl interface library
mkl_s.lib: CVF default interface library
mkl_c_dll.lib: cdecl interface library for dynamic library
mkl_s_dll.lib: CVF default interface library for dynamic library
mkl_lapack.lib: LAPACK routines and drivers
mkl_ia32.lib: Optimized kernels for Intel® Pentium®, Pentium® III, and Pentium® 4 processors
libguide.lib: Threading library

mkl60\ia64: Contains all libraries for Itanium®-based and Itanium® 2-based applications
mkl60\ia64\bin: Contains DLLs for Itanium®-based and Itanium® 2-based applications

mkl_lapack32.dll: LAPACK routines and drivers, single precision data types
mkl_lapack64.dll: LAPACK routines and drivers, double precision data types
mkl_itp.dll: Itanium processor kernel
mkl_vml_itp.dll: VML part of Itanium processor kernel
mkl_i2p.dll: Itanium 2 processor kernel
mkl_vml_i2p.dll: VML part of Itanium 2 processor kernel
libguide40.dll: Threading library

mkl60\ia64\lib: Contains static libraries and static interfaces to DLLs for Itanium®-based and Itanium® 2-based applications

mkl_lapack.lib: LAPACK routines and drivers
mkl_ipf.lib: Processor kernels for the Intel® Itanium® processor family which includes both the Intel® Itanium and Itanium® 2 processors
mkl_dll.lib: Interface library to DLL
libguide.lib: Threading library

With these simple techniques, you can speed up your MATLAB operations up to twice as fast, some even much faster. Note that, Intel Math Kernel Library is machine architecture-dependent. If you are using AMD machine, it may not guarantee to work as expected, since MKL, after all, is an Intel product.

Also, don’t forget to digg this on the official digg site.

Leave a Comment