-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathRecordComponent.cpp
More file actions
1183 lines (1078 loc) · 41.5 KB
/
RecordComponent.cpp
File metadata and controls
1183 lines (1078 loc) · 41.5 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
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright 2018-2025 Axel Huebl, Franz Poeschel, Luca Fedeli
*
* This file is part of openPMD-api.
*
* openPMD-api is free software: you can redistribute it and/or modify
* it under the terms of of either the GNU General Public License or
* the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* openPMD-api is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License and the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* and the GNU Lesser General Public License along with openPMD-api.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <limits>
#include <pybind11/detail/common.h>
#include <pybind11/numpy.h>
#include <pybind11/pybind11.h>
#include <pybind11/pytypes.h>
#include <pybind11/stl.h>
#include "openPMD/Dataset.hpp"
#include "openPMD/Datatype.hpp"
#include "openPMD/DatatypeHelpers.hpp"
#include "openPMD/Error.hpp"
#include "openPMD/RecordComponent.hpp"
#include "openPMD/Series.hpp"
#include "openPMD/backend/BaseRecordComponent.hpp"
#include "openPMD/binding/python/Common.hpp"
#include "openPMD/binding/python/Container.H"
#include "openPMD/binding/python/Numpy.hpp"
#include "openPMD/binding/python/Pickle.hpp"
#include "openPMD/binding/python/RecordComponent.hpp"
#include <algorithm>
#include <complex>
#include <cstdint>
#include <cstring>
#include <exception>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <string>
#include <tuple>
#include <type_traits>
#include <utility>
#include <vector>
/** Convert a py::tuple of py::slices to Offset & Extent
*
* https://docs.scipy.org/doc/numpy-1.15.0/reference/arrays.indexing.html
* https://github.com/numpy/numpy/blob/v1.16.1/numpy/core/src/multiarray/mapping.c#L348-L375
*/
inline std::tuple<Offset, Extent, std::vector<bool>> parseTupleSlices(
uint8_t const ndim, Extent const &full_extent, py::tuple const &slices)
{
uint8_t const numSlices = py::len(slices);
Offset offset(ndim, 0u);
Extent extent(ndim, 1u);
std::vector<bool> flatten(ndim, false);
int16_t curAxis = -1;
int16_t posEllipsis = -1;
for (uint8_t i = 0u; i < numSlices; ++i)
{
++curAxis;
if (i >= ndim && posEllipsis == -1 && slices[i].ptr() != Py_Ellipsis)
throw py::index_error(
"too many indices for dimension of record component!");
if (slices[i].ptr() == Py_Ellipsis)
{
// only allowed once
if (posEllipsis != -1)
throw py::index_error(
"an index can only have a single ellipsis ('...')");
posEllipsis = curAxis;
// might be omitted if all other indices are given as well
if (numSlices == ndim + 1)
{
--curAxis;
continue;
}
// how many slices were given after the ellipsis
uint8_t const numSlicesAfterEllipsis =
numSlices - uint8_t(posEllipsis) - 1u;
// how many slices does the ellipsis represent
uint8_t const numSlicesEllipsis = numSlices -
uint8_t(posEllipsis) // slices before
- numSlicesAfterEllipsis; // slices after
// fill ellipsis indices
// note: if enough further indices are given, the ellipsis
// might stand for no axis: valid and ignored
for (; curAxis < posEllipsis + int16_t(numSlicesEllipsis);
++curAxis)
{
offset.at(curAxis) = 0;
extent.at(curAxis) = full_extent.at(curAxis);
}
--curAxis;
continue;
}
if (PySlice_Check(slices[i].ptr()))
{
py::slice slice = py::cast<py::slice>(slices[i]);
size_t start, stop, step, slicelength;
auto mocked_extent = full_extent.at(curAxis);
// py::ssize_t is a signed type, so we will need to use another
// magic number for JOINED_DIMENSION in this computation, since the
// C++ API's JOINED_DIMENSION would be interpreted as a negative
// index
bool undo_mocked_extent = false;
constexpr auto PYTHON_JOINED_DIMENSION =
std::numeric_limits<py::ssize_t>::max() - 1;
if (mocked_extent == Dataset::JOINED_DIMENSION)
{
undo_mocked_extent = true;
mocked_extent = PYTHON_JOINED_DIMENSION;
}
if (!slice.compute(
mocked_extent, &start, &stop, &step, &slicelength))
throw py::error_already_set();
if (undo_mocked_extent)
{
// do the same calculation again, but with another global extent
// (that is not smaller than the previous in order to avoid
// cutting off the range)
// this is to avoid the unlikely case
// that the mocked alternative value is actually the intended
// one
size_t start2, stop2, step2, slicelength2;
if (!slice.compute(
mocked_extent + 1,
&start2,
&stop2,
&step2,
&slicelength2))
throw py::error_already_set();
if (slicelength == slicelength2)
{
// slicelength was given as an absolute value and
// accidentally hit our mocked value
// --> keep that value
undo_mocked_extent = false;
}
}
// TODO PySlice_AdjustIndices: Python 3.6.1+
// Adjust start/end slice indices assuming a sequence of the
// specified length. Out of bounds indices are clipped in a
// manner consistent with the handling of normal slices.
// slicelength = PySlice_AdjustIndices(full_extent[curAxis],
// (ssize_t*)&start, (ssize_t*)&stop, step);
if (step != 1u)
throw py::index_error(
"strides in selection are inefficient, not implemented!");
// verified for size later in C++ API
offset.at(curAxis) = start;
extent.at(curAxis) =
undo_mocked_extent && slicelength == PYTHON_JOINED_DIMENSION
? Dataset::JOINED_DIMENSION
: slicelength; // stop - start;
continue;
}
try
{
auto const index = py::cast<std::int64_t>(slices[i]);
if (index < 0)
offset.at(curAxis) = full_extent.at(curAxis) + index;
else
offset.at(curAxis) = index;
extent.at(curAxis) = 1;
flatten.at(curAxis) = true; // indices flatten the dimension
if (offset.at(curAxis) >= full_extent.at(curAxis))
throw py::index_error(
std::string("index ") + std::to_string(offset.at(curAxis)) +
std::string(" is out of bounds for axis ") +
std::to_string(i) + std::string(" with size ") +
std::to_string(full_extent.at(curAxis)));
continue;
}
// NOLINTNEXTLINE(bugprone-empty-catch)
catch (const py::cast_error &e)
{
// not an index
}
if (slices[i].ptr() == Py_None)
{
// py::none newaxis = py::cast< py::none >( slices[i] );;
throw py::index_error("None (newaxis) not implemented!");
// continue;
}
// if we get here, the last slice type was not successfully processed
--curAxis;
throw py::index_error(
std::string("unknown index type passed: ") +
py::str(slices[i]).cast<std::string>());
}
// fill omitted higher indices with "select all"
for (++curAxis; curAxis < int16_t(ndim); ++curAxis)
{
extent.at(curAxis) = full_extent.at(curAxis);
}
return std::make_tuple(offset, extent, flatten);
}
inline std::tuple<Offset, Extent, std::vector<bool>> parseJoinedTupleSlices(
uint8_t const ndim,
Extent const &full_extent,
py::tuple const &slices,
size_t joined_dim,
py::array const &a)
{
std::vector<bool> flatten;
Offset offset;
Extent extent;
std::tie(offset, extent, flatten) =
parseTupleSlices(ndim, full_extent, slices);
for (size_t i = 0; i < ndim; ++i)
{
if (offset.at(i) != 0)
{
throw std::runtime_error(
"Joined array: Cannot use non-zero offset in store_chunk "
"(offset[" +
std::to_string(i) + "] = " + std::to_string(offset[i]) + ").");
}
if (flatten.at(i))
{
throw std::runtime_error(
"Flattened slices unimplemented for joined arrays.");
}
if (i == joined_dim)
{
if (extent.at(i) == 0 || extent.at(i) == Dataset::JOINED_DIMENSION)
{
extent[i] = a.shape()[i];
}
}
else
{
if (extent.at(i) != full_extent.at(i))
{
throw std::runtime_error(
"Joined array: Must use full extent in store_chunk for "
"non-joined dimension "
"(local_extent[" +
std::to_string(i) + "] = " + std::to_string(extent[i]) +
" != global_extent[" + std::to_string(i) +
"] = " + std::to_string(full_extent[i]) + ").");
}
}
}
offset.clear();
return std::make_tuple(offset, extent, flatten);
}
/** Check an array is a contiguous buffer
*
* Required are contiguous buffers for store and load
*
* - not strided with paddings
* - not a view in another buffer that results in striding
*/
inline void check_buffer_is_contiguous(py::array &a)
{
auto *view = new Py_buffer();
int flags = PyBUF_STRIDES | PyBUF_FORMAT;
if (PyObject_GetBuffer(a.ptr(), view, flags) != 0)
{
delete view;
throw py::error_already_set();
}
bool isContiguous = (PyBuffer_IsContiguous(view, 'A') != 0);
PyBuffer_Release(view);
delete view;
if (!isContiguous)
throw py::index_error(
"strides in chunk are inefficient, not implemented!");
// @todo in order to implement stride handling, one needs to
// loop over the input data strides in store/load calls
}
namespace
{
struct StoreChunkFromPythonArray
{
template <typename T>
static void call(
RecordComponent &r,
py::array &a,
Offset const &offset,
Extent const &extent)
{
a.inc_ref();
void *data = a.mutable_data();
// here, we store an owning handle in the lambda capture so that
// temporary and lost-scope variables stay alive until we flush
// note: this does not yet prevent the user, as in C++, to build
// a race condition by manipulating the data that was passed
std::shared_ptr<T> shared(
(T *)data, [owning_handle = a.cast<py::object>()](T *) {
// no-op
});
r.storeChunk(std::move(shared), offset, extent);
}
static constexpr char const *errorMsg = "store_chunk()";
};
struct LoadChunkIntoPythonArray
{
template <typename T>
static void call(
RecordComponent &r,
py::array &a,
Offset const &offset,
Extent const &extent)
{
void *data = a.mutable_data();
// here, we store an owning handle in the lambda capture so that
// temporary and lost-scope variables stay alive until we flush
// note: this does not yet prevent the user, as in C++, to build
// a race condition by manipulating the data that was passed
std::shared_ptr<T> shared(
(T *)data, [owning_handle = a.cast<py::object>()](T *) {
// no-op
});
r.loadChunk(std::move(shared), offset, extent);
}
static constexpr char const *errorMsg = "load_chunk()";
};
struct LoadChunkIntoPythonBuffer
{
template <typename T>
static void call(
RecordComponent &r,
py::buffer &buffer,
py::buffer_info const &buffer_info,
Offset const &offset,
Extent const &extent)
{
void *data = buffer_info.ptr;
// here, we store an owning handle in the lambda capture so that
// temporary and lost-scope variables stay alive until we flush
// note: this does not yet prevent the user, as in C++, to build
// a race condition by manipulating the data that was passed
std::shared_ptr<T> shared(
(T *)data, [owning_handle = buffer.cast<py::object>()](T *) {
// no-op
});
r.loadChunk(std::move(shared), offset, extent);
}
static constexpr char const *errorMsg = "load_chunk()";
};
} // namespace
/** Store Chunk
*
* Called with offset and extent that are already in the record component's
* dimension.
*
* Size checks of the requested chunk (spanned data is in valid bounds)
* will be performed at C++ API part in RecordComponent::storeChunk .
*/
inline void store_chunk(
RecordComponent &r,
py::array &a,
Offset const &offset,
Extent const &extent,
std::vector<bool> const &flatten)
{
// @todo keep locked until flush() is performed
// a.flags.writable = false;
// a.flags.owndata = false;
// verify offset + extend fit in dataset extent
// some one-size dimensions might be flattended in our r due to selections
// by index
size_t const numFlattenDims =
std::count(flatten.begin(), flatten.end(), true);
auto const r_extent = r.getExtent();
auto const &s_extent(extent); // selected extent in r
std::vector<std::uint64_t> r_shape(r_extent.size() - numFlattenDims);
std::vector<std::uint64_t> s_shape(s_extent.size() - numFlattenDims);
auto maskIt = flatten.begin();
std::copy_if(
std::begin(r_extent),
std::end(r_extent),
std::begin(r_shape),
[&maskIt](std::uint64_t) { return !*(maskIt++); });
maskIt = flatten.begin();
std::copy_if(
std::begin(s_extent),
std::end(s_extent),
std::begin(s_shape),
[&maskIt](std::uint64_t) { return !*(maskIt++); });
// verify shape and extent
if (size_t(a.ndim()) != r_shape.size())
throw py::index_error(
std::string("dimension of chunk (") + std::to_string(a.ndim()) +
std::string(
"D) does not fit dimension of selection "
"in record component (") +
std::to_string(r_shape.size()) + std::string("D)"));
if (auto joined_dim = r.joinedDimension(); joined_dim.has_value())
{
for (py::ssize_t d = 0; d < a.ndim(); ++d)
{
// selection causes overflow of r
if (d != py::ssize_t(*joined_dim) && extent.at(d) != r_shape.at(d))
throw py::index_error(
std::string("selection for axis ") + std::to_string(d) +
" of record component with joined dimension " +
std::to_string(*joined_dim) +
" must be equivalent to its global extent " +
std::to_string(extent.at(d)) + ", but was " +
std::to_string(r_shape.at(d)) + ".");
// underflow of selection in r for given a
if (s_shape.at(d) != std::uint64_t(a.shape()[d]))
throw py::index_error(
std::string("size of chunk (") +
std::to_string(a.shape()[d]) + std::string(") for axis ") +
std::to_string(d) +
std::string(" does not match selection ") +
std::string("size in record component (") +
std::to_string(s_extent.at(d)) + std::string(")"));
}
}
else
{
for (auto d = 0; d < a.ndim(); ++d)
{
// selection causes overflow of r
if (offset.at(d) + extent.at(d) > r_shape.at(d))
throw py::index_error(
std::string("slice ") + std::to_string(offset.at(d)) +
std::string(":") + std::to_string(extent.at(d)) +
std::string(" is out of bounds for axis ") +
std::to_string(d) + std::string(" with size ") +
std::to_string(r_shape.at(d)));
// underflow of selection in r for given a
if (s_shape.at(d) != std::uint64_t(a.shape()[d]))
throw py::index_error(
std::string("size of chunk (") +
std::to_string(a.shape()[d]) + std::string(") for axis ") +
std::to_string(d) +
std::string(" does not match selection ") +
std::string("size in record component (") +
std::to_string(s_extent.at(d)) + std::string(")"));
}
}
check_buffer_is_contiguous(a);
if (!dtype_to_numpy(r.getDatatype()).is(a.dtype()))
{
std::stringstream err;
err << "Attempting store from Python array of type '"
<< dtype_from_numpy(a.dtype())
<< "' into Record Component of type '" << r.getDatatype() << "'.";
throw error::WrongAPIUsage(err.str());
}
switchDatasetType<StoreChunkFromPythonArray>(
r.getDatatype(), r, a, offset, extent);
}
/** Store Chunk
*
* Called with a py::tuple of slices and a py::array
*/
inline void
store_chunk(RecordComponent &r, py::array &a, py::tuple const &slices)
{
uint8_t ndim = r.getDimensionality();
auto const full_extent = r.getExtent();
Offset offset;
Extent extent;
std::vector<bool> flatten;
if (auto joined_dimension = r.joinedDimension();
joined_dimension.has_value())
{
std::tie(offset, extent, flatten) = parseJoinedTupleSlices(
ndim, full_extent, slices, *joined_dimension, a);
}
else
{
std::tie(offset, extent, flatten) =
parseTupleSlices(ndim, full_extent, slices);
}
store_chunk(r, a, offset, extent, flatten);
}
struct PythonDynamicMemoryView
{
using ShapeContainer = pybind11::array::ShapeContainer;
template <typename T>
PythonDynamicMemoryView(
DynamicMemoryView<T> dynamicView,
ShapeContainer arrayShape,
ShapeContainer strides)
: m_dynamicView(
std::shared_ptr<void>(
new DynamicMemoryView<T>(std::move(dynamicView))))
, m_arrayShape(std::move(arrayShape))
, m_strides(std::move(strides))
, m_datatype(determineDatatype<T>())
{}
[[nodiscard]] pybind11::object currentView() const;
std::shared_ptr<void> m_dynamicView;
ShapeContainer m_arrayShape;
ShapeContainer m_strides;
Datatype m_datatype;
};
namespace
{
struct GetCurrentView
{
template <typename T>
static pybind11::object call(PythonDynamicMemoryView const &dynamicView)
{
auto span =
static_cast<DynamicMemoryView<T> *>(dynamicView.m_dynamicView.get())
->currentBuffer();
if (!span.data())
{
/*
* Fallback for zero-sized store_chunk calls.
* py::memoryview cannot be used since it checks for nullpointers,
* even when the extent is zero.
* This may sound like an esoteric use case at first, but may happen
* in parallel usage when the chunk distribution ends up assigning
* zero-sized chunks to some rank.
*/
return py::array(
dtype_to_numpy(dynamicView.m_datatype),
dynamicView.m_arrayShape,
dynamicView.m_strides);
}
else
{
return py::memoryview::from_buffer(
span.data(),
dynamicView.m_arrayShape,
dynamicView.m_strides,
/* readonly = */ false);
}
}
static constexpr char const *errorMsg = "DynamicMemoryView";
};
} // namespace
pybind11::object PythonDynamicMemoryView::currentView() const
{
return switchDatasetType<GetCurrentView>(m_datatype, *this);
}
namespace
{
struct StoreChunkSpan
{
template <typename T>
static PythonDynamicMemoryView
call(RecordComponent &r, Offset const &offset, Extent const &extent)
{
DynamicMemoryView<T> dynamicView = r.storeChunk<T>(offset, extent);
pybind11::array::ShapeContainer arrayShape(
extent.begin(), extent.end());
std::vector<py::ssize_t> strides(extent.size());
{
py::ssize_t accumulator = sizeof(T);
size_t dim = extent.size();
while (dim > 0)
{
--dim;
strides[dim] = accumulator;
accumulator *= extent[dim];
}
}
return PythonDynamicMemoryView(
std::move(dynamicView),
std::move(arrayShape),
py::array::ShapeContainer(std::move(strides)));
}
static constexpr char const *errorMsg = "RecordComponent.store_chunk()";
};
} // namespace
inline PythonDynamicMemoryView store_chunk_span(
RecordComponent &r,
Offset const &offset,
Extent const &extent,
std::vector<bool> const &flatten)
{
// some one-size dimensions might be flattended in our output due to
// selections by index
size_t const numFlattenDims =
std::count(flatten.begin(), flatten.end(), true);
std::vector<ptrdiff_t> shape(extent.size() - numFlattenDims);
auto maskIt = flatten.begin();
std::copy_if(
std::begin(extent),
std::end(extent),
std::begin(shape),
[&maskIt](std::uint64_t) { return !*(maskIt++); });
return switchDatasetType<StoreChunkSpan>(
r.getDatatype(), r, offset, extent);
}
inline PythonDynamicMemoryView
store_chunk_span(RecordComponent &r, py::tuple const &slices)
{
uint8_t ndim = r.getDimensionality();
auto const full_extent = r.getExtent();
Offset offset;
Extent extent;
std::vector<bool> flatten;
std::tie(offset, extent, flatten) =
parseTupleSlices(ndim, full_extent, slices);
return store_chunk_span(r, offset, extent, flatten);
}
/** Load Chunk
*
* Called with offset and extent that are already in the record component's
* dimension.
*
* Size checks of the requested chunk (spanned data is in valid bounds)
* will be performed at C++ API part in RecordComponent::loadChunk .
*/
void load_chunk(
RecordComponent &r,
py::buffer &buffer,
Offset const &offset,
Extent const &extent)
{
auto const dtype = dtype_to_numpy(r.getDatatype());
py::buffer_info buffer_info = buffer.request(/* writable = */ true);
auto const &strides = buffer_info.strides;
// this function requires a contiguous slab of memory, so check the strides
// whether we have that
if (strides.size() == 0)
{
throw error::WrongAPIUsage(
"[Record_Component::load_chunk()] Empty buffer passed.");
}
{
py::ssize_t accumulator = toBytes(r.getDatatype());
if (buffer_info.itemsize != accumulator)
{
std::stringstream errorMsg;
errorMsg << "[Record_Component::load_chunk()] Loading from a "
"record component of type "
<< r.getDatatype() << " with item size " << accumulator
<< ", but Python buffer has item size "
<< buffer_info.itemsize << ".";
throw error::WrongAPIUsage(errorMsg.str());
}
size_t dim = strides.size();
while (dim > 0)
{
--dim;
if (strides[dim] != accumulator)
{
throw error::WrongAPIUsage(
"[Record_Component::load_chunk()] Requires contiguous slab"
" of memory.");
}
accumulator *= extent[dim];
}
}
switchDatasetType<LoadChunkIntoPythonBuffer>(
r.getDatatype(), r, buffer, buffer_info, offset, extent);
}
/** Load Chunk
*
* Called with offset and extent that are already in the record component's
* dimension.
*
* Size checks of the requested chunk (spanned data is in valid bounds)
* will be performed at C++ API part in RecordComponent::loadChunk .
*/
inline void load_chunk(
RecordComponent &r,
py::array &a,
Offset const &offset,
Extent const &extent)
{
// check array is large enough
size_t s_load = 1u;
size_t s_array = 1u;
std::string str_extent_shape;
std::string str_array_shape;
for (auto &si : extent)
{
s_load *= si;
str_extent_shape.append(" ").append(std::to_string(si));
}
for (py::ssize_t d = 0; d < a.ndim(); ++d)
{
s_array *= a.shape()[d];
str_array_shape.append(" ").append(std::to_string(a.shape()[d]));
}
/* we allow flattening of the result dimension
if( size_t(a.ndim()) > extent.size() )
throw py::index_error(
std::string("dimension of array (") +
std::to_string(a.ndim()) +
std::string("D) does not fit dimension of selection "
"in record component (") +
std::to_string(extent.size()) +
std::string("D)")
);
*/
if (s_array < s_load)
{
throw py::index_error(
std::string("size of array (") + std::to_string(s_array) +
std::string("; shape:") + str_array_shape +
std::string(
") is smaller than size of selection "
"in record component (") +
std::to_string(s_load) + std::string("; shape:") +
str_extent_shape + std::string(")"));
}
check_buffer_is_contiguous(a);
if (!dtype_to_numpy(r.getDatatype()).is(a.dtype()))
{
std::stringstream err;
err << "Attempting load into Python array of type '"
<< dtype_from_numpy(a.dtype())
<< "' from Record Component of type '" << r.getDatatype() << "'.";
throw error::WrongAPIUsage(err.str());
}
switchDatasetType<LoadChunkIntoPythonArray>(
r.getDatatype(), r, a, offset, extent);
}
/** Load Chunk
*
* Called with a py::tuple of slices.
*/
py::array load_chunk(RecordComponent &r, py::tuple const &slices)
{
uint8_t ndim = r.getDimensionality();
auto const full_extent = r.getExtent();
Offset offset;
Extent extent;
std::vector<bool> flatten;
std::tie(offset, extent, flatten) =
parseTupleSlices(ndim, full_extent, slices);
// some one-size dimensions might be flattended in our output due to
// selections by index
size_t const numFlattenDims =
std::count(flatten.begin(), flatten.end(), true);
std::vector<ptrdiff_t> shape(extent.size() - numFlattenDims);
auto maskIt = flatten.begin();
std::copy_if(
std::begin(extent),
std::end(extent),
std::begin(shape),
[&maskIt](std::uint64_t) { return !*(maskIt++); });
auto const dtype = dtype_to_numpy(r.getDatatype());
auto a = py::array(dtype, shape);
load_chunk(r, a, offset, extent);
return a;
}
void init_RecordComponent(py::module &m)
{
py::class_<PythonDynamicMemoryView>(m, "Dynamic_Memory_View")
.def(
"__repr__",
[](PythonDynamicMemoryView const &view) {
return "<openPMD.Dynamic_Memory_view of dimensionality '" +
std::to_string(view.m_arrayShape->size()) + "'>";
})
.def("current_buffer", [](PythonDynamicMemoryView const &view) {
return view.currentView();
});
auto py_rc_cnt =
declare_container<PyRecordComponentContainer, Attributable>(
m, "Record_Component_Container");
py::class_<RecordComponent, BaseRecordComponent> cl(m, "Record_Component");
cl.def(
"__repr__",
[](RecordComponent const &rc) {
std::stringstream stream;
stream << "<openPMD.Record_Component of type '"
<< rc.getDatatype() << "' and with extent ";
if (auto extent = rc.getExtent(); extent.empty())
{
stream << "[]>";
}
else
{
auto begin = extent.begin();
stream << '[' << *begin++;
for (; begin != extent.end(); ++begin)
{
stream << ", " << *begin;
}
stream << "]>";
}
return stream.str();
})
.def_property(
"unit_SI",
&BaseRecordComponent::unitSI,
&RecordComponent::setUnitSI)
.def("reset_dataset", &RecordComponent::resetDataset)
.def_property_readonly("ndim", &RecordComponent::getDimensionality)
.def_property_readonly("shape", &RecordComponent::getExtent)
.def_property_readonly("empty", &RecordComponent::empty)
// buffer types
.def(
"make_constant",
[](RecordComponent &rc, py::buffer &a) {
py::buffer_info buf = a.request();
auto const dtype = dtype_from_bufferformat(buf.format);
using DT = Datatype;
// allow one-element n-dimensional buffers as well
py::ssize_t numElements = 1;
if (buf.ndim > 0)
{
for (auto d = 0; d < buf.ndim; ++d)
numElements *= buf.shape.at(d);
}
// Numpy: Handling of arrays and scalars
// work-around for
// https://github.com/pybind/pybind11/issues/1224
// -> passing numpy scalars as buffers needs numpy 1.15+
// https://github.com/numpy/numpy/issues/10265
// https://github.com/pybind/pybind11/issues/1224#issuecomment-354357392
// scalars, see PEP 3118
// requires Numpy 1.15+
if (numElements == 1)
{
// refs:
// https://docs.scipy.org/doc/numpy-1.15.0/reference/arrays.interface.html
// https://docs.python.org/3/library/struct.html#format-characters
// std::cout << " scalar type '" << buf.format << "'" <<
// std::endl; typestring: encoding + type + number of bytes
switch (dtype)
{
case DT::CHAR:
return rc.makeConstant(*static_cast<char *>(buf.ptr));
break;
case DT::SHORT:
return rc.makeConstant(*static_cast<short *>(buf.ptr));
break;
case DT::INT:
return rc.makeConstant(*static_cast<int *>(buf.ptr));
break;
case DT::LONG:
return rc.makeConstant(*static_cast<long *>(buf.ptr));
break;
case DT::LONGLONG:
return rc.makeConstant(
*static_cast<long long *>(buf.ptr));
break;
case DT::UCHAR:
return rc.makeConstant(
*static_cast<unsigned char *>(buf.ptr));
break;
case DT::USHORT:
return rc.makeConstant(
*static_cast<unsigned short *>(buf.ptr));
break;
case DT::UINT:
return rc.makeConstant(
*static_cast<unsigned int *>(buf.ptr));
break;
case DT::ULONG:
return rc.makeConstant(
*static_cast<unsigned long *>(buf.ptr));
break;
case DT::ULONGLONG:
return rc.makeConstant(
*static_cast<unsigned long long *>(buf.ptr));
break;
case DT::FLOAT:
return rc.makeConstant(*static_cast<float *>(buf.ptr));
break;
case DT::DOUBLE:
return rc.makeConstant(*static_cast<double *>(buf.ptr));
break;
case DT::LONG_DOUBLE:
return rc.makeConstant(
*static_cast<long double *>(buf.ptr));
break;
case DT::CFLOAT:
return rc.makeConstant(
*static_cast<std::complex<float> *>(buf.ptr));
break;
case DT::CDOUBLE:
return rc.makeConstant(
*static_cast<std::complex<double> *>(buf.ptr));
break;
case DT::CLONG_DOUBLE:
return rc.makeConstant(
*static_cast<std::complex<long double> *>(buf.ptr));
break;
case DT::BOOL:
throw std::runtime_error(
"make_constant: "
"Boolean type not supported!");
break;
default:
throw std::runtime_error(
"make_constant: "
"Unknown Datatype!");
}
}
else
{
throw std::runtime_error(
"make_constant: "
"Only scalar values supported!");
}
},
py::arg("value"))
// allowed python intrinsics, after (!) buffer matching
.def(
"make_constant",
&RecordComponent::makeConstant<char>,
py::arg("value"))
.def(
"make_constant",
&RecordComponent::makeConstant<long>,
py::arg("value"))