Latest LQ Deal: Latest LQ Deals
Go Back > Linux Answers > Programming
User Name


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:
$ echo ~
The libraries under consideration in this part (Part 1 out of not that many) are:

1. BLAS (Basic Linear Algebra Subprograms) [ ]. 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 [ ]. A C interface to the BLAS.

Let us begin.

1. BLAS: For this library we will required to download its source code available in For this we could use the wget utility as follows:

$ wget
Similarly, we need to decompress the attained file by means of:

$ 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:

~/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:
$ 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:

~/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:

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:

~/Libraries/CBLAS$ rm
~/Libraries/CBLAS$ ln -s Makefile.LINUX
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:

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:

$ 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.

/* 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
 * 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 i, j;

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

  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:

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 08:58 PM.

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