-
Notifications
You must be signed in to change notification settings - Fork 51
/
SerialIOTest.cpp
7286 lines (6586 loc) · 252 KB
/
SerialIOTest.cpp
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
// expose private and protected members for invasive testing
#if openPMD_USE_INVASIVE_TESTS
#define OPENPMD_private public:
#define OPENPMD_protected public:
#endif
#include "openPMD/auxiliary/Environment.hpp"
#include "openPMD/auxiliary/Filesystem.hpp"
#include "openPMD/auxiliary/StringManip.hpp"
#include "openPMD/openPMD.hpp"
#if openPMD_HAVE_ADIOS2
#include <adios2.h>
#endif
#include <catch2/catch.hpp>
#include <algorithm>
#include <array>
#include <chrono>
#include <cmath>
#include <complex>
#include <fstream>
#include <iostream>
#include <limits>
#include <list>
#include <memory>
#include <numeric>
#include <sstream>
#include <stdexcept>
#include <string>
#include <thread>
#include <tuple>
#include <vector>
#ifdef __unix__
#include <dirent.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#endif
using namespace openPMD;
struct BackendSelection
{
std::string backendName;
std::string extension;
[[nodiscard]] inline std::string jsonBaseConfig() const
{
return R"({"backend": ")" + backendName + "\"}";
}
};
std::vector<BackendSelection> testedBackends()
{
auto variants = getVariants();
std::map<std::string, std::string> extensions{
{"json", "json"},
{"adios1", "adios1.bp"},
{"adios2", "bp"},
{"hdf5", "h5"}};
std::vector<BackendSelection> res;
for (auto const &pair : variants)
{
if (pair.second)
{
auto lookup = extensions.find(pair.first);
if (lookup != extensions.end())
{
std::string extension = lookup->second;
res.push_back({std::move(pair.first), std::move(extension)});
}
}
}
return res;
}
std::vector<std::string> testedFileExtensions()
{
auto allExtensions = getFileExtensions();
auto newEnd = std::remove_if(
allExtensions.begin(), allExtensions.end(), [](std::string const &ext) {
// sst and ssc need a receiver for testing
// bp4 is already tested via bp
return ext == "sst" || ext == "ssc" || ext == "bp4";
});
return {allExtensions.begin(), newEnd};
}
#if openPMD_HAVE_ADIOS2
TEST_CASE("adios2_char_portability", "[serial][adios2]")
{
/*
* This tests portability of char attributes in ADIOS2 in schema 20210209.
*/
if (auxiliary::getEnvString("OPENPMD_NEW_ATTRIBUTE_LAYOUT", "NOT_SET") ==
"NOT_SET")
{
/*
* @todo As soon as we have added automatic detection for the new
* layout, this environment variable should be ignore read-side.
* Then we can delete this if condition again.
*/
return;
}
// @todo remove new_attribute_layout key as soon as schema-based versioning
// is merged
std::string const config = R"END(
{
"adios2":
{
"new_attribute_layout": true,
"schema": 20210209
}
})END";
{
adios2::ADIOS adios;
auto IO = adios.DeclareIO("IO");
auto engine = IO.Open(
"../samples/adios2_char_portability.bp", adios2::Mode::Write);
engine.BeginStep();
// write default openPMD attributes
auto writeAttribute = [&engine,
&IO](std::string const &name, auto value) {
using variable_type = decltype(value);
engine.Put(IO.DefineVariable<variable_type>(name), value);
};
writeAttribute("/basePath", std::string("/data/%T/"));
writeAttribute("/date", std::string("2021-02-22 11:14:00 +0000"));
writeAttribute("/iterationEncoding", std::string("groupBased"));
writeAttribute("/iterationFormat", std::string("/data/%T/"));
writeAttribute("/openPMD", std::string("1.1.0"));
writeAttribute("/openPMDextension", uint32_t(0));
writeAttribute("/software", std::string("openPMD-api"));
writeAttribute("/softwareVersion", std::string("0.14.0-dev"));
IO.DefineAttribute<uint64_t>(
"__openPMD_internal/openPMD2_adios2_schema", 20210209);
IO.DefineAttribute<unsigned char>("__openPMD_internal/useSteps", 1);
// write char things that should be read back properly
std::string baseString = "abcdefghi";
// null termination not necessary, ADIOS knows the size of its variables
std::vector<signed char> signedVector(9);
std::vector<unsigned char> unsignedVector(9);
for (unsigned i = 0; i < 9; ++i)
{
signedVector[i] = baseString[i];
unsignedVector[i] = baseString[i];
}
engine.Put(
IO.DefineVariable<signed char>(
"/signedVector", {3, 3}, {0, 0}, {3, 3}),
signedVector.data());
engine.Put(
IO.DefineVariable<unsigned char>(
"/unsignedVector", {3, 3}, {0, 0}, {3, 3}),
unsignedVector.data());
engine.Put(
IO.DefineVariable<char>(
"/unspecifiedVector", {3, 3}, {0, 0}, {3, 3}),
baseString.c_str());
writeAttribute("/signedChar", (signed char)'a');
writeAttribute("/unsignedChar", (unsigned char)'a');
writeAttribute("/char", (char)'a');
engine.EndStep();
engine.Close();
}
{
if (auxiliary::getEnvString("OPENPMD_BP_BACKEND", "ADIOS2") != "ADIOS2")
{
return;
}
Series read(
"../samples/adios2_char_portability.bp", Access::READ_ONLY, config);
auto signedVectorAttribute = read.getAttribute("signedVector");
REQUIRE(signedVectorAttribute.dtype == Datatype::VEC_STRING);
auto unsignedVectorAttribute = read.getAttribute("unsignedVector");
REQUIRE(unsignedVectorAttribute.dtype == Datatype::VEC_STRING);
auto unspecifiedVectorAttribute =
read.getAttribute("unspecifiedVector");
REQUIRE(unspecifiedVectorAttribute.dtype == Datatype::VEC_STRING);
std::vector<std::string> desiredVector{"abc", "def", "ghi"};
REQUIRE(
signedVectorAttribute.get<std::vector<std::string>>() ==
desiredVector);
REQUIRE(
unsignedVectorAttribute.get<std::vector<std::string>>() ==
desiredVector);
REQUIRE(
unspecifiedVectorAttribute.get<std::vector<std::string>>() ==
desiredVector);
auto signedCharAttribute = read.getAttribute("signedChar");
// we don't have that datatype yet
// REQUIRE(unsignedCharAttribute.dtype == Datatype::SCHAR);
auto unsignedCharAttribute = read.getAttribute("unsignedChar");
REQUIRE(unsignedCharAttribute.dtype == Datatype::UCHAR);
auto charAttribute = read.getAttribute("char");
// might currently report Datatype::UCHAR on some platforms
// REQUIRE(unsignedCharAttribute.dtype == Datatype::CHAR);
REQUIRE(signedCharAttribute.get<char>() == char('a'));
REQUIRE(unsignedCharAttribute.get<char>() == char('a'));
REQUIRE(charAttribute.get<char>() == char('a'));
}
}
#endif
void write_and_read_many_iterations(
std::string const &ext, bool intermittentFlushes)
{
// the idea here is to trigger the maximum allowed number of file handles,
// e.g., the upper limit in "ulimit -n" (default: often 1024). Once this
// is reached, files should be closed automatically for open iterations
// By flushing the series before closing an iteration, we ensure that the
// iteration is not dirty before closing
// Our flushing logic must not forget to close even if the iteration is
// otherwise untouched and needs not be flushed.
unsigned int nIterations =
auxiliary::getEnvNum("OPENPMD_TEST_NFILES_MAX", 1030);
std::string filename =
"../samples/many_iterations/many_iterations_%T." + ext;
std::vector<float> data(10);
std::iota(data.begin(), data.end(), 0.);
Dataset ds{Datatype::FLOAT, {10}};
{
Series write(filename, Access::CREATE);
for (unsigned int i = 0; i < nIterations; ++i)
{
// std::cout << "Putting iteration " << i << std::endl;
Iteration it = write.iterations[i];
auto E_x = it.meshes["E"]["x"];
E_x.resetDataset(ds);
E_x.storeChunk(data, {0}, {10});
if (intermittentFlushes)
{
write.flush();
}
it.close();
}
// ~Series intentionally not yet called
Series read(
filename, Access::READ_ONLY, "{\"defer_iteration_parsing\": true}");
for (auto iteration : read.iterations)
{
iteration.second.open();
// std::cout << "Reading iteration " << iteration.first <<
// std::endl;
auto E_x = iteration.second.meshes["E"]["x"];
auto chunk = E_x.loadChunk<float>({0}, {10});
if (intermittentFlushes)
{
read.flush();
}
iteration.second.close();
auto array = chunk.get();
for (size_t i = 0; i < 10; ++i)
{
REQUIRE(array[i] == float(i));
}
}
}
Series list(filename, Access::READ_ONLY);
helper::listSeries(list);
}
TEST_CASE("write_and_read_many_iterations", "[serial]")
{
bool intermittentFlushes = false;
if (auxiliary::directory_exists("../samples/many_iterations"))
auxiliary::remove_directory("../samples/many_iterations");
for (auto const &t : testedFileExtensions())
{
if (t == "bp4" || t == "bp5")
{
continue;
}
write_and_read_many_iterations(t, intermittentFlushes);
intermittentFlushes = !intermittentFlushes;
}
}
TEST_CASE("multi_series_test", "[serial]")
{
std::list<Series> allSeries;
auto myfileExtensions = testedFileExtensions();
// this test demonstrates an ADIOS1 (upstream) bug, comment this section to
// trigger it
auto const rmEnd = std::remove_if(
myfileExtensions.begin(),
myfileExtensions.end(),
[](std::string const &beit) {
return beit == "bp" && determineFormat("test.bp") == Format::ADIOS1;
});
myfileExtensions.erase(rmEnd, myfileExtensions.end());
// have multiple serial series alive at the same time
for (auto const sn : {1, 2, 3})
{
for (auto const &t : myfileExtensions)
{
auto const file_ending = t;
std::cout << file_ending << std::endl;
allSeries.emplace_back(
std::string("../samples/multi_open_test_")
.append(std::to_string(sn))
.append(".")
.append(file_ending),
Access::CREATE);
allSeries.back().iterations[sn].setAttribute("wululu", sn);
allSeries.back().flush();
}
}
// skip some series: sn=1
auto it = allSeries.begin();
std::for_each(
myfileExtensions.begin(),
myfileExtensions.end(),
[&it](std::string const &) { it++; });
// remove some series: sn=2
std::for_each(
myfileExtensions.begin(),
myfileExtensions.end(),
[&it, &allSeries](std::string const &) { it = allSeries.erase(it); });
// write from last series: sn=3
std::for_each(
myfileExtensions.begin(),
myfileExtensions.end(),
[&it](std::string const &) {
it->iterations[10].setAttribute("wululu", 10);
it->flush();
it++;
});
// remove all leftover series
allSeries.clear();
}
TEST_CASE("available_chunks_test_json", "[serial][json]")
{
/*
* This test is JSON specific
* Our JSON backend does not store chunks explicitly,
* so the JSON backend will simply go through the multidimensional array
* and gather the data items into chunks
* Example dataset:
*
* 0123
* 0 ____
* 1 ____
* 2 ****
* 3 ****
* 4 ****
* 5 ****
* 6 ****
* 7 **__
* 8 **_*
* 9 ___*
*
* Will be read as three chunks:
* 1. (2,0) -- (5,4) (offset -- extent)
* 2. (7,0) -- (2,2) (offset -- extent)
* 3. (8,3) -- (2,1) (offset -- extent)
*
* Since the chunks are reconstructed, they won't necessarily
* correspond with the way that the chunks were written.
* As an example, let's write the first chunk in the above depiction
* line by line.
*/
constexpr unsigned height = 10;
std::string name = "../samples/available_chunks.json";
std::vector<int> data{2, 4, 6, 8};
{
Series write(name, Access::CREATE);
Iteration it0 = write.iterations[0];
auto E_x = it0.meshes["E"]["x"];
E_x.resetDataset({Datatype::INT, {height, 4}});
for (unsigned line = 2; line < 7; ++line)
{
E_x.storeChunk(data, {line, 0}, {1, 4});
}
for (unsigned line = 7; line < 9; ++line)
{
E_x.storeChunk(data, {line, 0}, {1, 2});
}
E_x.storeChunk(data, {8, 3}, {2, 1});
auto E_y = it0.meshes["E"]["y"];
E_y.resetDataset({Datatype::INT, {height, 4}});
E_y.makeConstant(1234);
it0.close();
}
{
Series read(name, Access::READ_ONLY);
Iteration it0 = read.iterations[0];
auto E_x = it0.meshes["E"]["x"];
ChunkTable table = E_x.availableChunks();
REQUIRE(table.size() == 3);
/*
* Explicitly convert things to bool, so Catch doesn't get the splendid
* idea to print the Chunk struct.
*/
REQUIRE(bool(table[0] == WrittenChunkInfo({2, 0}, {5, 4})));
REQUIRE(bool(table[1] == WrittenChunkInfo({7, 0}, {2, 2})));
REQUIRE(bool(table[2] == WrittenChunkInfo({8, 3}, {2, 1})));
auto E_y = it0.meshes["E"]["y"];
table = E_y.availableChunks();
REQUIRE(table.size() == 1);
REQUIRE(bool(table[0] == WrittenChunkInfo({0, 0}, {height, 4})));
}
}
TEST_CASE("multiple_series_handles_test", "[serial]")
{
/*
* clang also understands these pragmas.
*/
#if defined(__GNUC_MINOR__) && !defined(__INTEL_COMPILER)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#elif defined(__clang__)
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
#endif
/*
* First test: No premature flushes through destructor when another copy
* is still around
*/
{
std::unique_ptr<openPMD::Series> series_ptr;
{
openPMD::Series series(
"sample%T.json", openPMD::AccessType::CREATE);
series_ptr = std::make_unique<openPMD::Series>(series);
/*
* we have two handles for the same Series instance now:
* series and series_ptr
* series goes out of scope before series_ptr
* destructor should only do a flush after both of them go out of
* scope, but it will currently run directly after this comment
* since no iteration has been written yet, an error will be thrown
*/
}
series_ptr->iterations[0].meshes["E"]["x"].makeEmpty<int>(1);
}
/*
* Second test: A Series handle should remain accessible even if the
* original handle is destroyed
*/
{
std::unique_ptr<openPMD::Series> series_ptr;
{
openPMD::Series series(
"sample%T.json", openPMD::AccessType::CREATE);
series_ptr = std::make_unique<openPMD::Series>(series);
series_ptr->iterations[0].meshes["E"]["x"].makeEmpty<int>(1);
}
/*
* series_ptr is still in scope, but the original Series instance
* has been destroyed
* since internal pointers actually refer to the original *handle*,
* doing anything with series_ptr now (such as flushing it) yields
* nullpointer accesses
*/
series_ptr->flush();
}
#if defined(__GNUC_MINOR__) && !defined(__INTEL_COMPILER)
#pragma GCC diagnostic pop
#elif defined(__clang__)
#pragma clang diagnostic pop
#endif
}
void close_iteration_test(std::string file_ending)
{
std::string name = "../samples/close_iterations_%T." + file_ending;
std::vector<int> data{2, 4, 6, 8};
// { // we do *not* need these parentheses
Series write(name, Access::CREATE);
bool isAdios1 = write.backend() == "ADIOS1";
{
Iteration it0 = write.iterations[0];
auto E_x = it0.meshes["E"]["x"];
E_x.resetDataset({Datatype::INT, {2, 2}});
E_x.storeChunk(data, {0, 0}, {2, 2});
it0.close(/* flush = */ false);
}
write.flush();
// }
if (isAdios1)
{
// run a simplified test for Adios1 since Adios1 has issues opening
// twice in the same process
REQUIRE(auxiliary::file_exists("../samples/close_iterations_0.bp"));
}
else
{
Series read(name, Access::READ_ONLY);
Iteration it0 = read.iterations[0];
auto E_x_read = it0.meshes["E"]["x"];
auto chunk = E_x_read.loadChunk<int>({0, 0}, {2, 2});
it0.close(/* flush = */ false);
read.flush();
for (size_t i = 0; i < data.size(); ++i)
{
REQUIRE(data[i] == chunk.get()[i]);
}
}
{
Iteration it1 = write.iterations[1];
auto E_x = it1.meshes["E"]["x"];
E_x.resetDataset({Datatype::INT, {2, 2}});
E_x.storeChunk(data, {0, 0}, {2, 2});
it1.close(/* flush = */ true);
// illegally access iteration after closing
E_x.storeChunk(data, {0, 0}, {2, 2});
REQUIRE_THROWS(write.flush());
}
if (isAdios1)
{
// run a simplified test for Adios1 since Adios1 has issues opening
// twice in the same process
REQUIRE(auxiliary::file_exists("../samples/close_iterations_1.bp"));
}
else
{
Series read(name, Access::READ_ONLY);
Iteration it1 = read.iterations[1];
auto E_x_read = it1.meshes["E"]["x"];
auto chunk = E_x_read.loadChunk<int>({0, 0}, {2, 2});
it1.close(/* flush = */ true);
for (size_t i = 0; i < data.size(); ++i)
{
REQUIRE(data[i] == chunk.get()[i]);
}
auto read_again = E_x_read.loadChunk<int>({0, 0}, {2, 2});
REQUIRE_THROWS(read.flush());
}
{
Series list{name, Access::READ_ONLY};
helper::listSeries(list);
}
}
TEST_CASE("close_iteration_test", "[serial]")
{
for (auto const &t : testedFileExtensions())
{
close_iteration_test(t);
}
}
void close_iteration_interleaved_test(
std::string const file_ending, IterationEncoding const it_encoding)
{
std::string name = "../samples/close_iterations_interleaved_";
if (it_encoding == IterationEncoding::fileBased)
name.append("f_%T");
else if (it_encoding == IterationEncoding::groupBased)
name.append("g");
else if (it_encoding == IterationEncoding::variableBased)
name.append("v");
name.append(".").append(file_ending);
std::cout << name << std::endl;
std::vector<int> data{2, 4, 6, 8};
{
Series write(name, Access::CREATE);
write.setIterationEncoding(it_encoding);
// interleaved write pattern
Iteration it1 = write.iterations[1];
auto E_x = it1.meshes["E"]["x"];
E_x.resetDataset({Datatype::INT, {2, 2}});
E_x.storeChunk(data, {0, 0}, {1, 2});
E_x.seriesFlush();
Iteration it2 = write.iterations[2];
E_x = it2.meshes["E"]["x"];
E_x.resetDataset({Datatype::INT, {2, 2}});
E_x.storeChunk(data, {0, 0}, {1, 2});
E_x.seriesFlush();
E_x = it1.meshes["E"]["x"];
E_x.storeChunk(data, {1, 0}, {1, 1});
E_x.seriesFlush();
E_x = it2.meshes["E"]["x"];
E_x.storeChunk(data, {1, 0}, {1, 1});
E_x.seriesFlush();
// now we start a third iteration
Iteration it3 = write.iterations[3];
E_x = it3.meshes["E"]["x"];
E_x.resetDataset({Datatype::INT, {2, 2}});
E_x.storeChunk(data, {0, 0}, {1, 2});
E_x.seriesFlush();
// let's finish writing to 1 and 2
E_x = it1.meshes["E"]["x"];
E_x.storeChunk(data, {1, 1}, {1, 1});
E_x.seriesFlush();
it1.close();
E_x = it2.meshes["E"]["x"];
E_x.storeChunk(data, {1, 1}, {1, 1});
E_x.seriesFlush();
it2.close();
E_x = it3.meshes["E"]["x"];
E_x.storeChunk(data, {1, 0}, {1, 2});
E_x.seriesFlush();
it3.close();
}
}
TEST_CASE("close_iteration_interleaved_test", "[serial]")
{
bool const bp_prefer_adios1 =
(auxiliary::getEnvString("OPENPMD_BP_BACKEND", "NOT_SET") == "ADIOS1");
for (auto const &t : testedFileExtensions())
{
close_iteration_interleaved_test(t, IterationEncoding::fileBased);
close_iteration_interleaved_test(t, IterationEncoding::groupBased);
// run this test for ADIOS2 & JSON only
if (t == "h5")
continue;
if (t == "bp" && bp_prefer_adios1)
continue;
close_iteration_interleaved_test(t, IterationEncoding::variableBased);
}
}
void close_and_copy_attributable_test(std::string file_ending)
{
using position_t = int;
// open file for writing
Series series("electrons." + file_ending, Access::CREATE);
Datatype datatype = determineDatatype<position_t>();
constexpr unsigned long length = 10ul;
Extent global_extent = {length};
Dataset dataset = Dataset(datatype, global_extent);
std::shared_ptr<position_t> local_data(
new position_t[length], [](position_t const *ptr) { delete[] ptr; });
std::unique_ptr<Iteration> iteration_ptr;
for (size_t i = 0; i < 100; i += 10)
{
if (iteration_ptr)
{
*iteration_ptr = series.iterations[i];
}
else
{
// use copy constructor
iteration_ptr = std::make_unique<Iteration>(series.iterations[i]);
}
Record electronPositions = iteration_ptr->particles["e"]["position"];
// TODO set this automatically to zero if not provided
Record electronPositionsOffset =
iteration_ptr->particles["e"]["positionOffset"];
std::iota(local_data.get(), local_data.get() + length, i * length);
/*
* Hijack this test to additionally test the unique_ptr storeChunk API
*/
// scalar unique_ptr, default delete
auto pos_x = electronPositions["x"];
pos_x.resetDataset(Dataset{datatype, {1}});
pos_x.storeChunk(std::make_unique<int>(5), {0}, {1});
// array unique_ptr, default delete
auto posOff_x = electronPositionsOffset["x"];
posOff_x.resetDataset(dataset);
posOff_x.storeChunk(
std::unique_ptr<int[]>{new int[10]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}},
{0},
{global_extent});
using CD = auxiliary::CustomDelete<int>;
CD array_deleter{[](int const *ptr) { delete[] ptr; }};
// scalar unique_ptr, custom delete
auto pos_y = electronPositions["y"];
pos_y.resetDataset(dataset);
pos_y.storeChunk(
std::unique_ptr<int, CD>{
new int[10]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, array_deleter},
{0},
{global_extent});
// array unique_ptr, custom delete
auto posOff_y = electronPositionsOffset["y"];
posOff_y.resetDataset(dataset);
posOff_y.storeChunk(
std::unique_ptr<int[], CD>{
new int[10]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, array_deleter},
{0},
{global_extent});
// scalar UniquePtrWithLambda, default delete
auto pos_z = electronPositions["z"];
pos_z.resetDataset(dataset);
pos_z.storeChunk(
UniquePtrWithLambda<int>{
new int[10]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, array_deleter},
{0},
{global_extent});
// array UniquePtrWithLambda, default delete
auto posOff_z = electronPositionsOffset["z"];
posOff_z.resetDataset(dataset);
posOff_z.storeChunk(
UniquePtrWithLambda<int[]>{
new int[10]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, array_deleter},
{0},
{global_extent});
// scalar UniquePtrWithLambda, custom delete
// we're playing 4D now
auto pos_w = electronPositions["w"];
pos_w.resetDataset(dataset);
pos_w.storeChunk(
UniquePtrWithLambda<int>{
new int[10]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, array_deleter},
{0},
{global_extent});
// array UniquePtrWithLambda, custom delete
auto posOff_w = electronPositionsOffset["w"];
posOff_w.resetDataset(dataset);
posOff_w.storeChunk(
UniquePtrWithLambda<int[]>{
new int[10]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, array_deleter},
{0},
{global_extent});
iteration_ptr->close();
// force re-flush of previous iterations
series.flush();
}
}
TEST_CASE("close_and_copy_attributable_test", "[serial]")
{
// demonstrator for https://github.com/openPMD/openPMD-api/issues/765
for (auto const &t : testedFileExtensions())
{
close_and_copy_attributable_test(t);
}
}
#if openPMD_HAVE_ADIOS2
TEST_CASE("close_iteration_throws_test", "[serial]")
{
/*
* Iterations should not be accessed any more after closing.
* Test that the openPMD API detects that case and throws.
*/
{
Series series("../samples/close_iteration_throws_1.bp", Access::CREATE);
auto it0 = series.iterations[0];
auto E_x = it0.meshes["E"]["x"];
E_x.resetDataset({Datatype::INT, {5}});
std::vector<int> data{0, 1, 2, 3, 4};
E_x.storeChunk(data, {0}, {5});
it0.close();
auto B_y = it0.meshes["B"]["y"];
B_y.resetDataset({Datatype::INT, {5}});
B_y.storeChunk(data, {0}, {5});
REQUIRE_THROWS(series.flush());
}
{
Series series("../samples/close_iteration_throws_2.bp", Access::CREATE);
auto it0 = series.iterations[0];
auto E_x = it0.meshes["E"]["x"];
E_x.resetDataset({Datatype::INT, {5}});
std::vector<int> data{0, 1, 2, 3, 4};
E_x.storeChunk(data, {0}, {5});
it0.close();
auto e_position_x = it0.particles["e"]["position"]["x"];
e_position_x.resetDataset({Datatype::INT, {5}});
e_position_x.storeChunk(data, {0}, {5});
REQUIRE_THROWS(series.flush());
}
{
Series series("../samples/close_iteration_throws_3.bp", Access::CREATE);
auto it0 = series.iterations[0];
auto E_x = it0.meshes["E"]["x"];
E_x.resetDataset({Datatype::INT, {5}});
std::vector<int> data{0, 1, 2, 3, 4};
E_x.storeChunk(data, {0}, {5});
it0.close();
it0.setTimeUnitSI(2.0);
REQUIRE_THROWS(series.flush());
}
}
#endif
inline void empty_dataset_test(std::string file_ending)
{
{
Series series(
"../samples/empty_datasets." + file_ending, Access::CREATE);
auto makeEmpty_dim_7_int =
series.iterations[1].meshes["rho"]["makeEmpty_dim_7_int"];
auto makeEmpty_dim_7_long =
series.iterations[1].meshes["rho"]["makeEmpty_dim_7_bool"];
auto makeEmpty_dim_7_int_alt =
series.iterations[1].meshes["rho"]["makeEmpty_dim_7_int_alt"];
auto makeEmpty_dim_7_long_alt =
series.iterations[1].meshes["rho"]["makeEmpty_dim_7_bool_alt"];
auto makeEmpty_resetDataset_dim3 =
series.iterations[1].meshes["rho"]["makeEmpty_resetDataset_dim3"];
auto makeEmpty_resetDataset_dim3_notallzero =
series.iterations[1]
.meshes["rho"]["makeEmpty_resetDataset_dim3_notallzero"];
makeEmpty_dim_7_int.makeEmpty<int>(7);
makeEmpty_dim_7_long.makeEmpty<long>(7);
makeEmpty_dim_7_int_alt.makeEmpty(Datatype::INT, 7);
makeEmpty_dim_7_long_alt.makeEmpty(Datatype::LONG, 7);
makeEmpty_resetDataset_dim3.resetDataset(
Dataset(Datatype::LONG, Extent(3, 0)));
makeEmpty_resetDataset_dim3_notallzero.resetDataset(
Dataset(Datatype::LONG_DOUBLE, Extent{1, 2, 0}));
series.flush();
}
{
Series series(
"../samples/empty_datasets." + file_ending, Access::READ_ONLY);
REQUIRE(series.iterations.contains(1));
REQUIRE(series.iterations.count(1) == 1);
REQUIRE(series.iterations.count(123456) == 0);
REQUIRE(series.iterations[1].meshes.contains("rho"));
REQUIRE(
series.iterations[1].meshes["rho"].contains("makeEmpty_dim_7_int"));
auto makeEmpty_dim_7_int =
series.iterations[1].meshes["rho"]["makeEmpty_dim_7_int"];
auto makeEmpty_dim_7_long =
series.iterations[1].meshes["rho"]["makeEmpty_dim_7_bool"];
auto makeEmpty_dim_7_int_alt =
series.iterations[1].meshes["rho"]["makeEmpty_dim_7_int_alt"];
auto makeEmpty_dim_7_long_alt =
series.iterations[1].meshes["rho"]["makeEmpty_dim_7_bool_alt"];
auto makeEmpty_resetDataset_dim3 =
series.iterations[1].meshes["rho"]["makeEmpty_resetDataset_dim3"];
auto makeEmpty_resetDataset_dim3_notallzero =
series.iterations[1]
.meshes["rho"]["makeEmpty_resetDataset_dim3_notallzero"];
REQUIRE(makeEmpty_dim_7_int.getDimensionality() == 7);
REQUIRE(makeEmpty_dim_7_int.getExtent() == Extent(7, 0));
REQUIRE(isSame(
makeEmpty_dim_7_int.getDatatype(), determineDatatype<int>()));
REQUIRE(makeEmpty_dim_7_long.getDimensionality() == 7);
REQUIRE(makeEmpty_dim_7_long.getExtent() == Extent(7, 0));
REQUIRE(isSame(
makeEmpty_dim_7_long.getDatatype(), determineDatatype<long>()));
REQUIRE(makeEmpty_dim_7_int_alt.getDimensionality() == 7);
REQUIRE(makeEmpty_dim_7_int_alt.getExtent() == Extent(7, 0));
REQUIRE(isSame(
makeEmpty_dim_7_int_alt.getDatatype(), determineDatatype<int>()));
REQUIRE(makeEmpty_dim_7_long_alt.getDimensionality() == 7);
REQUIRE(makeEmpty_dim_7_long_alt.getExtent() == Extent(7, 0));
REQUIRE(isSame(
makeEmpty_dim_7_long_alt.getDatatype(), determineDatatype<long>()));
REQUIRE(makeEmpty_resetDataset_dim3.getDimensionality() == 3);
REQUIRE(makeEmpty_resetDataset_dim3.getExtent() == Extent(3, 0));
REQUIRE(
isSame(makeEmpty_resetDataset_dim3.getDatatype(), Datatype::LONG));
REQUIRE(
makeEmpty_resetDataset_dim3_notallzero.getDimensionality() == 3);
REQUIRE(
makeEmpty_resetDataset_dim3_notallzero.getExtent() ==
Extent{1, 2, 0});
REQUIRE(isSame(
makeEmpty_resetDataset_dim3_notallzero.getDatatype(),
Datatype::LONG_DOUBLE));
}
{
Series list{
"../samples/empty_datasets." + file_ending, Access::READ_ONLY};
helper::listSeries(list);
}
}
TEST_CASE("empty_dataset_test", "[serial]")
{
for (auto const &t : testedFileExtensions())
{
empty_dataset_test(t);
}
}
inline void constant_scalar(std::string file_ending)
{
Mesh::Geometry const geometry = Mesh::Geometry::spherical;
std::string const geometryParameters = "dummyGeometryParameters";
Mesh::DataOrder const dataOrder = Mesh::DataOrder::F;
std::vector<double> const gridSpacing{1.0, 2.0, 3.0};
std::vector<double> const gridGlobalOffset{11.0, 22.0, 33.0};
double const gridUnitSI = 3.14;
std::vector<std::string> const axisLabels{"x", "y", "z"};
std::map<UnitDimension, double> const unitDimensions{
{UnitDimension::I, 1.0}, {UnitDimension::J, 2.0}};
double const timeOffset = 1234.0;
{
// constant scalar
Series s =
Series("../samples/constant_scalar." + file_ending, Access::CREATE);
auto rho = s.iterations[1].meshes["rho"][MeshRecordComponent::SCALAR];
REQUIRE(s.iterations[1].meshes["rho"].scalar());
rho.resetDataset(Dataset(Datatype::CHAR, {1, 2, 3}));
rho.makeConstant(static_cast<char>('a'));
REQUIRE(rho.constant());
// mixed constant/non-constant
auto E_x = s.iterations[1].meshes["E"]["x"];
E_x.resetDataset(Dataset(Datatype::FLOAT, {1, 2, 3}));
E_x.makeConstant(static_cast<float>(13.37));
auto E_y = s.iterations[1].meshes["E"]["y"];
E_y.resetDataset(Dataset(Datatype::UINT, {1, 2, 3}));
UniquePtrWithLambda<unsigned int> E(
new unsigned int[6], [](unsigned int const *p) { delete[] p; });
unsigned int e{0};
std::generate(E.get(), E.get() + 6, [&e] { return e++; });
E_y.storeChunk(std::move(E), {0, 0, 0}, {1, 2, 3});
// store a number of predefined attributes in E
Mesh &E_mesh = s.iterations[1].meshes["E"];
E_mesh.setGeometry(geometry);
E_mesh.setGeometryParameters(geometryParameters);
E_mesh.setDataOrder(dataOrder);
E_mesh.setGridSpacing(gridSpacing);
E_mesh.setGridGlobalOffset(gridGlobalOffset);
E_mesh.setGridUnitSI(gridUnitSI);
E_mesh.setAxisLabels(axisLabels);
E_mesh.setUnitDimension(unitDimensions);
E_mesh.setTimeOffset(timeOffset);
// constant scalar
auto pos =
s.iterations[1].particles["e"]["position"][RecordComponent::SCALAR];
pos.resetDataset(Dataset(Datatype::DOUBLE, {3, 2, 1}));
pos.makeConstant(static_cast<double>(42.));
auto posOff =
s.iterations[1]
.particles["e"]["positionOffset"][RecordComponent::SCALAR];
posOff.resetDataset(Dataset(Datatype::INT, {3, 2, 1}));
posOff.makeConstant(static_cast<int>(-42));
// mixed constant/non-constant
auto vel_x = s.iterations[1].particles["e"]["velocity"]["x"];
vel_x.resetDataset(Dataset(Datatype::SHORT, {3, 2, 1}));
vel_x.makeConstant(static_cast<short>(-1));
auto vel_y = s.iterations[1].particles["e"]["velocity"]["y"];
vel_y.resetDataset(Dataset(Datatype::ULONGLONG, {3, 2, 1}));