Showing posts with label Blas. Show all posts
Showing posts with label Blas. Show all posts

Tuesday, July 30, 2013

How to link Intel MKL lapack

This article summarizes how to link existing C++ code with Intel MKL LAPACK without modifying a single line of the original source code that was written with standard LAPACK libraries and compiled by g++.

Intel MKL comprises so many things and for my purpose. In my case, I only want to know if MKL can provide better performance compared to GotoBLAS in my application. So I don't want to change any part of my code and just want to replace GotoBLAS with MKL. This article is written for pure newbies to Intel MKL and is not intended for learning MKL properly.

The task of replacing LAPACK with MKL turns out to be very simple: I used to link my code with GotoBLAS by flag -lgoto2 or standard LAPACK by -llapack. For MKL, this website helps a lot:

http://software.intel.com/en-us/articles/intel-mkl-link-line-advisor

After filling my platform information (GNU C/C++ and libgomp), the following flags are suggested:

 -L$MKLROOT/lib/intel64 -lmkl_rt -ldl -lpthread -lm

I replace -libgoto2 with the above flags, recompile my code, and then my code runs smoothly with MKL (and much faster than GotoBLAS for multithreaded cases but slower when single threaded).

This article here summarizes how to control the threads that MKL for the LAPACK part

http://software.intel.com/sites/products/documentation/hpc/mkl/lin/MKL_UG_managing_performance/Using_the_Intel_MKL_Parallelism.htm

In short, MKL automatically uses the number of physical cores as default number of threads. Hyperthreading is not considered as very little juice can be squeezed out when highly optimized and efficient code such MKL is used. If you want to control the number of threads, several global variables can be used and described clearly here:

http://software.intel.com/sites/products/documentation/hpc/mkl/lin/MKL_UG_managing_performance/Using_Additional_Threading_Control.htm

The global variables are independent from OpenMP counterparts. In my case, for example, when I want to use two threads only to solver my linear system Ax=b, I enter the following command in bash:

export MKL_NUM_THREADS=2

before I run my executable. Different performance being observed before and after setting up the global variable confirms the success of my attempt.







Saturday, June 5, 2010

Install and Link GotoBlas in Eclipse CDT

What is BLAS?
BLAS is the acronym of "Basic Linear Algebra Subprograms." It computes Level 1 (vector-vector), Level 2 (matrix-vector) and Level 3 (matrix-matrix) BLAS operations which are the basis of higher level operations such as linear system solve, least-squared solve, eigen-value decomposition and QR decomposition which are computed in Lapack. The debut of BLAS dates back to 1979 [Lawson1979] written in Fortran and later many higher level packages such as Eispack, Linpack (around 197x-198x) and Lapack (since 1992) built on top of it. Lapack (in Fortran as well) gradually becomes a success and widely used in many programs although many competitors showed up later aiming at its infamous interface (mostly due to the deficiency of Fortran), but Lapack's position in numerical computation seems never shaken by its competitors. Since BLAS is the core of Lapack, BLAS becomes an inevitably important component. (Note 1) Now the term "BLAS" is more like a concept than a specific interface or an implementation: any library which provides Level 1 to Level 3 BLAS operations can be called as a BLAS. For example, Boost::uBlas is a pure C++ implementation of Level 1 to Level 3 BLAS not with a different interface. For backward compatibility, only the one with an "exact" BLAS interface, or a "BLAS implementation" is compatible with Lapack hence interesting. Others implemented with "BLAS functions"  (e.g. uBlas) rather than a "BLAS interface" may grow up as an inevitable library only when an important higher level library relies on it, just as the relationship between Lapack and Blas. (A new and interesting Lapack competitor is Flame, which is also based on BLAS.)

What is GotoBlas?
GotoBlas2 is a Fortran and assembly code implementation of Blas (interface). It is considered the fastest (or one of the few fastest) Blas libraries on the earth. The original developer is Kazushige Goto, who was a research associate in University of Texas at Austin [1]. GotoBlas is manually optimized by writing architecture specific assembly code. Its performance is usually compared with another very fast Blas library: ATLAS. Contrast to the manual optimization strategy of GotoBlas, ATLAS automatically executes several subroutines during its installation on a single machine in order to find the best parameters (e.g. cache sizes). Some experiments report that GotoBlas outperforms ATLAS, for example in [2] and [3].

Why GotoBlas?
  1. GotoBlas is very fast as mentioned before. If speed is your first priority
  2. It supports multi-threads.  
  3. It is available in two licenses: commercial license and open source license. Re-distribution is not allowed but you can always download it here.
  4. Lapack is a widely-acceptable dependency library that has been a "must" for many machines for the numerical computation purpose. Even when your software users don't prefer installing a new library, they can still evaluate your code by using the original Lapack and Blas libraries on their machines.
  5. Some Lapack functions are built along with GotoBlas, such as some driver routines: LU linear system solve (xGESV), the inverse of a positive definite matrix (xPOTRI),  some computational routines: LU decomposition (xGETRF) and Cholesky factorization (xPOTRF), ans some auxiliary routines. If all you need are to solve a linear system and to do some BLAS operations, then installing GotoBlas suffices.
  6. The installation is very easy.
