|
| 1 | +# Developer and advanced user guide |
| 2 | + |
| 3 | +When creating the CMake build, make sure to add the `-DCMAKE_EXPORT_COMPILE_COMMANDS=1` flag. See its documentation [here](https://cmake.org/cmake/help/latest/variable/CMAKE_EXPORT_COMPILE_COMMANDS.html). |
| 4 | + |
| 5 | +A template project for using **aligator** with CMake and C++ can be found in the [aligator-cmake-example-project](https://github.com/Simple-Robotics/aligator-cmake-example-project) repository. |
| 6 | + |
| 7 | +## Creating a Python extension module |
| 8 | + |
| 9 | +When **aligator** is installed, the CMake configuration file (`aligatorConfig.cmake`) provides a CMake function to help users easily create a [Python extension module](https://docs.python.org/3/extending/extending.html). |
| 10 | +Users can write an extension module in C++ for performance reasons when providing e.g. custom constraints, cost functions, dynamics, and so on. |
| 11 | + |
| 12 | +The CMake function is called as follows: |
| 13 | +```{.cmake} |
| 14 | +aligator_create_python_extension(<name> [WITH_SOABI] <sources...>) |
| 15 | +``` |
| 16 | + |
| 17 | +This will create a CMake `MODULE` target named `<name>` on which the user can set properties and add an `install` directive. |
| 18 | + |
| 19 | +An usage example can be found in [this repo](https://github.com/Simple-Robotics/aligator-cmake-example-project). |
| 20 | + |
| 21 | +## Debugging |
| 22 | + |
| 23 | +### Debugging a C++ executable |
| 24 | + |
| 25 | +This project builds some C++ examples and tests. Debugging them is fairly straightforward using GDB: |
| 26 | + |
| 27 | +```bash |
| 28 | +gdb path/to/executable |
| 29 | +``` |
| 30 | + |
| 31 | +with the appropriate command line arguments. Examples will appear in the binaries of `build/examples`. Make sure to look at GDB's documentation. |
| 32 | + |
| 33 | +If you want to catch `std::exception` instances thrown, enter the following command once in GDB: |
| 34 | + |
| 35 | +```gdb |
| 36 | +(gdb) catch throw std::exception |
| 37 | +``` |
| 38 | + |
| 39 | +### Debugging a Python example or test |
| 40 | + |
| 41 | +In order for debug symbols to be loaded and important variables not being optimized out, you will want to compile in `DEBUG` mode. |
| 42 | + |
| 43 | +Then, you can run the module under `gdb` using |
| 44 | + |
| 45 | +```bash |
| 46 | +gdb --args python example/file.py |
| 47 | +``` |
| 48 | + |
| 49 | +If you want to look at Eigen types such as vectors and matrices, you should look into the [`eigengdb`](https://github.com/dmillard/eigengdb) plugin for GDB. |
| 50 | + |
| 51 | +### Hybrid debugging with Visual Studio Code |
| 52 | + |
| 53 | +[TODO] |
| 54 | + |
| 55 | +## Using **aligator**'s parallelization features |
| 56 | + |
| 57 | +The `SolverProxDDP` solver is able to leverage multicore CPU architectures. |
| 58 | + |
| 59 | +### Inside your code |
| 60 | + |
| 61 | +Before calling the solver make sure to enable parallelization as follows: |
| 62 | + |
| 63 | +In Python: |
| 64 | + |
| 65 | +```python |
| 66 | +solver.rollout_type = aligator.ROLLOUT_LINEAR |
| 67 | +solver.linear_solver_choice = aligator.LQ_SOLVER_PARALLEL |
| 68 | +solver.setNumThreads(num_threads) |
| 69 | +``` |
| 70 | + |
| 71 | +And in C++: |
| 72 | +```cpp |
| 73 | +std::size_t num_threads = 4ul; // for example |
| 74 | +solver.rollout_type = aligator::RolloutType::LINEAR; |
| 75 | +solver.linear_solver_choice = aligator::LQSolverChoice::PARALLEL; |
| 76 | +solver.setNumThreads(num_threads); |
| 77 | +``` |
| 78 | + |
| 79 | +### Shell setup for CPU core optimization |
| 80 | +**Aligator** uses OpenMP for parallelization which is setup using environment variables in your shell. The settings are local to your shell. |
| 81 | + |
| 82 | +#### Visualization |
| 83 | +Printing OpenMP parameters at launch: |
| 84 | +```bash |
| 85 | +export OMP_DISPLAY_ENV=VERBOSE |
| 86 | +``` |
| 87 | +Print when a thread is launched and with which affinity (CPU thread(s) on where it will try to run): |
| 88 | +```bash |
| 89 | +export OMP_DISPLAY_AFFINITY=TRUE |
| 90 | +``` |
| 91 | + |
| 92 | +#### Core and thread assignment |
| 93 | +OpenMP operates with **places** which define a CPU thread or core reserved for a thread. **Places** can be a CPU thread or an entire CPU core (which can have one thread, or multiple with hyperthreading). |
| 94 | + |
| 95 | +##### Assigning places with CPU threads: |
| 96 | +```bash |
| 97 | +export OMP_PLACES ="threads(n)" # Threads will run on the first nth CPU threads, with one thread per CPU thread. |
| 98 | +``` |
| 99 | +or |
| 100 | +```bash |
| 101 | +export OMP_PLACES="{0},{1},{2}" # Threads will run on CPU threads 0, 1 ,2 |
| 102 | +``` |
| 103 | +##### Assigning places with CPU cores: |
| 104 | + |
| 105 | +Threads will run on the first nth CPU cores, with one thread per core, even if the core has multiple threads |
| 106 | +```bash |
| 107 | +export OMP_PLACES="cores(n)" |
| 108 | +``` |
| 109 | + |
| 110 | +For more info on places see [here](https://www.ibm.com/docs/en/xl-fortran-linux/16.1.0?topic=openmp-omp-places). |
| 111 | + |
| 112 | +##### Using only performance cores (Intel performance hybrid architectures) |
| 113 | + |
| 114 | +Some modern CPUs have a mix of performance (P) and efficiency (E) cores. The E-cores are often slower, hence we should |
| 115 | +have OpenMP schedule threads on P-cores only. |
| 116 | + |
| 117 | +Get your CPU model with |
| 118 | +```bash |
| 119 | +lscpu | grep -i "Model Name" |
| 120 | +``` |
| 121 | +Get CPU core info with: |
| 122 | +```bash |
| 123 | +lscpu -e |
| 124 | + |
| 125 | +# with an i7-13800H |
| 126 | +CPU NODE SOCKET CORE L1d:L1i:L2:L3 ONLINE MAXMHZ MINMHZ MHZ |
| 127 | + 0 0 0 0 0:0:0:0 yes 5000.0000 400.0000 400.000 |
| 128 | + 1 0 0 0 0:0:0:0 yes 5000.0000 400.0000 400.000 |
| 129 | + 2 0 0 1 4:4:1:0 yes 5000.0000 400.0000 400.000 |
| 130 | + 3 0 0 1 4:4:1:0 yes 5000.0000 400.0000 400.000 |
| 131 | + 4 0 0 2 8:8:2:0 yes 5200.0000 400.0000 400.000 |
| 132 | + 5 0 0 2 8:8:2:0 yes 5200.0000 400.0000 5176.303 |
| 133 | + 6 0 0 3 12:12:3:0 yes 5200.0000 400.0000 1482.743 |
| 134 | + 7 0 0 3 12:12:3:0 yes 5200.0000 400.0000 400.000 |
| 135 | + 8 0 0 4 16:16:4:0 yes 5000.0000 400.0000 3485.561 |
| 136 | + 9 0 0 4 16:16:4:0 yes 5000.0000 400.0000 721.684 |
| 137 | + 10 0 0 5 20:20:5:0 yes 5000.0000 400.0000 1641.311 |
| 138 | + 11 0 0 5 20:20:5:0 yes 5000.0000 400.0000 400.000 |
| 139 | + 12 0 0 6 24:24:6:0 yes 4000.0000 400.0000 400.000 |
| 140 | + 13 0 0 7 25:25:6:0 yes 4000.0000 400.0000 2949.734 |
| 141 | + 14 0 0 8 26:26:6:0 yes 4000.0000 400.0000 2554.695 |
| 142 | + 15 0 0 9 27:27:6:0 yes 4000.0000 400.0000 3588.623 |
| 143 | + 16 0 0 10 28:28:7:0 yes 4000.0000 400.0000 400.000 |
| 144 | + 17 0 0 11 29:29:7:0 yes 4000.0000 400.0000 400.000 |
| 145 | + 18 0 0 12 30:30:7:0 yes 4000.0000 400.0000 400.000 |
| 146 | + 19 0 0 13 31:31:7:0 yes 4000.0000 400.0000 3610.068 |
| 147 | +``` |
| 148 | +A little digging on the internet tells us that this CPU has 6 performance cores and 8 efficiency cores for a total of 20 threads. We see higher frequencies in core 0 to 5: these are the performance cores. To use only performance cores on this CPU you would set: |
| 149 | +```bash |
| 150 | +export OMP_PLACES="cores(6)" |
| 151 | +# or |
| 152 | +export OMP_PLACES="threads(12)" |
| 153 | +``` |
| 154 | +> [!IMPORTANT] |
| 155 | +> Put your PC in performance mode (usually found in the power settings). |
| 156 | +
|
| 157 | +## Profiling |
| 158 | + |
| 159 | +We use [google benchmark](https://github.com/google/benchmark/tree/v1.5.0) to define C++ benchmarks |
| 160 | +which are able to aggregate data from runs, and [Flame Graphs](https://github.com/brendangregg/FlameGraph) to produce a breakdown of the various function calls and their importance as a proportion of the call stack. |
| 161 | + |
| 162 | +If you have the Rust toolchain and `cargo` installed, we suggest you install [cargo-flamegraph](https://github.com/flamegraph-rs/flamegraph). Then, you can create a flame graph with the following command: |
| 163 | + |
| 164 | +```bash |
| 165 | +flamegraph -o my_flamegraph.svg -- ./build/examples/example-croc-talos-arm |
| 166 | +``` |
| 167 | + |
| 168 | + |
| 169 | +Here's Crocoddyl's flame graph: |
| 170 | + |
| 171 | +Here's for `aligator::SolverFDDP`: |
| 172 | + |
0 commit comments