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

Fix segfault when importing _libwarpx without initializing WarpX #2580

Merged
merged 8 commits into from
Nov 30, 2021

Conversation

KZhu-ME
Copy link
Member

@KZhu-ME KZhu-ME commented Nov 18, 2021

When doing data analysis, we sometimes need to import modules that use and import _libwarpx, which causes a segfault when it tries to call finalize() when no WarpX simulation was initialized. This PR adds a check to see whether WarpX was actually initialized before calling finalize().

@lgtm-com
Copy link
Contributor

lgtm-com bot commented Nov 18, 2021

This pull request introduces 1 alert when merging e8894d1 into 1182fee - view on LGTM.com

new alerts:

  • 1 for Unused local variable

@ax3l ax3l requested review from ax3l and dpgrote November 19, 2021 00:42
@ax3l ax3l self-assigned this Nov 19, 2021
@ax3l ax3l added the component: Python Python layer label Nov 19, 2021
dpgrote
dpgrote previously approved these changes Nov 19, 2021
Copy link
Member

@dpgrote dpgrote left a comment

Choose a reason for hiding this comment

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

This looks good - thanks!

Copy link
Member

@peterscherpelz peterscherpelz left a comment

Choose a reason for hiding this comment

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

One style suggestion :)

Copy link
Member

@peterscherpelz peterscherpelz left a comment

Choose a reason for hiding this comment

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

@KZhu-ME It turns out that while python will read from module-level variables if there's no local variable in scope, writing a variable within a function is by default always a local variable. This global call seems to fix things for me.
cf https://stackoverflow.com/questions/7060711/accessing-module-level-variables-from-within-a-function-in-the-module

Co-authored-by: Peter Scherpelz <31747262+peterscherpelz@users.noreply.github.com>
@@ -334,7 +336,7 @@ def finalize(finalize_mpi=1):
the end of your script.

