Skip to content

lazy imports - #82

Merged
max-models merged 6 commits into
devel-tinyfrom
lazy-imports
Sep 29, 2026
Merged

max-models merged 6 commits into
devel-tinyfrom
lazy-imports

Conversation

@max-models

@max-models max-models commented Sep 19, 2026 •

Copy link
Copy Markdown
Member

Struphy PR: struphy-hub/struphy#383

Reduce feectools startup overhead by avoiding MPI initialization in ordinary serial processes, removing the SymPy import from partitioning, and deferring solver and compiler dependencies until needed. This supports the lightweight imports in struphy #383.

Core changes:

  • Add launched_under_mpi() to detect MPI launcher environment variables or an already initialized MPI runtime without importing mpi4py. STRUPHY_MPI can override launch detection.
  • Use MockMPI outside an MPI launch. Support explicit MPI opt-out through FEECTOOLS_MPI=0 or feectools.use_mpi = False, set before importing feectools.ddm.mpi. The in-process option is not inherited by MPI subprocesses.
  • Replace the partitioning module's SymPy factorization dependency with trial division supporting the dictionary and repeated-prime outputs used by callers.
  • Import SciPy LAPACK and sparse factorization routines when constructing the corresponding direct solvers.
  • Query the gfortran version only for the Apple Silicon compiler flags that require it, avoiding an unnecessary subprocess on other platforms.

CI fixes:

  • Constrain the petsc4py 3.23.3 build to Cython>=3.2,<3.3. Cython 3.3.0 rejects pointer indexing in PC.pyx with Invalid index type 'int', causing all six compilation jobs to fail during prerequisite installation. Retain the existing setuptools constraint and build without isolation so the constraints apply.
  • Authenticate the PR test container pull using github.actor and GITHUB_TOKEN, with packages: read, instead of the custom GHCR_PROJECT_TOKEN. This uses the package's configured Actions access for feectools.

Validation:

  • Reproduced the petsc4py Cython-generation failure with Cython 3.3.0 on Python 3.12; generation succeeds with Cython 3.2.9 on Python 3.10 and 3.12.
  • Parsed the modified action and workflow YAML, checked the petsc4py install shell syntax, and verified the registry authentication configuration and whitespace checks.
  • Full PETSc builds and the feectools test suite were not run locally. At drafting time, the C/Fortran container tests and compilation matrix are still running in CI.

@max-models
max-models marked this pull request as ready for review September 29, 2026 11:48
@max-models
max-models requested a review from spossann September 29, 2026 11:48
Comment thread feectools/ddm/partition.py
@max-models
max-models merged commit 280d21e into devel-tiny Sep 29, 2026
8 checks passed
max-models added a commit to struphy-hub/struphy that referenced this pull request Sep 29, 2026
feectools PR: struphy-hub/feectools#82
Importing `struphy` or `Simulation` eagerly loads models, propagators,
plotting libraries, and numerical dependencies that may never be used.
This change resolves public API objects, models, and propagators on
first access and moves expensive imports into the operations that need
them.

**Solves the following issue(s):**

Reduces Python startup overhead. Companion PR: [feectools
#82](struphy-hub/feectools#82).

**Core changes:**

- Add lazy module exports with cached lookups, `dir()` support, and
imports for static type checking. Update the package initializer
generator to produce lazy exports as well.
- Load all models when iterating over `StruphyModel`, so model discovery
still includes classes that have not yet been accessed.
- Defer PyVista, Matplotlib, IPython, and selected SciPy imports until
visualization, display, or numerical operations need them. Centralize
the optional IPython display fallbacks.
- Use MPI launch detection from feectools and avoid importing MPI solely
for type annotations. Serial startup uses the mock communicator without
initializing MPI.
- Reuse feectools' lightweight integer factorization and use
`scipy.special.ndtri` for normally distributed Sobol samples instead of
importing `scipy.stats.norm`.
- Update the feectools submodule to include the companion import
optimizations and CI fixes.

**Model-specific changes:**

No model equations or algorithms are intentionally changed; model
classes are loaded on demand.

**Documentation changes:**

Fix the escaped `mu` in the `Particles5D` mathematical docstring.

**Validation:**

The existing PR reports the following cluster measurement for:

```bash
python -c "from struphy import Simulation"
```

- Before: 10.6 s.
- After: 1.6 s (approximately 6.6 times faster).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants