Skip to content

Commit 258d331

Browse files
ax3lEZoni
andauthored
Add WarpX_UNITY_BUILD (#5702)
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. FYI: A CPU unity build of ImpactX is around 18sec on my laptop (BLAST-ImpactX/impactx#861), for WarpX around 3:30min. In both cases, this refers to the `lib*` of ImpactX/WarpX part. --------- Co-authored-by: Edoardo Zoni <[email protected]>
1 parent e614330 commit 258d331

File tree

10 files changed

+96
-40
lines changed

10 files changed

+96
-40
lines changed

CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ option(WarpX_PYTHON_IPO
166166
${_WarpX_PYTHON_IPO_DEFAULT}
167167
)
168168

169+
# Unity builds combine all .cpp files into a single one (a single translation
170+
# unit, TU).
171+
option(WarpX_UNITY_BUILD "WarpX library as unity build" OFF)
172+
169173
set(pyWarpX_VERSION_INFO "" CACHE STRING
170174
"PEP-440 conformant version (set by setup.py)")
171175

@@ -277,6 +281,17 @@ foreach(D IN LISTS WarpX_DIMS)
277281
POSITION_INDEPENDENT_CODE ON
278282
WINDOWS_EXPORT_ALL_SYMBOLS ON
279283
)
284+
285+
# Optional: build only a single TU
286+
if(WarpX_UNITY_BUILD)
287+
set_target_properties(lib_${SD} PROPERTIES
288+
UNITY_BUILD ON
289+
UNITY_BUILD_MODE BATCH
290+
UNITY_BUILD_UNIQUE_ID "WARPX_UNITY_ID"
291+
# Number must be more than the number of .cpp files in WarpX
292+
UNITY_BUILD_BATCH_SIZE 10000
293+
)
294+
endif()
280295
endif()
281296

282297
# executable application

Docs/source/install/cmake.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ CMake Option Default & Values Des
121121
============================= ============================================== ===========================================================
122122
``BUILD_SHARED_LIBS`` ON/**OFF** `Build shared libraries for dependencies <https://cmake.org/cmake/help/latest/variable/BUILD_SHARED_LIBS.html>`__
123123
``WarpX_CCACHE`` **ON**/OFF Search and use CCache to speed up rebuilds.
124+
``WarpX_UNITY_BUILD`` ON/**OFF** WarpX library as unity build (single TU)
124125
``AMReX_CUDA_PTX_VERBOSE`` ON/**OFF** Print CUDA code generation statistics from ``ptxas``.
125126
``WarpX_amrex_src`` *None* Path to AMReX source directory (preferred if set)
126127
``WarpX_amrex_repo`` ``https://github.com/AMReX-Codes/amrex.git`` Repository URI to pull and build AMReX from

Source/BoundaryConditions/PML_RZ.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ PML_RZ::PML_RZ (int lev, amrex::BoxArray const& grid_ba, amrex::DistributionMapp
4343
m_do_pml_in_domain(do_pml_in_domain),
4444
m_geom(geom)
4545
{
46+
using ablastr::fields::Direction;
47+
4648
auto & warpx = WarpX::GetInstance();
4749

4850
bool const remake = false;
@@ -73,6 +75,7 @@ PML_RZ::ApplyDamping (amrex::MultiFab* Et_fp, amrex::MultiFab* Ez_fp,
7375
amrex::MultiFab* Bt_fp, amrex::MultiFab* Bz_fp,
7476
amrex::Real dt, ablastr::fields::MultiFabRegister& fields)
7577
{
78+
using ablastr::fields::Direction;
7679

7780
amrex::Real const dr = m_geom->CellSize(0);
7881
amrex::Real const cdt_over_dr = PhysConst::c*dt/dr;
@@ -132,6 +135,8 @@ PML_RZ::ApplyDamping (amrex::MultiFab* Et_fp, amrex::MultiFab* Ez_fp,
132135
void
133136
PML_RZ::FillBoundaryE (ablastr::fields::MultiFabRegister& fields, PatchType patch_type, std::optional<bool> nodal_sync)
134137
{
138+
using ablastr::fields::Direction;
139+
135140
amrex::MultiFab * pml_Er = fields.get(FieldType::pml_E_fp, Direction{0}, 0);
136141
amrex::MultiFab * pml_Et = fields.get(FieldType::pml_E_fp, Direction{1}, 0);
137142

@@ -148,6 +153,8 @@ PML_RZ::FillBoundaryB (ablastr::fields::MultiFabRegister& fields, PatchType patc
148153
{
149154
if (patch_type == PatchType::fine)
150155
{
156+
using ablastr::fields::Direction;
157+
151158
amrex::MultiFab * pml_Br = fields.get(FieldType::pml_B_fp, Direction{0}, 0);
152159
amrex::MultiFab * pml_Bt = fields.get(FieldType::pml_B_fp, Direction{1}, 0);
153160

@@ -160,6 +167,8 @@ PML_RZ::FillBoundaryB (ablastr::fields::MultiFabRegister& fields, PatchType patc
160167
void
161168
PML_RZ::CheckPoint (ablastr::fields::MultiFabRegister& fields, std::string const& dir) const
162169
{
170+
using ablastr::fields::Direction;
171+
163172
if (fields.has(FieldType::pml_E_fp, Direction{0}, 0)) {
164173
amrex::VisMF::AsyncWrite(*fields.get(FieldType::pml_E_fp, Direction{0}, 0), dir+"_Er_fp");
165174
amrex::VisMF::AsyncWrite(*fields.get(FieldType::pml_E_fp, Direction{1}, 0), dir+"_Et_fp");
@@ -171,6 +180,8 @@ PML_RZ::CheckPoint (ablastr::fields::MultiFabRegister& fields, std::string const
171180
void
172181
PML_RZ::Restart (ablastr::fields::MultiFabRegister& fields, std::string const& dir)
173182
{
183+
using ablastr::fields::Direction;
184+
174185
if (fields.has(FieldType::pml_E_fp, Direction{0}, 0)) {
175186
amrex::VisMF::Read(*fields.get(FieldType::pml_E_fp, Direction{0}, 0), dir+"_Er_fp");
176187
amrex::VisMF::Read(*fields.get(FieldType::pml_E_fp, Direction{1}, 0), dir+"_Et_fp");
@@ -195,6 +206,8 @@ PML_RZ::PushPMLPSATDSinglePatchRZ (
195206
SpectralSolverRZ& solver,
196207
ablastr::fields::MultiFabRegister& fields)
197208
{
209+
using ablastr::fields::Direction;
210+
198211
SpectralFieldIndex const& Idx = solver.m_spectral_index;
199212
amrex::MultiFab * pml_Er = fields.get(FieldType::pml_E_fp, Direction{0}, 0);
200213
amrex::MultiFab * pml_Et = fields.get(FieldType::pml_E_fp, Direction{1}, 0);

Source/Diagnostics/FlushFormats/FlushFormatCheckpoint.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,20 @@
2222
#include <AMReX_Utility.H>
2323
#include <AMReX_VisMF.H>
2424

25+
#ifndef WARPX_UNITY_ID
26+
#define WARPX_UNITY_ID
27+
#endif
28+
2529
using namespace amrex;
2630
using warpx::fields::FieldType;
2731

2832
namespace
33+
{
34+
namespace WARPX_UNITY_ID
2935
{
3036
const std::string default_level_prefix {"Level_"};
3137
}
38+
}
3239

3340
void
3441
FlushFormatCheckpoint::WriteToFile (
@@ -48,6 +55,7 @@ FlushFormatCheckpoint::WriteToFile (
4855
bool /*isLastBTDFlush*/) const
4956
{
5057
using ablastr::fields::Direction;
58+
using WARPX_UNITY_ID::default_level_prefix;
5159

5260
WARPX_PROFILE("FlushFormatCheckpoint::WriteToFile()");
5361

Source/Diagnostics/FlushFormats/FlushFormatPlotfile.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,20 @@
4949
#include <utility>
5050
#include <vector>
5151

52+
#ifndef WARPX_UNITY_ID
53+
#define WARPX_UNITY_ID
54+
#endif
55+
5256
using namespace amrex;
5357
using warpx::fields::FieldType;
5458

5559
namespace
60+
{
61+
namespace WARPX_UNITY_ID
5662
{
5763
const std::string default_level_prefix {"Level_"};
5864
}
65+
}
5966

6067
void
6168
FlushFormatPlotfile::WriteToFile (
@@ -570,6 +577,7 @@ FlushFormatPlotfile::WriteAllRawFields(
570577
const bool plot_raw_fields_guards) const
571578
{
572579
using ablastr::fields::Direction;
580+
using WARPX_UNITY_ID::default_level_prefix;
573581

574582
if (!plot_raw_fields) { return; }
575583
auto & warpx = WarpX::GetInstance();

Source/Diagnostics/ReducedDiags/DifferentialLuminosity.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,12 @@
5454
#include <memory>
5555
#include <vector>
5656

57-
using ParticleType = WarpXParticleContainer::ParticleType;
58-
using ParticleTileType = WarpXParticleContainer::ParticleTileType;
59-
using ParticleTileDataType = ParticleTileType::ParticleTileDataType;
60-
using ParticleBins = amrex::DenseBins<ParticleTileDataType>;
61-
using index_type = ParticleBins::index_type;
62-
63-
using namespace amrex;
6457

6558
DifferentialLuminosity::DifferentialLuminosity (const std::string& rd_name)
6659
: ReducedDiags{rd_name}
6760
{
61+
using namespace amrex::literals;
62+
6863
// read colliding species names - must be 2
6964
const amrex::ParmParse pp_rd_name(m_rd_name);
7065
pp_rd_name.getarr("species", m_beam_name);
@@ -112,7 +107,7 @@ DifferentialLuminosity::DifferentialLuminosity (const std::string& rd_name)
112107
{
113108
ofs << m_sep;
114109
ofs << "[" << off++ << "]";
115-
const Real b = m_bin_min + m_bin_size*(Real(i)+0.5_rt);
110+
const amrex::Real b = m_bin_min + m_bin_size*(amrex::Real(i)+0.5_rt);
116111
ofs << "bin" << 1+i << "=" << b << "(eV)";
117112
}
118113
ofs << "\n";
@@ -129,6 +124,12 @@ void DifferentialLuminosity::ComputeDiags (int step)
129124
#else
130125
WARPX_PROFILE("DifferentialLuminosity::ComputeDiags");
131126

127+
using namespace amrex;
128+
using ParticleTileType = WarpXParticleContainer::ParticleTileType;
129+
using ParticleTileDataType = ParticleTileType::ParticleTileDataType;
130+
using ParticleBins = amrex::DenseBins<ParticleTileDataType>;
131+
using index_type = ParticleBins::index_type;
132+
132133
// Since this diagnostic *accumulates* the luminosity in the
133134
// array d_data, we add contributions at *each timestep*, but
134135
// we only write the data to file at intervals specified by the user.

Source/Diagnostics/ReducedDiags/DifferentialLuminosity2D.cpp

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,10 @@
6060
#include <memory>
6161
#include <vector>
6262

63-
using ParticleType = WarpXParticleContainer::ParticleType;
64-
using ParticleTileType = WarpXParticleContainer::ParticleTileType;
65-
using ParticleTileDataType = ParticleTileType::ParticleTileDataType;
66-
using ParticleBins = amrex::DenseBins<ParticleTileDataType>;
67-
using index_type = ParticleBins::index_type;
68-
6963
#ifdef WARPX_USE_OPENPMD
7064
namespace io = openPMD;
7165
#endif
7266

73-
using namespace amrex;
7467

7568
DifferentialLuminosity2D::DifferentialLuminosity2D (const std::string& rd_name)
7669
: ReducedDiags{rd_name}
@@ -80,6 +73,7 @@ DifferentialLuminosity2D::DifferentialLuminosity2D (const std::string& rd_name)
8073
WARPX_ABORT_WITH_MESSAGE(
8174
"DifferentialLuminosity2D diagnostics does not work in RZ geometry.");
8275
#endif
76+
using namespace amrex::literals;
8377

8478
// read colliding species names - must be 2
8579
amrex::ParmParse pp_rd_name(m_rd_name);
@@ -120,9 +114,9 @@ DifferentialLuminosity2D::DifferentialLuminosity2D (const std::string& rd_name)
120114
m_bin_size_2 = (bin_max_2 - bin_min_2) / bin_num_2;
121115

122116
// resize data array on the host
123-
Array<int,2> tlo{0,0}; // lower bounds
124-
Array<int,2> thi{m_bin_num_1-1, m_bin_num_2-1}; // inclusive upper bounds
125-
m_h_data_2D.resize(tlo, thi, The_Pinned_Arena());
117+
amrex::Array<int,2> tlo{0,0}; // lower bounds
118+
amrex::Array<int,2> thi{m_bin_num_1-1, m_bin_num_2-1}; // inclusive upper bounds
119+
m_h_data_2D.resize(tlo, thi, amrex::The_Pinned_Arena());
126120

127121
auto const& h_table_data = m_h_data_2D.table();
128122
// initialize data on the host
@@ -136,17 +130,22 @@ DifferentialLuminosity2D::DifferentialLuminosity2D (const std::string& rd_name)
136130
m_d_data_2D.resize(tlo, thi);
137131
// copy data from host to device
138132
m_d_data_2D.copy(m_h_data_2D);
139-
Gpu::streamSynchronize();
133+
amrex::Gpu::streamSynchronize();
140134
} // end constructor
141135

142136
void DifferentialLuminosity2D::ComputeDiags (int step)
143137
{
144138
#if defined(WARPX_DIM_RZ)
145139
amrex::ignore_unused(step);
146140
#else
147-
148141
WARPX_PROFILE("DifferentialLuminosity2D::ComputeDiags");
149142

143+
using namespace amrex;
144+
using ParticleTileType = WarpXParticleContainer::ParticleTileType;
145+
using ParticleTileDataType = ParticleTileType::ParticleTileDataType;
146+
using ParticleBins = amrex::DenseBins<ParticleTileDataType>;
147+
using index_type = ParticleBins::index_type;
148+
150149
// Since this diagnostic *accumulates* the luminosity in the
151150
// table m_d_data_2D, we add contributions at *each timestep*, but
152151
// we only write the data to file at intervals specified by the user.
@@ -336,7 +335,7 @@ void DifferentialLuminosity2D::WriteToFile (int step) const
336335

337336
#ifdef WARPX_USE_OPENPMD
338337
// only IO processor writes
339-
if ( !ParallelDescriptor::IOProcessor() ) { return; }
338+
if ( !amrex::ParallelDescriptor::IOProcessor() ) { return; }
340339

341340
// TODO: support different filename templates
342341
std::string filename = "openpmd";

0 commit comments

Comments
 (0)