71#ifndef CA_STREAMING_STORAGE_H
72#define CA_STREAMING_STORAGE_H
83#include <unordered_map>
94namespace ca_stream_detail {
98 const std::uint32_t
col)
noexcept
100 return (
static_cast<std::uint64_t
>(
row) << 32) |
static_cast<std::uint64_t
>(
col);
132template <
typename State, std::
size_t TileS
ide>
135 static_assert(std::is_trivially_copyable_v<State>,
136 "Tile_Cache requires a trivially copyable State");
137 static_assert(
TileSide >= 1,
"Tile_Cache requires TileSide >= 1");
160 using key_map = std::unordered_map<std::uint64_t, lru_iter>;
175 / (
"tile_" + std::to_string(
row) +
"_" + std::to_string(
col) +
".bin");
183 if (
not std::filesystem::exists(path))
188 std::ifstream
in(path, std::ios::binary);
190 <<
"Tile_Cache: cannot read tile (" <<
row <<
"," <<
col <<
") from '"
191 << path.string() <<
"'";
192 in.read(
reinterpret_cast<char *
>(buf.data()),
193 static_cast<std::streamsize
>(
tile_cells *
sizeof(State)));
195 <<
"Tile_Cache: short read of tile (" <<
row <<
"," <<
col <<
")";
203 std::ofstream
out(path, std::ios::binary | std::ios::trunc);
205 <<
"Tile_Cache: cannot write tile (" << entry.
row <<
"," << entry.
col
206 <<
") to '" << path.string() <<
"'";
207 out.write(
reinterpret_cast<const char *
>(entry.
data.data()),
208 static_cast<std::streamsize
>(
tile_cells *
sizeof(State)));
210 <<
"Tile_Cache: stream error while writing tile (" << entry.
row <<
","
238 cache_.push_front(std::move(entry));
239 const auto it =
cache_.begin();
247 const std::uint32_t
tr =
static_cast<std::uint32_t
>(
r /
TileSide);
248 const std::uint32_t
tc =
static_cast<std::uint32_t
>(c /
TileSide);
283 std::filesystem::path dir,
288 <<
"Tile_Cache: capacity must be >= 1";
290 <<
"Tile_Cache: extents must be positive";
292 <<
"Tile_Cache: extents (" <<
rows_ <<
"," <<
cols_
293 <<
") must be multiples of TileSide=" <<
TileSide;
296 if (
not std::filesystem::exists(
dir_))
297 std::filesystem::create_directories(
dir_);
330 <<
"Tile_Cache::at: (" <<
r <<
"," << c <<
") outside ("
346 <<
"Tile_Cache::set: (" <<
r <<
"," << c <<
") outside ("
364 for (
auto &entry :
cache_)
Exception handling system with formatted messages for Aleph-w.
#define ah_out_of_range_error_if(C)
Throws std::out_of_range if condition holds.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
#define ah_runtime_error_if(C)
Throws std::runtime_error if condition holds.
size_t size_t int32_t * out
Common typedefs and tag types for the Cellular Automata module.
Out-of-core 2D tile cache with LRU eviction.
typename lru_list::iterator lru_iter
static std::size_t local_index(ca_size_t r, ca_size_t c) noexcept
std::list< Tile_Entry > lru_list
void touch(lru_iter it)
Move it to the front of the LRU list (mark as most-recent).
std::size_t capacity() const noexcept
lru_iter resolve(ca_size_t r, ca_size_t c)
Locate the tile that owns (r, c), paging it in if absent.
std::array< State, tile_cells > tile_buffer
std::array< ca_size_t, 2 > extents() const noexcept
static constexpr std::size_t tile_cells
Number of cells per tile.
std::filesystem::path tile_path(std::uint32_t row, std::uint32_t col) const
std::filesystem::path dir_
ca_size_t tiles_cols() const noexcept
State state_type
Cell value type.
std::unordered_map< std::uint64_t, lru_iter > key_map
Tile_Cache_Stats stats() const noexcept
void write_tile_to_disk(const Tile_Entry &entry)
Persist tile to disk.
Tile_Cache(const std::array< ca_size_t, 2 > &extents, std::filesystem::path dir, const std::size_t capacity)
Build a tile cache.
static constexpr std::size_t tile_side
Tile side length (in cells).
State at(ca_size_t r, ca_size_t c)
Read the cell at (r, c).
std::size_t resident() const noexcept
ca_size_t tiles_rows() const noexcept
void read_tile_from_disk(std::uint32_t row, std::uint32_t col, tile_buffer &buf)
Read tile from disk into buf.
void flush()
Persist every dirty resident tile to disk and clear the dirty flag.
void set(ca_size_t r, ca_size_t c, const State &v)
Write the cell at (r, c) and mark its tile dirty.
lru_iter page_in(std::uint32_t row, std::uint32_t col)
Page a tile in, evicting the LRU when full.
Shape (per-axis sizes) of an mdspan, mixing compile-time and run-time extents.
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
constexpr std::uint64_t tile_key(const std::uint32_t row, const std::uint32_t col) noexcept
Pack (row, col) into a stable 64-bit key for the tile map.
std::size_t ca_size_t
Unsigned size component used for extents and counts.
Main namespace for Aleph-w library functions.
Statistics surfaced by Tile_Cache::stats().
std::uint64_t misses
accesses that paged a tile in
std::uint64_t reads
tile reads from disk (load on miss)
std::uint64_t hits
accesses served from RAM
std::uint64_t evictions
tiles evicted to disk to make room
std::uint64_t writes
tile writes to disk (eviction or flush)