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:
    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, -o *.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, passed to ld. It specifies the internal name of the library which will be used and linked by other programs accordingly. See here for more ideas about ld.
    6. -o the file name after compilation
    7. *.o: all .o files will be compiled
  11. Move to the folder you prefer, usually /usr/lib, and create a symbolic link to that
    >> sudo ln -s 
  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, 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.

No comments:

Post a Comment