GSoC Week 3: Profiling SU2 with Tracy

6 minute read

Published:

Introduction

SU2 is an open-source computational fluid dynamics (CFD) software suite widely used by researchers and engineers to simulate complex fluid flow problems. As part of my Google Summer of Code (GSoC) project, I am focused on improving the MLPCpp submodule within SU2, which integrates machine learning techniques to enhance CFD simulations. To achieve meaningful performance improvements, I required a profiling tool capable of providing detailed insights into execution time and resource utilization. While Valgrind is a well-known profiler, its significant overhead makes it less suitable for large-scale simulations. This led me to adopt Tracy, a real-time, nanosecond-resolution profiler with minimal overhead and an intuitive interface. In this blog post, I outline the process of integrating Tracy with SU2 and demonstrate its application in profiling my GSoC work.

What is Tracy?

Tracy is a high-performance profiler designed for C++ applications, offering real-time profiling with exceptionally low overhead. Unlike Valgrind, which can drastically slow down execution, Tracy is optimized for use in production environments, making it ideal for performance analysis in computationally intensive applications like SU2. Key features include:

  • Real-time visualization of profiling data.
  • Nanosecond-resolution timing for precise measurements.
  • Customizable instrumentation for targeted profiling.
  • A graphical user interface (GUI) for analyzing results.

For a deeper dive into Tracy’s capabilities, refer to the Tracy documentation.

Setting Up Tracy with SU2

Integrating Tracy with SU2 involves three main steps: installing the client, setting up the server, and instrumenting the code. Below, I detail the process based on my setup: Ubuntu 22.04.5 LTS, SU2 v8.2.0 (“Harrier”), Meson build system (≥0.61.1), and Git.

Client Installation

