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
840#ifdef ARCANE_REAL_NOT_BUILTIN
842nativeType(
Real)
const
854 case DT_Real2:
return saveType(Real2());
856 case DT_Real3:
return saveType(Real3());
866 throw ArgumentException(String::format(
"Bad type '{0}'",sd));
879 case DT_Real2:
return nativeType(Real2());
880 case DT_Real2x2:
return nativeType(Real2x2());
881 case DT_Real3:
return nativeType(Real3());
882 case DT_Real3x3:
return nativeType(Real3x3());
891 throw ArgumentException(String::format(
"Bad type '{0}'",sd));
902StandardArray(hid_t hfile,
const String& hpath)
905, m_ids_hpath(hpath +
"_Ids")
919 m_hdataset.open(m_hfile,m_hpath);
920 HSpace hspace(m_hdataset.getSpace());
922 const int max_dim = 256;
923 hsize_t hdf_dims[max_dim];
924 hsize_t max_dims[max_dim];
925 int nb_dim = H5Sget_simple_extent_ndims(hspace.id());
926 H5Sget_simple_extent_dims(hspace.id(),hdf_dims,max_dims);
927 for( Integer i=0; i<nb_dim; ++i ){
930 m_dimensions.
add((
Int64)hdf_dims[i]);
935 m_ids_dataset.openIfExists(m_hfile,m_ids_hpath);
963 m_dimensions.
clear();
967 if (m_hdataset.isBad())
983 dataset.openIfExists(m_hfile,m_hpath);
984 return dataset.id()>0;
990template<
typename DataType> StandardArrayT<DataType>::
991StandardArrayT(hid_t hfile,
const String& hpath)
992: StandardArray(hfile,hpath)
1002 m_hdataset.readWithException(st.nativeType(DataType()),buffer.data());
1012 buffer.resize(m_dimensions[0]);
1028 if (m_ids_dataset.id()>0){
1030 m_ids_dataset.read(st.nativeType(Int64()),
unique_ids.data());
1043 pm->broadcast(iav,master_rank);
1044 Integer buf_size = infos[0];
1045 has_ids = infos[1]!=0;
1046 buffer.resize(buf_size);
1047 unique_ids.resize(buf_size);
1048 pm->broadcast(buffer,master_rank);
1049 pm->broadcast(unique_ids,master_rank);
1052 for( Integer i=0, is=unique_ids.size(); i<is; ++i )
1060template<
typename DataType>
void StandardArrayT<DataType>::
1061write(StandardTypes& st,ConstArrayView<DataType> buffer)
1063 Integer nb_element = buffer.size();
1064 _write(buffer.data(),nb_element,st.saveType(DataType()),st.nativeType(DataType()));
1070template<
typename DataType>
void StandardArrayT<DataType>::
1071_writeSortedValues(ITraceMng* tm,StandardTypes& st,
1072 ConstArrayView<DataType> buffer,
1077 Integer total_size = buffer.size();
1078 Integer nb_element = unique_ids.size();
1080 if (nb_element != total_size){
1081 if (nb_element == 0)
1082 ARCANE_THROW(ArgumentException,
"unique_ids size is zero but not buffer size ({0})",
1084 dim2_size = total_size / nb_element;
1085 if (dim2_size*nb_element != total_size)
1086 ARCANE_THROW(ArgumentException,
"buffer size ({0}) is not a multiple of unique_ids size ({1})",
1087 total_size,nb_element);
1090 UniqueArray<ValueWithUid> values_to_sort(nb_element);
1091 UniqueArray<DataType> out_buffer(total_size);
1094 for( Integer i=0; i<nb_element; ++i ){
1095 values_to_sort[i].m_uid = unique_ids[i];
1096 values_to_sort[i].m_index = i;
1100 std::sort(std::begin(values_to_sort),std::end(values_to_sort));
1101 for( Integer i=0; i<nb_element; ++i ){
1102 Integer old_index = values_to_sort[i].m_index;
1103 for( Integer j=0; j<dim2_size; ++j ){
1104 Integer pos = (i*dim2_size)+j;
1105 out_buffer[pos] = buffer[(old_index*dim2_size)+j];
1111 write(st,out_buffer);
1117template<
typename DataType>
void StandardArrayT<DataType>::
1118parallelWrite(IParallelMng* pm,StandardTypes& st,
1134 _writeSortedValues(tm,st,buffer,unique_ids);
1141 Integer buf_size = buffer.size();
1142 Integer unique_id_size = unique_ids.size();
1150 buf[1] = unique_id_size;
1154 Integer buffer_total_size = 0;
1155 Integer unique_id_total_size = 0;
1159 for( Integer i=0; i<nb_rank; ++i ){
1160 buffer_rank_index[i] = buffer_total_size;
1161 buffer_total_size += rank_sizes[(i*2)];
1162 unique_id_rank_index[i] = unique_id_total_size;
1163 unique_id_total_size += rank_sizes[(i*2)+1];
1166 UniqueArray<DataType> full_buffer(buffer_total_size);
1169 for( Integer i=0; i<nb_rank; ++i ){
1171 if (rank_sizes[(i*2)]==0)
1173 ArrayView<DataType> local_buf(rank_sizes[(i*2)],&full_buffer[ buffer_rank_index[i] ]);
1174 Int64ArrayView local_unique_ids(rank_sizes[(i*2)+1],&full_unique_ids[ unique_id_rank_index[i] ]);
1175 if (i==master_rank){
1176 local_buf.copy(buffer);
1177 local_unique_ids.copy(unique_ids);
1180 pm->
recv(local_buf,i);
1181 pm->
recv(local_unique_ids,i);
1184 tm->
info(5) <<
"PARALLEL WRITE path=" << m_hpath <<
" total_size=" << full_buffer.size();
1185 _writeSortedValues(tm,st,full_buffer,full_unique_ids);
1190 buf[1] = unique_id_size;
1194 if (buffer.size()>0){
1195 pm->send(buffer,master_rank);
1196 pm->send(unique_ids,master_rank);
1204template class StandardArrayT<Real>;
1205template class StandardArrayT<Real3>;
1206template class StandardArrayT<Real3x3>;
1207template class StandardArrayT<Real2>;
1208template class StandardArrayT<Real2x2>;
1209template class StandardArrayT<Int16>;
1210template class StandardArrayT<Int32>;
1211template class StandardArrayT<Int64>;
1212template class StandardArrayT<Byte>;
1213template class StandardArrayT<Int8>;
1214template class StandardArrayT<Float32>;
1224template<
typename DataType> DataType
1229 m_hdataset.open(m_hfile,m_hpath);
1243 m_hdataset.read(st.nativeType(DataType()),&
dummy);
1257 m_hdataset.open(m_hfile,m_hpath);
1277template<
typename DataType>
void
1288 m_hdataset.recursiveCreate(m_hfile,m_hpath,st.saveType(DataType()),
hspace,
H5P_DEFAULT);
1289 if (m_hdataset.isBad())
1292 herr_t herr = m_hdataset.write(st.nativeType(DataType()),&
t);
1315 if (m_hdataset.isBad())