overview.html 33 KB

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