# uBLAS Overview

## Rationale

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:

- mathematical notation
- efficiency
- functionality
- compatibility

Another intention was to evaluate, if the abstraction penalty resulting from the use of such matrix and vector classes is acceptable.

## Resources

The development of this library was guided by a couple of similar efforts:

- BLAS by Jack Dongarra et al.
- Blitz++ by Todd Veldhuizen
- POOMA by Scott Haney et al.
- MTL by Jeremy Siek et al.

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.

## Concepts

### Mathematical Notation

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); matrix::operator(size_t i, size_t j);`
Assignment of vectors and matrices | `vector::operator = (const vector_expression &); vector::operator += (const vector_expression &); vector::operator -= (const vector_expression &); vector::operator *= (const scalar_expression &); matrix::operator = (const matrix_expression &); matrix::operator += (const matrix_expression &); matrix::operator -= (const matrix_expression &); matrix::operator *= (const scalar_expression &);`
Unary operations on vectors and matrices | `vector_expression operator - (const vector_expression &); matrix_expression operator - (const matrix_expression &);`
Binary operations on vectors and matrices | `vector_expression operator + (const vector_expression &, const vector_expression &); vector_expression operator - (const vector_expression &, const vector_expression &); matrix_expression operator + (const matrix_expression &, const matrix_expression &); matrix_expression operator - (const matrix_expression &, const matrix_expression &);`
Multiplication of vectors and matrices with a scalar | `vector_expression operator * (const scalar_expression &, const vector_expression &); vector_expression operator * (const vector_expression &, const scalar_expression &); matrix_expression operator * (const scalar_expression &, const matrix_expression &); matrix_expression operator * (const matrix_expression &, const scalar_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 &); vector_expression prod (const matrix_expression &, const vector_expression &);`
Right multiplication of vectors with a matrix | `vector_expression prod<``vector_type``> (const vector_expression &, const matrix_expression &); vector_expression prod (const vector_expression &, const matrix_expression &);`
Multiplication of matrices | `matrix_expression prod<``matrix_type``> (const matrix_expression &, const matrix_expression &); matrix_expression prod (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 &);`

### Efficiency

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.

#### Eliminating Temporaries

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.

#### Eliminating Virtual Function Calls

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.

#### Compilation times

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

### Functionality

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 Level 1

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) norm_inf_index (x)` | max |xi| | Computes the maximum norm of a vector. BLAS computes the index of the first element having this value.
`_dot _dotu _dotc` | `inner_prod (x, y)`or`inner_prod (conj (x), y)` | xT y or xH y | Computes the inner product of two vectors. BLAS implements certain loop unrollment.
`dsdot sdsdot` | `a + prec_inner_prod (x, y)` | a + xT y | Computes the inner product in double precision.
`_copy` | `x = y y.assign (x)` | 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 csscal zdscal` | `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 _rotm csrot zdrot` | `t.assign (a * x + b * y), y.assign (- b * x + a * y), x.assign (t)` | (x, y) <- (a x + b y, -b x + a y) | Applies a plane rotation.
`_rotg _rotmg` |  | (a, b) <- (? a / sqrt (a2 + b2), ? b / sqrt (a2 + b2)) or (1, 0) <- (0, 0) | Constructs a plane rotation.

#### Blas Level 2

BLAS Call | Mapped Library Expression | Mathematical Description | Comment
--- | --- | --- | ---
`_t_mv` | `x = prod (A, x)` or`x = prod (trans (A), x)` or`x = prod (herm (A), x)` | x <- A x or x <- AT x or x <- AH x | Computes the product of a matrix with a vector.
`_t_sv` | `x = solve (A, x, tag)` or `x = solve (trans (A), x, tag)` or`x = solve (herm (A), x, tag)` | x <- A-1 x or x <- AT-1 x or x <- AH-1 x | Solves a linear equation for a triangular matrix.
`_g_mv _s_mv _h_mv` | `y = a * prod (A, x) + b * y` or`y = a * prod (trans (A), x) + b * y` or`y = a * prod (herm (A), x) + b * y` | y <- A x + b y or y <- AT x + b y y <- AH x + b y | Adds the scaled product of a matrix with a vector.
`_g_r _g_ru _g_rc` | `A += a * outer_prod (x, y)` or`A += a * outer_prod (x, conj (y))` | A <- a x yT + A or A <- a x yH + A | Performs a rank 1 update.
`_s_r _h_r` | `A += a * outer_prod (x, x)` or`A += a * outer_prod (x, conj (x))` | A <- a x xT + A or A <- a x xH + A | Performs a symmetric or hermitian rank 1 update.
`_s_r2 _h_r2` | `A += a * outer_prod (x, y) + a * outer_prod (y, x))` or`A += a * outer_prod (x, conj (y)) + conj (a) * outer_prod (y, conj (x)))` | 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 Level 3

BLAS Call | Mapped Library Expression | Mathematical Description | Comment
--- | --- | --- | ---
`_t_mm` | `B = a * prod (A, B)` or `B = a * prod (trans (A), B)` or `B = a * prod (A, trans (B))` or `B = a * prod (trans (A), trans (B))` or `B = a * prod (herm (A), B)` or `B = a * prod (A, herm (B))` or `B = a * prod (herm (A), trans (B))` or `B = a * prod (trans (A), herm (B))` or `B = 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` | `B = solve (A, B, tag)` or `B = solve (trans (A), B, tag)` or`B = solve (herm (A), B, tag)` | B <- A-1 B or B <- AT-1 B or B <- AH-1 B | Solves a linear equation for a triangular matrix.
`_g_mm _s_mm _h_mm` | `C = a * prod (A, B) + b * C` or `C = a * prod (trans (A), B) + b * C` or `C = a * prod (A, trans (B)) + b * C` or `C = a * prod (trans (A), trans (B)) + b * C` or `C = a * prod (herm (A), B) + b * C` or `C = a * prod (A, herm (B)) + b * C` or `C = a * prod (herm (A), trans (B)) + b * C` or `C = a * prod (trans (A), herm (B)) + b * C` or `C = 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 _h_rk` | `B = a * prod (A, trans (A)) + b * B` or `B = a * prod (trans (A), A) + b * B` or `B = a * prod (A, herm (A)) + b * B` or `B = 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 _h_r2k` | `C = a * prod (A, trans (B)) + a * prod (B, trans (A)) + b * C` or `C = a * prod (trans (A), B) + a * prod (trans (B), A) + b * C` or `C = a * prod (A, herm (B)) + conj (a) * prod (B, herm (A)) + b * C` or `C = a * prod (herm (A), B) + conj (a) * prod (herm (B), A) + b * C` | C <- a A BT + a B AT + b C or C <- a AT B + a BTA + 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.

#### Storage Layouts

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, std::vector<T> > v (size)` | Constructs a dense vector, storage is provided by a standard vector. The storage layout usually is BLAS compliant.
`vector<T, unbounded_array<T> > v (size)` | Constructs a dense vector, storage is provided by a heap-based array. The storage layout usually is BLAS compliant.
`vector<T, bounded_array<T, N> > v (size)` | Constructs a dense vector, storage is provided by a stack-based array. The storage layout usually is BLAS compliant.
`unit_vector<T> v (size, index)` | Constructs the `index`-th canonical unit vector.
`zero_vector<T> v (size)` | Constructs a zero vector.
`sparse_vector<T, std::map<std::size_t, T> > v (size, non_zeros)` | Constructs a sparse vector, storage is provided by a standard map.
`sparse_vector<T, map_array<std::size_t, T> > v (size, non_zeros)` | Constructs a sparse vector, storage is provided by a map array.
`vector_range<V> vr (v, range)` | Constructs a sub vector of a dense, packed or sparse vector.
`vector_slice<V> vs (v, slice)` | Constructs a sub vector of a dense, packed or sparse vector. This class usually can be used to emulate BLAS vector slices.
`matrix<T, row_major, std::vector<T> > m (size1, size2)` | Constructs a dense matrix, orientation is row major, storage is provided by a standard vector.
`matrix<T, column_major, std::vector<T> > m (size1, size2)` | Constructs a dense matrix, orientation is column major, storage is provided by a standard vector. The storage layout usually is BLAS compliant.
`matrix<T, row_major, unbounded_array<T> > m (size1, size2)` | Constructs a dense matrix, orientation is row major, storage is provided by a heap-based array.
`matrix<T, column_major, unbounded_array<T> > m (size1, size2)` | 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, row_major, bounded_array<T, N1 * N2> > m (size1, size2)` | Constructs a dense matrix, orientation is row major, storage is provided by a stack-based array.
`matrix<T, column_major, bounded_array<T, N1 * N2> > m (size1, size2)` | 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> m (size)` | Constructs an identity matrix.
`zero_matrix<T> m (size1, size2)` | Constructs a zero matrix.
`triangular_matrix<T, row_major, F, A> m (size)` | Constructs a packed triangular matrix, orientation is row major.
`triangular_matrix<T, column_major, F, A> m (size)` | Constructs a packed triangular matrix, orientation is column major. The storage layout usually is BLAS compliant.
`banded_matrix<T, row_major, A> m (size1, size2, lower, upper)` | Constructs a packed banded matrix, orientation is row major.
`banded_matrix<T, column_major, A> m (size1, size2, lower, upper)` | Constructs a packed banded matrix, orientation is column major. The storage layout usually is BLAS compliant.
`symmetric_matrix<T, row_major, F, A> m (size)` | Constructs a packed symmetric matrix, orientation is row major.
`symmetric_matrix<T, column_major, F, A> m (size)` | Constructs a packed symmetric matrix, orientation is column major. The storage layout usually is BLAS compliant.
`hermitian_matrix<T, row_major, F, A> m (size)` | Constructs a packed hermitian matrix, orientation is row major.
`hermitian_matrix<T, column_major, F, A> m (size)` | Constructs a packed hermitian matrix, orientation is column major. The storage layout usually is BLAS compliant.
`sparse_matrix<T, row_major, std::map<std::size_t, T> > m (size1, size2, non_zeros)` | Constructs a sparse matrix, orientation is row major, storage is provided by a standard map.
`sparse_matrix<T, column_major, std::map<std::size_t, T> > m (size1, size2, non_zeros)` | Constructs a sparse matrix, orientation is column major, storage is provided by a standard map.
`sparse_matrix<T, row_major, map_array<std::size_t, T> > m (size1, size2, non_zeros)` | Constructs a sparse matrix, orientation is row major, storage is provided by a map array.
`sparse_matrix<T, column_major, map_array<std::size_t, T> > m (size1, size2, non_zeros)` | Constructs a sparse matrix, orientation is column major, storage is provided by a map array.
`matrix_row<M> mr (m, i)` | Constructs a sub vector of a dense, packed or sparse matrix, containing the i-th row.
`matrix_column<M> mc (m, j)` | Constructs a sub vector of a dense, packed or sparse matrix, containing the j-th column.
`matrix_range<M> mr (m, range1, range2)` | 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> ms (m, slice1, slice2)` | Constructs a sub matrix of a dense, packed or sparse matrix.
`triangular_adaptor<M, F> ta (m)` | 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> ba (m, lower, upper)` | 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> sa (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> ha (m)` | Constructs a hermitian view of a dense, packed or sparse matrix. This class usually can be used to generate corresponding BLAS matrix types.

### Compatibility

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.

## Reference

- Expression Concepts
  - Scalar Expression
  - Vector Expression
  - Matrix Expression
- Container Concepts
  - Vector
  - Matrix
- Iterator Concepts
  - Indexed Bidirectional Iterator
  - Indexed Random Access Iterator
  - Indexed Bidirectional Column/Row Iterator
  - Indexed Random Access Column/Row Iterator
- Storage
  - Unbounded Array
  - Bounded Array
  - Range
  - Slice
- Sparse Storage
  - Map Array
- Vector
  - Vector
  - Unit Vector
  - Zero Vector
- Sparse Vector
  - Sparse Vector
- Vector Proxies
  - Vector Range
  - Vector Slice
- Vector Expressions
  - Vector Expression
  - Vector References
  - Vector Operations
  - Vector Reductions
- Matrix
  - Matrix
  - Identity Matrix
  - Zero Matrix
- Triangular Matrix
  - Triangular Matrix
  - Triangular Adaptor
- Symmetric Matrix
  - Symmetric Matrix
  - Symmetric Adaptor
- Hermitian Matrix
  - Hermitian Matrix
  - Hermitian Adaptor
- Banded Matrix
  - Banded Matrix
  - Banded Adaptor
- Sparse Matrix
  - Sparse Matrix
- Matrix Proxies
  - Matrix Row
  - Matrix Column
  - Vector Range
  - Vector Slice
  - Matrix Range
  - Matrix Slice
- Matrix Expressions
  - Matrix Expression
  - Matrix References
  - Matrix Operations
  - Matrix Reductions
  - Matrix Vector Operations
  - Matrix Matrix Operations

## Benchmark Results

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: 8/3/2002