BOOST Library

From Micro and Nano Mechanics Group
Revision as of 20:29, 16 January 2009 by Correaa (talk | contribs) (New page: == BOOST Libraries == For those using C++ as a programming language, BOOST is a very useful set of libraries that help programming in C++ by providing tools and commonly used idioms that ...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

BOOST Libraries

For those using C++ as a programming language, BOOST is a very useful set of libraries that help programming in C++ by providing tools and commonly used idioms that solve recurring problems that emerge when developing in this language, even at a fairly low level. It also encourages certain style of programming, where genericity and efficiency are priority. These tools include macro definition, template functions and classes, functions, generic data containers, and common algorithms. It can be viewed as a natural extension of the C++ Standard Template Library (STL).

In my opinion BOOST has a very steep learning curve and the syntax can become sometimes very ugly and complicated, but it faster to learn BOOST than to reinvent the wheel and come with buggy home-brew solutions that at the end of the day will look even more ugly and won't be that good anyway. (By good I mean generic and efficient.) Note that I am not comparing BOOST and C++ with other tools like Matlab, or Fortran, or Python. Those language are likely to do better (and quick development) than C++ in many specific areas. (BTW, Boost also closes that gap without introducing modifications to the language.)

Libraries included in Boost range from very simple ones (like lexical_cast) to very complex ones like (boost.mpi and boost.gil).

The aim of this document/tutorial is to incrementally document the utility and working practical examples. Extra documentation added by users is very welcome, specially in the area of non-trivial but concise examples, related to our common programming problems (i.e. scientific programming).

Topics covered in the first stages of this document will include: Boost building and installation, boost.multi_array and boost.mpi. That are the libraries that I have to deal with at this point. Ideally I would like to add boost.ublas in the future and document examples of small libraries that are very usefull for everyday tasks. Ideally the focus will be to document the interaction of these libraries with numerical libraries such as FFTW and Lapack.

BOOST build and installation

It is a bless that most Linux distributions now include some versions of Boost. Many Boost libraries are header only, which means that no linking is necessary (an #include<boost/ ... > line set us ready to use a particular library). Linking is not less easy, it is just a matter of add something like "-lboost_..." during compilation.

The sad part is that most distributions are usually outdated, for example the current version of Boost is 1.36, while the most recent distributions come with version 1.34 at best. For "header only" libraries the situation is not that bad because the header files can be downloaded and they ready to use. For binary libraries we need to compile them. There are tools to compile specific libraries but it will be easier for the moment to compile the whole Boost set.

As usual, to be kind with our usage in admintrated system we will install Boost in our user space. for example in $HOME/user.

 mkdir $HOME/usr

the directories ~/usr/include/boost and ~/usr/lib will be created and populated after the building of Boost. Be brave and install the last current version from the link bellow:

 mkdir $HOME/soft
 cd $HOME/soft
 wget http://internap.dl.sourceforge.net/sourceforge/boost/boost_1_37_0.tar.gz
 

If the download fails, try to download the last version from [[1]] and then decompress it:

 tar -zxvf boost_1_37_0.tar.gz
 cd boost_1_37_0

Append the line "using mpi : mpicxx;" to ./tools/build/v2/user-config.jam

 echo "using mpi : mpicxx ;" >> ./tools/build/v2/user-config.jam

where mpicxx can be replaced by your mpi compiler wrapper, for example "/usr/lib/mpich/bin/mpiCC". Then compile and install the package

 bjam --with-mpi --prefix=$HOME/usr

Bjam is a Make-like tool specially designed for compiling Boost and I assume it is already installed in the system, if not, you will have to compile it yourself from sources in ./tools/jam. (Don't worry, you don't need bjam to compile bjam.)

After successful compilation and installation, all the header files will be installed in ~/usr/boost_1_37_0/boost, for example ~/usr/boost_1_37_0/boost/mpi.hpp and the binary linkable files will be in ~/usr/lib, to be specific:

 ~/usr/lib/libboost_serialization-gcc43-mt.a
 ~/usr/lib/libboost_mpi-gcc43-mt.a
 ~/usr/lib/libboost_serialization-gcc43-mt-1_37.so
 ~/usr/lib/libboost_mpi-gcc43-mt-1_37.so

depending on how you want to compile your programs is might be necesary that you change add these directories to the include path of compilation for example, LD_LIBRARY_PATH.

Boost.MultiArray Library

The MultiArray library is the first to be described in this tutorial because it is the most simple of these libraries and yet it solves a very annoying problem with C/C++ for our area. C/C++ have a very limited support for built-in arrays and the situation is even worst for multidimensional arrays (2,3 or more indices). Passing arrays to functions is very error prone, and functions with endless parameters with array dimensions must be continuously passed around. Moreover, writing algorithms for arbitrary array dimensionality is very hard.

We have to understand that the reason for this is that the language was designed to be very flexible, there are infinite ways to arrange multidimensional arrays in memory and infinite ways to resize them, so the language did not enforce any particular standard. Yet for scientific computing (which is only a subset of all computing areas), we can agree that there are a few multidimensional arrays, namely contiguous blocks memory with either column-major ordering (C-convention) or row-major ordering (Fortran-convention).

Among this restriction of memory layout, MultiArray provides a very flexible, very small and nice interface.

EXAMPLES HERE

Boost.MPI