AMReX-Codes / pyamrex

GPU-Enabled, Zero-Copy AMReX Python Bindings including AI/ML
http://pyamrex.readthedocs.io
Other
32 stars 15 forks source link

segmentation fault if AMReX is not initialized #327

Open BenWibking opened 1 month ago

BenWibking commented 1 month ago

The Python interpreter segfaults if a function is called without initializing AMReX:

$  pyamrex git:(add-plotfiledata) ✗ python
Python 3.12.3 (main, Apr  9 2024, 08:09:14) [Clang 15.0.0 (clang-1500.3.9.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import amrex.space3d as amr
>>> amr.PlotFileData("./tests/projz04000/")
[1]    94346 segmentation fault  python

whereas this works fine:

$ pyamrex git:(add-plotfiledata) ✗ python
Python 3.12.3 (main, Apr  9 2024, 08:09:14) [Clang 15.0.0 (clang-1500.3.9.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import amrex.space3d as amr
>>> amr.initialize([])
Initializing AMReX (24.05)...
MPI initialized with 1 MPI processes
MPI initialized with thread support level 0
AMReX (24.05) initialized
<amrex.space3d.amrex_3d_pybind.AMReX object at 0x10127d270>
>>> amr.PlotFileData("./tests/projz04000/")
<amrex.space3d.amrex_3d_pybind.PlotFileData object at 0x10127ddb0>
>>> amr.finalize()
AMReX (24.05) finalized
BenWibking commented 1 month ago

Discovered via this PR: https://github.com/AMReX-Codes/pyamrex/pull/320

WeiqunZhang commented 1 month ago

It's not surprising that a lot of amrex function won't work without amrex::Initialize called.

BenWibking commented 1 month ago

No, but I don't think it should segfault. This is very surprising (bad) behavior for a Python program. Python scripts should never segfault.

BenWibking commented 1 month ago

Can amrex be initialized when the module is imported?

ax3l commented 4 weeks ago

Hm, I am a bit hesitant to add a lot if if amrex initialized checks in pyAMReX. But if you add them upstream then we can just C++ throw and this will (should) propagate as a runtime exception to pyAMReX.

ax3l commented 4 weeks ago

Can amrex be initialized when the module is imported?

mpi4py style? Possibly... But there is a lot of runtime stuff people might want to pass to init

BenWibking commented 4 weeks ago

Can amrex be initialized when the module is imported?

mpi4py style? Possibly... But there is a lot of runtime stuff people might want to pass to init

Is it possible to re-init after the first (default) init?