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

PML Exchanges: Less Duplicate Code #2394

Merged
merged 14 commits into from
Jan 24, 2022

Conversation

EZoni
Copy link
Member

@EZoni EZoni commented Oct 8, 2021

Similarly to #2375, I'm trying to see if we can reduce the amount of duplicate code, in this case for the functions Exchange<E,B> of the PML class. Open for discussion.

@EZoni EZoni added cleaning Clean code, improve readability component: parallelization Guard cell exchanges and particle redistribution component: boundary PML, embedded boundaries, et al. labels Oct 8, 2021
Copy link
Member Author

@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.

Just adding a couple of questions as review comments.

Comment on lines 186 to 187
const amrex::Geometry* m_geom;
const amrex::Geometry* m_cgeom;
Copy link
Member Author

Choose a reason for hiding this comment

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

@ax3l @WeiqunZhang I made these two public because I would need to pass them as arguments when I call Exchange from a PML object elsewhere (see changes in Source/Parallelization/WarpXComm.cpp). Do you think I should define public "getters" instead? (I guess I'm a little undecided because these are pointers themselves ...)

Copy link
Member

Choose a reason for hiding this comment

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

Another option could be to pass a boolean flag is_coarse or similar to the PML exchange method. That way you don't have to pass a member variable as an argument to a member function of the same class (the same could be done for the mf_pml by the way, using an enum struct argument).
But honestly, I wouldn't know which solution would be considered best practice.

More generally, your PR looks good, thanks!

Copy link
Member Author

Choose a reason for hiding this comment

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

@NeilZaim Thanks for your comment. I think what you mention was the purpose of the original argument patch_type (of type PatchType instead of simply bool), which was inspected in the old functions ExchangeE and ExchangeB. However, the reason why I removed that argument and its inspection is that I think those checks were redundant, in some sense. In particular, the logic of those function calls were the following (for example for the fine patch):

// Here we are in WarpX::FillBoundaryE or WarpX::FillBoundaryB
// in Source/Parallelization/WarpXComm.cpp
if (patch_type == PatchType::fine)
{
    if (do_pml && pml[lev]->ok())
    {
        // call ExchangeE or ExchangeB from a PML object,
        // passing patch_type, which is inspected *again* with
        // if (patch_type == PatchType::fine && ... )
        // inside those functions
    }
}

I removed patch_type as an argument passed to the new function Exchange because in general I don't think there are good reasons for us to check if patch_type == PatchType::fine twice.

Copy link
Member

@NeilZaim NeilZaim Oct 8, 2021

Choose a reason for hiding this comment

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

If the double if check is the main issue, you could consider moving the if (do_pml && pml[lev]->ok()) block outside of the first if (patch_type == PatchType::fine) and then pass patch_type as an argument and do something like const auto geom = (patch_type == PatchType::fine) ? m_geom : m_cgeom;.

But as I said above, this will not make a difference in terms of either performance or code duplication, so I really don't know what's considered better (it just felt a bit weird to pass a member of class PML as argument to a function of class PML).

Copy link
Member Author

@EZoni EZoni Nov 15, 2021

Choose a reason for hiding this comment

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

I think we cannot move those calls outside the if (patch_type == PatchType::fine) because based on the result of that if condition we also choose to get the fp or cp pointer for E and B respectively. So this would require the refactoring of the similar functions at the level of Parallelization/WarpXComm.cpp, too. I could do that here, or maybe in another separate PR, to keep things simple. Anyways I agree with you that passing the Geometry members to a PML function does not make much sense, so I removed that. We still remove code duplication in the PML, even if we leave the double check on the patch type for now.

Copy link
Member Author

@EZoni EZoni Nov 17, 2021

Choose a reason for hiding this comment

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

With regard to this thread, I removed some more code duplication in the latest commits. Will leave the rest, if needed and if possible, to future PRs.

Comment on lines +875 to +893
if (mf_pml[0] && mf[0]) Exchange(*mf_pml[0], *mf[0], geom, do_pml_in_domain);
if (mf_pml[1] && mf[1]) Exchange(*mf_pml[1], *mf[1], geom, do_pml_in_domain);
if (mf_pml[2] && mf[2]) Exchange(*mf_pml[2], *mf[2], geom, do_pml_in_domain);
Copy link
Member Author

@EZoni EZoni Oct 8, 2021

Choose a reason for hiding this comment

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

Isn't it maybe a little redundant to perform these if checks here at this level? These functions are called elsewhere in Source/Parallelization/WarpXComm.cpp after conditional checks such as if (do_pml && pml[lev]->ok()) and similar. I wonder if we should perform more safety checks in the constructor of the PML class and less safety checks in functions like this one here. What do you think?

@EZoni
Copy link
Member Author

EZoni commented Nov 17, 2021

Note: #2211 should be merged first to avoid conflicts. This PR should then be rebased and merged afterwards (hence the "[WIP]" flag). In the meantime, though, I will request another round of review.

Copy link
Member

@NeilZaim NeilZaim left a comment

Choose a reason for hiding this comment

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

Hi @EZoni. Sorry for talking a long time to review. This PR looks good to me, I've just left some minor suggestions about const correctness.

@@ -544,57 +544,42 @@ WarpX::FillBoundaryE(int lev, IntVect ng)
}

