Debian Codegraph @

doc/file-layer.rst

raw
1 .. Copyright (c) 2016-2026 The Regents of the University of Michigan
2 .. Part of GSD, released under the BSD 2-Clause License.
3
4 File layer
5 ==========
6
7 .. highlight:: c
8
9 **Version: 2.x**
10
11 General simulation data (GSD) **file layer** design and rationale. These use
12 cases and design specifications define the low level GSD file format.
13
14 Differences from the 1.0 specification are noted.
15
16 Use-cases
17 ---------
18
19 * capabilities
20
21 * efficiently store many frames of data from simulation runs
22 * high performance file read and write
23 * support arbitrary chunks of data in each frame (position, orientation, type,
24 etc...)
25 * variable number of named chunks in each frame
26 * variable size of chunks in each frame
27 * each chunk identifies data type
28 * common use cases: NxM arrays in double, float, int, char types.
29 * generic use case: binary blob of N bytes
30 * can be integrated into other tools
31 * append frames to an existing file with a monotonically increasing frame
32 number
33 * resilient to job kills
34
35 * queries
36
37 * number of frames
38 * is named chunk present in frame *i*
39 * type and size of named chunk in frame *i*
40 * read data for named chunk in frame *i*
41 * read only a portion of a chunk
42 * list chunk names in the file
43
44 * writes
45
46 * write data to named chunk in the current frame
47 * end frame and commit to disk
48
49 These capabilities enable a simple and rich higher level schema for storing
50 particle and other types of data. The schema determine which named chunks exist
51 in a given file and what they mean.
52
53 Non use-cases
54 -------------
55
56 These capabilities are use-cases that GSD does **not** support, by design.
57
58 #. Modify data in the file: GSD is designed to capture simulation data.
59 #. Add chunks to frames in the middle of a file: See (1).
60 #. Transparent conversion between float and double: Callers must take care of
61 this.
62 #. Transparent compression: this gets in the way of parallel I/O. Disk space is
63 cheap.
64
65 Dependencies
66 ------------
67
68 The file layer is implemented in C (*not C++*) with no dependencies to enable
69 trivial installation and incorporation into existing projects. A single header
70 and C file completely implement the entire file layer. Python based projects
71 that need only read access can use :py:mod:`gsd.pygsd`, a pure Python gsd reader
72 implementation.
73
74 A Python interface to the file layer allows reference implementations and
75 convenience methods for schemas. Most non-technical users of GSD will probably
76 use these reference implementations directly in their scripts.
77
78 The low level C library is wrapped with cython. A Python ``pyproject.toml`` file will
79 provide simple installation on as many systems as possible. Cython c++ output is
80 checked in to the repository so users do not even need cython as a dependency.
81
82 Specifications
83 --------------
84
85 Support:
86
87 * Files as large as the underlying filesystem allows (up to 64-bit address
88 limits)
89 * Data chunk names of arbitrary length (v1.0 limits chunk names to 63 bytes)
90 * Reference up to 65535 different chunk names within a file
91 * Application and schema names up to 63 characters
92 * Store as many frames as can fit in a file up to file size limits
93 * Data chunks up to (64-bit) x (32-bit) elements
94
95 The limits on only 16-bit name indices and 32-bit column indices are to keep the
96 size of each index entry as small as possible to avoid wasting space in the file
97 index. The primary use cases in mind for column indices are Nx3 and Nx4 arrays
98 for position and quaternion values. Schemas that wish to store larger truly
99 n-dimensional arrays can store their dimensionality in metadata in another chunk
100 and store as an Nx1 index entry. Or use a file format more suited to
101 N-dimensional arrays such as HDF5.
102
103 File format
104 -----------
105
106 There are four types of data blocks in a GSD file.
107
108 #. Header block
109
110 * Overall header for the entire file, contains the magic cookie, a format
111 version, the name of the generating application, the schema name, and its
112 version. Some bytes in the header are reserved for future use. Header size:
113 256 bytes. The header block also includes a pointer to the index, the
114 number of allocated entries, the number of allocated entries in the index, a
115 pointer to the name list, and the size of the name list block.
116 * The header is the first 256 bytes in the file.
117
118 #. Index block
119
120 * Index the frame data, size information, location, name id, etc...
121 * The index contains space for any number of ``index_entry`` structs
122 * The first index in the list with a location of 0 marks the end of the list.
123 * When the index fills up, a new index block is allocated at the end of the
124 file with more space and all current index entries are rewritten there.
125 * Index entry size: 32 bytes
126
127 #. Name list
128
129 * List of string names used by index entries.
130 * v1.0 files: Each name is a 64-byte character string.
131 * v2.x files: Names may have any length and are separated by 0 terminators.
132 * The first name that starts with the 0 byte marks the end of the list
133 * The header stores the total size of the name list block.
134
135 #. Data chunk
136
137 * Raw binary data stored for the named frame data blocks.
138
139 Header index, and name blocks are stored in memory as C structs (or arrays of C
140 structs) and written to disk in whole chunks.
141
142 Header block
143 ^^^^^^^^^^^^
144
145 This is the header block::
146
147 struct gsd_header
148 {
149 uint64_t magic;
150 uint64_t index_location;
151 uint64_t index_allocated_entries;
152 uint64_t namelist_location;
153 uint64_t namelist_allocated_entries;
154 uint32_t schema_version;
155 uint32_t gsd_version;
156 char application[64];
157 char schema[64];
158 char reserved[80];
159 };
160
161
162 * ``magic`` is the magic number identifying this as a GSD file
163 (``0x65DF65DF65DF65DF``).
164 * ``gsd_version`` is the version number of the gsd file layer
165 (``0xaaaabbbb => aaaa.bbbb``).
166 * ``application`` is the name of the generating application.
167 * ``schema`` is the name of the schema for data in this gsd file.
168 * ``schema_version`` is the version of the schema (``0xaaaabbbb => aaaa.bbbb``).
169 * ``index_location`` is the file location f the index block.
170 * ``index_allocated_entries`` is the number of 64-byte segments available in the
171 namelist block.
172 * ``namelist_location`` is the file location of the namelist block.
173 * ``namelist_allocated_entries`` is the number of entries allocated in the
174 namelist block.
175 * ``reserved`` are bytes saved for future use.
176
177 This structure is ordered so that all known compilers at the time of writing
178 produced a tightly packed 256-byte header. Some compilers may required
179 non-standard packing attributes or pragmas to enforce this.
180
181 Index block
182 ^^^^^^^^^^^
183
184 An Index block is made of a number of line items that store a pointer to a
185 single data chunk::
186
187 struct gsd_index_entry
188 {
189 uint64_t frame;
190 uint64_t N;
191 int64_t location;
192 uint32_t M;
193 uint16_t *id*;
194 uint8_t type;
195 uint8_t flags;
196 };
197
198 * ``frame`` is the index of the frame this chunk belongs to
199 * ``N`` and ``M`` define the dimensions of the data matrix (NxM in C ordering
200 with M as the fast index).
201 * ``location`` is the location of the data chunk in the file
202 * ``id`` is the index of the name of this entry in the namelist.
203 * ``type`` is the type of the data (char, int, float, double) indicated by index
204 values
205 * ``flags`` is reserved for future use.
206
207 Many ``gsd_index_entry_t`` structs are combined into one index block. They are
208 stored densely packed and in the same order as the corresponding data chunks are
209 written to the file.
210
211 This structure is ordered so that all known compilers at the time of writing
212 produced a tightly packed 32-byte entry. Some compilers may required
213 non-standard packing attributes or pragmas to enforce this.
214
215 In v1.0 files, the frame index must monotonically increase from one index entry
216 to the next. The GSD API ensures this.
217
218 In v2.x files, the entire index block is stored sorted first by frame, then
219 by *id*.
220
221 Namelist block
222 ^^^^^^^^^^^^^^
223
224 In v2.x files, the namelist block stores a list of strings separated by 0
225 terminators.
226
227 In v1.0 files, the namelist block stores a list of 0-terminated strings in
228 64-byte segments.
229
230 The first string that starts with 0 marks the end of the list.
231
232 Data block
233 ^^^^^^^^^^
234
235 A data block stores raw data bytes on the disk. For a given index entry
236 ``entry``, the data starts at location ``entry.location`` and is the next
237 ``entry.N * entry.M * gsd_sizeof_type(entry.type)`` bytes.
238
239 Added in version 2.1
240 --------------------
241
242 * The ``GSD_CHARACTER`` chunk type represents a UTF-8 string (null termination is allowed, but not
243 required).