Austin Schuh | 7400da0 | 2018-01-28 19:54:58 -0800 | [diff] [blame^] | 1 | /*! |
| 2 | |
| 3 | \page performance Performance Optimization |
| 4 | - \subpage compile_time |
| 5 | - \subpage execution_speed |
| 6 | |
| 7 | The Control Toolbox is optimized for performance and, if used correctly, constitutes one of the fastest |
| 8 | implementation for many state-of-the-art control approaches. This page gives an overview of how to achieve |
| 9 | best performance. |
| 10 | |
| 11 | \page compile_time Optimize Compile Time |
| 12 | @tableofcontents |
| 13 | |
| 14 | Especially with increasing complexity of your project, compilation time of the CT can become long. However, |
| 15 | there are some tricks to reduce compilation time |
| 16 | - make sure you compile in Release mode (catkin build -DCMAKE_BUILD_TYPE=RELEASE) |
| 17 | - use CLANG instead of gcc. See the build flags in the \ref install "Installation Guide" on how to use clang. |
| 18 | - use \ref prespec |
| 19 | |
| 20 | \section prespec Explicit Template Instantiation |
| 21 | The Control Toolbox is a heavily templated library for best runtime performance. However, this means most |
| 22 | code lives in header files and gets recompiled when any changes are made to the code. Needless to say, that |
| 23 | this can become cumbersome after some time. However, there is a simple yet effective workaround: |
| 24 | Explicit template instantiation. The idea is simple: You define the templates that are used before compilation |
| 25 | and they get compiled into a library. In CT templates are: |
| 26 | |
| 27 | Template Parameter | Description |
| 28 | ------------------ | ----------- |
| 29 | STATE_DIM | The dimension of the system's system state |
| 30 | CONTROL_DIM | The dimension of the system's control input |
| 31 | SCALAR | The scalar type used (usually double) |
| 32 | POS_DIM | (optional) Dimension of the position vector for a symplectic system |
| 33 | VEL_DIM | (optional) Dimension of the velocity vector for a symplectic system |
| 34 | |
| 35 | In case you are multiple systems of different dimensions, you can prespecify each of their dimensions. |
| 36 | |
| 37 | To use explicit template instantiation follow these steps: |
| 38 | 1. add your dimensions to ct/ct/config/explicit_templates.cfg . You can set POS_DIM and VEL_DIM to 0 if you |
| 39 | are not using symplectic integrators. |
| 40 | 2. rerun cmake: catkin build -DCMAKE_BUILD_TYPE=RELEASE --force-cmake |
| 41 | 3. In your executable change the standard CT includes from their regular ones to the prespecified ones, |
| 42 | e.g. change \code{.cpp}#include <ct/core/core.h>\endcode to \code{.cpp}#include <ct/core/core-prespec.h>\endcode Remember to do this for |
| 43 | optcon and rbd as well. |
| 44 | |
| 45 | \page execution_speed Optimize Execution Speed |
| 46 | @tableofcontents |
| 47 | |
| 48 | - make use of \ref vectorization "Vectorization" |
| 49 | - use \ref core_tut_linearization "Auto-Differentiation" with just-in-time compilation or code generation |
| 50 | - if you do not want to use Auto-Diff, consider using the ct::rbd::RbdLinearizer for linearizing Rigid Body Dynamics |
| 51 | - use multi-threading for Nonlinear Optimal Control Solvers |
| 52 | - use HPIPM for running MPC or solving Optimal Control problems |
| 53 | |
| 54 | |
| 55 | \section vectorization Vectorization |
| 56 | Vectorization is a processor feature where a Single Instruction is applied to Multiple Data (SIMD). This |
| 57 | is especially useful for linear algebra operations. CT relies on [Eigen's Vectorization](http://eigen.tuxfamily.org/index.php?title=FAQ) |
| 58 | capabilities. This means CT supports SSE, FMA and AVX2 instructions. |
| 59 | |
| 60 | \warning Please study Eigen's documentation carefully. Especially the part about [memory alignment]( |
| 61 | https://eigen.tuxfamily.org/dox/group__DenseMatrixManipulation__Alignement.html) |
| 62 | |
| 63 | To enable vectorization in CT build it with vectorization flags. E.g. if you are on a fairly recent Intel CPU |
| 64 | the following build command will enable vectorization |
| 65 | |
| 66 | catkin build -DCMAKE_BUILD_TYPE=RELEASE -DCMAKE_CXX_FLAGS="-march=native -mtune=native -mavx2 -mfma" |
| 67 | |
| 68 | */ |