-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread.go
More file actions
372 lines (343 loc) · 10.3 KB
/
Copy pathread.go
File metadata and controls
372 lines (343 loc) · 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package npy
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"unsafe"
)
// AccessMode controls how a file's data region is brought into the process.
type AccessMode int
const (
// Auto picks between InMemory and Mmap based on the data size relative to
// available system memory. This is the default.
Auto AccessMode = iota
// InMemory reads the whole file into a heap buffer.
InMemory
// Mmap memory-maps the file (falling back to InMemory where unsupported).
Mmap
)
// autoMmapThreshold is the data size above which Auto switches to mmap when
// system memory cannot be determined.
const autoMmapThreshold = 512 << 20 // 512 MiB
var (
errMmapEmpty = errors.New("npy: cannot mmap an empty file")
errMmapUnsupported = errors.New("npy: mmap not supported on this platform")
)
// config holds resolved options.
type config struct {
mode AccessMode
maxRAMFraction float64
fortran bool // write-side: store in Fortran order
compress bool // write-side: deflate .npz entries
}
// Option customises read and write behaviour.
type Option func(*config)
// WithMode selects the access mode used when reading from a file path.
func WithMode(m AccessMode) Option { return func(c *config) { c.mode = m } }
// WithMaxRAMFraction sets the fraction of system memory above which Auto mode
// switches from reading into memory to memory-mapping. The default is 0.5.
func WithMaxRAMFraction(f float64) Option { return func(c *config) { c.maxRAMFraction = f } }
// WithFortran requests Fortran (column-major) ordering when writing.
func WithFortran(fortran bool) Option { return func(c *config) { c.fortran = fortran } }
// WithCompression enables DEFLATE compression of .npz archive entries (like
// numpy.savez_compressed). It has no effect on single .npy files. The default
// is no compression (like numpy.savez).
func WithCompression(compress bool) Option { return func(c *config) { c.compress = compress } }
func resolve(opts []Option) config {
c := config{mode: Auto, maxRAMFraction: 0.5}
for _, o := range opts {
if o != nil {
o(&c)
}
}
if c.maxRAMFraction <= 0 {
c.maxRAMFraction = 0.5
}
return c
}
// Open reads a .npy file, returning a dynamically-typed Array whose shape and
// element type are taken from the file. This is the most NumPy-like entry
// point:
//
// arr, err := npy.Open("data.npy")
// fmt.Println(arr.Shape)
//
// For memory-mapped arrays, call Close when done.
func Open(path string, opts ...Option) (*Array, error) {
cfg := resolve(opts)
useMmap, err := decideMmap(path, cfg)
if err != nil {
return nil, err
}
if useMmap {
if arr, err := openMmapped(path); err == nil {
return arr, nil
} else if !errors.Is(err, errMmapEmpty) {
return nil, err
}
// empty data region: fall through to the in-memory path.
}
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
return Decode(f)
}
// Decode reads a .npy stream into a dynamically-typed Array. The whole array
// is read into memory.
func Decode(r io.Reader) (*Array, error) {
br := bufferedReader(r)
h, err := readHeader(br)
if err != nil {
return nil, err
}
buf, err := readData(br, h)
if err != nil {
return nil, err
}
return buildArray(h, buf, true), nil
}
// Load reads a .npy file into a statically-typed NDArray[T]. T must match the
// file's element kind and size (byte order is converted automatically). This
// is the fastest entry point. For memory-mapped arrays, call Close when done.
func Load[T Element](path string, opts ...Option) (*NDArray[T], error) {
cfg := resolve(opts)
want, err := dtypeFor[T]()
if err != nil {
return nil, err
}
useMmap, err := decideMmap(path, cfg)
if err != nil {
return nil, err
}
if useMmap {
if nd, err := loadMmapped[T](path, want); err == nil {
return nd, nil
} else if !errors.Is(err, errMmapEmpty) {
return nil, err
}
}
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
return Read[T](f)
}
// Read reads a .npy stream into a statically-typed NDArray[T].
func Read[T Element](r io.Reader) (*NDArray[T], error) {
want, err := dtypeFor[T]()
if err != nil {
return nil, err
}
br := bufferedReader(r)
h, err := readHeader(br)
if err != nil {
return nil, err
}
if err := checkDtype(h.dtype, want); err != nil {
return nil, err
}
buf, err := readData(br, h)
if err != nil {
return nil, err
}
return buildTyped[T](h, buf, true), nil
}
// readData reads the array's data region into a freshly-allocated, 8-byte
// aligned buffer.
func readData(r io.Reader, h header) ([]byte, error) {
need := numElements(h.shape) * h.dtype.ItemSize
buf := alignedBuffer(need)
if _, err := io.ReadFull(r, buf); err != nil {
return nil, fmt.Errorf("npy: reading data: %w", err)
}
return buf, nil
}
// openMmapped maps the file and builds a dynamically-typed Array. The Array's
// Data may alias the mapping (zero-copy); Close unmaps it.
func openMmapped(path string) (*Array, error) {
m, h, region, err := mapAndLocate(path)
if err != nil {
return nil, err
}
buf, copied := normalizeData(region, h.dtype, false)
arr := &Array{Shape: h.shape, Fortran: h.fortran, Dtype: h.dtype, Data: sliceFromBytes(buf, h.dtype)}
if copied {
m.Close() // data no longer aliases the mapping
} else {
arr.closer = m.Close
}
return arr, nil
}
// loadMmapped maps the file and builds a statically-typed NDArray[T].
func loadMmapped[T Element](path string, want DType) (*NDArray[T], error) {
m, h, region, err := mapAndLocate(path)
if err != nil {
return nil, err
}
if err := checkDtype(h.dtype, want); err != nil {
m.Close()
return nil, err
}
buf, copied := normalizeData(region, h.dtype, false)
nd := &NDArray[T]{Values: bytesAsSlice[T](buf), Shape: h.shape, Fortran: h.fortran}
if copied {
m.Close()
} else {
nd.closer = m.Close
}
return nd, nil
}
// normalizeData returns the array bytes in host byte order and suitably
// aligned for reinterpretation. owned indicates the buffer may be mutated in
// place (e.g. a freshly read buffer); when owned is false (a view into an
// mmap) and a transformation is required, a new aligned buffer is allocated
// and copied is reported true.
func normalizeData(data []byte, dt DType, owned bool) (buf []byte, copied bool) {
if dt.needSwap() {
if owned {
swapBytes(data, dt)
return data, false
}
b := alignedBuffer(len(data))
copy(b, data)
swapBytes(b, dt)
return b, true
}
if !owned && !aligned(data, requiredAlign(dt)) {
b := alignedBuffer(len(data))
copy(b, data)
return b, true
}
return data, false
}
// buildArray builds a dynamically-typed Array from the data region.
func buildArray(h header, data []byte, owned bool) *Array {
buf, _ := normalizeData(data, h.dtype, owned)
return &Array{Shape: h.shape, Fortran: h.fortran, Dtype: h.dtype, Data: sliceFromBytes(buf, h.dtype)}
}
// buildTyped builds a statically-typed NDArray[T] from the data region.
func buildTyped[T Element](h header, data []byte, owned bool) *NDArray[T] {
buf, _ := normalizeData(data, h.dtype, owned)
return &NDArray[T]{Values: bytesAsSlice[T](buf), Shape: h.shape, Fortran: h.fortran}
}
// mapAndLocate maps the file, parses its header, and returns the data region
// slice (a view into the mapping).
func mapAndLocate(path string) (*mmapHandle, header, []byte, error) {
m, err := mmapOpen(path)
if err != nil {
return nil, header{}, nil, err
}
h, err := readHeader(bytes.NewReader(m.data))
if err != nil {
m.Close()
return nil, header{}, nil, err
}
need := numElements(h.shape) * h.dtype.ItemSize
if h.dataOffset+need > len(m.data) {
m.Close()
return nil, header{}, nil, fmt.Errorf("npy: data truncated: file has %d bytes, need %d", len(m.data), h.dataOffset+need)
}
if need == 0 {
m.Close()
return nil, header{}, nil, errMmapEmpty
}
return m, h, m.data[h.dataOffset : h.dataOffset+need], nil
}
// decideMmap returns whether to memory-map the file at path under cfg.
func decideMmap(path string, cfg config) (bool, error) {
switch cfg.mode {
case InMemory:
return false, nil
case Mmap:
return mmapSupported, nil
default: // Auto
if !mmapSupported {
return false, nil
}
fi, err := os.Stat(path)
if err != nil {
return false, err
}
size := fi.Size()
ram, ok := availableRAM()
if !ok {
return size > autoMmapThreshold, nil
}
return float64(size) > float64(ram)*cfg.maxRAMFraction, nil
}
}
// checkDtype verifies that the file's element kind and size match the
// requested Go type. Byte order may differ and is converted on read.
func checkDtype(have, want DType) error {
if have.Kind != want.Kind || have.ItemSize != want.ItemSize {
return fmt.Errorf("npy: file dtype is %s (%s) but %s was requested", have, have.GoType(), want.GoType())
}
return nil
}
// sliceFromBytes reinterprets buf as the Go slice type matching dt. The bytes
// must already be in host order and suitably aligned.
func sliceFromBytes(buf []byte, dt DType) any {
switch dt.key() {
case key('f', 4):
return bytesAsSlice[float32](buf)
case key('f', 8):
return bytesAsSlice[float64](buf)
case key('i', 1):
return bytesAsSlice[int8](buf)
case key('i', 2):
return bytesAsSlice[int16](buf)
case key('i', 4):
return bytesAsSlice[int32](buf)
case key('i', 8):
return bytesAsSlice[int64](buf)
case key('u', 1):
return bytesAsSlice[uint8](buf)
case key('u', 2):
return bytesAsSlice[uint16](buf)
case key('u', 4):
return bytesAsSlice[uint32](buf)
case key('u', 8):
return bytesAsSlice[uint64](buf)
case key('b', 1):
return bytesAsSlice[bool](buf)
case key('c', 8):
return bytesAsSlice[complex64](buf)
case key('c', 16):
return bytesAsSlice[complex128](buf)
}
return nil
}
// alignedBuffer allocates a byte buffer of length n whose first byte is
// 8-byte aligned, so it can safely be reinterpreted as any supported element
// type.
func alignedBuffer(n int) []byte {
if n == 0 {
return nil
}
backing := make([]uint64, (n+7)/8)
return sliceAsBytes(backing)[:n]
}
// aligned reports whether the first byte of b meets the given alignment.
func aligned(b []byte, align int) bool {
if len(b) == 0 || align <= 1 {
return true
}
return uintptr(unsafe.Pointer(&b[0]))%uintptr(align) == 0
}
// requiredAlign returns the alignment a buffer must satisfy to be cast to the
// Go type for dt.
func requiredAlign(dt DType) int {
a := dt.ItemSize
if dt.Kind == 'c' {
a = dt.ItemSize / 2 // complex aligns to its component
}
if a > 8 {
a = 8
}
return a
}