82 hsize_t offset[] = {
static_cast<hsize_t
>(linearX->getMap()->getGlobalElement(0)-X->getMap()->getIndexBase()),
83 static_cast<hsize_t
>(linearX->getMap()->getGlobalElement(0)-X->getMap()->getIndexBase())};
84 hsize_t stride[] = {1, 1};
85 hsize_t count[] = {
static_cast<hsize_t
>(linearX->getLocalLength()),
86 static_cast<hsize_t
>(linearX->getLocalLength())};
87 hsize_t block[] = {1, 1};
89 for (
int n = 0; n < NumVectors; ++n)
105 filespace_id = H5Dget_space(dset_id);
107 herr_t selStatus = H5Sselect_hyperslab(
108 filespace_id, H5S_SELECT_SET, offset, stride, count, block);
111 std::cerr <<
"[Rank " << comm_->getRank()
112 <<
"] ERROR selecting hyperslab: "
113 <<
"offset=(" << offset[0] <<
"," << offset[1] <<
"), "
114 <<
"count=(" << count[0] <<
"," << count[1] <<
")\n";
118 hsize_t dimsm[] = {
static_cast<hsize_t
>(linearX->getLocalLength()) };
119 memspace_id = H5Screate_simple(1, dimsm, NULL);
121 if (memspace_id < 0) {
122 std::cerr <<
"[Rank " << comm_->getRank()
123 <<
"] ERROR creating memory dataspace of size "
124 << dimsm[0] << std::endl;
135 herr_t writeStatus = H5Dwrite(
136 dset_id, H5T_NATIVE_DOUBLE, memspace_id, filespace_id,
137 plist_id_, linearX->getData(n).get());
139 if (writeStatus < 0) {
140 std::cerr <<
"[Rank " << comm_->getRank()
141 <<
"] ERROR during H5Dwrite for vector " << n << std::endl;
150 hid_t filespace = H5Dget_space(dset_id);
151 hsize_t totalElems = H5Sget_simple_extent_npoints(filespace);
156 hssize_t fileCount = H5Sget_select_npoints(filespace_id);
157 hssize_t memCount = H5Sget_select_npoints(memspace_id);
164 H5Sclose(filespace_id);
168 H5Sclose(memspace_id);
170 write(GroupName,
"GlobalLength", GlobalLength);
171 write(GroupName,
"NumVectors", NumVectors);
172 write(GroupName,
"__type__",
"Tpetra_MultiVector");
176template <
class SC,
class LO,
class GO,
class NO>
177void HDF5Toolbox<SC, LO, GO, NO>::read(
const std::string& GroupName,
const MapConstPtr_Type
Map,
178 MultiVectorPtr_Type X)
182 readIntVectorProperties(GroupName, GlobalLength);
184 MapPtr_Type linearMap = Teuchos::rcp(
new Map_Type(X->getMap()->getGlobalNumElements(),X->getMap()->getNodeNumElements(), X->getMap()->getIndexBase(), X->getMap()->getComm()));
188 MultiVectorPtr_Type linearX = Teuchos::rcp(
new MultiVector_Type(linearMap, X->getNumVectors()));
190 read(GroupName,
"Values", linearMap->getNodeNumElements(), linearMap->getGlobalNumElements(),
191 H5T_NATIVE_DOUBLE, linearX->getDataNonConst(0).get());
193 X->importFromVector(linearX);
198template <
class SC,
class LO,
class GO,
class NO>
199void HDF5Toolbox<SC, LO, GO, NO>::readIntVectorProperties(
const std::string& GroupName,
202 TEUCHOS_TEST_FOR_EXCEPTION(!isContained(GroupName),std::runtime_error,
"requested group " << GroupName <<
" not found");
205 read(GroupName,
"__type__", Label);
207 read(GroupName,
"GlobalLength", GlobalLength);
210template <
class SC,
class LO,
class GO,
class NO>
211void HDF5Toolbox<SC, LO, GO, NO>::read(
const std::string& GroupName,
const std::string& DataSetName,
212 GO MySize,
int GlobalSize,
213 const hid_t type,
void* data)
215 TEUCHOS_TEST_FOR_EXCEPTION(!isOpen(),std::runtime_error,
"HDF5Toolbox:: no file open yet");
218hsize_t MySize_t = MySize;
222tpetraScanSum(comm_, &MySize, &itmp, 1);
225hsize_t Offset_t =
static_cast<hsize_t
>(itmp - MySize);
235hid_t group_id = H5Gopen(file_id_, GroupName.c_str(), H5P_DEFAULT);
237 std::cerr <<
"[Rank " << comm_->getRank() <<
"] ERROR: H5Gopen failed for group '"
238 << GroupName <<
"'\n";
241hid_t dataset_id = H5Dopen(group_id, DataSetName.c_str(), H5P_DEFAULT);
243 std::cerr <<
"[Rank " << comm_->getRank() <<
"] ERROR: H5Dopen failed for dataset '"
244 << DataSetName <<
"'\n";
248hid_t filespace_id = H5Dget_space(dataset_id);
249if (filespace_id < 0) {
250 std::cerr <<
"[Rank " << comm_->getRank() <<
"] ERROR: H5Dget_space failed\n";
254hsize_t offset[2] = { 0, Offset_t };
255hsize_t count [2] = { 1, MySize_t };
257herr_t selStatus = H5Sselect_hyperslab(filespace_id, H5S_SELECT_SET, offset, NULL, count, NULL);
260 std::cerr <<
"[Rank " << comm_->getRank()
261 <<
"] ERROR selecting hyperslab: offset=" << Offset_t
262 <<
" count=" << MySize_t << std::endl;
264 std::cout <<
"[Rank " << comm_->getRank()
265 <<
"] Selected hyperslab: offset=" << Offset_t
266 <<
" count=" << MySize_t << std::endl;
270hid_t mem_dataspace = H5Screate_simple(1, &MySize_t, NULL);
271if (mem_dataspace < 0) {
272 std::cerr <<
"[Rank " << comm_->getRank()
273 <<
"] ERROR creating memory dataspace (size=" << MySize_t <<
")\n";
284herr_t status = H5Dread(dataset_id, type, mem_dataspace, filespace_id,
288 std::cerr <<
"[Rank " << comm_->getRank()
289 <<
"] ERROR during H5Dread (offset=" << Offset_t
290 <<
", count=" << MySize_t <<
")\n";
292 std::cout <<
"[Rank " << comm_->getRank()
293 <<
"] Successfully read " << MySize_t <<
" elements from dataset '"
294 << DataSetName <<
"'\n";
297hid_t filespace = H5Dget_space(dataset_id);
298hsize_t totalElems = H5Sget_simple_extent_npoints(filespace);
304hssize_t fileCount = H5Sget_select_npoints(filespace_id);
305hssize_t memCount = H5Sget_select_npoints(mem_dataspace);
312H5Sclose(mem_dataspace);
313H5Sclose(filespace_id);
326template <
class SC,
class LO,
class GO,
class NO>
329 TEUCHOS_TEST_FOR_EXCEPTION(
isOpen(),std::runtime_error,
"HDF5Toolbox:: an HDF5 is already open, first close the current one - using method Close(), then open/create a new one");
331 FileName_ = FileName;
334 plist_id_ = H5Pcreate(H5P_FILE_ACCESS);
343 MPI_Comm mpiComm = MPI_COMM_NULL;
345 Teuchos::RCP<const Teuchos::MpiComm<int>> mpiWrapper =
346 Teuchos::rcp_dynamic_cast<const Teuchos::MpiComm<int>>(comm_,
false);
348 if (!mpiWrapper.is_null()) {
349 mpiComm = *(mpiWrapper->getRawMpiComm());
353 Teuchos::RCP<const Teuchos::SerialComm<int>> serialWrapper =
354 Teuchos::rcp_dynamic_cast<const Teuchos::SerialComm<int>>(comm_,
false);
356 if (!serialWrapper.is_null()) {
359 mpiComm = MPI_COMM_SELF;
363 const char*
const errMsg =
364 "Tpetra::HDF5::Create: This HDF5 object was created with a "
365 "Teuchos::Comm<int> that is neither Teuchos::MpiComm<int> "
366 "nor Teuchos::SerialComm<int>. We don't know how to get an "
368 TEUCHOS_TEST_FOR_EXCEPTION(
true,std::runtime_error,errMsg);
374 if (mpiComm == MPI_COMM_NULL) {
375 const char*
const errMsg =
"TpetraExt::HDF5::Create: The Tpetra_Comm "
376 "object with which this HDF5 instance was created wraps MPI_COMM_NULL, "
377 "which is an invalid MPI communicator. HDF5 requires a valid MPI "
379 TEUCHOS_TEST_FOR_EXCEPTION(
true,std::runtime_error,errMsg);
388 H5Pset_fapl_mpio(plist_id_, mpiComm, MPI_INFO_NULL);
393 unsigned int boh = H5Z_FILTER_MAX;
394 H5Pset_filter(plist_id_, H5Z_FILTER_DEFLATE, H5Z_FILTER_MAX, 0, &boh);
398 file_id_ = H5Fcreate(FileName.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT,
409template <
class SC,
class LO,
class GO,
class NO>
412 TEUCHOS_TEST_FOR_EXCEPTION(!
isOpen(),std::runtime_error,
"HDF5Toolbox:: no file open yet");
419 size_t pos = Name.find(
"/");
420 if (pos != std::string::npos)
422 std::string NewGroupName = Name.substr(0, pos);
424 NewGroupName = GroupName +
"/" + NewGroupName;
425 std::string NewName = Name.substr(pos + 1);
429 GroupName =
"/" + GroupName;
432 H5Giterate(file_id_, GroupName.c_str(), NULL, FindDataset, (
void*)&data);
440template <
class SC,
class LO,
class GO,
class NO>
443 hid_t group_id = H5Gcreate(file_id_, GroupName.c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
453template <
class SC,
class LO,
class GO,
class NO>
457 if (!isContained(GroupName))
458 createGroup(GroupName);
460 hid_t filespace_id = H5Screate(H5S_SCALAR);
461 hid_t group_id = H5Gopen(file_id_, GroupName.c_str(), H5P_DEFAULT);
462 hid_t dset_id = H5Dcreate(group_id, DataSetName.c_str(), H5T_NATIVE_DOUBLE,
463 filespace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
465 herr_t status = H5Dwrite(dset_id, H5T_NATIVE_DOUBLE, H5S_ALL,
466 filespace_id, H5P_DEFAULT, &what);
471 H5Sclose(filespace_id);
476template <
class SC,
class LO,
class GO,
class NO>
480 if (!isContained(GroupName))
481 createGroup(GroupName);
483 hid_t filespace_id = H5Screate(H5S_SCALAR);
484 hid_t group_id = H5Gopen(file_id_, GroupName.c_str(), H5P_DEFAULT);
485 hid_t dset_id = H5Dcreate(group_id, DataSetName.c_str(), H5T_NATIVE_INT,
486 filespace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
488 herr_t status = H5Dwrite(dset_id, H5T_NATIVE_INT, H5S_ALL, filespace_id,
494 H5Sclose(filespace_id);
499template <
class SC,
class LO,
class GO,
class NO>
501 const std::string& DataSetName,
502 const std::string& data)
504 if (!isContained(GroupName))
505 createGroup(GroupName);
509 hid_t group_id = H5Gopen(file_id_, GroupName.c_str(), H5P_DEFAULT);
511 hid_t dataspace_id = H5Screate_simple(1, &len, NULL);
513 hid_t atype = H5Tcopy(H5T_C_S1);
514 H5Tset_size(atype, data.size() + 1);
516 hid_t dataset_id = H5Dcreate(group_id, DataSetName.c_str(), atype,
517 dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
519 H5Dwrite(dataset_id, atype, H5S_ALL, H5S_ALL,H5P_DEFAULT, data.c_str());
522 H5Dclose(dataset_id);
529template <
class SC,
class LO,
class GO,
class NO>
530void HDF5Toolbox<SC, LO, GO, NO>::read(
const std::string& GroupName,
const std::string& DataSetName,
int& data)
532 TEUCHOS_TEST_FOR_EXCEPTION(!isContained(GroupName),std::runtime_error,
"requested group " + GroupName +
" not found");
535 hid_t group_id = H5Gopen(file_id_, GroupName.c_str(), H5P_DEFAULT);
537 hid_t filespace_id = H5Screate(H5S_SCALAR);
538 hid_t dset_id = H5Dopen(group_id, DataSetName.c_str(), H5P_DEFAULT);
540 herr_t status = H5Dread(dset_id, H5T_NATIVE_INT, H5S_ALL, filespace_id,
543 H5Sclose(filespace_id);
548template <
class SC,
class LO,
class GO,
class NO>
549void HDF5Toolbox<SC, LO, GO, NO>::read(
const std::string& GroupName,
550 const std::string& DataSetName,
553 TEUCHOS_TEST_FOR_EXCEPTION(!isContained(GroupName),std::runtime_error,
"requested group " + GroupName +
" not found");
555 hid_t group_id = H5Gopen(file_id_, GroupName.c_str(), H5P_DEFAULT);
557 hid_t dataset_id = H5Dopen(group_id, DataSetName.c_str(), H5P_DEFAULT);
559 hid_t datatype_id = H5Dget_type(dataset_id);
561 H5T_class_t typeclass_id = H5Tget_class(datatype_id);
563 if(typeclass_id != H5T_STRING)
564 TEUCHOS_TEST_FOR_EXCEPTION(
true,std::runtime_error,
"requested group " + GroupName +
" is not a std::string");
567 H5Dread(dataset_id, datatype_id, H5S_ALL, H5S_ALL,H5P_DEFAULT, data2) ;
571 H5Dclose(dataset_id);
575template <
class SC,
class LO,
class GO,
class NO>
578 TEUCHOS_TEST_FOR_EXCEPTION(
isOpen(),std::runtime_error,
"HDF5Toolbox:: no file open yet");
580 FileName_ = FileName;
583 plist_id_ = H5Pcreate(H5P_FILE_ACCESS);
586 MPI_Comm mpiComm = MPI_COMM_WORLD;
588 Teuchos::RCP<const Teuchos::MpiComm<int>> mpiWrapper =
589 Teuchos::rcp_dynamic_cast<const Teuchos::MpiComm<int>>(comm_,
false);
591 if (!mpiWrapper.is_null()) {
592 mpiComm = *(mpiWrapper->getRawMpiComm());
595 H5Pset_fapl_mpio(plist_id_, mpiComm, MPI_INFO_NULL);
599 file_id_ = H5Fopen(FileName.c_str(), AccessType, plist_id_);