void
WarpX::FillBoundaryE (int lev, PatchType patch_type, IntVect ng)
WarpX::FillBoundaryE (const int lev, PatchType patch_type, amrex::IntVect ng)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
WarpX::FillBoundaryE (const int lev, PatchType patch_type, amrex::IntVect ng)
WarpX::FillBoundaryE (const int lev, const PatchType patch_type, const amrex::IntVect ng)

WarpXCommUtil::FillBoundary(*Efield_cp[lev][1], ng, cperiod);
WarpXCommUtil::FillBoundary(*Efield_cp[lev][2], ng, cperiod);
}
amrex::IntVect nghost = (safe_guard_cells) ? mf[i]->nGrowVect() : ng;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
amrex::IntVect nghost = (safe_guard_cells) ? mf[i]->nGrowVect() : ng;
const amrex::IntVect nghost = (safe_guard_cells) ? mf[i]->nGrowVect() : ng;

@@ -606,57 +591,42 @@ WarpX::FillBoundaryB (int lev, IntVect ng)
}

void
WarpX::FillBoundaryB (int lev, PatchType patch_type, IntVect ng)
WarpX::FillBoundaryB (const int lev, PatchType patch_type, amrex::IntVect ng)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
WarpX::FillBoundaryB (const int lev, PatchType patch_type, amrex::IntVect ng)
WarpX::FillBoundaryB (const int lev, const PatchType patch_type, const amrex::IntVect ng)

ng <= mf[i]->nGrowVect(),
"Error: in FillBoundaryB, requested more guard cells than allocated");

amrex::IntVect nghost = (safe_guard_cells) ? mf[i]->nGrowVect() : ng;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
amrex::IntVect nghost = (safe_guard_cells) ? mf[i]->nGrowVect() : ng;
const amrex::IntVect nghost = (safe_guard_cells) ? mf[i]->nGrowVect() : ng;

Source/WarpX.H Outdated
Comment on lines 847 to 848
void FillBoundaryB (const int lev, PatchType patch_type, amrex::IntVect ng);
void FillBoundaryE (const int lev, PatchType patch_type, amrex::IntVect ng);
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
void FillBoundaryB (const int lev, PatchType patch_type, amrex::IntVect ng);
void FillBoundaryE (const int lev, PatchType patch_type, amrex::IntVect ng);
void FillBoundaryB (const int lev, const PatchType patch_type, const amrex::IntVect ng);
void FillBoundaryE (const int lev, const PatchType patch_type, const amrex::IntVect ng);

@EZoni EZoni changed the title [WIP] PML Exchanges: Less Duplicate Code PML Exchanges: Less Duplicate Code Jan 6, 2022
@EZoni
Copy link
Member Author

EZoni commented Jan 20, 2022

@WeiqunZhang What is your opinion on the changes made in this PR?