'''
if warpx_initialized == True:
if warpx_initialized:
Copy link
Member

@ax3l ax3l Nov 19, 2021

Choose a reason for hiding this comment

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

Would this be needed here, too?

Suggested change
if warpx_initialized:
global warpx_initialized
if warpx_initialized:

Ah maybe not, because it's global anyway. (Is it good style though to mark it anyway?)

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, the Stack Overflow link above says that since functions will access module variables after checking local variables for read operations, python doesn't require the global call here. The problem is the default for write is to create the local variable, not modify the module var.

I'm indifferent on a style choice to specify it or not.

Copy link
Member

@ax3l ax3l Nov 24, 2021

Choose a reason for hiding this comment

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

Got ya, agreed.

Style-wise: I think since we generally try to avoid globals and this is kind of an exception for a module global, I would slightly prefer to add it here, just to be explicit and to avoid confusion.

Copy link
Member

@ax3l ax3l left a comment

Choose a reason for hiding this comment

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

General question: could we express this also as a member of the libwarpx object instead?

Seems cleaner to me.

@dpgrote
Copy link
Member

dpgrote commented Nov 21, 2021

I should have seen that right away, needing to use global. I've seen this type of thing many times.

BTW, there is a possible deeper solution here. The warpx_finalize routine is only calling the routine WarpX::ResetInstance which is deleting the WarpX instance. Could that delete be put inside an if test?

if (m_instance) {
delete m_instance;
}

@ax3l ax3l dismissed dpgrote’s stale review November 22, 2021 16:37

removing earlier approval to indicate open follow-ups.

@peterscherpelz
Copy link
Member

peterscherpelz commented Nov 22, 2021

General question: could we express this also as a member of the libwarpx object instead?

@ax3l I guess this one's a matter of preference - my instinct is that since libwarpx is specifically a ctypes.CDLL object, it's good to not assign it custom variables that aren't normally part of that object type. But syntactically yes it can work.

@peterscherpelz
Copy link
Member

I should have seen that right away, needing to use global. I've seen this type of thing many times.

BTW, there is a possible deeper solution here. The warpx_finalize routine is only calling the routine WarpX::ResetInstance which is deleting the WarpX instance. Could that delete be put inside an if test?

if (m_instance) {
delete m_instance;
}

@dpgrote It's actually not the WarpX finalize call but the AMReX finalize call that seems to cause the major difficulties, including MPI finalization. Since that's a much more complicated function, figuring out how to make it more robust against not-yet-initialized AMReX seems like a bigger project.

@PhilMiller
Copy link
Member

I should have seen that right away, needing to use global. I've seen this type of thing many times.

BTW, there is a possible deeper solution here. The warpx_finalize routine is only calling the routine WarpX::ResetInstance which is deleting the WarpX instance. Could that delete be put inside an if test?

if (m_instance) {
delete m_instance;
}

The C++ definition of delete accepts null pointer values, and is a no-op in that case.

@dpgrote
Copy link
Member

dpgrote commented Nov 22, 2021

Yikes, I missed the call to amrex finalize. That's what I get for trying to do some work on the weekend.

@lgtm-com
Copy link
Contributor

lgtm-com bot commented Nov 24, 2021

This pull request introduces 1 alert and fixes 1 when merging 081fd0d into 6a3f6d4 - view on LGTM.com

new alerts:

  • 1 for Syntax error

fixed alerts:

  • 1 for 'import *' may pollute namespace

Copy link
Member

@ax3l ax3l left a comment

Choose a reason for hiding this comment

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

LGTM, thank you!

@@ -97,6 +97,7 @@
if arg.startswith("amr.restart"):
restart_file_name = arg.split("=")[1]
sim.amr_restart = restart_file_name
sys.argv.remove(arg)
Copy link
Member

Choose a reason for hiding this comment

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

@KZhu-ME can you comment why this change was needed? :)
Seems unrelated.

Copy link
Member Author

Choose a reason for hiding this comment

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

This CI test errored out because with the message that there was an unrecognized command line argument amr.restart. I also encountered this error locally, and have ran into this issue in the past when trying to add arguments not recognized by the warpx parser. Not sure why it only started failing now for this PR though...

roelof-groenewald added a commit to ModernElectron/WarpX that referenced this pull request Dec 3, 2021
* Fix Init of Vector Members (ECP-WarpX#2595)

Fix default init of `Vector` member variables. The old construct
is not valid C++.

https://stackoverflow.com/a/11491003/2719194

* C++17: Work-Around NVCC gatherParticles (ECP-WarpX#2596)

The `noexcept` lambda does not compile in C++17 mode due to an NVCC compiler bug, at least in NVCC 11.3.109. Compiles in C++14 mode with the same compiler.

* requirements.txt - PICMI development version (ECP-WarpX#2588)

Document in `requirements.txt` on how to install a pre-release
version of PICMI.

* CONTRIBUTING: Update/Modernize (ECP-WarpX#2600)

* CONTRIBUTING: Update/Modernize

- Add GitHub account setup
- Add local git setup
- Modernize for CMake workflows

* Apply suggestions by Edoardo

Co-authored-by: Edoardo Zoni <59625522+EZoni@users.noreply.github.com>

* Replaced duplicated current deposition documentation (ECP-WarpX#2604)

* Throwing a warning if particle_shape>1 with EB (ECP-WarpX#2592)

* Aborting if particle_shape!=1 with EB

* Throw warning instead of aborting

* Checking at runtime if EB is initialized

* Added missing preprocessor directive

* Ignoring an unused variable

* Fix typo

* Improve style

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

* Fix segfault when importing _libwarpx without initializing WarpX (ECP-WarpX#2580)

* Added check for if warpx was initialized when calling finalize

* Renamed to be warpx_initialized

* Fixed reference to global variable

Co-authored-by: Peter Scherpelz <31747262+peterscherpelz@users.noreply.github.com>

* Changed global variable to member of libwarpx

* Fixed syntax errors

* Remove custom arg from argv to avoid parmparse error

Co-authored-by: Peter Scherpelz <31747262+peterscherpelz@users.noreply.github.com>

* Added parallel pragma to ApplyBoundaryConditions (ECP-WarpX#2612)

* Note that CCache 4.2 introduced large CUDA improvements (ECP-WarpX#2606)

* Dimensionality Docs: Default (ECP-WarpX#2609)

Just adds the note that 3D is the default geometry.

* AMReX: Weekly Update (ECP-WarpX#2613)

* MergeBuffersForPlotfile: Barrier (ECP-WarpX#2608)

Make sure that all MPI ranks are in sync, i.e., have closed the
files that they wrote, before trying to merge them.

* Fix installation location for libraries (ECP-WarpX#2583)

During configuration the installation location for libraries is given by
dumping the cmake variable `CMAKE_INSTALL_LIBDIR`.
This commit adjusts the installation of WarpX libraries (WarpX_LIB=ON)
to respect this setting.

Co-authored-by: Rolf Pfeffertal <tropf@users.noreply.github.com>

* Release 21.12 (ECP-WarpX#2614)

* AMReX: 21.12

* PICSAR: 21.12

* WarpX: 21.12

* div(E,B) Cleaning Options for PSATD (ECP-WarpX#2403)

* Implement div(E)/div(B) Cleaning with Standard PSATD

* Cleaning

* Update Benchmark

* Add Nodal Synchronization of F,G

* OneStep_multiJ: Nodal Syncs, Damp PML

* OneStep_multiJ: Push PSATD Fields in PML

* div Cleaning Defaults (Domain v. PML)

* Include Fix of ECP-WarpX#2429 until Merged

* Reset Benchmark of Langmuir_multi_psatd_div_cleaning

* Multi-J: Remove PML Support

* Include Fix of ECP-WarpX#2474 Until Merged

* Exchange All Guard Cells for F,G

* Fix Defaults

* Update Test, Reset Benchmark

* Fix Defaults

* Cleaning

* Default update_with_rho=1 if do_dive_cleaning=1

* Update CI Test pml_psatd_dive_divb_cleaning

* Replace Warning with Abort

* Add 2D Langmuir Test w/ MR & PSATD (ECP-WarpX#2605)

* Add 2D Langmuir Test w/ MR & PSATD

* Add Missing Compile String

* Fix out-of-bound in Inverse FFT of F,G (ECP-WarpX#2619)

* Mention that the potentail should be constant inside EB (ECP-WarpX#2618)

* Mention that the potentail should be constant inside EB

* Update text

* Replace AMREX_SPACEDIM: Boundary & Parallelization (ECP-WarpX#2620)

* AMREX_SPACEDIM : Boundary Conditions
* AMREX_SPACEDIM : Parallelization
* Fix compilation
* Update Source/Parallelization/WarpXComm_K.H

* Fix out-of-bound in the initialization of EB (ECP-WarpX#2607)

* Call FillBoundary when initializing EB

* Avoid out-of-bound

* Bug fix

* Apply suggestions from code review

* update version number

Co-authored-by: Axel Huebl <axel.huebl@plasma.ninja>
Co-authored-by: Edoardo Zoni <59625522+EZoni@users.noreply.github.com>
Co-authored-by: Remi Lehe <remi.lehe@normalesup.org>
Co-authored-by: Lorenzo Giacomel <47607756+lgiacome@users.noreply.github.com>
Co-authored-by: Kevin Z. Zhu <86268612+KZhu-ME@users.noreply.github.com>
Co-authored-by: Peter Scherpelz <31747262+peterscherpelz@users.noreply.github.com>
Co-authored-by: David Grote <grote1@llnl.gov>
Co-authored-by: Phil Miller <phil@intensecomputing.com>
Co-authored-by: s9105947 <80697868+s9105947@users.noreply.github.com>
Co-authored-by: Rolf Pfeffertal <tropf@users.noreply.github.com>
Co-authored-by: Prabhat Kumar <89051199+prkkumar@users.noreply.github.com>
@KZhu-ME KZhu-ME deleted the kzhu/fix_libwarpx_import branch March 22, 2022 21:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: Python Python layer
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants