ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
TwoLevelMemory.h File Reference

The two-level memory model of Aggarwal and Vitter, simulated so that the algorithms of the external memory can be run and their I/Os counted: an internal memory of \( M \) items and a disk of unbounded size, read and written by blocks of \( B \) items, an I/O moving one block. More...

#include "ADS/Contract.h"
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <utility>
#include <vector>
#include "ADS/external/impl/TwoLevelMemory.tpp"

Go to the source code of this file.

Classes

class  ADS::TwoLevelMemory
 the parameters \( B \) and \( M \) of the model, the counters of the I/Os and the occupation of the internal memory More...
class  ADS::MemoryLease
 \( k \) items of the internal memory, taken on construction and given back on destruction More...
class  ADS::DiskFile< T >
 a file of items on the disk of the model, in blocks of \( B \) items More...
class  ADS::DiskReader< T >
 reads a file through a buffer of one block, \( B \) items of the memory More...
class  ADS::DiskWriter< T >
 appends to a file through a buffer of one block, \( B \) items of the memory; the last partial block is written by close() or by the destructor More...

Namespaces

namespace  ADS

Detailed description

The two-level memory model of Aggarwal and Vitter, simulated so that the algorithms of the external memory can be run and their I/Os counted: an internal memory of \( M \) items and a disk of unbounded size, read and written by blocks of \( B \) items, an I/O moving one block.

The time of the computation in memory is free; what is counted is the number of reads and the number of writes.

  • TwoLevelMemory holds \( B \) and \( M \), the two counters of the I/Os and the occupation of the memory: every buffer and every array that an algorithm keeps in memory is allocated from it, by a MemoryLease, and an allocation beyond \( M \) violates a precondition, so that a test of an algorithm checks at once its result, its I/Os and the memory it used (peak());
  • DiskFile is a file on the disk, a sequence of items laid out in blocks of \( B \) items, the last one possibly partial; readBlock() and appendBlock() are the only accesses to its items, one I/O each, and contents() is the view without I/Os, for the checks of the tests;
  • DiskReader reads a file through a buffer of one block: next() scans it in order, \( \lceil n / B \rceil \) reads for \( n \) items, and at( i ) reads any item, loading its block only when it is not the one in the buffer, so that a sorted set of \( m \) positions costs at most \( \min\{ m , \lceil n / B \rceil \} \) reads;
  • DiskWriter appends to a file through a buffer of one block, a write per full block and one for the last partial block.

The bounds of the model are those of Ferragina's book (chapter 1, Introduction, and chapter 5, Sorting Atomic Items): a scan of \( n \) items takes \( \Theta( n / B ) \) I/Os, a sort \( \Theta( ( n / B ) \log_{M / B} ( n / M ) ) \) [see ExternalSorting.h], and an algorithm that follows pointers on the disk pays up to one I/O per pointer, which is what the external algorithms of this library avoid. The files are not containers with iterators on purpose: an iterator would read the items without paying the I/Os.

The contracts [see Contract.h]: \( B \ge 1 \) and \( M \ge 2 B \); the memory in use never exceeds \( M \); a block read exists; a block is appended only after full blocks; a file and its reader or writer share the memory. The functions are defined in TwoLevelMemory.tpp.

See also
P. Ferragina, Pearls of Algorithm Engineering, Cambridge University Press, 2023, chapters 1, Introduction, and 5, Sorting Atomic Items, doi:10.1017/9781009128933
A. Aggarwal, J. S. Vitter, The input/output complexity of sorting and related problems, Communications of the ACM 31(9), 1116-1127, 1988, doi:10.1145/48529.48535
J. S. Vitter, External memory algorithms and data structures: dealing with massive data, ACM Computing Surveys 33(2), 209-271, 2001, doi:10.1145/384192.384193
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa