LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Linux Answers > Programming
User Name
Password

Notices

By ejspeiro at 2012-05-15 19:12
By. Eduardo J. Sanchez P. (ejspeiro)

Hi guys, since my research work has lead me to the necessity of using some important scientific libraries, I have decided to contribute to the tutorials section with this humble collection of summaries about how to install these libraries. An important purpose shall be to explicitly depict the hierarchical dependence among the libraries, while at the same time, as it's been already stated, to explain the basic mechanics of the installations.

I will assume (i) that a first semester Computer Science student has been asked to install these to perform basic operations with them. I believe that the arising knowledge from using them is highly more valuable that the annoying research required to figure this out for the novice user.

I will also assume that (ii) all the libraries are meant to be installed in the ~/Libraries directory. For example, in my computer:
Code:
$ echo ~
/home/ejspeiro/
The libraries under consideration in this part (Part 1 out of not that many) are:

1. BLAS (Basic Linear Algebra Subprograms) [ http://www.netlib.org/blas/ ]. This library focuses on providing Fortran routines for basic operations in Linear Algebra. It is important since it serves as a dependence for other libraries that serve more elaborated purposes.
2. CBLAS [ http://www.netlib.org/blas/ ]. A C interface to the BLAS.

Let us begin.

1. BLAS: For this library we will required to download its source code available in http://www.netlib.org/blas/blas.tgz. For this we could use the wget utility as follows:

Code:
$ wget http://www.netlib.org/blas/blas.tgz
Similarly, we need to decompress the attained file by means of:

Code:
$ tar -xvzf blas.tgz
From this point on, we shall assume that (iii) the user has already been educated on how to decompress a tgz file.

To build this library, there's nothing else required than simply doing:

Code:
~/Libraries/BLAS$ make
Notice that the gfortran compiler is required to build this library (by default), in case of NOT having this compiler, a simple:
Code:
$ sudo apt-get install gfortran
will install it. Notice that the existence of this capability has been assumed (iv), therefore any further lack of a particular piece of software should be solved analogously. For example, [font=courier]ar[/code] and [font=courier]ranlib[/code] are necessary to archive the attained object files, therefore they can be installed, if missing, by an analog instruction.

Although this will do it, once the building process has finished, we realize that we have attained the following archive file: blas_LINUX.a, whose name conflicts with the -l convention on most linkers. This can easily be solved by means of creating a symbolic logic link, as follows:

Code:
~/Libraries/BLAS$ ln -s blas_LINUX.a libblas.a
The building process finishes now, since we have attained our libblas.a archive file.

1. CBLAS: For this library, we will perform similar steps to obtain the required compressed file: http://www.netlib.org/blas/blast-forum/cblas.tgz

We then refer to the file README to learn we we first need to create a logic link to the makefile inclusion file that suits the best to our architecture, so that we can the change it. In my case, I did:

Code:
~/Libraries/CBLAS$ rm Makefile.in
~/Libraries/CBLAS$ ln -s Makefile.LINUX Makefile.in
We then edit whichever Makefile.ARCH that we used as a target for the logic link. In my case, I've performed the following specifications:

Code:
SHELL = /bin/bash
BLLIB = ~/Libraries/BLAS/libblas.a
CBLIB = ../lib/libcblas.a
RANLIB = ranlib
Notice first that in order for you to learn about which type of shell are you using, you can use the ps application. For example, in my computer that yields:

Code:
$ ps
  PID TTY          TIME CMD
 7297 pts/2    00:00:00 bash
10026 pts/2    00:00:00 ps
therefore, I am using Bash.

You can then execute [font=courier]make help[\font] which will yield the options you can use... In my case I instructed [font=courier]make all[\font]. Form this I obtained libcblas.a and performed all the related tests to make sure I have a valid installation of the library.

When it come to its usage, it should be clear by now that we can now use BLAS from both Fortran and C or C++ programs. An excellent example can be found in [1]. For the sake of completion of this tutorial I shall quote the following source code, which depicts how to construct a driver code to perform a vector - matrix multiplication using CBLAS which depends on BLAS.

Code:
/* testblas.c - test program for the cblas_dgemv function
 *
 * Copyright (C) 2004  Jochen Voss.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * $Id: testblas.c 5698 2004-05-12 18:17:53Z voss $
 */

#include <stdio.h>
#include <cblas.h>

double m[] = {
  3, 1, 3,
  1, 5, 9,
  2, 6, 5
};

double x[] = {
  -1, -1, 1
};

double y[] = {
  0, 0, 0
};

int
main()
{
  int i, j;

  for (i=0; i<3; ++i) {
    for (j=0; j<3; ++j) printf("%5.1f", m[i*3+j]);
    putchar('\n');
  }

  cblas_dgemv(CblasRowMajor, CblasNoTrans, 3, 3, 1.0, m, 3,
	      x, 1, 0.0, y, 1);

  for (i=0; i<3; ++i)  printf("%5.1f\n", y[i]);

  return 0;
}
The previous code can be compiled by doing:

Code:
cc -c testblas.c -I/home/ejspeiro/Libraries/CBLAS/include/
gfortran testblas.o -o testblas -L/home/ejspeiro/Libraries/BLAS -L/home/ejspeiro/Libraries/CBLAS/lib -lcblas -lblas -lm
:) On Part 2, I shall introduce LAPACK and ATLAS.


  



All times are GMT -5. The time now is 10:24 AM.

Main Menu
Advertisement

My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration