==================================== Avoiding the Creation of Temporaries [TOC] ==================================== *By default* FLENS will never create any temporaries during the evaluation of linear algebra expressions. As a consequence some linear algebra expressions can not be evaluated through (CXX)BLAS calls. If possible such cases will trigger a compile time error. If such cases can not be checked at compile time you at least get an runtime assertion unless you compile with `-DNDEBUG`. However, you can *change the default* such that temporaries get created but only when necessary. In addition you get a warning in the log-file such that you subsequently can optimize your code for completely avoiding the unintended creation of temporaries. Of course this become much more useful when your linear algebra expression become more complex. Expressions that require Temporaries ==================================== Consider $x := Ax$ where $x$ is a (dense) vector and $A$ a general matrix then the __CXXBLAS__ function __gemv__ can not be used directly. This is because __gemv__ does computations of the form $y \leftarrow \beta y + \alpha A x$ where $x$ and $y$ do not reference the same memory (i.e. $x$ and $y$ must be different vectors). Hence, we need a temporary $y_\text{tmp}$ and evaluate $x := Ax$ in two steps: - $y_\text{tmp} = x$ (using __copy__). - $x = A y_\text{tmp}$ (using __gemv__), :links: __CXXBLAS__ -> dir:cxxblas/ __copy__ -> file:cxxblas/level1/copy.h __gemv__ -> file:cxxblas/level2/gemv.h Allowing and Logging the Creation of Temporaries ================================================ Like in the last example we turn on logging before the first linear algebra expression get evaluated. :import: flens/examples/tut02-page04-example1.cc [stripped, downloadable] Compile, Run and Examine the Log-File ===================================== Like in the last example we compile the example with `-DFLENS_DEBUG_CLOSURES` and link against some auxiliary object files from the `flens/debug/auxiliary` directory. *--[SHELL]---------------------------------------------------------------* | | | cd flens/examples | | # | | # cleanup old object files and compile some stuff needed for logging | | # | | rm -f *.o | | g++ -Wall -DFLENS_DEBUG_CLOSURES -std=c++11 +++| | -I../.. -c ../../flens/debug/auxiliary/*.cc | | # | | # compile with -DFLENS_DEBUG_CLOSURES and link against the log stuff | | # | | g++ -Wall -DFLENS_DEBUG_CLOSURES -std=c++11 +++| | -I../.. *.o tut02-page04-example1.cc | | ./a.out | | # | | # look what is in the log file | | # | | cat mylogfile | | | *------------------------------------------------------------------------* Other Expressions that require Temporaries (Important!) ======================================================= No Associative property ----------------------- Floating point number are not associative. That means in general we have $x + (y + z) \neq (x + y) + z$ if $x, y$ and $z$ are floating point numbers or matrices/vectors of floating point numbers. In FLENS expressions without parentheses the expression gets done according to the usual C/C++ precedence. That means for example that $x + y + z = (x + y) + z$. No Distributive property ------------------------ There is also no distributive property for floating point numbers. That means for example that $A(x+y) \neq Ax + Ay$ if $A$ is a matrix and $x, y$ are vectors of floating point numbers. Example Code ------------ In the following code example temporaries are required due to the lack of the above property. :import: flens/examples/tut02-page04-example2.cc [stripped, downloadable] Compile, Run and Examine the Log-File ------------------------------------- Like before ... *--[SHELL]---------------------------------------------------------------* | | | cd flens/examples | | # | | # cleanup old object files and compile some stuff needed for logging | | # | | rm -f *.o | | g++ -Wall -DFLENS_DEBUG_CLOSURES -std=c++11 +++| | -I../.. -c ../../flens/debug/auxiliary/*.cc | | # | | # compile with -DFLENS_DEBUG_CLOSURES and link against the log stuff | | # | | g++ -Wall -DFLENS_DEBUG_CLOSURES -std=c++11 +++| | -I../.. *.o tut02-page04-example2.cc | | ./a.out | | # | | # look what is in the log file | | # | | cat mylogfile | | | *------------------------------------------------------------------------* :navigate: __up__ -> doc:flens/examples/tutorial __back__ -> doc:flens/examples/tut02-page03 __next__ -> doc:flens/examples/tut02-page05