How to install GotoBlas?
Very easy. Simply download it, decompress it, make it and move it to the preferred folder. The detailed steps are in the following:
  1. Here I use GotoBLAS2-1.13 as an example.
  2. Install a Fortran compiler if there is none in your computer (if it is in ubuntu)
    >> sudo apt-get install gfortran
  3. Download it here.
  4. >> tar -xvf GotoBLAS2-1.13.tar.gz
  5. >> cd GotoBLAS2
  6. Edit Makefile.rule. Follow the comment instructions.
  7. >> make
  8. The command "make" will compile the source code according to the rule you wrote in Makefile.rule. It will generate four files in the same folder:
    libgoto2.a
    libgoto2_architecture.13.a
    libgoto2.so
    libgoto2_architecture.13.so
    The first one is the symbolic link to the second one because a general compiler usually searches for a name like it instead of the second one. The second one is the static library itself. architecture changes according to your computer. The third and the last one are the dynamic counterpart of the first two. See here for the knowledge of .a and .so libraries.
  9. If all you need is the static library. Simply move libgoto2_architecture.13.a to /usr/lib and create a symbolic link to that:
    >> sudo ln -s libgoto2_architecture.13.a libgoto2.a
    Then you are done.
  10. If you need the dynamic link library, then you need some extra steps. The older version of GotoBlas doesn't compile a dynamic link library by default. But even if this 1.13 version provides .so library, it seems not working properly. Instead, we can still build a dynamic library ourselves:
    >> mkdir temp

    >> cp libgoto2_architecture-r1.13.a temp
    >> cd temp
    >> ar x libgoto2_architecture-r1.13.a
    >> gfortran -shared -lpthread -Wl,-soname,libgoto2.so -o libgoto2.so.1.13 *.o

    The meaning of the last command:
    1. gfortran: use gfortran to compile
    2. -shared: create a shared object
    3. -lpthread: use the pthread library. No harm to keep it there if your GotoBLAS is single threaded.
    4. -Wl: the options separated by comma after -Wl will be passed to the linker, ld in gcc.
    5. -soname,libgoto2.so: passed to ld. It specifies the internal name libgoto2.so of the library which will be used and linked by other programs accordingly. See here for more ideas about ld.
    6. -o libgoto2.so.1.13: the file name after compilation
    7. *.o: all .o files will be compiled
  11. Move libgoto2.so.1.13 to the folder you prefer, usually /usr/lib, and create a symbolic link to that
    >> sudo ln -s libgoto2.so.1.13 libgoto2.so 
  12. And tell the linker ld that you add a new dynamic library by entering
    >> sudo ldconfig
    So the linker can find it when a program requests to link the new library. Then you are done.
How to link GotoBlas in gcc / gfortran / g++?
Simple. Just add a library option when you compile. For example in g++,
>> g++ -lgoto2 -o yourprogram yourcode.cpp
Notice that -lgoto2 will be interpreted as linking a library called libgoto2.a or libgoto2.so, so don't put both of them in the library search path (-L) or in /usr/lib altogether. That'll be confusing.

How to link GotoBlas in Eclipse
Let's go back to the title. That will be very easy again.
  1. Create a HelloWorld project.
  2. In Project Explorer, right click on your project name and choose Properties
  3. Go to C/C++ Build -> Settings
  4. In Tool Settings tab, go to GCC C++ Linker -> Libraries 
  5. In Libraries (-l), add "goto2"
  6. In Library search path (-L), add the path of libgoto2 if it is not in /usr/lib
  7. Then you are done!

Note 1:
We are never able to say a library is the best. A library lasts for many reasons: performance, interface, productivity, available licenses, dependencies, backward compatibility, etc, especially backward compatibility. A new library must provide other way better features such as way faster computation or way more efficient interfaces in order to persuade a software engineer to adopt at the risk of lower publicity because the new library may not be available or has some compatible problems on some machines, or the license may limit the use of the library, so as the software. A famous example of the license issue is about Qt. Before the Qt development company Trolltech was purchased by Nokia, the Qt library only provides GPL and proprietary licenses. The former says the source code should be open as the library you use is. The latter requires very high licensing fee. These two options limit the acceptance of Qt. Now Qt has a new option: LGPL, which says the use of the library is free if it is dynamically linked and the library can be distributed along with the software.  Lapack doesn't have this issue and is free available here.

Saturday, December 26, 2009

Ubuntu 9.04, 9.10, 10.04 Installation Tweaks

