-
Notifications
You must be signed in to change notification settings - Fork 26
/
xtensor_access.hxx
525 lines (420 loc) · 21.5 KB
/
xtensor_access.hxx
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
#pragma once
#include <math.h>
#include "z5/dataset.hxx"
#include "z5/types/types.hxx"
#include "z5/multiarray/xtensor_util.hxx"
#include "z5/util/threadpool.hxx"
#include "xtensor/xarray.hpp"
#include "xtensor/xstrided_view.hpp"
#include "xtensor/xadapt.hpp"
namespace z5 {
namespace multiarray {
template<typename T, typename ARRAY>
inline void readSubarraySingleThreaded(const Dataset & ds,
xt::xexpression<ARRAY> & outExpression,
const types::ShapeType & offset,
const types::ShapeType & shape,
const std::vector<types::ShapeType> & chunkRequests) {
// need to cast to the actual xtensor implementation
auto & out = outExpression.derived_cast();
types::ShapeType offsetInRequest, requestShape, chunkShape;
types::ShapeType offsetInChunk;
const std::size_t maxChunkSize = ds.defaultChunkSize();
const auto & maxChunkShape = ds.defaultChunkShape();
std::size_t chunkSize, chunkStoreSize;
std::vector<T> buffer(maxChunkSize);
const auto & chunking = ds.chunking();
const bool isZarr = ds.isZarr();
// get the fillvalue
T fillValue;
ds.getFillValue(&fillValue);
// iterate over the chunks
for(const auto & chunkId : chunkRequests) {
// std::cout << "Reading chunk " << chunkId << std::endl;
bool completeOvlp = chunking.getCoordinatesInRoi(chunkId,
offset,
shape,
offsetInRequest,
requestShape,
offsetInChunk);
// get the view in our array
xt::xstrided_slice_vector offsetSlice;
sliceFromRoi(offsetSlice, offsetInRequest, requestShape);
auto view = xt::strided_view(out, offsetSlice);
// check if this chunk exists, if not fill output with fill value
if(!ds.chunkExists(chunkId)) {
view = fillValue;;
continue;
}
// get the shape and size of the chunk (in the actual grid)
ds.getChunkShape(chunkId, chunkShape);
chunkSize = std::accumulate(chunkShape.begin(), chunkShape.end(),
1, std::multiplies<std::size_t>());
// read the data from storage
std::vector<char> dataBuffer;
ds.readRawChunk(chunkId, dataBuffer);
// get the shape of the chunk (as stored it is stored)
chunkStoreSize = maxChunkSize;
if(!isZarr) {
if(util::read_n5_header(dataBuffer, chunkStoreSize)) {
throw std::runtime_error("Can't read from varlen chunks to multiarray");
}
}
// if this is an edge chunk and the size of the chunk stored is different from
// the size of the chunk in the grid (this is the case when written by zarr)
// we need to set complete ovlp to false and use the chunkStoreSize
if(chunkStoreSize != chunkSize) {
completeOvlp = false;
// reset chunk shape and chunk size to the full chunk size/shape
chunkSize = maxChunkSize;
chunkShape = maxChunkShape;
}
// resize the buffer if necessary
if(chunkSize != buffer.size()) {
buffer.resize(chunkSize);
}
// decompress the data
ds.decompress(dataBuffer, &buffer[0], chunkSize);
// reverse the endianness for N5 data (unless datatype is byte)
if(!isZarr && sizeof(T) > 1) {
util::reverseEndiannessInplace<T>(&buffer[0], &buffer[0] + chunkSize);
}
// request and chunk overlap completely
// -> we can read all the data from the chunk
if(completeOvlp) {
copyBufferToView(buffer, view, out.strides());
}
// request and chunk overlap only partially
// -> we can read the chunk data only partially
else {
// get a view to the part of the buffer we are interested in
auto fullBuffView = xt::adapt(buffer, chunkShape);
xt::xstrided_slice_vector bufSlice;
sliceFromRoi(bufSlice, offsetInChunk, requestShape);
auto bufView = xt::strided_view(fullBuffView, bufSlice);
// could also implement fast copy for this
// but this would be harder and might be premature optimization
view = bufView;
}
}
}
template<typename T, typename ARRAY>
inline void readSubarrayMultiThreaded(const Dataset & ds,
xt::xexpression<ARRAY> & outExpression,
const types::ShapeType & offset,
const types::ShapeType & shape,
const std::vector<types::ShapeType> & chunkRequests,
const int numberOfThreads) {
// need to cast to the actual xtensor implementation
auto & out = outExpression.derived_cast();
// construct threadpool and make a buffer for each thread
util::ThreadPool tp(numberOfThreads);
const int nThreads = tp.nThreads();
const std::size_t maxChunkSize = ds.defaultChunkSize();
const auto & maxChunkShape = ds.defaultChunkShape();
typedef std::vector<T> Buffer;
// TODO the thread buffers should be allocated by the thread that uses them
// for optimal performance
std::vector<Buffer> threadBuffers(nThreads, Buffer(maxChunkSize));
const auto & chunking = ds.chunking();
const bool isZarr = ds.isZarr();
// get the fillvalue
T fillValue;
ds.getFillValue(&fillValue);
// read the chunks in parallel
const std::size_t nChunks = chunkRequests.size();
util::parallel_foreach(tp, nChunks, [&](const int tId, const std::size_t chunkIndex){
const auto & chunkId = chunkRequests[chunkIndex];
auto & buffer = threadBuffers[tId];
types::ShapeType offsetInRequest, requestShape, chunkShape;
types::ShapeType offsetInChunk;
//std::cout << "Reading chunk " << chunkId << std::endl;
bool completeOvlp = chunking.getCoordinatesInRoi(chunkId,
offset,
shape,
offsetInRequest,
requestShape,
offsetInChunk);
// get the view in our array
xt::xstrided_slice_vector offsetSlice;
sliceFromRoi(offsetSlice, offsetInRequest, requestShape);
auto view = xt::strided_view(out, offsetSlice);
// check if this chunk exists, if not fill output with fill value
if(!ds.chunkExists(chunkId)) {
view = fillValue;;
return;
}
// get the current chunk-shape
ds.getChunkShape(chunkId, chunkShape);
std::size_t chunkSize = std::accumulate(chunkShape.begin(), chunkShape.end(),
1, std::multiplies<std::size_t>());
// read the data from storage
std::vector<char> dataBuffer;
ds.readRawChunk(chunkId, dataBuffer);
// get the shape of the chunk (as stored it is stored)
std::size_t chunkStoreSize = maxChunkSize;
if(!isZarr) {
if(util::read_n5_header(dataBuffer, chunkStoreSize)) {
throw std::runtime_error("Can't read from varlen chunks to multiarray");
}
}
// if this is an edge chunk and the size of the chunk stored is different from
// the size of the chunk in the grid (this is the case when written by zarr)
// we need to set complete ovlp to false and use the chunkStoreSize
if(chunkStoreSize != chunkSize) {
completeOvlp = false;
// reset chunk shape and chunk size to the full chunk size/shape
chunkSize = maxChunkSize;
chunkShape = maxChunkShape;
}
// resize the buffer if necessary
if(chunkSize != buffer.size()) {
buffer.resize(chunkSize);
}
// decompress the data
ds.decompress(dataBuffer, &buffer[0], chunkSize);
// reverse the endianness for N5 data (unless datatype is byte)
if(!isZarr && sizeof(T) > 1) {
util::reverseEndiannessInplace<T>(&buffer[0], &buffer[0] + chunkSize);
}
// request and chunk overlap completely
// -> we can read all the data from the chunk
if(completeOvlp) {
// fast copy implementation
copyBufferToView(buffer, view, out.strides());
}
// request and chunk overlap only partially
// -> we can read the chunk data only partially
else {
// get a view to the part of the buffer we are interested in
auto fullBuffView = xt::adapt(buffer, chunkShape);
xt::xstrided_slice_vector bufSlice;
sliceFromRoi(bufSlice, offsetInChunk, requestShape);
auto bufView = xt::strided_view(fullBuffView, bufSlice);
// could also implement smart view for this,
// but this would be kind of hard and premature optimization
view = bufView;
}
});
}
template<typename T, typename ARRAY, typename ITER>
inline void readSubarray(const Dataset & ds,
xt::xexpression<ARRAY> & outExpression,
ITER roiBeginIter,
const int numberOfThreads=1) {
// need to cast to the actual xtensor implementation
auto & out = outExpression.derived_cast();
// get the offset and shape of the request and check if it is valid
types::ShapeType offset(roiBeginIter, roiBeginIter+out.dimension());
types::ShapeType shape(out.shape().begin(), out.shape().end());
ds.checkRequestShape(offset, shape);
ds.checkRequestType(typeid(T));
// get the chunks that are involved in this request
std::vector<types::ShapeType> chunkRequests;
const auto & chunking = ds.chunking();
chunking.getBlocksOverlappingRoi(offset, shape, chunkRequests);
// read single or multi-threaded
if(numberOfThreads == 1) {
readSubarraySingleThreaded<T>(ds, out, offset, shape, chunkRequests);
} else {
readSubarrayMultiThreaded<T>(ds, out, offset, shape, chunkRequests, numberOfThreads);
}
}
template<typename T, typename ARRAY>
inline void writeSubarraySingleThreaded(const Dataset & ds,
const xt::xexpression<ARRAY> & inExpression,
const types::ShapeType & offset,
const types::ShapeType & shape,
const std::vector<types::ShapeType> & chunkRequests) {
const auto & in = inExpression.derived_cast();
types::ShapeType offsetInRequest, requestShape, chunkShape;
types::ShapeType offsetInChunk;
// get the fillvalue
T fillValue;
ds.getFillValue(&fillValue);
const std::size_t maxChunkSize = ds.defaultChunkSize();
std::size_t chunkSize = maxChunkSize;
std::vector<T> buffer(chunkSize, fillValue);
const auto & chunking = ds.chunking();
// if we have a zarr dataset, we always write the full chunk
const bool isZarr = ds.isZarr();
// iterate over the chunks
for(const auto & chunkId : chunkRequests) {
bool completeOvlp = chunking.getCoordinatesInRoi(chunkId, offset,
shape, offsetInRequest,
requestShape, offsetInChunk);
// get shape and size of this chunk
ds.getChunkShape(chunkId, chunkShape);
chunkSize = std::accumulate(chunkShape.begin(), chunkShape.end(),
1, std::multiplies<std::size_t>());
// get the view into the in-array
xt::xstrided_slice_vector offsetSlice;
sliceFromRoi(offsetSlice, offsetInRequest, requestShape);
const auto view = xt::strided_view(in, offsetSlice);
// if this is an edge chunk and we are writing zarr format,
// we need to set complete ovlp to false and clear the buffer
if(chunkSize != maxChunkSize && isZarr) {
completeOvlp = false;
// reset chunk shape and chunk size
chunkShape = ds.defaultChunkShape();
chunkSize = maxChunkSize;
// clear the buffer
std::fill(buffer.begin(), buffer.end(), fillValue);
}
// resize the buffer if necessary
if(chunkSize != buffer.size()) {
buffer.resize(chunkSize);
}
// request and chunk overlap completely
// -> we can write the whole chunk
if(completeOvlp) {
copyViewToBuffer(view, buffer, in.strides());
ds.writeChunk(chunkId, &buffer[0]);
}
// request and chunk overlap only partially
// -> we can only write partial data and need
// to preserve the data that will not be written
else {
// check if this chunk exists, and if it does, read the chunk's data
// to preserve the part that is not written to
if(ds.chunkExists(chunkId)) {
// load the current data into the buffer
if(ds.readChunk(chunkId, &buffer[0])) {
throw std::runtime_error("Can't write to varlen chunks from multiarray");
}
} else {
std::fill(buffer.begin(), buffer.end(), fillValue);
}
// overwrite the data that is covered by the request
auto fullBuffView = xt::adapt(buffer, chunkShape);
xt::xstrided_slice_vector bufSlice;
sliceFromRoi(bufSlice, offsetInChunk, requestShape);
auto bufView = xt::strided_view(fullBuffView, bufSlice);
// could also implement smart view for this,
// but this would be kind of hard and premature optimization
bufView = view;
// write the chunk
ds.writeChunk(chunkId, &buffer[0]);
}
}
}
template<typename T, typename ARRAY>
inline void writeSubarrayMultiThreaded(const Dataset & ds,
const xt::xexpression<ARRAY> & inExpression,
const types::ShapeType & offset,
const types::ShapeType & shape,
const std::vector<types::ShapeType> & chunkRequests,
const int numberOfThreads) {
const auto & in = inExpression.derived_cast();
// get the fillvalue
T fillValue;
ds.getFillValue(&fillValue);
// construct threadpool and make a buffer for each thread
util::ThreadPool tp(numberOfThreads);
const int nThreads = tp.nThreads();
const std::size_t maxChunkSize = ds.defaultChunkSize();
typedef std::vector<T> Buffer;
std::vector<Buffer> threadBuffers(nThreads, Buffer(maxChunkSize, fillValue));
const auto & chunking = ds.chunking();
const bool isZarr = ds.isZarr();
// write the chunks in parallel
const std::size_t nChunks = chunkRequests.size();
util::parallel_foreach(tp, nChunks, [&](const int tId, const std::size_t chunkIndex){
const auto & chunkId = chunkRequests[chunkIndex];
auto & buffer = threadBuffers[tId];
types::ShapeType offsetInRequest, requestShape, chunkShape;
types::ShapeType offsetInChunk;
bool completeOvlp = chunking.getCoordinatesInRoi(chunkId, offset,
shape, offsetInRequest,
requestShape, offsetInChunk);
ds.getChunkShape(chunkId, chunkShape);
std::size_t chunkSize = std::accumulate(chunkShape.begin(), chunkShape.end(),
1, std::multiplies<std::size_t>());
// get the view into the in-array
xt::xstrided_slice_vector offsetSlice;
sliceFromRoi(offsetSlice, offsetInRequest, requestShape);
const auto view = xt::strided_view(in, offsetSlice);
// if this is an edge chunk and we are writing zarr format,
// we need to set complete ovlp to false and clear the buffer
if(chunkSize != maxChunkSize && isZarr) {
completeOvlp = false;
// reset chunk shape and chunk size
chunkSize = maxChunkSize;
chunkShape = ds.defaultChunkShape();
// clear the buffer
std::fill(buffer.begin(), buffer.end(), fillValue);
}
// resize buffer if necessary
if(chunkSize != buffer.size()) {
buffer.resize(chunkSize);
}
// request and chunk overlap completely
// -> we can write the whole chunk
if(completeOvlp) {
copyViewToBuffer(view, buffer, in.strides());
ds.writeChunk(chunkId, &buffer[0]);
}
// request and chunk overlap only partially
// -> we can only write partial data and need
// to preserve the data that will not be written
else {
if(ds.chunkExists(chunkId)) {
// load the current data into the buffer
if(ds.readChunk(chunkId, &buffer[0])) {
throw std::runtime_error("Can't write to varlen chunks from multiarray");
}
} else {
std::fill(buffer.begin(), buffer.end(), fillValue);
}
// overwrite the data that is covered by the request
auto fullBuffView = xt::adapt(buffer, chunkShape);
xt::xstrided_slice_vector bufSlice;
sliceFromRoi(bufSlice, offsetInChunk, requestShape);
auto bufView = xt::strided_view(fullBuffView, bufSlice);
// could also implement smart view for this,
// but this would be kind of hard and premature optimization
bufView = view;
// write the chunk
ds.writeChunk(chunkId, &buffer[0]);
}
});
}
template<typename T, typename ARRAY, typename ITER>
inline void writeSubarray(const Dataset & ds,
const xt::xexpression<ARRAY> & inExpression,
ITER roiBeginIter,
const int numberOfThreads=1) {
const auto & in = inExpression.derived_cast();
// get the offset and shape of the request and check if it is valid
types::ShapeType offset(roiBeginIter, roiBeginIter+in.dimension());
types::ShapeType shape(in.shape().begin(), in.shape().end());
ds.checkRequestShape(offset, shape);
ds.checkRequestType(typeid(T));
// get the chunks that are involved in this request
std::vector<types::ShapeType> chunkRequests;
const auto & chunking = ds.chunking();
chunking.getBlocksOverlappingRoi(offset, shape, chunkRequests);
// write data multi or single threaded
if(numberOfThreads == 1) {
writeSubarraySingleThreaded<T>(ds, in, offset, shape, chunkRequests);
} else {
writeSubarrayMultiThreaded<T>(ds, in, offset, shape, chunkRequests, numberOfThreads);
}
}
// unique ptr API
template<typename T, typename ARRAY, typename ITER>
inline void readSubarray(std::unique_ptr<Dataset> & ds,
xt::xexpression<ARRAY> & out,
ITER roiBeginIter,
const int numberOfThreads=1) {
readSubarray<T>(*ds, out, roiBeginIter, numberOfThreads);
}
template<typename T, typename ARRAY, typename ITER>
inline void writeSubarray(std::unique_ptr<Dataset> & ds,
const xt::xexpression<ARRAY> & in,
ITER roiBeginIter,
const int numberOfThreads=1) {
writeSubarray<T>(*ds, in, roiBeginIter, numberOfThreads);
}
}
}