1<?xml version="1.0" encoding="UTF-8"?> 2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 3<html xmlns="http://www.w3.org/1999/xhtml"> 4<head> 5<meta name="generator" content="HTML Tidy for Linux/x86 (vers 1st March 2004), see www.w3.org" /> 6<meta http-equiv="Content-Type" content="text/html; charset=us-ascii" /> 7<link rel="stylesheet" href="../../../../boost.css" type="text/css"/> 8<link rel="stylesheet" href="ublas.css" type="text/css" /> 9<script type="text/javascript" src="js/jquery-1.3.2.min.js" async="async" ></script> 10<script type="text/javascript" src="js/jquery.toc-gw.js" async="async" ></script> 11<title>uBLAS Overview</title> 12</head> 13<body> 14<h1><img src="../../../../boost.png" align="middle" alt="logo"/>uBLAS Overview</h1> 15<div class="toc" id="toc"></div> 16<h2><a name="rationale">Rationale</h2> 17<p><cite>It would be nice if every kind of numeric software could 18be written in C++ without loss of efficiency, but unless something 19can be found that achieves this without compromising the C++ type 20system it may be preferable to rely on Fortran, assembler or 21architecture-specific extensions (Bjarne Stroustrup).</cite></p> 22<p>This C++ library is directed towards scientific computing on the 23level of basic linear algebra constructions with matrices and 24vectors and their corresponding abstract operations. The primary 25design goals were:</p> 26<ul type="disc"> 27<li>mathematical notation</li> 28<li>efficiency</li> 29<li>functionality</li> 30<li>compatibility</li> 31</ul> 32<p>Another intention was to evaluate, if the abstraction penalty 33resulting from the use of such matrix and vector classes is 34acceptable.</p> 35<h2>Resources</h2> 36<p>The development of this library was guided by a couple of 37similar efforts:</p> 38<ul type="disc"> 39<li><a href="http://www.netlib.org/blas/index.html">BLAS</a> by 40Jack Dongarra et al.</li> 41<li><a href="http://www.oonumerics.org/blitz/">Blitz++</a> by Todd 42Veldhuizen</li> 43<li><a href="http://acts.nersc.gov/pooma/">POOMA</a> by Scott 44Haney et al.</li> 45<li><a href="http://www.lsc.nd.edu/research/mtl/">MTL</a> by Jeremy 46Siek et al.</li> 47</ul> 48<p>BLAS seems to be the most widely used library for basic linear 49algebra constructions, so it could be called a de-facto standard. 50Its interface is procedural, the individual functions are somewhat 51abstracted from simple linear algebra operations. Due to the fact 52that is has been implemented using Fortran and its optimizations, 53it also seems to be one of the fastest libraries available. As we 54decided to design and implement our library in an object-oriented 55way, the technical approaches are distinct. However anyone should 56be able to express BLAS abstractions in terms of our library 57operators and to compare the efficiency of the implementations.</p> 58<p>Blitz++ is an impressive library implemented in C++. Its main 59design seems to be oriented towards multidimensional arrays and 60their associated operators including tensors. The author of Blitz++ 61states, that the library achieves performance on par or better than 62corresponding Fortran code due to his implementation technique 63using expression templates and template metaprograms. However we 64see some reasons, to develop an own design and implementation 65approach. We do not know whether anybody tries to implement 66traditional linear algebra and other numerical algorithms using 67Blitz++. We also presume that even today Blitz++ needs the most 68advanced C++ compiler technology due to its implementation idioms. 69On the other hand, Blitz++ convinced us, that the use of expression 70templates is mandatory to reduce the abstraction penalty to an 71acceptable limit.</p> 72<p>POOMA's design goals seem to parallel Blitz++'s in many parts . 73It extends Blitz++'s concepts with classes from the domains of 74partial differential equations and theoretical physics. The 75implementation supports even parallel architectures.</p> 76<p>MTL is another approach supporting basic linear algebra 77operations in C++. Its design mainly seems to be influenced by BLAS 78and the C++ Standard Template Library. We share the insight that a 79linear algebra library has to provide functionality comparable to 80BLAS. On the other hand we think, that the concepts of the C++ 81standard library have not yet been proven to support numerical 82computations as needed. As another difference MTL currently does 83not seem to use expression templates. This may result in one of two 84consequences: a possible loss of expressiveness or a possible loss 85of performance.</p> 86<h2>Concepts</h2> 87<h3>Mathematical Notation</h3> 88<p>The usage of mathematical notation may ease the development of 89scientific algorithms. So a C++ library implementing basic linear 90algebra concepts carefully should overload selected C++ operators 91on matrix and vector classes.</p> 92<p>We decided to use operator overloading for the following 93primitives:</p> 94<table border="1" summary="operators"> 95<tbody> 96<tr> 97<th align="left">Description</th> 98<th align="left">Operator</th> 99</tr> 100<tr> 101<td>Indexing of vectors and matrices</td> 102<td><code>vector::operator(size_t i);<br /> 103matrix::operator(size_t i, size_t j);</code></td> 104</tr> 105<tr> 106<td>Assignment of vectors and matrices</td> 107<td><code>vector::operator = (const vector_expression &);<br /> 108vector::operator += (const vector_expression &);<br /> 109vector::operator -= (const vector_expression &);<br /> 110vector::operator *= (const scalar_expression &);<br /> 111matrix::operator = (const matrix_expression &);<br /> 112matrix::operator += (const matrix_expression &);<br /> 113matrix::operator -= (const matrix_expression &);<br /> 114matrix::operator *= (const scalar_expression &);</code></td> 115</tr> 116<tr> 117<td>Unary operations on vectors and matrices</td> 118<td><code>vector_expression operator - (const vector_expression 119&);<br /> 120matrix_expression operator - (const matrix_expression 121&);</code></td> 122</tr> 123<tr> 124<td>Binary operations on vectors and matrices</td> 125<td><code>vector_expression operator + (const vector_expression 126&, const vector_expression &);<br /> 127vector_expression operator - (const vector_expression &, const 128vector_expression &);<br /> 129matrix_expression operator + (const matrix_expression &, const 130matrix_expression &);<br /> 131matrix_expression operator - (const matrix_expression &, const 132matrix_expression &);</code></td> 133</tr> 134<tr> 135<td>Multiplication of vectors and matrices with a scalar</td> 136<td><code>vector_expression operator * (const scalar_expression 137&, const vector_expression &);<br /> 138vector_expression operator * (const vector_expression &, const 139scalar_expression &);<br /> 140matrix_expression operator * (const scalar_expression &, const 141matrix_expression &);<br /> 142matrix_expression operator * (const matrix_expression &, const 143scalar_expression &);</code></td> 144</tr> 145</tbody> 146</table> 147<p>We decided to use no operator overloading for the following 148other primitives:</p> 149<table border="1" summary="functions"> 150<tbody> 151<tr> 152<th align="left">Description</th> 153<th align="left">Function</th> 154</tr> 155<tr> 156<td>Left multiplication of vectors with a matrix</td> 157<td><code>vector_expression 158prod<</code><code><em>vector_type</em></code> <code>> (const 159matrix_expression &, const vector_expression &);<br /> 160vector_expression prod (const matrix_expression &, const 161vector_expression &);</code></td> 162</tr> 163<tr> 164<td>Right multiplication of vectors with a matrix</td> 165<td><code>vector_expression 166prod<</code><code><em>vector_type</em></code> <code>> (const 167vector_expression &, const matrix_expression &);<br /> 168vector_expression prod (const vector_expression &, const 169matrix_expression &);<br /></code></td> 170</tr> 171<tr> 172<td>Multiplication of matrices</td> 173<td><code>matrix_expression 174prod<</code><code><em>matrix_type</em></code> <code>> (const 175matrix_expression &, const matrix_expression &);<br /> 176matrix_expression prod (const matrix_expression &, const 177matrix_expression &);</code></td> 178</tr> 179<tr> 180<td>Inner product of vectors</td> 181<td><code>scalar_expression inner_prod (const vector_expression 182&, const vector_expression &);</code></td> 183</tr> 184<tr> 185<td>Outer product of vectors</td> 186<td><code>matrix_expression outer_prod (const vector_expression 187&, const vector_expression &);</code></td> 188</tr> 189<tr> 190<td>Transpose of a matrix</td> 191<td><code>matrix_expression trans (const matrix_expression 192&);</code></td> 193</tr> 194</tbody> 195</table> 196<h3>Efficiency</h3> 197<p>To achieve the goal of efficiency for numerical computing, one 198has to overcome two difficulties in formulating abstractions with 199C++, namely temporaries and virtual function calls. Expression 200templates solve these problems, but tend to slow down compilation 201times.</p> 202<h4>Eliminating Temporaries</h4> 203<p>Abstract formulas on vectors and matrices normally compose a 204couple of unary and binary operations. The conventional way of 205evaluating such a formula is first to evaluate every leaf operation 206of a composition into a temporary and next to evaluate the 207composite resulting in another temporary. This method is expensive 208in terms of time especially for small and space especially for 209large vectors and matrices. The approach to solve this problem is 210to use lazy evaluation as known from modern functional programming 211languages. The principle of this approach is to evaluate a complex 212expression element wise and to assign it directly to the 213target.</p> 214<p>Two interesting and dangerous facts result:</p> 215<h4>Aliases</h4> 216<p>One may get serious side effects using element wise 217evaluation on vectors or matrices. Consider the matrix vector 218product <em>x = A x</em>. Evaluation of 219<em>A</em><sub><em>1</em></sub><em>x</em> and assignment to 220<em>x</em><sub><em>1</em></sub> changes the right hand side, so 221that the evaluation of <em>A</em><sub><em>2</em></sub><em>x</em> 222returns a wrong result. In this case there are <strong>aliases</strong> of the elements 223<em>x</em><sub><em>n</em></sub> on both the left and right hand side of the assignment.</p> 224<p>Our solution for this problem is to 225evaluate the right hand side of an assignment into a temporary and 226then to assign this temporary to the left hand side. To allow 227further optimizations, we provide a corresponding member function 228for every assignment operator and also a 229<a href="operations_overview.html#noalias"> <code>noalias</code> syntax.</a> 230By using this syntax a programmer can confirm, that the left and right hand sides of an 231assignment are independent, so that element wise evaluation and 232direct assignment to the target is safe.</p> 233<h4>Complexity</h4> 234<p>The computational complexity may be unexpectedly large under certain 235cirumstances. Consider the chained matrix vector product <em>A (B 236x)</em>. Conventional evaluation of <em>A (B x)</em> is quadratic. 237Deferred evaluation of <em>B x</em><sub><em>i</em></sub> is linear. 238As every element <em>B x</em><sub><em>i</em></sub> is needed 239linearly depending of the size, a completely deferred evaluation of 240the chained matrix vector product <em>A (B x)</em> is cubic. In 241such cases one needs to reintroduce temporaries in the 242expression.</p> 243<h4>Eliminating Virtual Function Calls</h4> 244<p>Lazy expression evaluation normally leads to the definition of a 245class hierarchy of terms. This results in the usage of dynamic 246polymorphism to access single elements of vectors and matrices, 247which is also known to be expensive in terms of time. A solution 248was found a couple of years ago independently by David Vandervoorde 249and Todd Veldhuizen and is commonly called expression templates. 250Expression templates contain lazy evaluation and replace dynamic 251polymorphism with static, i.e. compile time polymorphism. 252Expression templates heavily depend on the famous Barton-Nackman 253trick, also coined 'curiously defined recursive templates' by Jim 254Coplien.</p> 255<p>Expression templates form the base of our implementation.</p> 256<h4>Compilation times</h4> 257<p>It is also a well known fact, that expression templates 258challenge currently available compilers. We were able to 259significantly reduce the amount of needed expression templates 260using the Barton-Nackman trick consequently.</p> 261<p>We also decided to support a dual conventional implementation 262(i.e. not using expression templates) with extensive bounds and 263type checking of vector and matrix operations to support the 264development cycle. Switching from debug mode to release mode is 265controlled by the <code>NDEBUG</code> preprocessor symbol of 266<code><cassert></code>.</p> 267 268<h2><a name="functionality">Functionality</h2> 269 270<p>Every C++ library supporting linear algebra will be measured 271against the long-standing Fortran package BLAS. We now describe how 272BLAS calls may be mapped onto our classes.</p> 273 274<p>The page <a href="operations_overview.html">Overview of Matrix and Vector Operations</a> 275gives a short summary of the most used operations on vectors and 276matrices.</p> 277 278<h4>Blas Level 1</h4> 279<table border="1" summary="level 1 blas"> 280<tbody> 281<tr> 282<th align="left">BLAS Call</th> 283<th align="left">Mapped Library Expression</th> 284<th align="left">Mathematical Description</th> 285<th align="left">Comment</th> 286</tr> 287<tr> 288<td><code>sasum</code> OR <code>dasum</code></td> 289<td><code>norm_1 (x)</code></td> 290<td><em>sum |x<sub>i</sub>|</em></td> 291<td>Computes the <em>l<sub>1</sub></em> (sum) norm of a real vector.</td> 292</tr> 293<tr> 294<td><code>scasum</code> OR <code>dzasum</code></td> 295<td><em><code>real (sum (v)) + imag (sum (v))</code></em></td> 296<td><em>sum re(x<sub>i</sub>) + sum im(x<sub>i</sub>)</em></td> 297<td>Computes the sum of elements of a complex vector.</td> 298</tr> 299<tr> 300<td><code>_nrm2</code></td> 301<td><code>norm_2 (x)</code></td> 302<td><em>sqrt (sum 303|x</em><sub><em>i</em></sub>|<sup><em>2</em></sup> <em>)</em></td> 304<td>Computes the <em>l<sub>2</sub></em> (euclidean) norm of a vector.</td> 305</tr> 306<tr> 307<td><code>i_amax</code></td> 308<td><code>norm_inf (x)<br /> 309index_norm_inf (x)</code></td> 310<td><em>max |x</em><sub><em>i</em></sub><em>|</em></td> 311<td>Computes the <em>l<sub>inf</sub></em> (maximum) norm of a vector.<br /> 312BLAS computes the index of the first element having this 313value.</td> 314</tr> 315<tr> 316<td><code>_dot<br /> 317_dotu<br /> 318_dotc</code></td> 319<td><code>inner_prod (x, y)</code>or<code><br /> 320inner_prod (conj (x), y)</code></td> 321<td><em>x</em><sup><em>T</em></sup> <em>y</em> or<br /> 322<em>x</em><sup><em>H</em></sup> <em>y</em></td> 323<td>Computes the inner product of two vectors.<br /> 324BLAS implements certain loop unrollment.</td> 325</tr> 326<tr> 327<td><code>dsdot<br /> 328sdsdot</code></td> 329<td><code>a + prec_inner_prod (x, y)</code></td> 330<td><em>a + x</em><sup><em>T</em></sup> <em>y</em></td> 331<td>Computes the inner product in double precision.</td> 332</tr> 333<tr> 334<td><code>_copy</code></td> 335<td><code>x = y<br /> 336y.assign (x)</code></td> 337<td><em>x <- y</em></td> 338<td>Copies one vector to another.<br /> 339BLAS implements certain loop unrollment.</td> 340</tr> 341<tr> 342<td><code>_swap</code></td> 343<td><code>swap (x, y)</code></td> 344<td><em>x <-> y</em></td> 345<td>Swaps two vectors.<br /> 346BLAS implements certain loop unrollment.</td> 347</tr> 348<tr> 349<td><code>_scal<br /> 350csscal<br /> 351zdscal</code></td> 352<td><code>x *= a</code></td> 353<td><em>x <- a x</em></td> 354<td>Scales a vector.<br /> 355BLAS implements certain loop unrollment.</td> 356</tr> 357<tr> 358<td><code>_axpy</code></td> 359<td><code>y += a * x</code></td> 360<td><em>y <- a x + y</em></td> 361<td>Adds a scaled vector.<br /> 362BLAS implements certain loop unrollment.</td> 363</tr> 364<tr> 365<td><code>_rot<br /> 366_rotm<br /> 367csrot<br /> 368zdrot</code></td> 369<td><code>t.assign (a * x + b * y),<br /> 370y.assign (- b * x + a * y),<br /> 371x.assign (t)</code></td> 372<td><em>(x, y) <- (a x + b y, -b x + a y)</em></td> 373<td>Applies a plane rotation.</td> 374</tr> 375<tr> 376<td><code>_rotg<br /> 377_rotmg</code></td> 378<td> </td> 379<td><em>(a, b) <-<br /> 380 (? a / sqrt (a</em><sup><em>2</em></sup> + 381<em>b</em><sup><em>2</em></sup><em>),<br /> 382 ? b / sqrt (a</em><sup><em>2</em></sup> + 383<em>b</em><sup><em>2</em></sup><em>))</em> or<em><br /> 384(1, 0) <- (0, 0)</em></td> 385<td>Constructs a plane rotation.</td> 386</tr> 387</tbody> 388</table> 389<h4>Blas Level 2</h4> 390<table border="1" summary="level 2 blas"> 391<tbody> 392<tr> 393<th align="left">BLAS Call</th> 394<th align="left">Mapped Library Expression</th> 395<th align="left">Mathematical Description</th> 396<th align="left">Comment</th> 397</tr> 398<tr> 399<td><code>_t_mv</code></td> 400<td><code>x = prod (A, x)</code> or<code><br /> 401x = prod (trans (A), x)</code> or<code><br /> 402x = prod (herm (A), x)</code></td> 403<td><em>x <- A x</em> or<em><br /> 404x <- A</em><sup><em>T</em></sup> <em>x</em> or<em><br /> 405x <- A</em><sup><em>H</em></sup> <em>x</em></td> 406<td>Computes the product of a matrix with a vector.</td> 407</tr> 408<tr> 409<td><code>_t_sv</code></td> 410<td><code>y = solve (A, x, tag)</code> or<br /> 411<code>inplace_solve (A, x, tag)</code> or<br /> 412<code>y = solve (trans (A), x, tag)</code> or<br /> 413<code>inplace_solve (trans (A), x, tag)</code> or<br /> 414<code>y = solve (herm (A), x, tag)</code>or<br /> 415<code>inplace_solve (herm (A), x, tag)</code></td> 416<!-- TODO: replace nested sub/sup --> 417<td><em>y <- A</em><sup><em>-1</em></sup> <em>x</em> 418or<em><br /> 419x <- A</em><sup><em>-1</em></sup> <em>x</em> or<em><br /> 420y <- 421A</em><sup><em>T</em></sup><sup><sup><em>-1</em></sup></sup> 422<em>x</em> or<em><br /> 423x <- 424A</em><sup><em>T</em></sup><sup><sup><em>-1</em></sup></sup> 425<em>x</em> or<em><br /> 426y <- 427A</em><sup><em>H</em></sup><sup><sup><em>-1</em></sup></sup> 428<em>x</em> or<em><br /> 429x <- 430A</em><sup><em>H</em></sup><sup><sup><em>-1</em></sup></sup> 431<em>x</em></td> 432<td>Solves a system of linear equations with triangular form, i.e. 433<em>A</em> is triangular.</td> 434</tr> 435<tr> 436<td><code>_g_mv<br /> 437_s_mv<br /> 438_h_mv</code></td> 439<td><code>y = a * prod (A, x) + b * y</code> or<code><br /> 440y = a * prod (trans (A), x) + b * y</code> or<code><br /> 441y = a * prod (herm (A), x) + b * y</code></td> 442<td><em>y <- a A x + b y</em> or<em><br /> 443y <- a A</em><sup><em>T</em></sup> <em>x + b y<br /> 444y <- a A</em><sup><em>H</em></sup> <em>x + b y</em></td> 445<td>Adds the scaled product of a matrix with a vector.</td> 446</tr> 447<tr> 448<td><code>_g_r<br /> 449_g_ru<br /> 450_g_rc</code></td> 451<td><code>A += a * outer_prod (x, y)</code> or<code><br /> 452A += a * outer_prod (x, conj (y))</code></td> 453<td><em>A <- a x y</em><sup><em>T</em></sup> <em>+ A</em> 454or<em><br /> 455A <- a x y</em><sup><em>H</em></sup> <em>+ A</em></td> 456<td>Performs a rank <em>1</em> update.</td> 457</tr> 458<tr> 459<td><code>_s_r<br /> 460_h_r</code></td> 461<td><code>A += a * outer_prod (x, x)</code> or<code><br /> 462A += a * outer_prod (x, conj (x))</code></td> 463<td><em>A <- a x x</em><sup><em>T</em></sup> <em>+ A</em> 464or<em><br /> 465A <- a x x</em><sup><em>H</em></sup> <em>+ A</em></td> 466<td>Performs a symmetric or hermitian rank <em>1</em> update.</td> 467</tr> 468<tr> 469<td><code>_s_r2<br /> 470_h_r2</code></td> 471<td><code>A += a * outer_prod (x, y) +<br /> 472 a * outer_prod (y, x))</code> or<code><br /> 473A += a * outer_prod (x, conj (y)) +<br /> 474 conj (a) * outer_prod (y, conj (x)))</code></td> 475<td><em>A <- a x y</em><sup><em>T</em></sup> <em>+ a y 476x</em><sup><em>T</em></sup> <em>+ A</em> or<em><br /> 477A <- a x y</em><sup><em>H</em></sup> <em>+ 478a</em><sup><em>-</em></sup> <em>y x</em><sup><em>H</em></sup> <em>+ 479A</em></td> 480<td>Performs a symmetric or hermitian rank <em>2</em> update.</td> 481</tr> 482</tbody> 483</table> 484<h4>Blas Level 3</h4> 485<table border="1" summary="level 3 blas"> 486<tbody> 487<tr> 488<th align="left">BLAS Call</th> 489<th align="left">Mapped Library Expression</th> 490<th align="left">Mathematical Description</th> 491<th align="left">Comment</th> 492</tr> 493<tr> 494<td><code>_t_mm</code></td> 495<td><code>B = a * prod (A, B)</code> or<br /> 496<code>B = a * prod (trans (A), B)</code> or<br /> 497<code>B = a * prod (A, trans (B))</code> or<br /> 498<code>B = a * prod (trans (A), trans (B))</code> or<br /> 499<code>B = a * prod (herm (A), B)</code> or<br /> 500<code>B = a * prod (A, herm (B))</code> or<br /> 501<code>B = a * prod (herm (A), trans (B))</code> or<br /> 502<code>B = a * prod (trans (A), herm (B))</code> or<br /> 503<code>B = a * prod (herm (A), herm (B))</code></td> 504<td><em>B <- a op (A) op (B)</em> with<br /> 505 <em>op (X) = X</em> or<br /> 506 <em>op (X) = X</em><sup><em>T</em></sup> or<br /> 507 <em>op (X) = X</em><sup><em>H</em></sup></td> 508<td>Computes the scaled product of two matrices.</td> 509</tr> 510<tr> 511<td><code>_t_sm</code></td> 512<td><code>C = solve (A, B, tag)</code> or<br /> 513<code>inplace_solve (A, B, tag)</code> or<br /> 514<code>C = solve (trans (A), B, tag)</code> or<code><br /> 515inplace_solve (trans (A), B, tag)</code> or<code><br /> 516C = solve (herm (A), B, tag)</code> or<code><br /> 517inplace_solve (herm (A), B, tag)</code></td> 518<td><em>C <- A</em><sup><em>-1</em></sup> <em>B</em> 519or<em><br /> 520B <- A</em><sup><em>-1</em></sup> <em>B</em> or<em><br /> 521C <- 522A</em><sup><em>T</em></sup><sup><sup><em>-1</em></sup></sup> 523<em>B</em> or<em><br /> 524B <- A</em><sup><em>-1</em></sup> <em>B</em> or<em><br /> 525C <- 526A</em><sup><em>H</em></sup><sup><sup><em>-1</em></sup></sup> 527<em>B</em> or<em><br /> 528B <- 529A</em><sup><em>H</em></sup><sup><sup><em>-1</em></sup></sup> 530<em>B</em></td> 531<td>Solves a system of linear equations with triangular form, i.e. 532<em>A</em> is triangular.</td> 533</tr> 534<tr> 535<td><code>_g_mm<br /> 536_s_mm<br /> 537_h_mm</code></td> 538<td><code>C = a * prod (A, B) + b * C</code> or<br /> 539<code>C = a * prod (trans (A), B) + b * C</code> or<br /> 540<code>C = a * prod (A, trans (B)) + b * C</code> or<br /> 541<code>C = a * prod (trans (A), trans (B)) + b * C</code> or<br /> 542<code>C = a * prod (herm (A), B) + b * C</code> or<br /> 543<code>C = a * prod (A, herm (B)) + b * C</code> or<br /> 544<code>C = a * prod (herm (A), trans (B)) + b * C</code> or<br /> 545<code>C = a * prod (trans (A), herm (B)) + b * C</code> or<br /> 546<code>C = a * prod (herm (A), herm (B)) + b * C</code></td> 547<td><em>C <- a op (A) op (B) + b C</em> with<br /> 548 <em>op (X) = X</em> or<br /> 549 <em>op (X) = X</em><sup><em>T</em></sup> or<br /> 550 <em>op (X) = X</em><sup><em>H</em></sup></td> 551<td>Adds the scaled product of two matrices.</td> 552</tr> 553<tr> 554<td><code>_s_rk<br /> 555_h_rk</code></td> 556<td><code>B = a * prod (A, trans (A)) + b * B</code> or<br /> 557<code>B = a * prod (trans (A), A) + b * B</code> or<br /> 558<code>B = a * prod (A, herm (A)) + b * B</code> or<br /> 559<code>B = a * prod (herm (A), A) + b * B</code></td> 560<td><em>B <- a A A</em><sup><em>T</em></sup> <em>+ b B</em> 561or<em><br /> 562B <- a A</em><sup><em>T</em></sup> <em>A + b B</em> or<br /> 563<em>B <- a A A</em><sup><em>H</em></sup> <em>+ b B</em> 564or<em><br /> 565B <- a A</em><sup><em>H</em></sup> <em>A + b B</em></td> 566<td>Performs a symmetric or hermitian rank <em>k</em> update.</td> 567</tr> 568<tr> 569<td><code>_s_r2k<br /> 570_h_r2k</code></td> 571<td><code>C = a * prod (A, trans (B)) +<br /> 572 a * prod (B, trans (A)) + b * C</code> or<br /> 573<code>C = a * prod (trans (A), B) +<br /> 574 a * prod (trans (B), A) + b * C</code> or<br /> 575<code>C = a * prod (A, herm (B)) +<br /> 576 conj (a) * prod (B, herm (A)) + b * C</code> or<br /> 577<code>C = a * prod (herm (A), B) +<br /> 578 conj (a) * prod (herm (B), A) + b * C</code></td> 579<td><em>C <- a A B</em><sup><em>T</em></sup> <em>+ a B 580A</em><sup><em>T</em></sup> <em>+ b C</em> or<em><br /> 581C <- a A</em><sup><em>T</em></sup> <em>B + a 582B</em><sup><em>T</em></sup> <em>A + b C</em> or<em><br /> 583C <- a A B</em><sup><em>H</em></sup> <em>+ 584a</em><sup><em>-</em></sup> <em>B A</em><sup><em>H</em></sup> <em>+ 585b C</em> or<em><br /> 586C <- a A</em><sup><em>H</em></sup> <em>B + 587a</em><sup><em>-</em></sup> <em>B</em><sup><em>H</em></sup> <em>A + 588b C</em></td> 589<td>Performs a symmetric or hermitian rank <em>2 k</em> 590update.</td> 591</tr> 592</tbody> 593</table> 594 595<h2>Storage Layout</h2> 596 597<p>uBLAS supports many different storage layouts. The full details can be 598found at the <a href="types_overview.html">Overview of Types</a>. Most types like 599<code>vector<double></code> and <code>matrix<double></code> are 600by default compatible to C arrays, but can also be configured to contain 601FORTAN compatible data. 602</p> 603 604<h2>Compatibility</h2> 605<p>For compatibility reasons we provide array like indexing for vectors and matrices. For some types (hermitian, sparse etc) this can be expensive for matrices due to the needed temporary proxy objects.</p> 606<p>uBLAS uses STL compatible allocators for the allocation of the storage required for it's containers.</p> 607<h2>Benchmark Results</h2> 608<p>The following tables contain results of one of our benchmarks. 609This benchmark compares a native C implementation ('C array') and 610some library based implementations. The safe variants based on the 611library assume aliasing, the fast variants do not use temporaries 612and are functionally equivalent to the native C implementation. 613Besides the generic vector and matrix classes the benchmark 614utilizes special classes <code>c_vector</code> and 615<code>c_matrix</code>, which are intended to avoid every overhead 616through genericity.</p> 617<p>The benchmark program <strong>bench1</strong> was compiled with GCC 4.0 and run on an Athlon 64 3000+. Times are scales for reasonable precision by running <strong>bench1 100</strong>.</p> 618<p>First we comment the results for double vectors and matrices of dimension 3 and 3 x 3, respectively.</p> 619<table border="1" summary="1st benchmark"> 620<tbody> 621<tr> 622<th align="left">Comment</th> 623</tr> 624<tr> 625<td rowspan="3">inner_prod</td> 626<td>C array</td> 627<td align="right">0.61</td> 628<td align="right">782</td> 629<td rowspan="3">Some abstraction penalty</td> 630</tr> 631<tr> 632<td>c_vector</td> 633<td align="right">0.86</td> 634<td align="right">554</td> 635</tr> 636<tr> 637<td>vector<unbounded_array></td> 638<td align="right">1.02</td> 639<td align="right">467</td> 640</tr> 641<tr> 642<td rowspan="5">vector + vector</td> 643<td>C array</td> 644<td align="right">0.51</td> 645<td align="right">1122</td> 646<td rowspan="5">Abstraction penalty: factor 2</td> 647</tr> 648<tr> 649<td>c_vector fast</td> 650<td align="right">1.17</td> 651<td align="right">489</td> 652</tr> 653<tr> 654<td>vector<unbounded_array> fast</td> 655<td align="right">1.32</td> 656<td align="right">433</td> 657</tr> 658<tr> 659<td>c_vector safe</td> 660<td align="right">2.02</td> 661<td align="right">283</td> 662</tr> 663<tr> 664<td>vector<unbounded_array> safe</td> 665<td align="right">6.95</td> 666<td align="right">82</td> 667</tr> 668<tr> 669<td rowspan="5">outer_prod</td> 670<td>C array</td> 671<td align="right">0.59</td> 672<td align="right">872</td> 673<td rowspan="5">Some abstraction penalty</td> 674</tr> 675<tr> 676<td>c_matrix, c_vector fast</td> 677<td align="right">0.88</td> 678<td align="right">585</td> 679</tr> 680<tr> 681<td>matrix<unbounded_array>, vector<unbounded_array> fast</td> 682<td align="right">0.90</td> 683<td align="right">572</td> 684</tr> 685<tr> 686<td>c_matrix, c_vector safe</td> 687<td align="right">1.66</td> 688<td align="right">310</td> 689</tr> 690<tr> 691<td>matrix<unbounded_array>, vector<unbounded_array> safe</td> 692<td align="right">2.95</td> 693<td align="right">175</td> 694</tr> 695<tr> 696<td rowspan="5">prod (matrix, vector)</td> 697<td>C array</td> 698<td align="right">0.64</td> 699<td align="right">671</td> 700<td rowspan="5">No significant abstraction penalty</td> 701</tr> 702<tr> 703<td>c_matrix, c_vector fast</td> 704<td align="right">0.70</td> 705<td align="right">613</td> 706</tr> 707<tr> 708<td>matrix<unbounded_array>, vector<unbounded_array> fast</td> 709<td align="right">0.79</td> 710<td align="right">543</td> 711</tr> 712<tr> 713<td>c_matrix, c_vector safe</td> 714<td align="right">0.95</td> 715<td align="right">452</td> 716</tr> 717<tr> 718<td>matrix<unbounded_array>, vector<unbounded_array> safe</td> 719<td align="right">2.61</td> 720<td align="right">164</td> 721</tr> 722<tr> 723<td rowspan="5">matrix + matrix</td> 724<td>C array</td> 725<td align="right">0.75</td> 726<td align="right">686</td> 727<td rowspan="5">No significant abstraction penalty</td> 728</tr> 729<tr> 730<td>c_matrix fast</td> 731<td align="right">0.99</td> 732<td align="right">520</td> 733</tr> 734<tr> 735<td>matrix<unbounded_array> fast</td> 736<td align="right">1.29</td> 737<td align="right">399</td> 738</tr> 739<tr> 740<td>c_matrix safe</td> 741<td align="right">1.7</td> 742<td align="right">303</td> 743</tr> 744<tr> 745<td>matrix<unbounded_array> safe</td> 746<td align="right">3.14</td> 747<td align="right">164</td> 748</tr> 749<tr> 750<td rowspan="5">prod (matrix, matrix)</td> 751<td>C array</td> 752<td align="right">0.94</td> 753<td align="right">457</td> 754<td rowspan="5">No significant abstraction penalty</td> 755</tr> 756<tr> 757<td>c_matrix fast</td> 758<td align="right">1.17</td> 759<td align="right">367</td> 760</tr> 761<tr> 762<td>matrix<unbounded_array> fast</td> 763<td align="right">1.34</td> 764<td align="right">320</td> 765</tr> 766<tr> 767<td>c_matrix safe</td> 768<td align="right">1.56</td> 769<td align="right">275</td> 770</tr> 771<tr> 772<td>matrix<unbounded_array> safe</td> 773<td align="right">2.06</td> 774<td align="right">208</td> 775</tr> 776</tbody> 777</table> 778<p>We notice a two fold 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.</p> 779<p>Next we comment the results for double vectors and matrices of 780dimension 100 and 100 x 100, respectively.</p> 781<table border="1" summary="2nd benchmark"> 782<tbody> 783<tr> 784<th align="left">Operation</th> 785<th align="left">Implementation</th> 786<th align="left">Elapsed [s]</th> 787<th align="left">MFLOP/s</th> 788<th align="left">Comment</th> 789</tr> 790<tr> 791<td rowspan="3">inner_prod</td> 792<td>C array</td> 793<td align="right">0.64</td> 794<td align="right">889</td> 795<td rowspan="3">No significant abstraction penalty</td> 796</tr> 797<tr> 798<td>c_vector</td> 799<td align="right">0.66</td> 800<td align="right">862</td> 801</tr> 802<tr> 803<td>vector<unbounded_array></td> 804<td align="right">0.66</td> 805<td align="right">862</td> 806</tr> 807<tr> 808<td rowspan="5">vector + vector</td> 809<td>C array</td> 810<td align="right">0.64</td> 811<td align="right">894</td> 812<td rowspan="5">No significant abstraction penalty</td> 813</tr> 814<tr> 815<td>c_vector fast</td> 816<td align="right">0.66</td> 817<td align="right">867</td> 818</tr> 819<tr> 820<td>vector<unbounded_array> fast</td> 821<td align="right">0.66</td> 822<td align="right">867</td> 823</tr> 824<tr> 825<td>c_vector safe</td> 826<td align="right">1.14</td> 827<td align="right">501</td> 828</tr> 829<tr> 830<td>vector<unbounded_array> safe</td> 831<td align="right">1.23</td> 832<td align="right">465</td> 833</tr> 834<tr> 835<td rowspan="5">outer_prod</td> 836<td>C array</td> 837<td align="right">0.50</td> 838<td align="right">1144</td> 839<td rowspan="5">No significant abstraction penalty</td> 840</tr> 841<tr> 842<td>c_matrix, c_vector fast</td> 843<td align="right">0.71</td> 844<td align="right">806</td> 845</tr> 846<tr> 847<td>matrix<unbounded_array>, vector<unbounded_array> fast</td> 848<td align="right">0.57</td> 849<td align="right">1004</td> 850</tr> 851<tr> 852<td>c_matrix, c_vector safe</td> 853<td align="right">1.91</td> 854<td align="right">300</td> 855</tr> 856<tr> 857<td>matrix<unbounded_array>, vector<unbounded_array> safe</td> 858<td align="right">0.89</td> 859<td align="right">643</td> 860</tr> 861<tr> 862<td rowspan="5">prod (matrix, vector)</td> 863<td>C array</td> 864<td align="right">0.65</td> 865<td align="right">876</td> 866<td rowspan="5">No significant abstraction penalty</td> 867</tr> 868<tr> 869<td>c_matrix, c_vector fast</td> 870<td align="right">0.65</td> 871<td align="right">876</td> 872</tr> 873<tr> 874<td>matrix<unbounded_array>, vector<unbounded_array> 875fast</td> 876<td align="right">0.66</td> 877<td align="right">863</td> 878</tr> 879<tr> 880<td>c_matrix, c_vector safe</td> 881<td align="right">0.66</td> 882<td align="right">863</td> 883</tr> 884<tr> 885<td>matrix<unbounded_array>, vector<unbounded_array> 886safe</td> 887<td align="right">0.66</td> 888<td align="right">863</td> 889</tr> 890<tr> 891<td rowspan="5">matrix + matrix</td> 892<td>C array</td> 893<td align="right">0.96</td> 894<td align="right">596</td> 895<td rowspan="5">No significant abstraction penalty</td> 896</tr> 897<tr> 898<td>c_matrix fast</td> 899<td align="right">1.21</td> 900<td align="right">473</td> 901</tr> 902<tr> 903<td>matrix<unbounded_array> fast</td> 904<td align="right">1.00</td> 905<td align="right">572</td> 906</tr> 907<tr> 908<td>c_matrix safe</td> 909<td align="right">2.44</td> 910<td align="right">235</td> 911</tr> 912<tr> 913<td>matrix<unbounded_array> safe</td> 914<td align="right">1.30</td> 915<td align="right">440</td> 916</tr> 917<tr> 918<td rowspan="5">prod (matrix, matrix)</td> 919<td>C array</td> 920<td align="right">0.70</td> 921<td align="right">813</td> 922<td rowspan="5">No significant abstraction penalty</td> 923</tr> 924<tr> 925<td>c_matrix fast</td> 926<td align="right">0.73</td> 927<td align="right">780</td> 928</tr> 929<tr> 930<td>matrix<unbounded_array> fast</td> 931<td align="right">0.76</td> 932<td align="right">749</td> 933</tr> 934<tr> 935<td>c_matrix safe</td> 936<td align="right">0.75</td> 937<td align="right">759</td> 938</tr> 939<tr> 940<td>matrix<unbounded_array> safe</td> 941<td align="right">0.76</td> 942<td align="right">749</td> 943</tr> 944</tbody> 945</table> 946<p>For larger vectors and matrices the general abstraction penalty 947for using classes seems to decrease, the small loss when using 948generic vector and matrix classes seems to remain. The difference 949w.r.t. alias assumptions remains visible, too.</p> 950<hr /> 951<p>Copyright (©) 2000-2002 Joerg Walter, Mathias Koch<br /> 952 Use, modification and distribution are subject to the 953 Boost Software License, Version 1.0. 954 (See accompanying file LICENSE_1_0.txt 955 or copy at <a href="http://www.boost.org/LICENSE_1_0.txt"> 956 http://www.boost.org/LICENSE_1_0.txt 957 </a>). 958</p> 959<script type="text/javascript"> 960(function($) { 961 $('#toc').toc(); 962})(jQuery); 963</script> 964</body> 965</html> 966