Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 137 additions & 0 deletions bench/mmap_block_locality.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
#######################################################################
# Copyright (c) 2019-present, Blosc Development Team <blosc@blosc.org>
# All rights reserved.
#
# SPDX-License-Identifier: BSD-3-Clause
#######################################################################

"""Mmap reads across block sizes and block-boundary alignment.

Similar to tip_02_chunk_aligned_slicing.py, but benchmarks the effect
of block size variation and block-boundary alignment on mmap read times.

Read times can be 1.5-2x worse when crossing block boundaries instead
of aligning, increasing alongside the chosen block size.

Mmap accesses compressed data through OS-managed file pages. However,
these timings show the combined cost of that access and decompression;
they do not count pages or isolate page-cache activity.
"""

import tempfile
from pathlib import Path

import matplotlib.pyplot as plt
import numpy as np
from optim_tips.common import measure

import blosc2

# Test differences in block size, keep all other variables stable
ROWS, COLS = 16_000, 2_000
K_READS = 400
CHUNK_ROWS = 4_000
READ_ROWS = 50
BLOCK_ROWS = (50, 100, 200)

# Pick K_READS=400 slice locations that are on the boundaries for every
# block size.
_positions_rng = np.random.default_rng(1)
_chunk_ids = _positions_rng.integers(0, ROWS // CHUNK_ROWS, size=K_READS)
_block_ids = _positions_rng.integers(1, CHUNK_ROWS // max(BLOCK_ROWS), size=K_READS)

# Pick block-aligned reads inside chunks, then shift to cross block
# boundaries.
_aligned = _chunk_ids * CHUNK_ROWS + _block_ids * max(BLOCK_ROWS)
_crossing = _aligned - READ_ROWS // 2

# Same data construction as tip 02; aligns with the chosen block sizes.
_base = np.arange(COLS, dtype=np.float64)
_data = np.tile(_base, (ROWS, 1)) + np.arange(ROWS, dtype=np.float64)[:, None] * 0.001

# Write three temporary files, then open each through mmap for the read
# tests.
_temp_dir = tempfile.TemporaryDirectory(prefix="blosc2-mmap-blocks-")
_paths = {rows: Path(_temp_dir.name) / f"blocks-{rows}.b2nd" for rows in BLOCK_ROWS}
_arrays = {}
for _rows, _path in _paths.items():
blosc2.asarray(_data, chunks=(CHUNK_ROWS, COLS), blocks=(_rows, COLS), urlpath=_path, mode="w")
_arrays[_rows] = blosc2.open(_path, mmap_mode="r")
del _data


def read_slices(arr, starts):
for start in starts:
arr[start : start + READ_ROWS, :]


def read_50_aligned():
return read_slices(_arrays[50], _aligned)


def read_50_crossing():
return read_slices(_arrays[50], _crossing)


def read_100_aligned():
return read_slices(_arrays[100], _aligned)


def read_100_crossing():
return read_slices(_arrays[100], _crossing)


def read_200_aligned():
return read_slices(_arrays[200], _aligned)


def read_200_crossing():
return read_slices(_arrays[200], _crossing)


def save_plot(results):
x = np.arange(len(results))
width = 0.35

fig, ax = plt.subplots(figsize=(6, 3.5))
ax.bar(x - width / 2, [row[1] for row in results], width, label="Aligned")
crossing_bars = ax.bar(x + width / 2, [row[2] for row in results], width, label="Crossing")
ax.bar_label(
crossing_bars,
labels=[f"{crossing / aligned:.2f}x" for _, aligned, crossing in results],
padding=3,
)
ax.set_ylim(0, max(row[2] for row in results) * 1.2)

ax.set_xticks(x, [str(row[0]) for row in results])
ax.set_xlabel("Block rows")
ax.set_ylabel(f"Time for {K_READS} reads (s)")
ax.set_title("Mmap read time by block size and alignment")
ax.legend()
fig.tight_layout()

path = Path(__file__).resolve().parent.parent / "doc/guides/mmap_block_locality.png"
fig.savefig(path, dpi=150)
plt.close(fig)
print(f"Plot saved to {path}")


if __name__ == "__main__":
print(f"Mmap reads: {K_READS} slices of {READ_ROWS} X {COLS} float64 values")
print(f"Fixed chunk shape: {CHUNK_ROWS} X {COLS}")
print(f"{'block rows':>10} {'file MiB':>10} {'aligned s':>11} {'crossing s':>12} {'penalty':>9}")

results = []
for rows, path in _paths.items():
# Perform and measure aligned and crossing reads
aligned_time, _ = measure(__file__, f"read_{rows}_aligned")
crossing_time, _ = measure(__file__, f"read_{rows}_crossing")

results.append((rows, aligned_time, crossing_time))
print(
f"{rows:10d} {path.stat().st_size / 1024**2:10.1f} "
f"{aligned_time:11.4f} {crossing_time:12.4f} "
f"{crossing_time / aligned_time:8.2f}x"
)

save_plot(results)
1 change: 1 addition & 0 deletions doc/guides/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Topics
remote_tables
sharing_across_processes
pandas_engine
memory_mapped_storage

Command line tools
------------------
Expand Down
81 changes: 81 additions & 0 deletions doc/guides/memory_mapped_storage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Out-of-core processing with memory-mapped arrays

## How out-of-core mmap reads work

```python
# Avoid (for read-heavy, scattered access): regular I/O per chunk
arr = blosc2.open(path)

# Prefer: map the file once, read pages directly
arr = blosc2.open(path, mmap_mode="r")
```

## Memory profiling for standard vs. mmap reads

## Modes

The four `mmap_mode` options are:

| `mmap_mode` | Can read? | Can modify in-memory? | Changes saved to file? | Use case |
| --- | --- | --- | --- | --- |
| `"r"` | Yes | No | No | Read an existing file. |
| `"c"` | Yes | Yes | No | Copy-on-write: make purely in-memory changes. |
| `"r+"` | Yes | Yes | Yes | Update or append to an existing file. |
| `"w+"` | Yes | Yes | Yes | Create or overwrite an existing file; specified in constructor. |

Notes:

- the `mmap_mode` argument also overrides the separate `mode` argument.
- `mmap_mode` is only supported for contiguous files.

## Chunk and block sizing for mmap

Arrays in Blosc2 have a double-partition, by which arrays are initially
split into chunks, and those chunks are split into blocks. Blocks are
sized to fit CPU caches, and are the individual units of decompression.
For the same-sized read, decompressing less blocks generally means
less work. With that said, aligning reads with blocks and chunks (as
opposed to having the reads cross boundaries) is good practice.

The [Optimization Tips](https://blosc.org/python-blosc2/guides/optimization_tips.html#align-your-reads-with-the-double-partition) guide benchmarks how
read time is affected by keeping reads aligned to the double-partition.
However, the benchmark was recorded with regular I/O instead of mmap.

For mmap reads, keeping reads aligned still matters. The following chart
shows how crossing blocks increases mmap read time on different block
sizes. It was recorded on an AMD Ryzen™ AI 7 PRO 350 machine, with fixed
sizes for read requests, chunks and the array itself:

![mmap-block-locality.png](mmap_block_locality.png)

In this run, reading across block boundaries leads to a 1.8-2x penalty
in wall time, compared to staying aligned with blocks. The penalty
increases when block size increases. The measured times account for the
combined cost of mapped-file access and decompression work, although it
does not separate them. See mmap_block_locality.py to reproduce.

## Concurrency with mmap

For reading the same Blosc2 file across multiple processes, using
`mmap_mode=r` is recommended. Regular-I/O access leads to each process
copying the OS page cache data for each block read individually,
whereas mmap allows each process to map onto the page cache data and
effectively share the corresponding blocks:

![mmap_concurrency.png](mmap_concurrency.png)

The chart below quantifies this effect. It compares reads with regular
I/O and mmap across 1-8 concurrent readers on the same Apple M4 Pro
machine, 269 MB NDarray, and a warm OS cache. The difference gets
larger as more readers are added:

![tip_10_mmap_many_readers.png](optim_tips/tip_10_mmap_many_readers.png)

With 8 concurrent readers, mmap yields ~1.1x faster wall time and 5%
less total CPU. The improvement accounts for regular file I/O requiring
a syscall and buffer copy for every block read. Although the improvement
is modest, this makes mmap a default for concurrent access.

See [Sharing
Containers Across Processes](https://blosc.org/python-blosc2/guides/sharing_across_processes.html#colophon-how-much-does-mmap-help-many-readers) for more details,
and run [`tip_10_mmap_many_readers.py`](https://github.com/Blosc/python-blosc2/blob/main/bench/optim_tips/tip_10_mmap_many_readers.py) to reproduce.
Binary file added doc/guides/mmap_block_locality.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/guides/mmap_concurrency.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading