From 61b13e453ea5e953b8a5f985539d3e6833cf088f Mon Sep 17 00:00:00 2001 From: Axel Huebl Date: Mon, 24 Feb 2025 15:47:35 -0800 Subject: [PATCH] Add `ImpactX_UNITY_BUILD` (#861) * Add `ImpactX_UNITY_BUILD` Unity builds combine all `.cpp` files into a single one (a single translation unit, TU). This is useful for performance optimization, computer science experiments, or differentiable programming. * CI: Add a Unity Build * Fix Ambiguous `diagnostics` namespace --- .github/workflows/ubuntu.yml | 1 + CMakeLists.txt | 14 ++++++++++++++ cmake/ImpactXFunctions.cmake | 3 +++ docs/source/install/cmake.rst | 1 + src/initialization/InitElement.cpp | 3 ++- 5 files changed, 21 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ubuntu.yml b/.github/workflows/ubuntu.yml index 04859698a..6dba23dbd 100644 --- a/.github/workflows/ubuntu.yml +++ b/.github/workflows/ubuntu.yml @@ -49,6 +49,7 @@ jobs: -DCMAKE_VERBOSE_MAKEFILE=ON \ -DImpactX_FFT=ON \ -DImpactX_PYTHON=ON \ + -DImpactX_UNITY_BUILD=ON \ -DMPIEXEC_POSTFLAGS="--use-hwthread-cpus" cmake --build build -j 4 diff --git a/CMakeLists.txt b/CMakeLists.txt index 4e9814c8a..4670fde51 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -116,6 +116,10 @@ option(ImpactX_PYTHON_IPO ${_ImpactX_PYTHON_IPO_DEFAULT} ) +# Unity builds combine all .cpp files into a single one (a single translation +# unit, TU). +option(ImpactX_UNITY_BUILD "ImpactX library as unity build" OFF) + if(ImpactX_FFT) set(ABLASTR_FFT ON CACHE STRING "FFT-based solvers" FORCE) endif() @@ -165,6 +169,16 @@ set_target_properties(lib PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON ) +# Optional: build only a single TU +if(ImpactX_UNITY_BUILD) + set_target_properties(lib PROPERTIES + UNITY_BUILD ON + UNITY_BUILD_MODE BATCH + # Number must be more than the number of .cpp files in ImpactX + UNITY_BUILD_BATCH_SIZE 10000 + ) +endif() + # executable application # note: we currently avoid a dependency on a core library # for simpler usage, but could make this an option diff --git a/cmake/ImpactXFunctions.cmake b/cmake/ImpactXFunctions.cmake index 88263d80b..065d0b893 100644 --- a/cmake/ImpactXFunctions.cmake +++ b/cmake/ImpactXFunctions.cmake @@ -395,6 +395,9 @@ function(impactx_print_summary) else() set(LIB_TYPE "static") endif() + if(ImpactX_UNITY_BUILD) + set(LIB_TYPE "${LIB_TYPE} (unity build)") + endif() #message(" Testing: ${BUILD_TESTING}") message(" Build options:") message(" APP: ${ImpactX_APP}") diff --git a/docs/source/install/cmake.rst b/docs/source/install/cmake.rst index 8fe5a85c7..7392278dc 100644 --- a/docs/source/install/cmake.rst +++ b/docs/source/install/cmake.rst @@ -126,6 +126,7 @@ CMake Option Default & Values Des ============================= ============================================== =========================================================== ``BUILD_SHARED_LIBS`` ON/**OFF** Build shared libraries for dependencies ``ImpactX_CCACHE`` **ON**/OFF Search and use CCache to speed up rebuilds. +``ImpactX_UNITY_BUILD`` ON/**OFF** ImpactX library as unity build (single TU) ``ImpactX_ablastr_src`` *None* Path to ABLASTR source directory (preferred if set) ``ImpactX_ablastr_repo`` ``https://github.com/ECP-WarpX/WarpX.git`` Repository URI to pull and build ABLASTR from ``ImpactX_ablastr_branch`` *we set and maintain a compatible commit* Repository branch for ``ImpactX_ablastr_repo`` diff --git a/src/initialization/InitElement.cpp b/src/initialization/InitElement.cpp index 0e5dceea0..3e3557724 100644 --- a/src/initialization/InitElement.cpp +++ b/src/initialization/InitElement.cpp @@ -502,7 +502,8 @@ namespace detail pp_element.queryAddWithParser("cn", cn); } - m_lattice.emplace_back(diagnostics::BeamMonitor(openpmd_name, openpmd_backend, openpmd_encoding, period_sample_intervals)); + using impactx::elements::diagnostics::BeamMonitor; + m_lattice.emplace_back(BeamMonitor(openpmd_name, openpmd_backend, openpmd_encoding, period_sample_intervals)); } else if (element_type == "source") { std::string distribution, openpmd_path;