63#ifdef H5_HAVE_PARALLEL
90extern "C" ARCANE_HDF5_EXPORT herr_t
91_ArcaneHdf5UtilsGroupIterateMe(hid_t g,
const char* mn,
void* ptr)
94 HGroupSearch* rw =
reinterpret_cast<HGroupSearch*
>(ptr);
95 return rw->iterateMe(mn);
102splitString(
const String& str,Array<String>& str_array,
char c)
104 const char* str_str = str.localstr();
108 if (str_str[i]==c && i!=offset){
109 str_array.add(std::string_view(str_str+offset,i-offset));
114 str_array.add(std::string_view(str_str+offset,len-offset));
121openTruncate(
const String& var)
124 _setId(H5Fcreate(var.localstr(),H5F_ACC_TRUNC,H5P_DEFAULT,H5P_DEFAULT));
126 ARCANE_THROW(ReaderWriterException,
"Can not open file '{0}'",var);
130openAppend(
const String& var)
133 _setId(H5Fopen(var.localstr(),H5F_ACC_RDWR,H5P_DEFAULT));
135 ARCANE_THROW(ReaderWriterException,
"Can not open file '{0}'",var);
139openRead(
const String& var)
142 _setId(H5Fopen(var.localstr(),H5F_ACC_RDONLY,H5P_DEFAULT));
144 ARCANE_THROW(ReaderWriterException,
"Can not open file '{0}'",var);
148openTruncate(
const String& var,hid_t plist_id)
151 _setId(H5Fcreate(var.localstr(),H5F_ACC_TRUNC,H5P_DEFAULT,plist_id));
153 ARCANE_THROW(ReaderWriterException,
"Can not open file '{0}'",var);
157openAppend(
const String& var,hid_t plist_id)
160 _setId(H5Fopen(var.localstr(),H5F_ACC_RDWR,plist_id));
162 ARCANE_THROW(ReaderWriterException,
"Can not open file '{0}'",var);
166openRead(
const String& var,hid_t plist_id)
169 _setId(H5Fopen(var.localstr(),H5F_ACC_RDONLY,plist_id));
171 ARCANE_THROW(ReaderWriterException,
"Can not open file '{0}'",var);
196 ARCANE_THROW(ReaderWriterException,
"Can not close file");
203recursiveCreate(
const Hid& loc_id,
const String& var)
205 UniqueArray<String> bufs;
206 splitString(var,bufs,
'/');
207 recursiveCreate(loc_id,bufs);
211recursiveCreate(
const Hid& loc_id,
const Array<String>& bufs)
214 hid_t last_hid = loc_id.id();
215 Integer nb_create = bufs.size();
216 UniqueArray<hid_t> ref_ids(nb_create);
217 for( Integer i=0; i<nb_create; ++i ){
218 last_hid = _checkOrCreate(last_hid,bufs[i]);
219 ref_ids[i] = last_hid;
222 for( Integer i=0; i<nb_create-1; ++i )
223 H5Gclose(ref_ids[i]);
231checkDelete(
const Hid& loc_id,
const String& var)
233 UniqueArray<String> bufs;
234 splitString(var,bufs,
'/');
235 hid_t last_hid = loc_id.id();
236 hid_t parent_hid = last_hid;
239 for( ; i<size; ++i ){
240 parent_hid = last_hid;
241 last_hid = _checkExist(last_hid,bufs[i]);
246 if (last_hid>0 && parent_hid>0 && i==size){
248 H5Gunlink(parent_hid,bufs[size-1].localstr());
256recursiveOpen(
const Hid& loc_id,
const String& var)
259 UniqueArray<String> bufs;
260 splitString(var,bufs,
'/');
261 hid_t last_hid = loc_id.id();
263 UniqueArray<hid_t> ref_ids(nb_open);
264 for( Integer i=0; i<nb_open; ++i ){
265 last_hid = _H5Gopen(last_hid,bufs[i].localstr());
266 ref_ids[i] = last_hid;
269 for( Integer i=0; i<nb_open-1; ++i )
270 H5Gclose(ref_ids[i]);
278openIfExists(
const Hid& loc_id,
const Array<String>& paths)
281 hid_t last_hid = loc_id.id();
282 bool is_valid =
true;
283 Integer nb_open = paths.size();
284 UniqueArray<hid_t> ref_ids;
285 ref_ids.reserve(nb_open);
286 for( Integer i=0; i<nb_open; ++i ){
287 if (HGroup::hasChildren(last_hid,paths[i].localstr())){
288 last_hid = _H5Gopen(last_hid,paths[i].localstr());
289 ref_ids.add(last_hid);
299 for( Integer i=0; i<ref_ids.size(); ++i ){
300 if (ref_ids[i]!=last_hid)
301 H5Gclose(ref_ids[i]);
309hasChildren(
const String& var)
311 return hasChildren(
id(),var);
318hasChildren(hid_t loc_id,
const String& var)
320 HGroupSearch gs(var);
321 herr_t v = H5Giterate(loc_id,
".",0,_ArcaneHdf5UtilsGroupIterateMe,&gs);
322 bool has_children = v>0;
331_checkOrCreate(hid_t loc_id,
const String& group_name)
337 HGroupSearch gs(group_name);
339 herr_t v = H5Giterate(loc_id,
".",0,_ArcaneHdf5UtilsGroupIterateMe,&gs);
346 return _H5Gopen(loc_id,group_name.localstr());
348 hid_t new_id = _H5Gcreate(loc_id,group_name.localstr());
357create(
const Hid& loc_id,
const String& group_name)
359 _setId(H5Gcreate2(loc_id.id(), group_name.localstr(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT));
366openOrCreate(
const Hid& loc_id,
const String& group_name)
368 hid_t
id = _checkOrCreate(loc_id.id(),group_name);
370 ARCANE_THROW(ReaderWriterException,
"Can not open or create group named '{0}'",group_name);
378open(
const Hid& loc_id,
const String& var)
380 hid_t
id = _H5Gopen(loc_id.id(),var.localstr());
382 ARCANE_THROW(ReaderWriterException,
"Can not find group named '{0}'",var);
402_checkExist(hid_t loc_id,
const String& group_name)
408 HGroupSearch gs(group_name);
410 herr_t v = H5Giterate(loc_id,
".",0,_ArcaneHdf5UtilsGroupIterateMe,&gs);
417 return _H5Gopen(loc_id,group_name.localstr());
431createSimple(
int nb, hsize_t dims[])
433 _setId(H5Screate_simple(nb, dims,
nullptr));
440createSimple(
int nb, hsize_t dims[], hsize_t max_dims[])
442 _setId(H5Screate_simple(nb, dims, max_dims));
451 return H5Sget_simple_extent_ndims(
id());
458getDimensions(hsize_t dims[], hsize_t max_dims[])
460 return H5Sget_simple_extent_dims(
id(), dims, max_dims);
470create(
const Hid& loc_id,
const String& var,hid_t save_type,
471 const HSpace& space_id,hid_t plist)
473 hid_t hid = H5Dcreate2(loc_id.id(),var.localstr(),save_type,space_id.id(),
474 plist,H5P_DEFAULT,H5P_DEFAULT);
483create(
const Hid& loc_id,
const String& var,hid_t save_type,
484 const HSpace& space_id,
const HProperty& link_plist,
485 const HProperty& creation_plist,
const HProperty& access_plist)
487 hid_t hid = H5Dcreate2(loc_id.id(),var.localstr(),save_type,space_id.id(),
488 link_plist.id(),creation_plist.id(),access_plist.id());
497write(hid_t native_type,
const void* array)
500 return H5Dwrite(
id(),native_type,H5S_ALL,H5S_ALL,H5P_DEFAULT,array);
507write(hid_t native_type,
const void* array,
const HSpace& memspace_id,
508 const HSpace& filespace_id,hid_t plist)
511 return H5Dwrite(
id(),native_type,memspace_id.id(),filespace_id.id(),plist,array);
518write(hid_t native_type,
const void* array,
const HSpace& memspace_id,
519 const HSpace& filespace_id,
const HProperty& plist)
522 return H5Dwrite(
id(),native_type,memspace_id.id(),filespace_id.id(),plist.id(),array);
529readWithException(hid_t native_type,
void* array)
531 herr_t err = H5Dread(
id(),native_type,H5S_ALL,H5S_ALL,H5P_DEFAULT,array);
542 return HSpace(H5Dget_space(
id()));
549setExtent(
const hsize_t new_dims[])
551 return H5Dset_extent(
id(),new_dims);
558recursiveCreate(
const Hid& loc_id,
const String& var,hid_t save_type,
559 const HSpace& space_id,hid_t plist)
563 UniqueArray<String> paths;
564 splitString(var,paths,
'/');
565 Integer nb_path = paths.size();
567 if (HGroup::hasChildren(loc_id.id(),var)){
568 _remove(loc_id.id(),var);
570 create(loc_id,var,save_type,space_id,plist);
573 String last_name = paths[nb_path-1];
574 paths.resize(nb_path-1);
576 group.recursiveCreate(loc_id,paths);
577 if (group.hasChildren(last_name)){
578 _remove(group.id(),last_name);
580 create(group.id(),last_name,save_type,space_id,plist);
587_remove(hid_t hid,
const String& var)
589 H5Gunlink(hid,var.localstr());
596open(
const Hid& loc_id,
const String& var)
598 _setId(H5Dopen2(loc_id.id(),var.localstr(),H5P_DEFAULT));
600 ARCANE_THROW(IOException,
"Can not open dataset '{0}'",var);
607openIfExists(
const Hid& loc_id,
const String& var)
609 UniqueArray<String> paths;
610 splitString(var,paths,
'/');
611 Integer nb_path = paths.size();
613 String last_name = var;
615 last_name = paths[nb_path-1];
616 paths.resize(nb_path-1);
617 parent_group.openIfExists(loc_id,paths);
620 parent_group.open(loc_id,
".");
622 if (parent_group.isBad())
624 if (parent_group.hasChildren(last_name))
625 open(loc_id.id(),var.localstr());
638 _setId(H5Pcreate(cls_id));
647#ifdef H5_HAVE_PARALLEL
668#ifdef H5_HAVE_PARALLEL
816#ifdef ARCANE_REAL_NOT_BUILTIN
818nativeType(
Real)
const
820 throw FatalErrorException(
"Hdf5Utils::StandardTypes::nativeType(Real)",
"Real is a complex type");
830 case DT_Real2:
return saveType(Real2());
832 case DT_Real3:
return saveType(Real3());
838 throw ArgumentException(
"Bad type");
851 case DT_Real2:
return nativeType(Real2());
852 case DT_Real2x2:
return nativeType(Real2x2());
853 case DT_Real3:
return nativeType(Real3());
854 case DT_Real3x3:
return nativeType(Real3x3());
859 throw ArgumentException(
"Bad type");
870StandardArray(hid_t hfile,
const String& hpath)
873, m_ids_hpath(hpath +
"_Ids")
887 m_hdataset.open(m_hfile,m_hpath);
888 HSpace hspace(m_hdataset.getSpace());
890 const int max_dim = 256;
891 hsize_t hdf_dims[max_dim];
892 hsize_t max_dims[max_dim];
893 int nb_dim = H5Sget_simple_extent_ndims(hspace.id());
894 H5Sget_simple_extent_dims(hspace.id(),hdf_dims,max_dims);
895 for( Integer i=0; i<nb_dim; ++i ){
898 m_dimensions.
add((
Int64)hdf_dims[i]);
903 m_ids_dataset.openIfExists(m_hfile,m_ids_hpath);
931 m_dimensions.
clear();
935 if (m_hdataset.isBad())
951 dataset.openIfExists(m_hfile,m_hpath);
952 return dataset.id()>0;
958template<
typename DataType> StandardArrayT<DataType>::
959StandardArrayT(hid_t hfile,
const String& hpath)
960: StandardArray(hfile,hpath)
970 m_hdataset.readWithException(st.nativeType(DataType()),buffer.data());
980 buffer.resize(m_dimensions[0]);
996 if (m_ids_dataset.id()>0){
998 m_ids_dataset.read(st.nativeType(Int64()),
unique_ids.data());
1011 pm->broadcast(iav,master_rank);
1012 Integer buf_size = infos[0];
1013 has_ids = infos[1]!=0;
1014 buffer.resize(buf_size);
1015 unique_ids.resize(buf_size);
1016 pm->broadcast(buffer,master_rank);
1017 pm->broadcast(unique_ids,master_rank);
1020 for( Integer i=0, is=unique_ids.size(); i<is; ++i )
1028template<
typename DataType>
void StandardArrayT<DataType>::
1029write(StandardTypes& st,ConstArrayView<DataType> buffer)
1031 Integer nb_element = buffer.size();
1032 _write(buffer.data(),nb_element,st.saveType(DataType()),st.nativeType(DataType()));
1038template<
typename DataType>
void StandardArrayT<DataType>::
1039_writeSortedValues(ITraceMng* tm,StandardTypes& st,
1040 ConstArrayView<DataType> buffer,
1045 Integer total_size = buffer.size();
1046 Integer nb_element = unique_ids.size();
1048 if (nb_element != total_size){
1049 if (nb_element == 0)
1050 ARCANE_THROW(ArgumentException,
"unique_ids size is zero but not buffer size ({0})",
1052 dim2_size = total_size / nb_element;
1053 if (dim2_size*nb_element != total_size)
1054 ARCANE_THROW(ArgumentException,
"buffer size ({0}) is not a multiple of unique_ids size ({1})",
1055 total_size,nb_element);
1058 UniqueArray<ValueWithUid> values_to_sort(nb_element);
1059 UniqueArray<DataType> out_buffer(total_size);
1062 for( Integer i=0; i<nb_element; ++i ){
1063 values_to_sort[i].m_uid = unique_ids[i];
1064 values_to_sort[i].m_index = i;
1068 std::sort(std::begin(values_to_sort),std::end(values_to_sort));
1069 for( Integer i=0; i<nb_element; ++i ){
1070 Integer old_index = values_to_sort[i].m_index;
1071 for( Integer j=0; j<dim2_size; ++j ){
1072 Integer pos = (i*dim2_size)+j;
1073 out_buffer[pos] = buffer[(old_index*dim2_size)+j];
1079 write(st,out_buffer);
1085template<
typename DataType>
void StandardArrayT<DataType>::
1086parallelWrite(IParallelMng* pm,StandardTypes& st,
1102 _writeSortedValues(tm,st,buffer,unique_ids);
1109 Integer buf_size = buffer.size();
1110 Integer unique_id_size = unique_ids.size();
1118 buf[1] = unique_id_size;
1122 Integer buffer_total_size = 0;
1123 Integer unique_id_total_size = 0;
1127 for( Integer i=0; i<nb_rank; ++i ){
1128 buffer_rank_index[i] = buffer_total_size;
1129 buffer_total_size += rank_sizes[(i*2)];
1130 unique_id_rank_index[i] = unique_id_total_size;
1131 unique_id_total_size += rank_sizes[(i*2)+1];
1134 UniqueArray<DataType> full_buffer(buffer_total_size);
1137 for( Integer i=0; i<nb_rank; ++i ){
1139 if (rank_sizes[(i*2)]==0)
1141 ArrayView<DataType> local_buf(rank_sizes[(i*2)],&full_buffer[ buffer_rank_index[i] ]);
1142 Int64ArrayView local_unique_ids(rank_sizes[(i*2)+1],&full_unique_ids[ unique_id_rank_index[i] ]);
1143 if (i==master_rank){
1144 local_buf.copy(buffer);
1145 local_unique_ids.copy(unique_ids);
1148 pm->
recv(local_buf,i);
1149 pm->
recv(local_unique_ids,i);
1152 tm->
info(5) <<
"PARALLEL WRITE path=" << m_hpath <<
" total_size=" << full_buffer.size();
1153 _writeSortedValues(tm,st,full_buffer,full_unique_ids);
1158 buf[1] = unique_id_size;
1162 if (buffer.size()>0){
1163 pm->send(buffer,master_rank);
1164 pm->send(unique_ids,master_rank);
1172template class StandardArrayT<Real>;
1173template class StandardArrayT<Real3>;
1174template class StandardArrayT<Real3x3>;
1175template class StandardArrayT<Real2>;
1176template class StandardArrayT<Real2x2>;
1177template class StandardArrayT<Int16>;
1178template class StandardArrayT<Int32>;
1179template class StandardArrayT<Int64>;
1180template class StandardArrayT<Byte>;
1185template<
typename DataType> DataType
1190 m_hdataset.open(m_hfile,m_hpath);
1204 m_hdataset.read(st.nativeType(DataType()),&
dummy);
1218 m_hdataset.open(m_hfile,m_hpath);
1238template<
typename DataType>
void
1249 m_hdataset.recursiveCreate(m_hfile,m_hpath,st.saveType(DataType()),
hspace,
H5P_DEFAULT);
1250 if (m_hdataset.isBad())
1253 herr_t herr = m_hdataset.write(st.nativeType(DataType()),&
t);
1276 if (m_hdataset.isBad())