numba Module
Numba acceleration utilities and compatibility layer.
Overview
The numba subpackage provides njit_switch, RNG functions (binomial, multinomial),
and Numba enable/disable controls for the simulation engine.
Complete Module Reference
natal.numba
Numba acceleration subpackage.
Provides configurable JIT compilation decorators (:mod:natal.numba.utils)
and Numba-compatible helper functions with dual implementations
(:mod:natal.numba.compat).
disable_numba
Disable Numba JIT compilation globally (use pure Python).
Useful for debugging or when you need Python-compatible error messages. Note: Numba is ENABLED by default.
Source code in src/natal/numba/utils.py
disable_numba_log
Disable all formatted Numba JIT/cache status output.
Source code in src/natal/numba/utils.py
disable_numba_signature_trace
Disable signature diagnostics for newly created Numba specializations.
enable_numba
Re-enable Numba JIT compilation globally (default state).
Numba is enabled by default for maximum performance. Call this to restore Numba after it was disabled.
Source code in src/natal/numba/utils.py
enable_numba_log
Enable formatted Numba JIT/cache status output (default state).
Source code in src/natal/numba/utils.py
enable_numba_signature_trace
Enable signature diagnostics for newly created Numba specializations.
get_numba_cache_dir
is_numba_enabled
Check if Numba JIT compilation is currently enabled.
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
True if Numba is enabled (default), False if disabled |
is_numba_log_enabled
is_numba_signature_trace_enabled
njit_switch
njit_switch(func: Optional[Callable[P, R]] = None, *, cache: bool = True, parallel: bool = False, fastmath: bool = False, **njit_kwargs: Any) -> Callable[P, R] | Callable[[Callable[P, R]], Callable[P, R]]
Numba @njit decorator for functions (controlled by global NUMBA_ENABLED flag).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Optional[Callable[P, R]]
|
Function to decorate |
None
|
cache
|
bool
|
Cache compiled functions (default: True) |
True
|
parallel
|
bool
|
Enable automatic parallelization (default: False) |
False
|
fastmath
|
bool
|
Enable fast math optimizations (default: False) |
False
|
**njit_kwargs
|
Any
|
Additional arguments for numba.njit |
{}
|
Examples:
@njit_switch
def my_func(x):
...
@njit_switch(parallel=True, fastmath=True)
def my_parallel_func(x):
...
Source code in src/natal/numba/utils.py
numba_disabled
Context manager to temporarily disable Numba (use pure Python).
Useful for debugging within a specific block. Numba is enabled by default, so it will be automatically restored after exiting the context.
Examples:
from natal.numba_utils import numba_disabled
# Default: Numba enabled
with numba_disabled():
pop.run(n_steps=10) # Temporarily uses pure Python
# Numba automatically re-enabled here
Source code in src/natal/numba/utils.py
numba_enabled
Context manager to ensure Numba is enabled (default state).
Numba is enabled by default, but this can force re-enable it if temporarily disabled elsewhere. The original state is restored after exiting the context.
Examples:
from natal.numba_utils import numba_enabled
with numba_enabled():
pop.run(n_steps=100) # Guaranteed to run with JIT
# Original state is restored after
Source code in src/natal/numba/utils.py
with_numba_disabled
Decorator to run a function with Numba disabled (pure Python).
Useful for testing functions with pure Python, which provides better error messages and debugging capabilities. Numba is enabled by default, so this decorator will restore it after the function returns.
Examples:
from natal.numba_utils import with_numba_disabled
@with_numba_disabled
def debug_version():
pop.run(n_steps=10)
# Runs with pure Python for easier debugging
debug_version() # Temporarily disables Numba
# Numba automatically restored after
Source code in src/natal/numba/utils.py
with_numba_enabled
Decorator to ensure a function runs with Numba enabled (default state).
Since Numba is enabled by default, this is primarily useful for ensuring a function uses JIT even if Numba was temporarily disabled elsewhere.
Examples:
from natal.numba_utils import with_numba_enabled
@with_numba_enabled
def performance_critical():
pop.run(n_steps=1000)
# Guaranteed to run with JIT for maximum speed
performance_critical() # Numba guaranteed enabled
# Original state is restored after