-
Notifications
You must be signed in to change notification settings - Fork 2
10 list comprehension bottleneck sandbox #81
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
base: dev
Are you sure you want to change the base?
Changes from all commits
8f7c35c
ce6adfe
b7b6f65
084708b
1105085
e3bd3dd
d17cedb
e5cea69
0b0a635
f151679
bf55c73
b0edf60
58a5e5d
4e66bac
db1182f
b7ae56c
a3fcab9
d129138
0e723e1
1c964d8
3ae92e9
bf14ed0
4304639
6d20d6d
f1ae4ff
87973fe
a7b2743
c4bddaf
67f90fe
c8fa3a7
52e0a09
40295ce
277cc2d
6f12185
1cdec22
50d20ec
1bb05c1
9177a6f
7015174
648ba02
50c4bc7
a55d347
494532d
ee9ec3f
946f916
f110170
35a82be
89fdecf
01375c3
5ddec32
5411f2d
d9c7f8b
9ef428a
c838113
4bd1690
93e2721
af92bc9
d4610db
cce37eb
55897c4
0f1bd40
95f9020
a2df2ea
881b8fa
166c4e6
7a2c28c
cc6d7f2
48de1fe
87fb12c
d9376a0
582f55f
3c1c10a
7f3eff0
e0c21e6
f6af5a8
fffa69d
fd3086d
134ba7f
6c66625
5a63ddb
cf50261
cc63a2b
6867264
930b2ba
94e571a
9e695f4
6912fb3
2bdc586
7a6facc
9b87961
f177dc4
23c6e07
c833bde
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -172,3 +172,12 @@ cython_debug/ | |
|
|
||
| # Ignore vscode settings | ||
| .vscode/ | ||
|
|
||
| *prof | ||
|
|
||
| # Add to .gitignore | ||
| *.c | ||
| *.html | ||
| *.so | ||
| *.pyd | ||
| build/ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| # Cythonizing ModularCirc HelperRoutines | ||
|
|
||
| This directory contains a Cythonized version of the `HelperRoutines` module for improved performance. | ||
|
|
||
| ## Quick Start (Automatic Build) | ||
|
|
||
| **The Cython extension is now built automatically during package installation!** | ||
|
|
||
| ```bash | ||
| # Install with Cython support (requires cython and numpy) | ||
| pip install cython numpy | ||
| pip install -e . | ||
| ``` | ||
|
|
||
| The setup will automatically detect Cython and build the extension. If Cython is not available or the build fails, the package will fall back to the Numba implementation. | ||
|
|
||
| ## Manual Build (Development) | ||
|
|
||
| For quick rebuilds during development without reinstalling the entire package: | ||
|
|
||
| ```bash | ||
| bash build_cython.sh | ||
| ``` | ||
|
|
||
| Or manually: | ||
|
|
||
| ```bash | ||
| python setup_cython.py build_ext --inplace | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can't find
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's no longer relevant, everything was moved to setup.py. |
||
| ``` | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - Python >= 3.10 | ||
| - Cython >= 3.0 | ||
| - NumPy >= 1.20 | ||
| - C compiler (gcc, clang, or MSVC) | ||
|
|
||
| ## Using the Cython Module | ||
|
|
||
| ### Option 1: Automatic (Recommended) | ||
|
|
||
| Add this at the top of the script: | ||
| ```python | ||
| # Try to import Cython version, fall back to Numba if unavailable | ||
| try: | ||
| from ModularCirc.HelperRoutines import resistor_model_flow | ||
| print("Using Cythonized HelperRoutines") | ||
| except ImportError: | ||
| # Fall back to current Numba implementation | ||
| pass | ||
| ``` | ||
|
|
||
| ### Option 2: Manual Import (where function hasn't been cythonised) | ||
|
|
||
| In any module that uses HelperRoutines: | ||
|
|
||
| ```python | ||
| try: | ||
| from ModularCirc.HelperRoutines.HelperRoutinesCython import resistor_model_flow, chamber_volume_rate_change | ||
| except ImportError: | ||
| from ModularCirc.HelperRoutines.HelperRoutines import resistor_model_flow, chamber_volume_rate_change | ||
| ``` | ||
|
|
||
| ## Performance Benefits | ||
|
|
||
| Cython provides: | ||
| - **No JIT compilation overhead**: Functions are pre-compiled to machine code | ||
| - **Faster setup time**: No Numba compilation delays on first run | ||
| - **C-level performance**: Direct C math library calls (`sqrt`, `exp`, `log`, etc.) | ||
| - **Type safety**: Compile-time type checking | ||
| - **GIL release**: Many functions use `nogil` for better multi-threading potential | ||
|
|
||
| Expected improvements: | ||
| - **Setup time**: Near-instant (no JIT compilation) | ||
| - **Runtime**: Comparable to or faster than Numba (0-15% improvement typical) | ||
| - **Memory**: Slightly lower memory footprint | ||
|
|
||
| ## Debugging | ||
|
|
||
| If compilation fails, check: | ||
|
|
||
| 1. **Compiler availability**: Ensure you have a C compiler (gcc, clang, or MSVC) | ||
| 2. **NumPy headers**: Make sure NumPy is installed: `pip install numpy` | ||
| 3. **Cython version**: Use Cython >= 0.29: `pip install --upgrade cython` | ||
|
|
||
| View detailed annotation (optimization opportunities): | ||
|
|
||
| ```bash | ||
| # After building, check the generated HTML file | ||
| open src/ModularCirc/HelperRoutines.html | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| ``` | ||
|
|
||
| ## Cleanup | ||
|
|
||
| To remove compiled artifacts: | ||
|
|
||
| ```bash | ||
| # Remove compiled extensions | ||
| rm -f src/ModularCirc/HelperRoutines/HelperRoutinesCython*.so | ||
| rm -f src/ModularCirc/HelperRoutines/HelperRoutinesCython*.pyd | ||
| rm -f src/ModularCirc/HelperRoutines/HelperRoutines.c | ||
|
|
||
| # Remove build artifacts | ||
| rm -rf build/ | ||
| ``` | ||
|
|
||
| ## Notes | ||
|
|
||
| - The `.pyx` file maintains API compatibility with the original `.py` file | ||
| - All Numba `@nb.njit` decorators are replaced with Cython equivalents | ||
| - Type annotations use Cython's static typing for maximum performance | ||
| - Functions marked `nogil` can run without the Python GIL, enabling true parallelism | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| # ModularCirc Installation Options | ||
|
|
||
| ModularCirc supports two performance backends for the `HelperRoutines` module: | ||
|
|
||
| 1. **Cython** (C-compiled, faster startup, recommended for production) | ||
| 2. **Numba** (JIT-compiled, slower startup, easier development) | ||
|
|
||
| ## Installation Methods | ||
|
|
||
| ### Option 1: Install with Cython (Recommended) | ||
|
|
||
| For best performance with faster startup times: | ||
|
|
||
| ```bash | ||
| # Install with Cython dependencies | ||
| pip install -e .[performance] | ||
|
|
||
| # Build the Cython extension | ||
| python setup.py build_ext --inplace | ||
| ``` | ||
|
|
||
| ### Option 2: Install with Numba only | ||
|
|
||
| For development or if you encounter Cython build issues: | ||
|
|
||
| ```bash | ||
| # Set environment variable to skip Cython build | ||
| export MODULARCIRC_USE_CYTHON=0 | ||
|
|
||
| # Install package | ||
| pip install -e . | ||
| ``` | ||
|
|
||
| Or install normally - if Cython is not available, it will automatically fall back to Numba. | ||
|
|
||
| ## Runtime Configuration | ||
|
|
||
| You can control which implementation is used at runtime with environment variables: | ||
|
|
||
| ### Force Numba implementation | ||
|
|
||
| Even if Cython is installed, you can force the use of Numba: | ||
|
|
||
| ```bash | ||
| export MODULARCIRC_FORCE_NUMBA=1 | ||
| python your_script.py | ||
| ``` | ||
|
|
||
| Or in Python: | ||
|
|
||
| ```python | ||
| import os | ||
| os.environ['MODULARCIRC_FORCE_NUMBA'] = '1' | ||
|
|
||
| import ModularCirc # Will use Numba | ||
| ``` | ||
|
|
||
| ### Enable verbose output | ||
|
|
||
| To see which implementation is being used: | ||
|
|
||
| ```bash | ||
| export MODULARCIRC_VERBOSE=1 | ||
| python your_script.py | ||
| ``` | ||
|
|
||
| Or: | ||
|
|
||
| ```python | ||
| import os | ||
| os.environ['MODULARCIRC_VERBOSE'] = '1' | ||
|
|
||
| import ModularCirc | ||
| # Will print: "✓ Using Cythonized HelperRoutines" or "⚠ Using Numba HelperRoutines" | ||
| ``` | ||
|
|
||
| ### Check which implementation is active | ||
|
|
||
| In your Python code: | ||
|
|
||
| ```python | ||
| from ModularCirc.HelperRoutines import USING_CYTHON | ||
|
|
||
| if USING_CYTHON: | ||
| print("Using fast Cython implementation") | ||
| else: | ||
| print("Using Numba implementation") | ||
| ``` | ||
|
|
||
| ## Environment Variables Summary | ||
|
|
||
| | Variable | Values | Default | Description | | ||
| |----------|--------|---------|-------------| | ||
| | `MODULARCIRC_USE_CYTHON` | 0 or 1 | 1 | Controls whether to build Cython extension during installation | | ||
| | `MODULARCIRC_FORCE_NUMBA` | 0 or 1 | 0 | Forces use of Numba implementation at runtime | | ||
| | `MODULARCIRC_VERBOSE` | 0 or 1 | 0 | Enables verbose output about which implementation is used | | ||
|
|
||
| ## Performance Comparison | ||
|
|
||
| - **Cython**: No JIT compilation overhead, faster startup (~2-3x faster first run) | ||
| - **Numba**: JIT compilation on first use, slightly slower startup but similar runtime performance | ||
|
|
||
| For production use or when running many small simulations, Cython is recommended. | ||
| For development or when C compiler is not available, Numba is a good fallback. | ||
|
|
||
| ## Troubleshooting | ||
|
|
||
| ### Cython build fails | ||
|
|
||
| If you encounter errors building the Cython extension: | ||
|
|
||
| ```bash | ||
| # Disable Cython and use Numba | ||
| export MODULARCIRC_USE_CYTHON=0 | ||
| pip install -e . | ||
| ``` | ||
|
|
||
| ### Want to rebuild Cython extension | ||
|
|
||
| ```bash | ||
| # Clean and rebuild | ||
| python setup.py clean --all | ||
| python setup.py build_ext --inplace | ||
| ``` | ||
|
|
||
| ### Verify installation | ||
|
|
||
| ```bash | ||
| python verify_installation.py | ||
| ``` | ||
|
|
||
| This will show which implementation is active and available. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| include README.md | ||
| include LICENSE | ||
| include CONTRIBUTING.md | ||
| include pyproject.toml | ||
| include setup.py | ||
| recursive-include src/ModularCirc *.pyx *.pxd | ||
| recursive-include src/ModularCirc *.py |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this requires
setuptools, which don't get installed.Add
setuptoolsto pyproject.toml in dependencies.