Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WarpX_UNITY_BUILD #5702

Open
wants to merge 2 commits into
base: development
Choose a base branch
from
Open

Conversation

ax3l
Copy link
Member

@ax3l ax3l commented Feb 24, 2025

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.

@@ -846,13 +846,13 @@
for (auto idx=0; idx<pc->NumRealComps(); idx++) {
if (write_real_comp[idx]) {
// handle scalar and non-scalar records by name
const auto [record_name, component_name] = detail::name2openPMD(real_comp_names[idx]);
const auto [record_name, component_name] = ::detail::name2openPMD(real_comp_names[idx]);

Check notice

Code scanning / CodeQL

Unused local variable Note

Variable component_name is not used.
@@ -869,13 +869,13 @@
for (auto idx=0; idx<int_counter; idx++) {
if (write_int_comp[idx]) {
// handle scalar and non-scalar records by name
const auto [record_name, component_name] = detail::name2openPMD(int_comp_names[idx]);
const auto [record_name, component_name] = ::detail::name2openPMD(int_comp_names[idx]);

Check notice

Code scanning / CodeQL

Unused local variable Note

Variable component_name is not used.
@lucafedeli88
Copy link
Member

Uh! We have to be careful with anonymous namespaces if we use unity builds!

@EZoni
Copy link
Member

EZoni commented Mar 1, 2025

@ax3l @lucafedeli88

This comment by Luca,

Uh! We have to be careful with anonymous namespaces if we use unity builds!

sounds like something we need to discuss properly before we proceed with moving more functions to anonymous namespaces. At this time I don't have enough knowledge of unity builds to have an informed understanding of Luca's statement, but if you do, it is probably a good idea to touch base on this and see what is the best way to move forward.

@ax3l
Copy link
Member Author

ax3l commented Mar 1, 2025

To clarify: anonymous namespaces are not the problem, identically named global variables repeated with the same name in different .cpp files are what one wants to avoid (in or outside global namespaces).

@lucafedeli88
Copy link
Member

To clarify: anonymous namespaces are not the problem, identically named global variables repeated with the same name in different .cpp files are what one wants to avoid (in or outside global namespaces).

Yes, sorry, I should have explained that better.... My point is that we are using anonymous namespaces more often than before, so the risk of conflicts may increase in the future. Maybe we could consider not using anonymous namespaces, but parent_namespace::details in the .cpp file to reduce the risk of conflicts. What do you think?

@ax3l
Copy link
Member Author

ax3l commented Mar 3, 2025

That is possible for sure and clean :)

Anonymous namespaces are generally still great to use, because what they do is they do not create an unnecessary symbol in the final binary and thus keep the binary a bit smaller. (That said, we can use compiler symbol hiding flags when we want to optimize for that, we do that in pybind11 for non-debug builds.)

Another solution I could have done for this one collision here: move the global variable to a header file (in a regular namespace) and define it inline or static.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have updated this file in PR #5726.
If you merge development, there would be a few additional synchronizations Gpu::streamSynchronize();, which I guess need to be updated into amrex:: Gpu::streamSynchronize();

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! That will be automatically merged and does not create a merge conflict, as far as I can see.

@ax3l
Copy link
Member Author

ax3l commented Mar 7, 2025

Let me know if you have any further questions, but this is otherwise ready to go. cc @EZoni

Copy link
Member

@EZoni EZoni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add a CI job that builds WarpX with -DWarpX_UNITY_BUILD=ON?

@ax3l
Copy link
Member Author

ax3l commented Mar 10, 2025

Yes, def!

I added one in ImpactX already but was refraining from one just yet here because it still threw a few (mild) warnings. Would do in a follow-up where I fix those warnings, so we can build the unity build also with -Werror.

ax3l added 2 commits March 10, 2025 10:05
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.
- Errors: redefinitions (fixed)
- Warnings: shadowing (started)

Ref.:
https://cmake.org/cmake/help/latest/prop_tgt/UNITY_BUILD_UNIQUE_ID.html
@ax3l ax3l force-pushed the topic-unity-build branch from 2900120 to 41f3585 Compare March 10, 2025 17:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants