Metadata-Version: 2.4
Name: hbag
Version: 0.3.1
Summary: Sparse x Dense matrix multiply with adaptive OpenMP scheduling
Author: HBAG contributors
License: Apache-2.0
Project-URL: Homepage, https://example.invalid/hbag-spmm-adaptive
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy
Requires-Dist: scipy
Provides-Extra: test
Requires-Dist: pytest>=7; extra == "test"
Requires-Dist: rr-pf; extra == "test"
Dynamic: license-file
Dynamic: requires-python

# HBAG-SpMM-Adaptive v0.3.1

[![tests](https://github.com/dcpracmatic-prog/hbag-spmm-benchmark/actions/workflows/test.yml/badge.svg)](https://github.com/dcpracmatic-prog/hbag-spmm-benchmark/actions/workflows/test.yml)

SpMM (Sparse x Dense) CSR con schedule OpenMP **adaptativo** . con un
schedule que reacciona a la irregularidad de densidad por fila. Bit-exact paralelo al kernel clasico.

> Que trae v0.3.1:
> - `spmm_hbag_bounded` y `spmm_hbag_bounded_omp`: SpMM con cota de error por entrada; C es bit-exacto al clasico (build con FMA o sin FMA).
> - Que trae v0.3.0 (base):
> - `spmm_hbag_core_omp64_adaptive`: 4 modos (`0` dynamic, `2` static, `3` balanced, otros = guided).
> - Modo **balanced** (3): particion EXACTA por nnz acumulado, no es heuristica.
> - Bindings Python: `spmm_hbag` / `spmm_hbag_omp` / `spmm_hbag_native` (int64) + `spmm_hbag_adaptive`.
> - `bench.c`: 7 kernels, `--self-test`, `--csv`, flags `--rows/--cols/--k/--density/--trials/--threads/--seed`.
> - `make test` corre bench + pytest; CI en `.github/workflows/test.yml`.

**No hay binario cerrado dentro del ZIP.** La `.so` que usa la libreria
de Python se compila localmente al instalar; `setup.py` ejecuta `nm -D`
sobre `hbag/libhbag.so` y exige que exporte los 4 simbolos que
`hbag/__init__.py` requiere. Si tu toolchain no los soporta, la build
aborta con `RuntimeError`.

## Quick start

```bash
make all            # bench + cache_probe + libhbag.so
make test           # test-c (./bench --self-test) + pytest
pip install -e . --no-build-isolation
python3 -c 'import hbag; print(hbag.__version__)'   # 0.3.1
```

Uso como libreria:

```python
import hbag, numpy as np
from scipy.sparse import random

A = random(2048, 2048, density=0.02, format='csr', dtype=np.float32)
B = np.random.rand(2048, 128).astype(np.float32)

C    = hbag.spmm_hbag(A, B)                            # single-thread core
C_omp  = hbag.spmm_hbag_omp(A, B)                      # OpenMP 32-bit
C_64   = hbag.spmm_hbag_native(A, B)                   # indices int64
C_ad, state = hbag.spmm_hbag_adaptive(A, B, return_state=True)
print(state.schedule_mode, state.chunk, state.TERM_U, state.nnz_cv)
```

## Parámetros SOL-ART (qué mide cada uno)

`spmm_hbag_adaptive(..., return_state=True)` devuelve un `GovernanceState`
calculado sobre la matriz de salida `C`. Los nombres se conservan tal cual
están en el motor; la línea de cada uno dice en llano qué mide.

Estabilidad de la varianza de `C`. Vale 1 cuando los valores de `C` casi no se dispersan y baja cuando se dispersan mucho:
- **`C_Coherencia`** = `1 / (1 + var(C))`

Estabilidad de la media de `C`. Vale 1 cuando la media de `C` está en cero y baja cuando se aleja de cero:
- **`B_Balance`** = `exp(-|mean(C)|)`

Suavidad local de `C`. Vale 1 cuando posiciones vecinas tienen valores casi iguales y baja con saltos bruscos (se mide sobre las primeras 100 000 posiciones):
- **`G_Gradiente`** = `1 / (1 + mean|diff(C)|)`

Cercanía a la escala de referencia. Vale 1 cuando el máximo de `|C|` está en `V_xi` (14.9 por defecto) y baja cuando se aleja en cualquier dirección:
- **`A_Azar`** = `exp(-|max|C| - V_xi| / V_xi)`

Índice global de homeostasis. Es la suma ponderada de los cuatro anteriores; si es ≥ 0.5, `Homeostasis` es `True`:
- **`TERM_U`** = `0.60·C_Coherencia + 0.30·B_Balance + 0.05·G_Gradiente + 0.05·A_Azar`

Irregularidad del trabajo, no de los valores: desviación estándar dividida por la media de nnz por fila. Vale 0 cuando todas las filas tienen el mismo número de nnz:
- **`nnz_cv`** = `std(nnz por fila) / mean(nnz por fila)`

El schedule se decide con `TERM_U` y `nnz_cv` (ver `SOLARTEngine.decide_schedule`):
TERM_U ≥ 0.75 y CV < 0.35 → static; TERM_U ≥ 0.55 y CV < 0.80 → guided;
CV ≥ 1.0 → balanced (partición exacta por nnz); el resto → dynamic.

## Comandos del Makefile (todos verificados, exit 0)

| Comando | Que hace |
|---|---|
| `make help` | Lista los targets |
| `make all` | Compila `bench` + `cache_probe` + `hbag/libhbag.so` |
| `make bench` | Solo `./bench` |
| `make cache_probe` | Solo `./cache_probe` (cachegrind helper) |
| `make shared` | Alias de `make hbag/libhbag.so` |
| `make hbag/libhbag.so` | Solo la `.so` + verificacion `nm -D` |
| `make libhbag.so` | `.so` legacy en la raiz |
| `make run` | `./bench --csv /tmp/bench_results.csv` |
| `make run-all` | bench 1/2/4 hilos, 3 CSV en /tmp |
| `make test-c` | `./bench --self-test` |
| `make test-py` | `pytest tests/ -v` |
| `make test` | test-c + test-py |
| `make install` | `pip install -e . --no-build-isolation` |
| `make clean` | Borra binarios, `.so`, `__pycache__` |
| `make distclean` | Borra tambien CSV en /tmp |

## Uso del benchmark `./bench`

```
./bench                                          sweep default (4 configs), tabla texto
./bench --self-test                              exit 2 si algun bit-exact falla
./bench --csv [FILE]                             CSV (stdout si FILE se omite)
./bench --rows N --cols N --k K --density D      una sola config arbitraria
   [--seed S] [--trials T] [--threads T]
./bench --threads T                              fija omp_set_num_threads(T); 0 = entorno
./bench --list                                   imprime el sweep y sale
./bench -h | --help                              ayuda
```

Columnas del CSV (orden estricto):

```
config,label,N,K,density,nnz,threads,kernel,t_ms,max_err,status
```

- `t_ms` = **mejor** de N corridas (`--trials`), con 2 warmups no contados.
- `speedup` = `t(spmm_csr_std) / t(kernel)` (solo en tabla texto).
- `max_err` = error absoluto maximo frente a `spmm_csr_std`.
- `status` = `OK` si `max_err < 1.0` Y los 3 modos adaptativos son **bit-exact** contra `spmm_hbag_core_omp64`.

Los 3 modos de `spmm_hbag_core_omp64_adaptive` (dyn / static / balanced)
deben producir el mismo C bit-a-bit que `spmm_hbag_core_omp64`;
`--self-test` lo verifica y aborta (`exit 2`) si no se cumple.

Ejemplos:

```bash
./bench --csv results.csv
./bench --csv                                # CSV a stdout
./bench --rows 8192 --cols 8192 --k 256 --density 0.005 --trials 3 --csv big.csv
for t in 1 2 4; do OMP_NUM_THREADS=$t ./bench --csv bench_t${t}.csv --threads $t; done
./bench --self-test                          # smoke test bit-exact
./bench --list                                # muestra el sweep por defecto
./bench --help
```

## Suite de pruebas (pytest)

```bash
pip install -e . --no-build-isolation
pip install pytest
make test-py
```

Archivos:
- `tests/test_correctness.py` -- correctness numerico + smoke-tests C/Python.
- `tests/test_adaptive.py` -- comportamiento del schedule SOL-ART.

Cobertura:
- Cada `spmm_hbag_*` vs `scipy.sparse.csr.dot(B)` en multiples K no multiplos de 8.
- Reproducibilidad de las columnas deterministas del CSV (`N,K,density,nnz,threads,kernel,max_err,status`); `t_ms` se excluye porque es tiempo de pared (~3% jitter).
- Existencia y exportacion de los 4 simbolos en `hbag/libhbag.so` via `nm -D`.
- `--self-test` pasa end-to-end.
- `make test-c` y `make test-py` pasan.

## Estructura del repositorio

```
hbag-spmm-adaptive/
├── .github/workflows/test.yml
├── .gitignore
├── BENCHMARKS.md
├── LICENSE
├── Makefile
├── README.md
├── pyproject.toml
├── setup.py
├── hbag/__init__.py
├── src/
│   ├── bench.c
│   ├── spmm.c
│   └── spmm.h
├── tests/
│   ├── test_adaptive.py
│   └── test_correctness.py
└── tools/
    ├── adaptive_homeostasis_demo.py
    ├── cache_probe.c
    ├── large_scale_mkl_check.py
    └── multi_env_check.py
```

## Verificar el mecanismo de cache (reproducible)

```bash
make cache_probe
valgrind --tool=cachegrind --cachegrind-out-file=out.csr  ./cache_probe csr
valgrind --tool=cachegrind --cachegrind-out-file=out.hbag ./cache_probe hbag
cg_annotate out.csr  | head -25
cg_annotate out.hbag | head -25
```

## Limitaciones conocidas

- AVX2 + OpenMP requeridos para variantes multi-hilo.
- Python: indices `int32` por defecto en `spmm_hbag` / `spmm_hbag_omp`.
- Para mas de 2.1 mil millones NNZ usa `spmm_hbag_native` (`int64`).
- "Speedup adaptativo" no se reporta como constante: depende del patron de carga.

## Licencia

Apache License 2.0 -- ver [LICENSE](LICENSE).
