forked from openPMD/openPMD-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommonADIOS1IOHandler.cpp
More file actions
1794 lines (1663 loc) · 65.6 KB
/
CommonADIOS1IOHandler.cpp
File metadata and controls
1794 lines (1663 loc) · 65.6 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 2017-2021 Fabian Koller, Axel Huebl
*
* 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 "openPMD/IO/ADIOS/CommonADIOS1IOHandler.hpp"
#if openPMD_HAVE_ADIOS1
#include "openPMD/auxiliary/JSON_internal.hpp"
#include "openPMD/Error.hpp"
#include "openPMD/IO/ADIOS/ADIOS1IOHandlerImpl.hpp"
#include "openPMD/IO/ADIOS/ParallelADIOS1IOHandlerImpl.hpp"
#include <adios.h>
#include <algorithm>
#include <complex>
#include <cstring>
#include <iostream>
#include <map>
#include <memory>
#include <string>
#include <string>
#include <tuple>
namespace openPMD
{
#if openPMD_USE_VERIFY
# define VERIFY(CONDITION, TEXT) { if(!(CONDITION)) throw std::runtime_error((TEXT)); }
#else
# define VERIFY(CONDITION, TEXT) do{ (void)sizeof(CONDITION); } while( 0 )
#endif
template< typename ChildClass >
void
CommonADIOS1IOHandlerImpl< ChildClass >::close(int64_t fd)
{
int status;
status = adios_close(fd);
VERIFY(status == err_no_error, "[ADIOS1] Internal error: Failed to close ADIOS file (open_write)");
}
template< typename ChildClass >
void
CommonADIOS1IOHandlerImpl< ChildClass >::close(ADIOS_FILE* f)
{
int status;
status = adios_read_close(f);
VERIFY(status == err_no_error, "[ADIOS1] Internal error: Failed to close ADIOS file (open_read)");
}
template< typename ChildClass >
void
CommonADIOS1IOHandlerImpl< ChildClass >::flush_attribute(int64_t group, std::string const& name, Attribute const& att)
{
auto dtype = att.dtype;
// https://github.com/ComputationalRadiationPhysics/picongpu/pull/1756
if( dtype == Datatype::BOOL )
dtype = Datatype::UCHAR;
int nelems = 0;
switch( dtype )
{
using DT = Datatype;
case DT::VEC_CHAR:
nelems = att.get< std::vector< char > >().size();
break;
case DT::VEC_SHORT:
nelems = att.get< std::vector< short > >().size();
break;
case DT::VEC_INT:
nelems = att.get< std::vector< int > >().size();
break;
case DT::VEC_LONG:
nelems = att.get< std::vector< long > >().size();
break;
case DT::VEC_LONGLONG:
nelems = att.get< std::vector< long long > >().size();
break;
case DT::VEC_UCHAR:
nelems = att.get< std::vector< unsigned char > >().size();
break;
case DT::VEC_USHORT:
nelems = att.get< std::vector< unsigned short > >().size();
break;
case DT::VEC_UINT:
nelems = att.get< std::vector< unsigned int > >().size();
break;
case DT::VEC_ULONG:
nelems = att.get< std::vector< unsigned long > >().size();
break;
case DT::VEC_ULONGLONG:
nelems = att.get< std::vector< unsigned long long > >().size();
break;
case DT::VEC_FLOAT:
nelems = att.get< std::vector< float > >().size();
break;
case DT::VEC_DOUBLE:
nelems = att.get< std::vector< double > >().size();
break;
case DT::VEC_LONG_DOUBLE:
nelems = att.get< std::vector< long double > >().size();
break;
case DT::VEC_STRING:
nelems = att.get< std::vector< std::string > >().size();
break;
case DT::ARR_DBL_7:
nelems = 7;
break;
case DT::UNDEFINED:
throw std::runtime_error("[ADIOS1] Unknown Attribute datatype (ADIOS1 Attribute flush)");
default:
nelems = 1;
}
auto values = auxiliary::allocatePtr(dtype, nelems);
switch( att.dtype )
{
using DT = Datatype;
case DT::CHAR:
{
auto ptr = reinterpret_cast< char* >(values.get());
*ptr = att.get< char >();
break;
}
case DT::UCHAR:
{
auto ptr = reinterpret_cast< unsigned char* >(values.get());
*ptr = att.get< unsigned char >();
break;
}
case DT::SHORT:
{
auto ptr = reinterpret_cast< short* >(values.get());
*ptr = att.get< short >();
break;
}
case DT::INT:
{
auto ptr = reinterpret_cast< int* >(values.get());
*ptr = att.get< int >();
break;
}
case DT::LONG:
{
auto ptr = reinterpret_cast< long* >(values.get());
*ptr = att.get< long >();
break;
}
case DT::LONGLONG:
{
auto ptr = reinterpret_cast< long long* >(values.get());
*ptr = att.get< long long >();
break;
}
case DT::USHORT:
{
auto ptr = reinterpret_cast< unsigned short* >(values.get());
*ptr = att.get< unsigned short >();
break;
}
case DT::UINT:
{
auto ptr = reinterpret_cast< unsigned int* >(values.get());
*ptr = att.get< unsigned int >();
break;
}
case DT::ULONG:
{
auto ptr = reinterpret_cast< unsigned long* >(values.get());
*ptr = att.get< unsigned long >();
break;
}
case DT::ULONGLONG:
{
auto ptr = reinterpret_cast< unsigned long long* >(values.get());
*ptr = att.get< unsigned long long >();
break;
}
case DT::FLOAT:
{
auto ptr = reinterpret_cast< float* >(values.get());
*ptr = att.get< float >();
break;
}
case DT::DOUBLE:
{
auto ptr = reinterpret_cast< double* >(values.get());
*ptr = att.get< double >();
break;
}
case DT::LONG_DOUBLE:
{
auto ptr = reinterpret_cast< long double* >(values.get());
*ptr = att.get< long double >();
break;
}
case DT::CFLOAT:
{
auto ptr = reinterpret_cast< std::complex< float >* >(values.get());
*ptr = att.get< std::complex< float > >();
break;
}
case DT::CDOUBLE:
{
auto ptr = reinterpret_cast< std::complex< double >* >(values.get());
*ptr = att.get< std::complex< double > >();
break;
}
case DT::CLONG_DOUBLE:
{
throw std::runtime_error("[ADIOS1] Unknown Attribute datatype (CLONG_DOUBLE)");
break;
}
case DT::STRING:
{
auto const & v = att.get< std::string >();
values = auxiliary::allocatePtr(Datatype::CHAR, v.length() + 1u);
strcpy((char*)values.get(), v.c_str());
break;
}
case DT::VEC_CHAR:
{
auto ptr = reinterpret_cast< char* >(values.get());
auto const& vec = att.get< std::vector< char > >();
for( size_t i = 0; i < vec.size(); ++i )
ptr[i] = vec[i];
break;
}
case DT::VEC_SHORT:
{
auto ptr = reinterpret_cast< short* >(values.get());
auto const& vec = att.get< std::vector< short > >();
for( size_t i = 0; i < vec.size(); ++i )
ptr[i] = vec[i];
break;
}
case DT::VEC_INT:
{
auto ptr = reinterpret_cast< int* >(values.get());
auto const& vec = att.get< std::vector< int > >();
for( size_t i = 0; i < vec.size(); ++i )
ptr[i] = vec[i];
break;
}
case DT::VEC_LONG:
{
auto ptr = reinterpret_cast< long* >(values.get());
auto const& vec = att.get< std::vector< long > >();
for( size_t i = 0; i < vec.size(); ++i )
ptr[i] = vec[i];
break;
}
case DT::VEC_LONGLONG:
{
auto ptr = reinterpret_cast< long long* >(values.get());
auto const& vec = att.get< std::vector< long long > >();
for( size_t i = 0; i < vec.size(); ++i )
ptr[i] = vec[i];
break;
}
case DT::VEC_UCHAR:
{
auto ptr = reinterpret_cast< unsigned char* >(values.get());
auto const& vec = att.get< std::vector< unsigned char > >();
for( size_t i = 0; i < vec.size(); ++i )
ptr[i] = vec[i];
break;
}
case DT::VEC_USHORT:
{
auto ptr = reinterpret_cast< unsigned short* >(values.get());
auto const& vec = att.get< std::vector< unsigned short > >();
for( size_t i = 0; i < vec.size(); ++i )
ptr[i] = vec[i];
break;
}
case DT::VEC_UINT:
{
auto ptr = reinterpret_cast< unsigned int* >(values.get());
auto const& vec = att.get< std::vector< unsigned int > >();
for( size_t i = 0; i < vec.size(); ++i )
ptr[i] = vec[i];
break;
}
case DT::VEC_ULONG:
{
auto ptr = reinterpret_cast< unsigned long* >(values.get());
auto const& vec = att.get< std::vector< unsigned long > >();
for( size_t i = 0; i < vec.size(); ++i )
ptr[i] = vec[i];
break;
}
case DT::VEC_ULONGLONG:
{
auto ptr = reinterpret_cast< unsigned long long* >(values.get());
auto const& vec = att.get< std::vector< unsigned long long > >();
for( size_t i = 0; i < vec.size(); ++i )
ptr[i] = vec[i];
break;
}
case DT::VEC_FLOAT:
{
auto ptr = reinterpret_cast< float* >(values.get());
auto const& vec = att.get< std::vector< float > >();
for( size_t i = 0; i < vec.size(); ++i )
ptr[i] = vec[i];
break;
}
case DT::VEC_DOUBLE:
{
auto ptr = reinterpret_cast< double* >(values.get());
auto const& vec = att.get< std::vector< double > >();
for( size_t i = 0; i < vec.size(); ++i )
ptr[i] = vec[i];
break;
}
case DT::VEC_LONG_DOUBLE:
{
auto ptr = reinterpret_cast< long double* >(values.get());
auto const& vec = att.get< std::vector< long double > >();
for( size_t i = 0; i < vec.size(); ++i )
ptr[i] = vec[i];
break;
}
/* not supported by ADIOS 1.13.1:
* https://github.com/ornladios/ADIOS/issues/212
*/
case DT::VEC_CFLOAT:
case DT::VEC_CDOUBLE:
case DT::VEC_CLONG_DOUBLE:
{
throw std::runtime_error("[ADIOS1] Arrays of complex attributes are not supported");
break;
}
case DT::VEC_STRING:
{
auto ptr = reinterpret_cast< char** >(values.get());
auto const& vec = att.get< std::vector< std::string > >();
for( size_t i = 0; i < vec.size(); ++i )
{
size_t size = vec[i].size() + 1;
ptr[i] = new char[size];
strncpy(ptr[i], vec[i].c_str(), size);
}
break;
}
case DT::ARR_DBL_7:
{
auto ptr = reinterpret_cast< double* >(values.get());
auto const& arr = att.get< std::array< double, 7> >();
for( size_t i = 0; i < 7; ++i )
ptr[i] = arr[i];
break;
}
case DT::BOOL:
{
auto ptr = reinterpret_cast< unsigned char* >(values.get());
*ptr = static_cast< unsigned char >(att.get< bool >());
break;
}
case DT::UNDEFINED:
throw std::runtime_error("[ADIOS1] Unknown Attribute datatype (ADIOS1 Attribute flush)");
default:
throw std::runtime_error("[ADIOS1] Datatype not implemented in ADIOS IO");
}
int status;
status = adios_define_attribute_byvalue(group,
name.c_str(),
"",
getBP1DataType(att.dtype),
nelems,
values.get());
VERIFY(status == err_no_error, "[ADIOS1] Internal error: Failed to define ADIOS attribute by value");
if( att.dtype == Datatype::VEC_STRING )
{
auto ptr = reinterpret_cast< char** >(values.get());
for( int i = 0; i < nelems; ++i )
delete[] ptr[i];
}
}
template< typename ChildClass >
void
CommonADIOS1IOHandlerImpl< ChildClass >::createFile(Writable* writable,
Parameter< Operation::CREATE_FILE > const& parameters)
{
if( m_handler->m_backendAccess == Access::READ_ONLY )
throw std::runtime_error("[ADIOS1] Creating a file in read-only mode is not possible.");
if( !writable->written )
{
if( !auxiliary::directory_exists(m_handler->directory) )
{
bool success = auxiliary::create_directories(m_handler->directory);
VERIFY(success, "[ADIOS1] Internal error: Failed to create directories during ADIOS file creation");
}
std::string name = m_handler->directory + parameters.name;
if( !auxiliary::ends_with(name, ".bp") )
name += ".bp";
writable->written = true;
writable->abstractFilePosition = std::make_shared< ADIOS1FilePosition >("/");
m_filePaths[writable] = std::make_shared< std::string >(name);
/* our control flow allows for more than one open file handle
* if multiple files are opened with the same group, data might be lost */
/* defer actually opening the file handle until the first Operation::WRITE_DATASET occurs */
m_existsOnDisk[m_filePaths[writable]] = false;
GetFileHandle(writable);
}
}
template< typename ChildClass >
void
CommonADIOS1IOHandlerImpl< ChildClass >::createPath(Writable* writable,
Parameter< Operation::CREATE_PATH > const& parameters)
{
if( m_handler->m_backendAccess == Access::READ_ONLY )
throw std::runtime_error("[ADIOS1] Creating a path in a file opened as read only is not possible.");
if( !writable->written )
{
/* Sanitize path */
std::string path = parameters.path;
if( auxiliary::starts_with(path, '/') )
path = auxiliary::replace_first(path, "/", "");
if( !auxiliary::ends_with(path, '/') )
path += '/';
/* ADIOS has no concept for explicitly creating paths.
* They are implicitly created with the paths of variables/attributes. */
writable->written = true;
writable->abstractFilePosition = std::make_shared< ADIOS1FilePosition >(path);
Writable* position;
if( writable->parent )
position = writable->parent;
else
position = writable; /* root does not have a parent but might still have to be written */
auto res = m_filePaths.find(position);
m_filePaths[writable] = res->second;
}
}
static auxiliary::Option< std::string > datasetTransform(
json::TracingJSON config )
{
using ret_t = auxiliary::Option< std::string >;
if( !config.json().contains( "dataset" ) )
{
return ret_t{};
}
config = config[ "dataset" ];
if( !config.json().contains( "transform" ) )
{
return ret_t{};
}
config = config[ "transform" ];
auto maybeRes = json::asStringDynamic( config.json() );
if( maybeRes.has_value() )
{
return std::move( maybeRes.get() );
}
else
{
throw error::BackendConfigSchema(
{ "adios1", "dataset", "transform" },
"Key must convertible to type string." );
}
}
template< typename ChildClass >
void
CommonADIOS1IOHandlerImpl< ChildClass >::createDataset(Writable* writable,
Parameter< Operation::CREATE_DATASET > const& parameters)
{
if( m_handler->m_backendAccess == Access::READ_ONLY )
throw std::runtime_error("[ADIOS1] Creating a dataset in a file opened as read only is not possible.");
if( !writable->written )
{
/* ADIOS variable definitions require the file to be (re-)opened to take effect/not cause errors */
auto res = m_filePaths.find(writable);
if( res == m_filePaths.end() )
res = m_filePaths.find(writable->parent);
int64_t group = m_groups[res->second];
/* Sanitize name */
std::string name = parameters.name;
if( auxiliary::starts_with(name, '/') )
name = auxiliary::replace_first(name, "/", "");
if( auxiliary::ends_with(name, '/') )
name = auxiliary::replace_last(name, "/", "");
std::string path = concrete_bp1_file_position(writable) + name;
size_t ndims = parameters.extent.size();
std::vector< std::string > chunkSize(ndims, "");
std::vector< std::string > chunkOffset(ndims, "");
int64_t id;
for( size_t i = 0; i < ndims; ++i )
{
chunkSize[i] = "/tmp" + path + "_chunkSize" + std::to_string(i);
id = adios_define_var(group, chunkSize[i].c_str(), "", adios_unsigned_long, "", "", "");
VERIFY(id != 0, "[ADIOS1] Internal error: Failed to define ADIOS variable during Dataset creation");
chunkOffset[i] = "/tmp" + path + "_chunkOffset" + std::to_string(i);
id = adios_define_var(group, chunkOffset[i].c_str(), "", adios_unsigned_long, "", "", "");
VERIFY(id != 0, "[ADIOS1] Internal error: Failed to define ADIOS variable during Dataset creation");
}
std::string chunkSizeParam = auxiliary::join(chunkSize, ",");
std::string globalSize = getBP1Extent(parameters.extent);
std::string chunkOffsetParam = auxiliary::join(chunkOffset, ",");
id = adios_define_var(group,
path.c_str(),
"",
getBP1DataType(parameters.dtype),
chunkSizeParam.c_str(),
globalSize.c_str(),
chunkOffsetParam.c_str());
VERIFY(id != 0, "[ADIOS1] Internal error: Failed to define ADIOS variable during Dataset creation");
std::string transform = "";
{
json::TracingJSON options = json::parseOptions(
parameters.options, /* considerFiles = */ false );
if( options.json().contains( "adios1" ) )
{
options = options["adios1"];
auto maybeTransform = datasetTransform( options );
if( maybeTransform.has_value() )
{
transform = maybeTransform.get();
}
parameters.warnUnusedParameters(
options,
"ADIOS1",
"Warning: parts of the backend configuration for "
"ADIOS1 dataset '" + name + "' remain unused:\n" );
}
}
// Fallback: global option
if( transform.empty() )
{
transform = m_defaultTransform;
}
if( !transform.empty() )
{
int status;
status = adios_set_transform(id, transform.c_str());
VERIFY(status == err_no_error, "[ADIOS1] Internal error: Failed to set ADIOS transform during Dataset creation");
}
writable->written = true;
writable->abstractFilePosition = std::make_shared< ADIOS1FilePosition >(name);
m_filePaths[writable] = res->second;
}
}
template< typename ChildClass >
void
CommonADIOS1IOHandlerImpl< ChildClass >::extendDataset(Writable*,
Parameter< Operation::EXTEND_DATASET > const&)
{
throw std::runtime_error("[ADIOS1] Dataset extension not implemented in ADIOS backend");
}
template< typename ChildClass >
void
CommonADIOS1IOHandlerImpl< ChildClass >::openFile(Writable* writable,
Parameter< Operation::OPEN_FILE > const& parameters)
{
if( !auxiliary::directory_exists(m_handler->directory) )
throw no_such_file_error("[ADIOS1] Supplied directory is not valid: " + m_handler->directory);
std::string name = m_handler->directory + parameters.name;
if( !auxiliary::ends_with(name, ".bp") )
name += ".bp";
std::shared_ptr< std::string > filePath;
auto it = std::find_if(m_filePaths.begin(), m_filePaths.end(),
[name](std::unordered_map< Writable*, std::shared_ptr< std::string > >::value_type const& entry){ return *entry.second == name; });
if( it == m_filePaths.end() )
filePath = std::make_shared< std::string >(name);
else
filePath = it->second;
if( m_handler->m_backendAccess == Access::CREATE )
{
// called at Series::flush for iterations that has been flushed before
// this is to make sure to point the Series.m_writer points to this iteration
// so when call Series.flushAttribute(), the attributes can be flushed to the iteration level file.
m_filePaths[writable] = filePath;
writable->written = true;
writable->abstractFilePosition = std::make_shared< ADIOS1FilePosition >("/");
return;
}
/* close the handle that corresponds to the file we want to open */
if( m_openWriteFileHandles.find(filePath) != m_openWriteFileHandles.end() )
{
close(m_openWriteFileHandles[filePath]);
m_openWriteFileHandles.erase(filePath);
}
if( m_groups.find(filePath) == m_groups.end() )
m_groups[filePath] =
static_cast< ChildClass * >( this )->initialize_group(name);
if( m_openReadFileHandles.find(filePath) == m_openReadFileHandles.end() )
{
ADIOS_FILE* f = static_cast< ChildClass * >( this )->open_read(name);
m_openReadFileHandles[filePath] = f;
}
writable->written = true;
writable->abstractFilePosition = std::make_shared< ADIOS1FilePosition >("/");
m_filePaths[writable] = filePath;
m_existsOnDisk[filePath] = true;
}
template< typename ChildClass >
void
CommonADIOS1IOHandlerImpl< ChildClass >::closeFile(
Writable * writable,
Parameter< Operation::CLOSE_FILE > const & )
{
auto myFile = m_filePaths.find( writable );
if( myFile == m_filePaths.end() )
{
return;
}
// finish write operations
auto myGroup = m_groups.find( myFile->second );
if( myGroup != m_groups.end() )
{
auto attributeWrites = m_attributeWrites.find( myGroup->second );
if( this->m_handler->m_backendAccess != Access::READ_ONLY &&
attributeWrites != m_attributeWrites.end() )
{
for( auto & att : attributeWrites->second )
{
flush_attribute( myGroup->second, att.first, att.second );
}
m_attributeWrites.erase( attributeWrites );
}
m_groups.erase( myGroup );
}
auto handle_write = m_openWriteFileHandles.find( myFile->second );
if( handle_write != m_openWriteFileHandles.end() )
{
close( handle_write->second );
m_openWriteFileHandles.erase( handle_write );
}
// finish read operations
auto handle_read = m_openReadFileHandles.find( myFile->second );
if( handle_read != m_openReadFileHandles.end() )
{
auto scheduled = m_scheduledReads.find( handle_read->second );
if( scheduled != m_scheduledReads.end() )
{
auto status = adios_perform_reads( scheduled->first, 1 );
VERIFY(
status == err_no_error,
"[ADIOS1] Internal error: Failed to perform ADIOS reads during "
"dataset reading" );
for( auto & sel : scheduled->second )
adios_selection_delete( sel );
m_scheduledReads.erase( scheduled );
}
close( handle_read->second );
m_openReadFileHandles.erase( handle_read );
}
m_existsOnDisk.erase( myFile->second );
m_filePaths.erase( myFile );
}
template< typename ChildClass >
void
CommonADIOS1IOHandlerImpl< ChildClass >::availableChunks(
Writable * writable,
Parameter< Operation::AVAILABLE_CHUNKS > & params )
{
ADIOS_FILE * f;
f = m_openReadFileHandles.at( m_filePaths.at( writable ) );
std::string name = concrete_bp1_file_position( writable );
VERIFY(
std::strcmp( f->path, m_filePaths.at( writable )->c_str() ) == 0,
"[ADIOS1] Internal Error: Invalid ADIOS read file handle" );
ADIOS_VARINFO * varinfo = adios_inq_var( f, name.c_str() );
VERIFY(
adios_errno == err_no_error,
"[ADIOS1] Internal error: Failed to inquire ADIOS variable while "
"querying available chunks." );
int err = adios_inq_var_blockinfo( f, varinfo );
VERIFY(
err == 0,
"[ADIOS1] Internal error: Failed to obtain ADIOS varinfo while "
"querying available chunks." );
int nblocks =
varinfo->nblocks[ 0 ]; // we don't use steps, so index 0 is fine
int ndim = varinfo->ndim;
auto & table = *params.chunks;
table.reserve( nblocks );
for( int block = 0; block < nblocks; ++block )
{
ADIOS_VARBLOCK & varblock = varinfo->blockinfo[ block ];
Offset offset( ndim );
Extent extent( ndim );
for( int i = 0; i < ndim; ++i )
{
offset[ i ] = varblock.start[ i ];
extent[ i ] = varblock.count[ i ];
}
table.emplace_back( offset, extent, int( varblock.process_id ) );
}
adios_free_varinfo( varinfo );
}
template< typename ChildClass >
void
CommonADIOS1IOHandlerImpl< ChildClass >::openPath(
Writable * writable,
Parameter< Operation::OPEN_PATH > const & parameters )
{
/* Sanitize path */
std::string path = parameters.path;
if( !path.empty() )
{
if( auxiliary::starts_with(path, '/') )
path = auxiliary::replace_first(path, "/", "");
if( !auxiliary::ends_with(path, '/') )
path += '/';
}
writable->written = true;
writable->abstractFilePosition = std::make_shared< ADIOS1FilePosition >(path);
auto res = writable->parent ? m_filePaths.find(writable->parent) : m_filePaths.find(writable);
m_filePaths[writable] = res->second;
}
template< typename ChildClass >
void
CommonADIOS1IOHandlerImpl< ChildClass >::openDataset(Writable* writable,
Parameter< Operation::OPEN_DATASET >& parameters)
{
ADIOS_FILE *f;
auto res = m_filePaths.find(writable);
if( res == m_filePaths.end() )
res = m_filePaths.find(writable->parent);
f = m_openReadFileHandles.at(res->second);
/* Sanitize name */
std::string name = parameters.name;
if( auxiliary::starts_with(name, '/') )
name = auxiliary::replace_first(name, "/", "");
std::string datasetname = writable->abstractFilePosition
? concrete_bp1_file_position(writable)
: concrete_bp1_file_position(writable) + name;
ADIOS_VARINFO* vi;
vi = adios_inq_var(f,
datasetname.c_str());
std::string error_string("[ADIOS1] Internal error: ");
error_string.append("Failed to inquire about ADIOS variable '")
.append(datasetname)
.append("' during dataset opening");
VERIFY(adios_errno == err_no_error, error_string);
VERIFY(vi != nullptr, error_string);
Datatype dtype;
// note the ill-named fixed-byte adios_... types
// https://github.com/ornladios/ADIOS/issues/187
switch( vi->type )
{
using DT = Datatype;
case adios_byte:
dtype = DT::CHAR;
break;
case adios_short:
if( sizeof(short) == 2u )
dtype = DT::SHORT;
else if( sizeof(int) == 2u )
dtype = DT::INT;
else if( sizeof(long) == 2u )
dtype = DT::LONG;
else if( sizeof(long long) == 2u )
dtype = DT::LONGLONG;
else
throw unsupported_data_error("[ADIOS1] No native equivalent for Datatype adios_short found.");
break;
case adios_integer:
if( sizeof(short) == 4u )
dtype = DT::SHORT;
else if( sizeof(int) == 4u )
dtype = DT::INT;
else if( sizeof(long) == 4u )
dtype = DT::LONG;
else if( sizeof(long long) == 4u )
dtype = DT::LONGLONG;
else
throw unsupported_data_error("[ADIOS1] No native equivalent for Datatype adios_integer found.");
break;
case adios_long:
if( sizeof(short) == 8u )
dtype = DT::SHORT;
else if( sizeof(int) == 8u )
dtype = DT::INT;
else if( sizeof(long) == 8u )
dtype = DT::LONG;
else if( sizeof(long long) == 8u )
dtype = DT::LONGLONG;
else
throw unsupported_data_error("[ADIOS1] No native equivalent for Datatype adios_long found.");
break;
case adios_unsigned_byte:
dtype = DT::UCHAR;
break;
case adios_unsigned_short:
if( sizeof(unsigned short) == 2u )
dtype = DT::USHORT;
else if( sizeof(unsigned int) == 2u )
dtype = DT::UINT;
else if( sizeof(unsigned long) == 2u )
dtype = DT::ULONG;
else if( sizeof(unsigned long long) == 2u )
dtype = DT::ULONGLONG;
else
throw unsupported_data_error("[ADIOS1] No native equivalent for Datatype adios_unsigned_short found.");
break;
case adios_unsigned_integer:
if( sizeof(unsigned short) == 4u )
dtype = DT::USHORT;
else if( sizeof(unsigned int) == 4u )
dtype = DT::UINT;
else if( sizeof(unsigned long) == 4u )
dtype = DT::ULONG;
else if( sizeof(unsigned long long) == 4u )
dtype = DT::ULONGLONG;
else
throw unsupported_data_error("[ADIOS1] No native equivalent for Datatype adios_unsigned_integer found.");
break;
case adios_unsigned_long:
if( sizeof(unsigned short) == 8u )
dtype = DT::USHORT;
else if( sizeof(unsigned int) == 8u )
dtype = DT::UINT;
else if( sizeof(unsigned long) == 8u )
dtype = DT::ULONG;
else if( sizeof(unsigned long long) == 8u )
dtype = DT::ULONGLONG;
else
throw unsupported_data_error("[ADIOS1] No native equivalent for Datatype adios_unsigned_long found.");
break;
case adios_real:
dtype = DT::FLOAT;
break;
case adios_double:
dtype = DT::DOUBLE;
break;
case adios_long_double:
dtype = DT::LONG_DOUBLE;
break;
case adios_complex:
dtype = DT::CFLOAT;
break;
case adios_double_complex:
dtype = DT::CDOUBLE;
break;
case adios_string:
case adios_string_array:
default:
throw unsupported_data_error("[ADIOS1] Datatype not implemented for ADIOS dataset writing");
}
*parameters.dtype = dtype;
Extent e;
e.resize(vi->ndim);
for( int i = 0; i < vi->ndim; ++i )
e[i] = vi->dims[i];
*parameters.extent = e;
writable->written = true;
if( !writable->abstractFilePosition )
{
writable->abstractFilePosition
= std::make_shared< ADIOS1FilePosition >(name);
}
m_openReadFileHandles[res->second] = f;
m_filePaths[writable] = res->second;
}
template< typename ChildClass >
void
CommonADIOS1IOHandlerImpl< ChildClass >::deleteFile(Writable* writable,
Parameter< Operation::DELETE_FILE > const& parameters)
{
if( m_handler->m_backendAccess == Access::READ_ONLY )
throw std::runtime_error("[ADIOS1] Deleting a file opened as read only is not possible.");
if( writable->written )
{
auto path = m_filePaths.at(writable);
if( m_openReadFileHandles.find(path) != m_openReadFileHandles.end() )
{
close(m_openReadFileHandles.at(path));
m_openReadFileHandles.erase(path);
}
if( m_openWriteFileHandles.find(path) != m_openWriteFileHandles.end() )
{
close(m_openWriteFileHandles.at(path));
m_openWriteFileHandles.erase(path);
}
std::string name = m_handler->directory + parameters.name;
if( !auxiliary::ends_with(name, ".bp") )
name += ".bp";
if( !auxiliary::file_exists(name) )
throw std::runtime_error("[ADIOS1] File does not exist: " + name);
auxiliary::remove_file(name);
writable->written = false;
writable->abstractFilePosition.reset();
m_filePaths.erase(writable);
}
}
template< typename ChildClass >
void
CommonADIOS1IOHandlerImpl< ChildClass >::deletePath(Writable*,
Parameter< Operation::DELETE_PATH > const&)
{
throw std::runtime_error("[ADIOS1] Path deletion not implemented in ADIOS backend");
}
template< typename ChildClass >
void
CommonADIOS1IOHandlerImpl< ChildClass >::deleteDataset(Writable*,
Parameter< Operation::DELETE_DATASET > const&)
{
throw std::runtime_error("[ADIOS1] Dataset deletion not implemented in ADIOS backend");
}
template< typename ChildClass >
void
CommonADIOS1IOHandlerImpl< ChildClass >::deleteAttribute(Writable*,
Parameter< Operation::DELETE_ATT > const&)
{
throw std::runtime_error("[ADIOS1] Attribute deletion not implemented in ADIOS backend");
}
template< typename ChildClass >
int64_t CommonADIOS1IOHandlerImpl< ChildClass >::GetFileHandle(Writable* writable)
{
auto res = m_filePaths.find(writable);
if( res == m_filePaths.end() )