The “client” refers to the Tracy library embedded within the SU2 executable, responsible for collecting profiling data during runtime. To integrate it into SU2 using Meson, follow these steps:

  1. Create a wrap file for Tracy:
    • Navigate to the SU2 directory: cd SU2.
    • Create a file named subprojects/tracy.wrap with the following content:
      [wrap-git]
      url = https://github.com/wolfpld/tracy.git
      revision = master
      depth = 1
      
  2. Update Meson options:
    • Edit meson_options.txt (or meson.options) to add:
      option('tracy_enable', 
             type: 'boolean', 
             value: false, 
             description: 'Enable Tracy profiling support')
      
  3. Modify the main Meson build file:
    • In meson.build, add Tracy as a dependency when enabled:
      if get_option('tracy_enable')
          tracy_dep = dependency('tracy', static: true)
          su2_deps += tracy_dep
          su2_cpp_args += '-DTRACY_ENABLE'
               
          if get_option('buildtype') != 'debugoptimized'
              warning('For optimal Tracy profiling, use --buildtype=debugoptimized')
          endif
      endif
      
    • Update the default options at the top:
      default_options: ['buildtype=release',
                        'warning_level=0',
                        'c_std=c99',
                        'cpp_std=c++11',
                        'tracy_enable=false']
      
    • Update the Build Summary To display whether Tracy support is enabled in the terminal summary, modify the format string at the end of meson.build: Insert the get_option('tracy_enable') as shown below:

      '''.format(get_option('prefix')+'/bin', meson.project_source_root(), get_option('enable-tecio'), get_option('enable-cgns'),
               get_option('enable-autodiff'), get_option('enable-directdiff'), get_option('enable-pywrapper'), get_option('enable-mkl'),
               get_option('enable-openblas'), get_option('enable-pastix'), get_option('enable-mixedprec'), get_option('enable-librom'),
               get_option('enable-coolprop'), get_option('enable-mlpcpp'), get_option('tracy_enable'),
               meson.project_build_root().startswith(meson.project_source_root()) ? meson.project_build_root().split('/')[-1] : meson.project_build_root())
      

      Insert this line into the summary format string:-

      Tracy Profiler: @14@
      

      Replace the existing install line with:

      Use './ninja -C @15@ install' to compile and install SU2
      
  4. Build SU2 with Tracy:
    • Tracy project requires meson version >=1.3.0, however the version of meson provided by SU2 is much lower. Thus it needs to be manually installed.
      pip install --user --upgrade meson
      
    • Run the preconfigure script since we are not using the provided meson.py script in the SU2 directory
      ./preconfigure.py 
      
    • ninja must also be installed manually since we are skipping the use of meson.py
      sudo apt install ninja-build 
      
    • Configure and build SU2:
      meson setup build_tracy -Dwith-mpi=disabled -Denable-mlpcpp=true --buildtype=debugoptimized -Dtracy_enable=true --prefix=/home/user/SU2_install
      ninja -C build_tracy install
      
    • Adjust the --prefix path to your desired installation directory.

This process embeds the Tracy client into SU2, preparing it for profiling.

Server Installation

The “server” is the Tracy profiler application that visualizes the data collected by the client. To set it up:

  1. Install dependencies:
    • On Ubuntu, install the required libraries:
      sudo apt install libfreetype6-dev libcapstone-dev libdbus-1-dev \
      libxkbcommon-dev libwayland-dev wayland-protocols \
      libegl1-mesa-dev libglvnd-dev \
      libgtk-3-dev
      
  2. Build the Tracy profiler:
    • Navigate to the Tracy directory: cd ~/SU2/subprojects/tracy.
    • Use CMake to build the profiler:
      cmake -B profiler/build -S profiler -DCMAKE_BUILD_TYPE=Release -DLEGACY=ON
      cmake --build profiler/build --config Release --parallel
      
    • Note: I used -DLEGACY=ON for X11 support due to compilation issues with Wayland, though this is not the recommended approach.

The Tracy server is now ready to visualize profiling data.

Instrumenting the Code

To gather useful profiling data, the SU2 code must be instrumented with Tracy macros. For my GSoC project, I focused on profiling the Predict_MLP function in the CDataDrivenFluid class. Here’s how to do it:

  1. Include the Tracy header:
    • Add at the top of the source file:
      #include <tracy/Tracy.hpp>
      
  2. Add profiling macros:
    • Instrument the function with ZoneScopedN:
      unsigned long CDataDrivenFluid::Predict_MLP(su2double rho, su2double e) {
          ZoneScopedN("Predict_MLP");
          unsigned long exit_code = 0;
          // Function implementation
          return exit_code;
      }
      
    • The ZoneScopedN("name") macro labels the profiling zone, making it identifiable in the Tracy GUI.

Repeat this process for other functions or sections you wish to profile.

Using Tracy for Profiling

With the client and server set up and the code instrumented, profiling SU2 is straightforward:

  1. Launch the Tracy profiler:
    • Navigate to the profiler build directory:
      cd ~/SU2/subprojects/tracy/profiler/build
      
    • Run the profiler:
      ./tracy-profiler
      
    • Click “Connect” in the GUI; it will wait for a connection from SU2.
  2. Execute the instrumented SU2 simulation:
    • In a separate terminal, navigate to your simulation directory and run:
      /home/user/SU2_install/bin/SU2_CFD config_NICFD_PINN.cfg
      
    • Replace the path and configuration file as needed.

As the simulation runs, Tracy displays real-time profiling data. The GUI allows you to explore performance metrics, identify bottlenecks, and optimize code effectively.

Tracy GUI

Conclusion

Using Tracy to profile my SU2 code has been an important step in my GSoC project. Its low overhead and real-time visualization have enables me to pinpoint performance issues with precision. By following the steps outlined above, you can integrate Tracy into your own SU2 projects and leverage its capabilities for performance analysis. For advanced features and troubleshooting, consult the Tracy documentation. I also came across this blog, which served as a helpful starting point for understanding the Tracy profiler and getting it up and running.

Leave a Comment