|
| 1 | +--- |
| 2 | +title: Integrating Tracy Profiler with SU2 |
| 3 | +permalink: /docs_v7/Tracy-Integration/ |
| 4 | +--- |
| 5 | + |
| 6 | +--- |
| 7 | + |
| 8 | +- [Introduction](#introduction) |
| 9 | +- [Prerequisites](#prerequisites) |
| 10 | +- [Integrating Tracy with SU2](#integrating-tracy-with-su2) |
| 11 | +- [Setting Up the Tracy Server](#setting-up-the-tracy-server) |
| 12 | +- [Instrumenting SU2 Code](#instrumenting-su2-code) |
| 13 | +- [Using Tracy for Profiling](#using-tracy-for-profiling) |
| 14 | +- [Conclusion](#conclusion) |
| 15 | + |
| 16 | +--- |
| 17 | + |
| 18 | +## Introduction |
| 19 | + |
| 20 | +Tracy is a high-performance, real-time profiler designed for C++ applications, offering nanosecond-resolution timing with minimal overhead. It is an excellent tool for profiling computationally intensive software like SU2, where traditional profilers such as Valgrind may introduce significant slowdowns. This guide provides step-by-step instructions for integrating Tracy with SU2, enabling users to analyze and optimize the performance of their CFD simulations effectively. |
| 21 | + |
| 22 | +## Prerequisites |
| 23 | + |
| 24 | +Before integrating Tracy with SU2, ensure the following software is installed: |
| 25 | + |
| 26 | +- **SU2 Source Code**: Version 8.2.0 or later. |
| 27 | +- **Meson Build System**: Version 0.61.1 or later. |
| 28 | +- **Git**: For retrieving the Tracy repository. |
| 29 | +- **C++ Compiler**: Such as GCC. |
| 30 | +- **CMake**: Required for building the Tracy profiler. |
| 31 | + |
| 32 | +For Ubuntu users, install additional dependencies required for the Tracy server with: |
| 33 | + |
| 34 | +```bash |
| 35 | +sudo apt install libfreetype6-dev libcapstone-dev libdbus-1-dev \ |
| 36 | +libxkbcommon-dev libwayland-dev wayland-protocols \ |
| 37 | +libegl1-mesa-dev libglvnd-dev libgtk-3-dev |
| 38 | +``` |
| 39 | + |
| 40 | +## Integrating Tracy with SU2 |
| 41 | + |
| 42 | +To embed the Tracy client into SU2, you need to modify the Meson build configuration. The client collects profiling data during runtime. Follow these steps: |
| 43 | + |
| 44 | +1. **Create a Wrap File for Tracy:** |
| 45 | + |
| 46 | + - Navigate to the SU2 source directory: `cd <SU2_SOURCE_DIR>`. |
| 47 | + - Create a file named `subprojects/tracy.wrap` with the following content: |
| 48 | + |
| 49 | + ```ini |
| 50 | + [wrap-git] |
| 51 | + url = https://github.com/wolfpld/tracy.git |
| 52 | + revision = master |
| 53 | + depth = 1 |
| 54 | + ``` |
| 55 | + |
| 56 | +2. **Update Meson Options:** |
| 57 | + |
| 58 | + - Edit `meson_options.txt` (or `meson.options`) in `<SU2_SOURCE_DIR>` to include: |
| 59 | + |
| 60 | + ```meson |
| 61 | + option('tracy_enable', |
| 62 | + type: 'boolean', |
| 63 | + value: false, |
| 64 | + description: 'Enable Tracy profiling support') |
| 65 | + ``` |
| 66 | + |
| 67 | +3. **Modify the Main Meson Build File:** |
| 68 | + |
| 69 | + - Open `meson.build` in `<SU2_SOURCE_DIR>` and add Tracy as a dependency when enabled: |
| 70 | + |
| 71 | + ```meson |
| 72 | + if get_option('tracy_enable') |
| 73 | + tracy_dep = dependency('tracy', static: true) |
| 74 | + su2_deps += tracy_dep |
| 75 | + su2_cpp_args += '-DTRACY_ENABLE' |
| 76 | + |
| 77 | + if get_option('buildtype') != 'debugoptimized' |
| 78 | + warning('For optimal Tracy profiling, use --buildtype=debugoptimized') |
| 79 | + endif |
| 80 | + endif |
| 81 | + ``` |
| 82 | + |
| 83 | + - Update the `default_options` at the top of `meson.build`: |
| 84 | + |
| 85 | + ```meson |
| 86 | + default_options: ['buildtype=release', |
| 87 | + 'warning_level=0', |
| 88 | + 'c_std=c99', |
| 89 | + 'cpp_std=c++11', |
| 90 | + 'tracy_enable=false'] |
| 91 | + ``` |
| 92 | + |
| 93 | + - Update the Build Summary to display Tracy status by inserting `get_option('tracy_enable')` into the summary format string (adjust the index accordingly, e.g., `@14@`): |
| 94 | + |
| 95 | + ```meson |
| 96 | + Tracy Profiler: @14@ |
| 97 | + ``` |
| 98 | + |
| 99 | + And update the install instruction line: |
| 100 | + |
| 101 | + ```meson |
| 102 | + Use './ninja -C @15@ install' to compile and install SU2 |
| 103 | + ``` |
| 104 | + |
| 105 | +4. **Build SU2 with Tracy:** |
| 106 | + |
| 107 | + - Ensure Meson is updated: |
| 108 | + |
| 109 | + ```bash |
| 110 | + pip install --user --upgrade meson |
| 111 | + ``` |
| 112 | + |
| 113 | + - Run the preconfigure script: |
| 114 | + |
| 115 | + ```bash |
| 116 | + ./preconfigure.py |
| 117 | + ``` |
| 118 | + |
| 119 | + - Configure and build SU2: |
| 120 | + |
| 121 | + ```bash |
| 122 | + meson build_tracy -Dwith-mpi=disabled -Denable-pywrapper=true -Denable-mlpcpp=true --buildtype=debugoptimized -Dtracy_enable=true --prefix=<SU2_INSTALL_PATH> |
| 123 | + ninja -C build_tracy install |
| 124 | + ``` |
| 125 | + |
| 126 | + - Replace `<SU2_SOURCE_DIR>` with the path to your SU2 source code and `<SU2_INSTALL_PATH>` with your desired installation directory. |
| 127 | + |
| 128 | +This process integrates the Tracy client into SU2, enabling profiling capabilities. |
| 129 | + |
| 130 | +## Setting Up the Tracy Server |
| 131 | + |
| 132 | +The Tracy server is the graphical application that visualizes profiling data collected by the client. To set it up: |
| 133 | + |
| 134 | +1. **Build the Tracy Profiler:** |
| 135 | + |
| 136 | + - Navigate to the Tracy directory: `cd <SU2_SOURCE_DIR>/subprojects/tracy`. |
| 137 | + - Use CMake to build the profiler: |
| 138 | + |
| 139 | + ```bash |
| 140 | + cmake -B profiler/build -S profiler -DCMAKE_BUILD_TYPE=Release -DLEGACY=ON |
| 141 | + cmake --build profiler/build --config Release --parallel |
| 142 | + ``` |
| 143 | + |
| 144 | + - The `-DLEGACY=ON` flag enables X11 support, which may be necessary for some systems. Omit this flag if Wayland is preferred and supported. |
| 145 | + |
| 146 | +The Tracy server is now ready to display profiling data. |
| 147 | + |
| 148 | +## Instrumenting SU2 Code |
| 149 | + |
| 150 | +To collect meaningful profiling data, instrument the SU2 source code with Tracy macros. For example, to profile a specific function: |
| 151 | + |
| 152 | +1. **Include the Tracy Header:** |
| 153 | + |
| 154 | + - Add the following at the top of the source file: |
| 155 | + |
| 156 | + ```c++ |
| 157 | + #include <tracy/Tracy.hpp> |
| 158 | + ``` |
| 159 | + |
| 160 | +2. **Add Profiling Macros:** |
| 161 | + |
| 162 | + - Instrument the function with `ZoneScopedN`: |
| 163 | + |
| 164 | + ```c++ |
| 165 | + void MyFunction() { |
| 166 | + ZoneScopedN("MyFunction"); |
| 167 | + // Function implementation |
| 168 | + } |
| 169 | + ``` |
| 170 | + |
| 171 | + - The `ZoneScopedN("name")` macro defines a profiling zone, labeled for identification in the Tracy GUI. |
| 172 | + |
| 173 | +Repeat this process for any functions or code sections you wish to profile. |
| 174 | + |
| 175 | +## Using Tracy for Profiling |
| 176 | + |
| 177 | +With the client integrated, the server built, and the code instrumented, you can profile SU2 simulations: |
| 178 | + |
| 179 | +1. **Launch the Tracy Profiler:** |
| 180 | + |
| 181 | + - Navigate to the profiler build directory: |
| 182 | + |
| 183 | + ```bash |
| 184 | + cd <SU2_SOURCE_DIR>/subprojects/tracy/profiler/build |
| 185 | + ``` |
| 186 | + |
| 187 | + - Run the profiler: |
| 188 | + |
| 189 | + ```bash |
| 190 | + ./tracy-profiler |
| 191 | + ``` |
| 192 | + |
| 193 | + - In the GUI, click "Connect" to wait for a connection from SU2. |
| 194 | + |
| 195 | +2. **Execute the Instrumented SU2 Simulation:** |
| 196 | + |
| 197 | + - In a separate terminal, navigate to your simulation directory and run: |
| 198 | + |
| 199 | + ```bash |
| 200 | + <SU2_INSTALL_PATH>/bin/SU2_CFD <your_config_file>.cfg |
| 201 | + ``` |
| 202 | + |
| 203 | + - Replace `<SU2_INSTALL_PATH>` with your installation path and `<your_config_file>` with your configuration file. |
| 204 | + |
| 205 | +As the simulation runs, Tracy will display real-time profiling data, allowing you to analyze performance metrics and identify bottlenecks. |
| 206 | + |
| 207 | +## Conclusion |
| 208 | + |
| 209 | +Integrating Tracy with SU2 equips users with a powerful, low-overhead tool for profiling and optimizing CFD simulations. Its real-time visualization and precise timing capabilities make it ideal for performance analysis. For advanced features, troubleshooting, or additional details, consult the [Tracy documentation](https://github.com/wolfpld/tracy/releases/latest/download/tracy.pdf). |
0 commit comments