...one of the most highly
regarded and expertly designed C++ library projects in the
world.
— Herb Sutter and Andrei
Alexandrescu, C++
Coding Standards
It would be nice if every kind of numeric software could be written in C++ without loss of efficiency, but unless something can be found that achieves this without compromising the C++ type system it may be preferable to rely on Fortran, assembler or architecture-specific extensions (Bjarne Stroustrup).
This C++ library is directed towards scientific computing on the level of basic linear algebra constructions with matrices and vectors and their corresponding abstract operations. The primary design goals were:
Another intention was to evaluate, if the abstraction penalty resulting from the use of such matrix and vector classes is acceptable.
The development of this library was guided by a couple of similar efforts:
BLAS seems to be the most widely used library for basic linear algebra constructions, so it could be called a de-facto standard. Its interface is procedural, the individual functions are somewhat abstracted from simple linear algebra operations. Due to the fact that is has been implemented using Fortran and its optimizations, it also seems to be one of the fastest libraries available. As we decided to design and implement our library in an object-oriented way, the technical approaches are distinct. However anyone should be able to express BLAS abstractions in terms of our library operators and to compare the efficiency of the implementations.
Blitz++ is an impressive library implemented in C++. Its main design seems to be oriented towards multidimensional arrays and their associated operators including tensors. The author of Blitz++ states, that the library achieves performance on par or better than corresponding Fortran code due to his implementation technique using expression templates and template metaprograms. However we see some reasons, to develop an own design and implementation approach. We do not know whether anybody tries to implement traditional linear algebra and other numerical algorithms using Blitz++. We also presume that even today Blitz++ needs the most advanced C++ compiler technology due to its implementation idioms. On the other hand, Blitz++ convinced us, that the use of expression templates is mandatory to reduce the abstraction penalty to an acceptable limit.
POOMA's design goals seem to parallel Blitz++'s in many parts . It extends Blitz++'s concepts with classes from the domains of partial differential equations and theoretical physics. The implementation supports even parallel architectures.
MTL is another approach supporting basic linear algebra operations in C++. Its design mainly seems to be influenced by BLAS and the C++ Standard Template Library. We share the insight that a linear algebra library has to provide functionality comparable to BLAS. On the other hand we think, that the concepts of the C++ standard library have not yet been proven to support numerical computations as needed. As another difference MTL currently does not seem to use expression templates. This may result in one of two consequences: a possible loss of expressiveness or a possible loss of performance.
The usage of mathematical notation may ease the development of scientific algorithms. So a C++ library implementing basic linear algebra concepts carefully should overload selected C++ operators on matrix and vector classes.
We decided to use operator overloading for the following primitives:
Description | Operator |
---|---|
Indexing of vectors and matrices | vector::operator(size_t i); |
Assignment of vectors and matrices | vector::operator = (const vector_expression &); |
Unary operations on vectors and matrices | vector_expression operator - (const vector_expression
&); |
Binary operations on vectors and matrices | vector_expression operator + (const vector_expression
&, const vector_expression &); |
Multiplication of vectors and matrices with a scalar | vector_expression operator * (const scalar_expression
&, const vector_expression &); |
We decided to use no operator overloading for the following other primitives:
Description | Function |
---|---|
Left multiplication of vectors with a matrix | vector_expression
prod< vector_type > (const
matrix_expression &, const vector_expression &); |
Right multiplication of vectors with a matrix | vector_expression
prod< vector_type > (const
vector_expression &, const matrix_expression &); |
Multiplication of matrices | matrix_expression
prod< matrix_type > (const
matrix_expression &, const matrix_expression &); |
Inner product of vectors | scalar_expression inner_prod (const vector_expression
&, const vector_expression &); |
Outer product of vectors | matrix_expression outer_prod (const vector_expression
&, const vector_expression &); |
Transpose of a matrix | matrix_expression trans (const matrix_expression
&); |
To achieve the goal of efficiency for numerical computing, one has to overcome two difficulties in formulating abstractions with C++, namely temporaries and virtual function calls. Expression templates solve these problems, but tend to slow down compilation times.
Abstract formulas on vectors and matrices normally compose a couple of unary and binary operations. The conventional way of evaluating such a formula is first to evaluate every leaf operation of a composition into a temporary and next to evaluate the composite resulting in another temporary. This method is expensive in terms of time especially for small and space especially for large vectors and matrices. The approach to solve this problem is to use lazy evaluation as known from modern functional programming languages. The principle of this approach is to evaluate a complex expression element wise and to assign it directly to the target.
Two interesting and dangerous facts result.
First one may get serious side effects using element wise evaluation on vectors or matrices. Consider the matrix vector product x = A x. Evaluation of A1x and assignment to x1 changes the right hand side, so that the evaluation of A2x returns a wrong result. Our solution for this problem is to evaluate the right hand side of an assignment into a temporary and then to assign this temporary to the left hand side. To allow further optimizations, we provide a corresponding member function for every assignment operator. By using this member function a programmer can confirm, that the left and right hand sides of an assignment are independent, so that element wise evaluation and direct assignment to the target is safe.
Next one can get worse computational complexity under certain cirumstances. Consider the chained matrix vector product A (B x). Conventional evaluation of A (B x) is quadratic. Deferred evaluation of B xi is linear. As every element B xi is needed linearly depending of the size, a completely deferred evaluation of the chained matrix vector product A (B x) is cubic. In such cases one needs to reintroduce temporaries in the expression.
Lazy expression evaluation normally leads to the definition of a class hierarchy of terms. This results in the usage of dynamic polymorphism to access single elements of vectors and matrices, which is also known to be expensive in terms of time. A solution was found a couple of years ago independently by David Vandervoorde and Todd Veldhuizen and is commonly called expression templates. Expression templates contain lazy evaluation and replace dynamic polymorphism with static, i.e. compile time polymorphism. Expression templates heavily depend on the famous Barton-Nackman trick, also coined 'curiously defined recursive templates' by Jim Coplien.
Expression templates form the base of our implementation.
It is also a well known fact, that expression templates challenge currently available compilers. We were able to significantly reduce the amount of needed expression templates using the Barton-Nackman trick consequently.
We also decided to support a dual conventional implementation
(i.e. not using expression templates) with extensive bounds and
type checking of vector and matrix operations to support the
development cycle. Switching from debug mode to release mode is
controlled by the NDEBUG
preprocessor symbol of
<cassert>
.
Every C++ library supporting linear algebra will be measured against the long-standing Fortran package BLAS. We now describe how BLAS calls may be mapped onto our classes.
BLAS Call | Mapped Library Expression | Mathematical Description | Comment |
---|---|---|---|
_asum |
norm_1 (x) |
sum |xi| | Computes the sum norm of a vector. |
_nrm2 |
norm_2 (x) |
sqrt (sum |xi|2 ) | Computes the euclidean norm of a vector. |
i_amax |
norm_inf (x) |
max |xi| | Computes the maximum norm of a vector. BLAS computes the index of the first element having this value. |
_dot |
inner_prod (x, y) or
|
xT y or xH y |
Computes the inner product of two vectors. BLAS implements certain loop unrollment. |
dsdot |
a + prec_inner_prod (x, y) |
a + xT y | Computes the inner product in double precision. |
_copy |
x = y |
x <- y | Copies one vector to another. BLAS implements certain loop unrollment. |
_swap |
swap (x, y) |
x <-> y | Swaps two vectors. BLAS implements certain loop unrollment. |
_scal |
x *= a |
x <- a x | Scales a vector. BLAS implements certain loop unrollment. |
_axpy |
y += a * x |
y <- a x + y | Adds a scaled vector. BLAS implements certain loop unrollment. |
_rot |
t.assign (a * x + b * y), |
(x, y) <- (a x + b y, -b x + a y) | Applies a plane rotation. |
_rotg |
(a, b) <- (? a / sqrt (a2 + b2), ? b / sqrt (a2 + b2)) or (1, 0) <- (0, 0) |
Constructs a plane rotation. |
BLAS Call | Mapped Library Expression | Mathematical Description | Comment |
---|---|---|---|
_t_mv |
x = prod (A, x) or or
|
x <- A x or x <- AT x or x <- AH x |
Computes the product of a matrix with a vector. |
_t_sv |
y = solve (A, x, tag) orinplace_solve (A, x, tag) ory = solve (trans (A), x, tag) or or y = solve (herm (A), x,
tag) or
|
y <- A-1 x or x <- A-1 x or y <- AT-1 x or x <- AT-1 x or y <- AH-1 x or x <- AH-1 x |
Solves a system of linear equations with triangular form, i.e. A is triangular. |
_g_mv |
y = a * prod (A, x) + b * y or or
|
y <- a A x + b y or y <- a AT x + b y y <- a AH x + b y |
Adds the scaled product of a matrix with a vector. |
_g_r |
A += a * outer_prod (x, y) or
|
A <- a x yT + A
or A <- a x yH + A |
Performs a rank 1 update. |
_s_r |
A += a * outer_prod (x, x) or
|
A <- a x xT + A
or A <- a x xH + A |
Performs a symmetric or hermitian rank 1 update. |
_s_r2 |
A += a * outer_prod (x, y) + or
|
A <- a x yT + a y
xT + A or A <- a x yH + a- y xH + A |
Performs a symmetric or hermitian rank 2 update. |
BLAS Call | Mapped Library Expression | Mathematical Description | Comment |
---|---|---|---|
_t_mm |
B = a * prod (A, B) orB = a * prod (trans (A), B) orB = a * prod (A, trans (B)) orB = a * prod (trans (A), trans (B)) orB = a * prod (herm (A), B) orB = a * prod (A, herm (B)) orB = a * prod (herm (A), trans (B)) orB = a * prod (trans (A), herm (B)) orB = a * prod (herm (A), herm (B)) |
B <- a op (A) op (B) with op (X) = X or op (X) = XT or op (X) = XH |
Computes the scaled product of two matrices. |
_t_sm |
C = solve (A, B, tag) orinplace_solve (A, B, tag) orC = solve (trans (A), B, tag) or or or
|
C <- A-1 B or B <- A-1 B or C <- AT-1 B or B <- A-1 B or C <- AH-1 B or B <- AH-1 B |
Solves a system of linear equations with triangular form, i.e. A is triangular. |
_g_mm |
C = a * prod (A, B) + b * C orC = a * prod (trans (A), B) + b * C orC = a * prod (A, trans (B)) + b * C orC = a * prod (trans (A), trans (B)) + b * C orC = a * prod (herm (A), B) + b * C orC = a * prod (A, herm (B)) + b * C orC = a * prod (herm (A), trans (B)) + b * C orC = a * prod (trans (A), herm (B)) + b * C orC = a * prod (herm (A), herm (B)) + b * C |
C <- a op (A) op (B) + b C with op (X) = X or op (X) = XT or op (X) = XH |
Adds the scaled product of two matrices. |
_s_rk |
B = a * prod (A, trans (A)) + b * B orB = a * prod (trans (A), A) + b * B orB = a * prod (A, herm (A)) + b * B orB = a * prod (herm (A), A) + b * B |
B <- a A AT + b B
or B <- a AT A + b B or B <- a A AH + b B or B <- a AH A + b B |
Performs a symmetric or hermitian rank k update. |
_s_r2k |
C = a * prod (A, trans (B)) + orC = a * prod (trans (A), B) + orC = a * prod (A, herm (B)) + orC = a * prod (herm (A), B) + |
C <- a A BT + a B
AT + b C or C <- a AT B + a BT A + b C or C <- a A BH + a- B AH + b C or C <- a AH B + a- BH A + b C |
Performs a symmetric or hermitian rank 2 k update. |
The library supports conventional dense, packed and basic sparse vector and matrix storage layouts. The description of the most common constructions of vectors and matrices comes next.
Construction | Comment |
---|---|
vector<T, |
Constructs a dense vector, storage is provided by a standard
vector. The storage layout usually is BLAS compliant. |
vector<T, |
Constructs a dense vector, storage is provided by a heap-based
array. The storage layout usually is BLAS compliant. |
vector<T, |
Constructs a dense vector, storage is provided by a stack-based
array. The storage layout usually is BLAS compliant. |
unit_vector<T> |
Constructs the index -th canonical unit
vector. |
zero_vector<T> |
Constructs a zero vector. |
sparse_vector<T, |
Constructs a sparse vector, storage is provided by a standard map. |
sparse_vector<T, |
Constructs a sparse vector, storage is provided by a map array. |
compressed_vector<T> |
Constructs a compressed vector. The storage layout usually is BLAS compliant. |
coordinate_vector<T> |
Constructs a coordinate vector. The storage layout usually is BLAS compliant. |
vector_range<V> |
Constructs a sub vector of a dense, packed or sparse vector. |
vector_slice<V> |
Constructs a sub vector of a dense, packed or sparse
vector. This class usually can be used to emulate BLAS vector slices. |
matrix<T, |
Constructs a dense matrix, orientation is row major, storage is provided by a standard vector. |
matrix<T, |
Constructs a dense matrix, orientation is column major, storage
is provided by a standard vector. The storage layout usually is BLAS compliant. |
matrix<T, |
Constructs a dense matrix, orientation is row major, storage is provided by a heap-based array. |
matrix<T, |
Constructs a dense matrix, orientation is column major, storage
is provided by a heap-based array. The storage layout usually is BLAS compliant. |
matrix<T, |
Constructs a dense matrix, orientation is row major, storage is provided by a stack-based array. |
matrix<T, |
Constructs a dense matrix, orientation is column major, storage
is provided by a stack-based array. The storage layout usually is BLAS compliant. |
identity_matrix<T> |
Constructs an identity matrix. |
zero_matrix<T> |
Constructs a zero matrix. |
triangular_matrix<T, |
Constructs a packed triangular matrix, orientation is row major. |
triangular_matrix<T, |
Constructs a packed triangular matrix, orientation is column
major. The storage layout usually is BLAS compliant. |
banded_matrix<T, |
Constructs a packed banded matrix, orientation is row major. |
banded_matrix<T, |
Constructs a packed banded matrix, orientation is column
major. The storage layout usually is BLAS compliant. |
symmetric_matrix<T, |
Constructs a packed symmetric matrix, orientation is row major. |
symmetric_matrix<T, |
Constructs a packed symmetric matrix, orientation is column
major. The storage layout usually is BLAS compliant. |
hermitian_matrix<T, |
Constructs a packed hermitian matrix, orientation is row major. |
hermitian_matrix<T, |
Constructs a packed hermitian matrix, orientation is column
major. The storage layout usually is BLAS compliant. |
sparse_matrix<T, |
Constructs a sparse matrix, orientation is row major, storage is provided by a standard map. |
sparse_matrix<T, |
Constructs a sparse matrix, orientation is column major, storage is provided by a standard map. |
sparse_matrix<T, |
Constructs a sparse matrix, orientation is row major, storage is provided by a map array. |
sparse_matrix<T, |
Constructs a sparse matrix, orientation is column major, storage is provided by a map array. |
compressed_matrix<T, |
Constructs a compressed matrix, orientation is row major. The storage layout usually is BLAS compliant. |
compressed_matrix<T, |
Constructs a compressed matrix, orientation is column
major. The storage layout usually is BLAS compliant. |
coordinate_matrix<T, |
Constructs a coordinate matrix, orientation is row major. The storage layout usually is BLAS compliant. |
coordinate_matrix<T, |
Constructs a coordinate matrix, orientation is column
major. The storage layout usually is BLAS compliant. |
matrix_row<M> |
Constructs a sub vector of a dense, packed or sparse matrix, containing the i-th row. |
matrix_column<M> |
Constructs a sub vector of a dense, packed or sparse matrix, containing the j-th column. |
matrix_range<M> |
Constructs a sub matrix of a dense, packed or sparse
matrix. This class usually can be used to emulate BLAS leading dimensions. |
matrix_slice<M> |
Constructs a sub matrix of a dense, packed or sparse matrix. |
triangular_adaptor<M, F> |
Constructs a triangular view of a dense, packed or sparse
matrix. This class usually can be used to generate corresponding BLAS matrix types. |
banded_adaptor<M> |
Constructs a banded view of a dense, packed or sparse
matrix. This class usually can be used to generate corresponding BLAS matrix types. |
symmetric_adaptor<M> |
Constructs a symmetric view of a dense, packed or sparse
matrix. This class usually can be used to generate corresponding BLAS matrix types. |
hermitian_adaptor<M> |
Constructs a hermitian view of a dense, packed or sparse
matrix. This class usually can be used to generate corresponding BLAS matrix types. |
For compatibility reasons we provide array like indexing for vectors and matrices, although this could be expensive for matrices due to the needed temporary proxy objects.
To support the most widely used C++ compilers our design and implementation do not depend on partial template specialization essentially.
The library presumes standard compliant allocation through
operator new
and operator delete
. So
programs which are intended to run under MSVC 6.0 should set a
correct new handler throwing a std::bad_alloc
exception via _set_new_handler
to detect out of memory
conditions.
To get the most performance out of the box with MSVC 6.0, you
should change the preprocessor definition of
BOOST_UBLAS_INLINE
to __forceinline
in
the header file config.hpp. But we suspect this optimization to be
fragile.
The following tables contain results of one of our benchmarks.
This benchmark compares a native C implementation ('C array') and
some library based implementations. The safe variants based on the
library assume aliasing, the fast variants do not use temporaries
and are functionally equivalent to the native C implementation.
Besides the generic vector and matrix classes the benchmark
utilizes special classes c_vector
and
c_matrix
, which are intended to avoid every overhead
through genericity.
The benchmark program was compiled with MSVC 6.0 and run on an Intel Pentium II with 333 Mhz. First we comment the results for double vectors and matrices of dimension 3 and 3 x 3, respectively.
Operation | Implementation | Elapsed [s] | MFLOP/s | Comment |
---|---|---|---|---|
inner_prod | C array | 0.1 | 47.6837 | No significant abstraction penalty |
c_vector | 0.06 | 79.4729 | ||
vector<unbounded_array> | 0.11 | 43.3488 | ||
vector<std::vector> | 0.11 | 43.3488 | ||
vector + vector | C array | 0.05 | 114.441 | Abstraction penalty: factor 2 |
c_vector safe | 0.22 | 26.0093 | ||
c_vector fast | 0.11 | 52.0186 | ||
vector<unbounded_array> safe | 1.05 | 5.44957 | ||
vector<unbounded_array> fast | 0.16 | 35.7628 | ||
vector<std::vector> safe | 1.16 | 4.9328 | ||
vector<std::vector> fast | 0.16 | 35.7628 | ||
outer_prod | C array | 0.06 | 85.8307 | Abstraction penalty: factor 2 |
c_matrix, c_vector safe | 0.22 | 23.4084 | ||
c_matrix, c_vector fast | 0.11 | 46.8167 | ||
matrix<unbounded_array>, vector<unbounded_array> safe | 0.38 | 13.5522 | ||
matrix<unbounded_array>, vector<unbounded_array> fast | 0.16 | 32.1865 | ||
matrix<std::vector>, vector<std::vector> safe | 0.5 | 10.2997 | ||
matrix<std::vector>, vector<std::vector> fast | 0.11 | 46.8167 | ||
prod (matrix, vector) | C array | 0.06 | 71.5256 | No significant abstraction penalty |
c_matrix, c_vector safe | 0.11 | 39.0139 | ||
c_matrix, c_vector fast | 0.11 | 39.0139 | ||
matrix<unbounded_array>, vector<unbounded_array> safe | 0.33 | 13.0046 | ||
matrix<unbounded_array>, vector<unbounded_array> fast | 0.11 | 39.0139 | ||
matrix<std::vector>, vector<std::vector> safe | 0.38 | 11.2935 | ||
matrix<std::vector>, vector<std::vector> fast | 0.05 | 85.8307 | ||
matrix + matrix | C array | 0.11 | 46.8167 | No significant abstraction penalty |
c_matrix safe | 0.17 | 30.2932 | ||
c_matrix fast | 0.11 | 46.8167 | ||
matrix<unbounded_array> safe | 0.44 | 11.7042 | ||
matrix<unbounded_array> fast | 0.16 | 32.1865 | ||
matrix<std::vector> safe | 0.6 | 8.58307 | ||
matrix<std::vector> fast | 0.17 | 30.2932 | ||
prod (matrix, matrix) | C array | 0.11 | 39.0139 | No significant abstraction penalty |
c_matrix safe | 0.11 | 39.0139 | ||
c_matrix fast | 0.11 | 39.0139 | ||
matrix<unbounded_array> safe | 0.22 | 19.507 | ||
matrix<unbounded_array> fast | 0.11 | 39.0139 | ||
matrix<std::vector> safe | 0.27 | 15.8946 | ||
matrix<std::vector> fast | 0.11 | 39.0139 |
We notice a twofold performance loss for small vectors and matrices: first the general abstraction penalty for using classes, and then a small loss when using the generic vector and matrix classes. The difference w.r.t. alias assumptions is also significant.
Next we comment the results for double vectors and matrices of dimension 100 and 100 x 100, respectively.
Operation | Implementation | Elapsed [s] | MFLOP/s | Comment |
---|---|---|---|---|
inner_prod | C array | 0.05 | 113.869 | No significant abstraction penalty |
c_vector | 0.06 | 94.8906 | ||
vector<unbounded_array> | 0.05 | 113.869 | ||
vector<std::vector> | 0.06 | 94.8906 | ||
vector + vector | C array | 0.05 | 114.441 | No significant abstraction penalty |
c_vector safe | 0.11 | 52.0186 | ||
c_vector fast | 0.11 | 52.0186 | ||
vector<unbounded_array> safe | 0.11 | 52.0186 | ||
vector<unbounded_array> fast | 0.06 | 95.3674 | ||
vector<std::vector> safe | 0.17 | 33.6591 | ||
vector<std::vector> fast | 0.11 | 52.0186 | ||
outer_prod | C array | 0.05 | 114.441 | No significant abstraction penalty |
c_matrix, c_vector safe | 0.28 | 20.4359 | ||
c_matrix, c_vector fast | 0.11 | 52.0186 | ||
matrix<unbounded_array>, vector<unbounded_array> safe | 0.27 | 21.1928 | ||
matrix<unbounded_array>, vector<unbounded_array> fast | 0.06 | 95.3674 | ||
matrix<std::vector>, vector<std::vector> safe | 0.28 | 20.4359 | ||
matrix<std::vector>, vector<std::vector> fast | 0.11 | 52.0186 | ||
prod (matrix, vector) | C array | 0.11 | 51.7585 | No significant abstraction penalty |
c_matrix, c_vector safe | 0.11 | 51.7585 | ||
c_matrix, c_vector fast | 0.05 | 113.869 | ||
matrix<unbounded_array>, vector<unbounded_array> safe | 0.11 | 51.7585 | ||
matrix<unbounded_array>, vector<unbounded_array> fast | 0.06 | 94.8906 | ||
matrix<std::vector>, vector<std::vector> safe | 0.1 | 56.9344 | ||
matrix<std::vector>, vector<std::vector> fast | 0.06 | 94.8906 | ||
matrix + matrix | C array | 0.22 | 26.0093 | No significant abstraction penalty |
c_matrix safe | 0.49 | 11.6776 | ||
c_matrix fast | 0.22 | 26.0093 | ||
matrix<unbounded_array> safe | 0.39 | 14.6719 | ||
matrix<unbounded_array> fast | 0.22 | 26.0093 | ||
matrix<std::vector> safe | 0.44 | 13.0046 | ||
matrix<std::vector> fast | 0.27 | 21.1928 | ||
prod (matrix, matrix) | C array | 0.06 | 94.8906 | No significant abstraction penalty |
c_matrix safe | 0.06 | 94.8906 | ||
c_matrix fast | 0.05 | 113.869 | ||
matrix<unbounded_array> safe | 0.11 | 51.7585 | ||
matrix<unbounded_array> fast | 0.17 | 33.4908 | ||
matrix<std::vector> safe | 0.11 | 51.7585 | ||
matrix<std::vector> fast | 0.16 | 35.584 |
For larger vectors and matrices the general abstraction penalty for using classes seems to decrease, the small loss when using generic vector and matrix classes seems to remain. The difference w.r.t. alias assumptions remains visible, too.
Copyright (©) 2000-2002 Joerg Walter, Mathias Koch
Permission to copy, use, modify, sell and distribute this document
is granted provided this copyright notice appears in all copies.
This document is provided ``as is'' without express or implied
warranty, and with no claim as to its suitability for any
purpose.
Last revised: 1/15/2003