@WeiqunZhang WeiqunZhang merged commit 0865f6b into ECP-WarpX:development Jan 24, 2022
roelof-groenewald added a commit to ModernElectron/WarpX that referenced this pull request Jan 27, 2022
* remove check on geometry (ECP-WarpX#2771)

* RZ: Add error message when using 0 order for azimuthal decomposition (ECP-WarpX#2772)

* CI: GNUmake (ECP-WarpX#2769)

Since we migrated also Azure regression tests to CMake now, we should add a GNUmake build to CI, so we don't accidentally lose this capability (we will not duplicate the whole CI matrix).
Does a standard CUDA build without dependencies but enabling PSATD.

* PML Exchanges: Less Duplicate Code (ECP-WarpX#2394)

Similarly to ECP-WarpX#2375, I'm trying to see if we can reduce the amount of duplicate code, in this case for the functions `Exchange<E,B>` of the PML class. Open for discussion.

* AMReX: Update latest (ECP-WarpX#2773)

* Docs: Crusher Load `cce` module (ECP-WarpX#2770)

* Docs: Crusher Load `cce` module

Without that module, HIP RDC builds do not work.

https://docs.olcf.ornl.gov/systems/crusher_quick_start_guide.html#compilers

* cce/13.0.0 -> cce/13.0.1

* Crusher: load `cce` after `rocm`

* RZ FDTD: Filter Not Working (Abort) (ECP-WarpX#2775)

* RZ FDTD: Filter Not Working (Abort)

* Update CI Tests and Benchmarks

* Funding/Acknowledgements: CEA-LIDYL. (ECP-WarpX#2781)

* Update .zenodo.json (ECP-WarpX#2782)

* Add SyncRho call to warpx_depositChargeDensity (ECP-WarpX#2777)

* add SyncRho call to warpx_depositChargeDensity

* expose SyncRho to Python; add warpx_clearChargeDensity to reset rho_fp before deposition

* remove unneeded warpx_clearChargeDensity function

* Apply suggestions from code review

Co-authored-by: Axel Huebl <axel.huebl@plasma.ninja>

* add (default) option to depositChargeDensity to call SyncRho after deposition

* added option to depositChargeDensity to zero out rho_fp before deposition

* code cleanup

* import fields from inside depositChargeDensity

Co-authored-by: Axel Huebl <axel.huebl@plasma.ninja>

* udpate zenodo (ECP-WarpX#2784)

Co-authored-by: Hannah Klion <hannah.klion@gmail.com>

* expose WarpXParticleContainer::sumParticleCharge to Python (ECP-WarpX#2790)

* Add tiny profiling to reduced diagnostics (ECP-WarpX#2794)

* fix typo in docstring for ImpactIonizationTransformFunc constructor (ECP-WarpX#2793)

* style fixes in WarpXWrappers.cpp (ECP-WarpX#2792)

* Regressions: Remove Tolerance (ECP-WarpX#2789)

... otherwise we build the `fvarnames` tools automatically.

* Add Python callbacks before and after collisions (ECP-WarpX#2791)

* add python callbacks before and after collisions

* re-order callback declarations

* Fix Docs of MinAndMaxPositions (ECP-WarpX#2787)

Fix order of returned tuple (doc string).

* Track injection statistics for MCC (#145)

* added verbosity flag to MCC

* added MCC verbosity flag to mewarpx

* expose WarpXParticleContainer::sumParticleCharge to Python

* add python callbacks before and after collisions

* example of tracking MCC statistics

* add MCC injection tracking ability via flux_diagnostic

* added MCC to runinfo in diode_setup so that it is added to the flux diagnostic; also updated the thermionic_ignition.py example file to use setups_store

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* remove unneeded call to init_inert_gas in tests using setups_store/diode_setup.py

* updated changelog and version number

* revert MCC verbosity flag, more info is better than less

* avoid MCC injection tracking if it is not used in the simulation

* remove duplicated code due to merge

* add appropriate date to changelog

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>

Co-authored-by: Luca Fedeli <luca.fedeli@cea.fr>
Co-authored-by: Neïl Zaim <49716072+NeilZaim@users.noreply.github.com>
Co-authored-by: Axel Huebl <axel.huebl@plasma.ninja>
Co-authored-by: Edoardo Zoni <59625522+EZoni@users.noreply.github.com>
Co-authored-by: Prabhat Kumar <89051199+prkkumar@users.noreply.github.com>
Co-authored-by: Hannah Klion <hannah.klion@gmail.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
@EZoni EZoni deleted the Exchange_PML_lesscode branch September 19, 2024 16:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cleaning Clean code, improve readability component: boundary PML, embedded boundaries, et al. component: parallelization Guard cell exchanges and particle redistribution
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants