Installation and Use of Common Scientific Libraries in Unix Part 1: BLAS and CBLAS
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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:
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:
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;
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.