MATLAB
  • JRE 
    • sudo apt-get install sun-java6-jre

  • VPN 
    • sudo aptitude install network-manager-vpnc 
    • Panel --> VPN connections --> Configure VPN --> Import the .pcf file
    • IPv4 Tab --> click Routes --> uncheck "Use this connection only..."
    • click OK, Apply, Close
    • Reboot
    • Panel --> VPN connections --> MITnet VPN Connection
  • MATLAB
    • tar -xvf thefile 
    • cd thefolder
    • sudo ./install...sh

    Eclipse
    • download: http://www.eclipse.org/downloads/download.php?file=/technology/epp/downloads/release/galileo/SR1/eclipse-cpp-galileo-SR1-linux-gtk.tar.gz
    • tar -xvf thefile

    Microsoft Office
    • WineHQ 
      • wget -q http://wine.budgetdedicated.com/apt/387EE263.gpg -O- | sudo apt-key add -
      • sudo wget http://wine.budgetdedicated.com/apt/sources.list.d/jaunty.list -O /etc/apt/sources.list.d/winehq.list
      • sudo apt-get update
      • sudo apt-get install wine
      • (some steps may be redundant. need further checks.)

    • WineHQ (for 10.04)
      • Just go to Ubuntu Software Center. Search for it and install it.

    • Windows Programs
      • Right click on "setup.exe --> Open with "Wine Windows Program Loader"
      Latex
      • TexLive 
        • sudo apt-get install texlive (~200 MB) or
        • sudo apt-get install texlive-full (~700 MB)

      • TexMaker
        An Latex IDE with fewer KDE library dependencies
        Pros: smaller (~200 MB), looks nice, intuitive configuration
        Cons: not very good auto completion, no block indent 
        • sudo apt-get install texmaker
      • Kile
        An Latex IDE with huge KDE library dependencies
        Pros: looks nicer (much better than the screenshots in its official website) and many utilities
        Cons: fat (~500 MB)
        • sudo apt-get install kile

        Input Method (also works for 9.10 and 10.04)
        • noseeing 
          • sudo apt-get install gcin 
          • set up default input method manager: 
          • im-switch -s gcin 
          • download the mapping table 
          • wget http://edt1023.sayya.org/gcin/noseeing-12.tar.gz 
          • move the table to the gcin folder 
          • mv noseeing.gtab ~/.gcin  
          • Reboot and right click gcin-setup

          Numerical Libraries
          • Lapack (share)
            • sudo apt-get install liblapack3gf 
            • sudo ln -s /usr/lib/liblapack.so.3gf /usr/lib/liblapack.so

          • Blas (share)
            • sudo apt-get install libblas3gf 
            • sudo ln -s /usr/lib/libblas.so.3gf /usr/lib/libblas.so

          • GotoBlas (static)
            • download http://www.tacc.utexas.edu/tacc-projects/ 
            • tar -xvf thefile 
            • cd thefolder 
            • ./quickbuild.32bit or 
            • gedit Makefile.rule 
            • make 
            • mkdir temp --> cp libgoto2_penryn-r1.09p3.a ./temp --> cd temp 
            • unarchive: ar x libgoto2_penryn-r1.09p3.a 
            • recompile into a shared library:
            • gfortran -shared -Wl,-soname,libgoto2.so -o libgoto2.so.1.09p3 *.o
            • See here for the knowledge of shared libraries. 
            • sudo cp libgoto2.so.1.09p3 /usr/lib 
            • sudo ldconfig
          Misc.
          • Turn off the terminal beep (9.04 only)
            • Terminal --> Profiles --> Edit --> General Tab --> uncheck Terminal bell

          • RAR Archiver 
            • sudo apt-get install rar
            • sudo ln -fs /usr/bin/rar /usr/bin/unrar
          • 7z Archiver 
            • sudo apt-get install p7zip-full

            • Skype (works well for 9.04, 9.10, 10.04) 
              • http://www.skype.com/intl/zh-Hant/download/skype/linux/choose/ 
              • 8.10+ 32-bit works fine on 9.04 32-bit

            • Printer (D-Link DI-707p) 
              • System --> Administration --> Printing --> Devices --> Network Printer --> LPD/LPR Host or Printer --> Host: 192.168.0.1 Queue: lp
              • Select your printer driver

            • Codec (obsolete)
              • sudo apt-get install ubuntu-restricted-extras

            • Battery management (Thinkpad)
              • not yet done....

            • CPU hiss "CPU Whine" (from ThinkWiki)
              (for grub2, applicable to ubuntu 9.10 and after)
              • sudo joe /etc/default/grub 
              • Search for the line GURB_CMDLINE_LINUX="" 
              • Replace "" with "processor.max_cstate=2" 
              • If the original "" is not empty inside the double quote, simply add processor.max_cstate=2 before the closing quote mark, e.g. replace
              • GRUB_CMDLINE_LINUX="acpi_osi="Linux"" with 
              • GRUB_CMDLINE_LINUX="acpi_osi="Linux" processor.max_cstate=2"
              • sudo update-grub
              • sudo reboot
            • Install restricted codec and more
              • Launch Amarok 
              • Choose to install all recommended packages