Metadata-Version: 2.5
Name: unipram
Version: 0.1.0
Summary: Write PRAM algorithms once; check them on a simulator, run them on threads or a GPU
Project-URL: Documentation, https://laveneau.github.io/unipram/
Project-URL: Source, https://github.com/laveneau/unipram
Project-URL: Issues, https://github.com/laveneau/unipram/issues
Author: Lilian Aveneau
License-Expression: MIT
License-File: LICENSE
Keywords: GPU,PRAM,parallel algorithms,simulator,teaching
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Education
Classifier: Natural Language :: French
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Topic :: Education
Classifier: Typing :: Typed
Requires-Python: >=3.12
Requires-Dist: numpy>=2.0
Provides-Extra: cuda
Requires-Dist: unigpu[cuda]>=0.3; extra == 'cuda'
Provides-Extra: cuda13
Requires-Dist: unigpu[cuda13]>=0.4; extra == 'cuda13'
Provides-Extra: docs
Requires-Dist: mkdocs-material>=9.6; extra == 'docs'
Requires-Dist: mkdocs<2,>=1.6; extra == 'docs'
Requires-Dist: mkdocstrings[python]>=0.29; extra == 'docs'
Provides-Extra: gpu
Requires-Dist: unigpu>=0.3; extra == 'gpu'
Provides-Extra: metal
Requires-Dist: unigpu[metal]>=0.3; extra == 'metal'
Provides-Extra: opencl
Requires-Dist: unigpu[opencl]>=0.3; extra == 'opencl'
Provides-Extra: test
Requires-Dist: mypy>=1.15; extra == 'test'
Requires-Dist: pytest-cov>=5; extra == 'test'
Requires-Dist: pytest>=8; extra == 'test'
Requires-Dist: ruff>=0.9; extra == 'test'
Requires-Dist: unigpu>=0.3; extra == 'test'
Description-Content-Type: text/markdown

# unipram: write a PRAM algorithm once, check it, run it anywhere

`unipram` is made to **teach parallel algorithms**. A PRAM algorithm is written
once, in plain Python. The same code then runs on three machines:

| Machine | What it does |
|---|---|
| `SimulatedPRAM(model, writes)` | an ideal machine, with as many processors as you need: it **checks every memory access** against EREW, CREW or CRCW, and counts steps and work |
| `ThreadedPRAM(nb_threads)` | runs each step on a few threads (Brent's theorem); use free-threaded Python for a real speed-up |
| `GpuPRAM(device)` | runs each step as a GPU kernel, with [unigpu](https://pypi.org/project/unigpu/) |

**[Documentation](https://laveneau.github.io/unipram/)** (in French, like the
error messages and the docstrings).

## Install

```bash
python -m pip install unipram            # simulator and threads (needs only NumPy)
python -m pip install "unipram[metal]"   # with a GPU: metal, cuda, cuda13 or opencl
```

## Example

```python
from unipram import Model, SimulatedPRAM


def max_step(pe, jump, t):                # one processor, during one step
    if pe + jump < t.size:
        t[pe] = max(t[pe], t[pe + jump])


def maximum(machine, values):             # the program
    t = machine.array(values, name="T")
    jump = 1
    while jump < t.size:
        machine.parallel_for(t.size, max_step, jump, t)
        jump *= 2
    return int(machine.read(t)[0])


machine = SimulatedPRAM(Model.CREW)
print(maximum(machine, [3, 9, 4, 1]), machine.costs())
# 9 2 pas, travail 8, 4 PE au plus
```

During a step, every read sees the memory as it was at the start of the step,
and writes take effect at its end. This algorithm is not EREW, and the
simulator says so:

```python
maximum(SimulatedPRAM(Model.EREW), [3, 9, 4, 1])
# AccessViolation: EREW violé au pas 1 : la case T[1] est lue par 2 PE (0, 1)
```

Only the simulator checks the accesses: debug there first. An instruction must
be a plain function, defined with `def` in a file or a notebook. It takes the
number of its processor, then numbers and shared arrays, and writes only to
these arrays and its local variables. Every machine refuses anything else.

## See every step

```python
from unipram import TraceRecorder, TraceView, wait_for_enter

trace = TraceRecorder()
maximum(SimulatedPRAM(Model.CREW, observers=[trace]), [3, 9, 4, 1])
TraceView(print, pause=wait_for_enter).show(trace.records)
```

This shows what each processor reads and writes, then the arrays before and
after the step. Changed cells are in brackets, and large data are shortened.

## Extend it without changing it

- **Another model:** `Model("ERCW", concurrent_reads=False, concurrent_writes=True)`.
- **Another rule for concurrent writes:** a subclass of `WritePolicy`, like
  `ArbitraryWrite`, `ConsistentWrite` and `MergeWrite(operator)`.
- **Another measure:** a subclass of `StepObserver`, given to the simulator.
- **Another machine:** a subclass of `PRAM`.

To raise an error when the written values are incompatible, a `WritePolicy`
raises `WriteConflict`.

## Develop

```bash
python -m pip install -e ".[test]"
python -m ruff check .
python -m mypy
python -m pytest --cov

python -m pip install -e ".[docs]"
python -m mkdocs serve
```

## License

MIT
