Finite Element Domain Decomposition Library
FEDDLib
Loading...
Searching...
No Matches
HDF5Toolbox_def.hpp
1#ifndef HDF5TOOLBOX_DEF_hpp
2#define HDF5TOOLBOX_DEF_hpp
3
4/*
5
6
7*/
8
9namespace FEDD {
10
12{
13 std::string name;
14 bool found;
15};
16
17static herr_t FindDataset(hid_t loc_id, const char *name, void *opdata)
18{
19 std::string& token = ((FindDataset_t*)opdata)->name;
20 if (token == name)
21 ((FindDataset_t*)opdata)->found = true;
22
23 return(0);
24}
25
26template <class SC, class LO, class GO, class NO>
27HDF5Toolbox<SC, LO, GO, NO>::HDF5Toolbox(CommConstPtr_Type comm):
28 comm_(comm),
29 isOpen_(false)
30{}
31
32// ----------------------------------------------------------------
34template <class SC, class LO, class GO, class NO>
35void HDF5Toolbox<SC, LO, GO, NO>::write(const std::string& GroupName, const MultiVectorConstPtr_Type X, bool writeTranspose)
36{
37
38 TEUCHOS_TEST_FOR_EXCEPTION(!isOpen(),std::runtime_error,"HDF5Toolbox:: no file open yet");
39
40 hid_t group_id, dset_id;
41 hid_t filespace_id, memspace_id;
42 herr_t status;
43
44 // need a linear distribution to use hyperslabs
45 MultiVectorPtr_Type linearX;
46
47 MapPtr_Type linearMap = Teuchos::rcp(new Map_Type(X->getMap()->getGlobalNumElements(),X->getMap()->getNodeNumElements(), X->getMap()->getIndexBase(), X->getMap()->getComm()));
48 linearX = Teuchos::rcp(new MultiVector_Type(linearMap, X->getNumVectors()));
49 linearX->importFromVector(X);
51
52 int NumVectors = X->getNumVectors();
53 int GlobalLength = X->getMap()->getGlobalNumElements(); //X.GlobalLength();
54
55 // Whether or not we do writeTranspose or not is
56 // handled by one of the components of q_dimsf, offset and count.
57 // They are determined by indexT
58 int indexT(0);
59 if (writeTranspose) indexT = 1;
60
61 hsize_t q_dimsf[] = {static_cast<hsize_t>(GlobalLength), static_cast<hsize_t>(GlobalLength)};
62 q_dimsf[indexT] = NumVectors;
63
64 filespace_id = H5Screate_simple(2, q_dimsf, NULL);
65
66 if (!isContained(GroupName))
67 createGroup(GroupName);
68
69 group_id = H5Gopen(file_id_, GroupName.c_str(), H5P_DEFAULT);
70
71 // Create the dataset with default properties and close filespace_id.
72 dset_id = H5Dcreate(group_id, "Values", H5T_NATIVE_DOUBLE, filespace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
73
74 // Create property list for collective dataset write.
75 plist_id_ = H5Pcreate(H5P_DATASET_XFER);
76#ifdef HAVE_MPI
77 H5Pset_dxpl_mpio(plist_id_, H5FD_MPIO_COLLECTIVE);
78#endif
80
81 // Select hyperslab in the file.
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};
88
89 for (int n = 0; n < NumVectors; ++n)
90 {
91 // Select hyperslab in the file
92 offset[indexT] = n;
93 count [indexT] = 1;
94
95 // Print local info before selecting hyperslab
96 // std::cout << "[Rank " << comm_->getRank() << "] "
97 // << "Writing vector " << n
98 // << " | file offset = (" << offset[0] << ", " << offset[1] << ")"
99 // << " | file count = (" << count[0] << ", " << count[1] << ")"
100 // << " | local length = " << linearX->getLocalLength()
101 // << " | global length = " << 1
102 // << std::endl;
103
104 // Get the dataset's file dataspace and select the correct hyperslab
105 filespace_id = H5Dget_space(dset_id);
106
107 herr_t selStatus = H5Sselect_hyperslab(
108 filespace_id, H5S_SELECT_SET, offset, stride, count, block);
109
110 if (selStatus < 0) {
111 std::cerr << "[Rank " << comm_->getRank()
112 << "] ERROR selecting hyperslab: "
113 << "offset=(" << offset[0] << "," << offset[1] << "), "
114 << "count=(" << count[0] << "," << count[1] << ")\n";
115 }
116
117 // Each process defines dataset in memory (its local buffer)
118 hsize_t dimsm[] = { static_cast<hsize_t>(linearX->getLocalLength()) };
119 memspace_id = H5Screate_simple(1, dimsm, NULL);
120
121 if (memspace_id < 0) {
122 std::cerr << "[Rank " << comm_->getRank()
123 << "] ERROR creating memory dataspace of size "
124 << dimsm[0] << std::endl;
125 }
126
127 // Print detailed info before writing
128 // std::cout << "[Rank " << comm_->getRank() << "] "
129 // << "Calling H5Dwrite for vector " << n
130 // << " | memspace dims = " << dimsm[0]
131 // << " | plist_id = " << plist_id_
132 // << std::endl;
133
134 // Perform the write
135 herr_t writeStatus = H5Dwrite(
136 dset_id, H5T_NATIVE_DOUBLE, memspace_id, filespace_id,
137 plist_id_, linearX->getData(n).get());
138
139 if (writeStatus < 0) {
140 std::cerr << "[Rank " << comm_->getRank()
141 << "] ERROR during H5Dwrite for vector " << n << std::endl;
142 }
143
144
145 // Optional: flush stdout to preserve rank ordering in logs
146 std::cout.flush();
147 std::cerr.flush();
148 }
149
150 hid_t filespace = H5Dget_space(dset_id);
151 hsize_t totalElems = H5Sget_simple_extent_npoints(filespace);
152
153 // std::cout << "[Rank " << comm_->getRank()
154 // << "] Dataset total elements = " << totalElems << std::endl;
155
156 hssize_t fileCount = H5Sget_select_npoints(filespace_id);
157 hssize_t memCount = H5Sget_select_npoints(memspace_id);
158
159 // std::cout << "[Rank " << comm_->getRank() << "] "
160 // << "Hyperslab file selection = " << fileCount
161 // << ", memory selection = " << memCount << std::endl;
162
163 H5Gclose(group_id);
164 H5Sclose(filespace_id);
165 H5Dclose(dset_id);
166 H5Pclose(plist_id_);
167 // Close local memory dataspace
168 H5Sclose(memspace_id);
169
170 write(GroupName, "GlobalLength", GlobalLength);
171 write(GroupName, "NumVectors", NumVectors);
172 write(GroupName, "__type__", "Tpetra_MultiVector");
173}
174// ---------------------------------------------------------
175// ==========================================================================
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)
179{
180 // gets the length of the std::vector
181 int GlobalLength;
182 readIntVectorProperties(GroupName, GlobalLength);
183
184 MapPtr_Type linearMap = Teuchos::rcp(new Map_Type(X->getMap()->getGlobalNumElements(),X->getMap()->getNodeNumElements(), X->getMap()->getIndexBase(), X->getMap()->getComm()));
185
186 // we need to first create a linear map, read the std::vector,
187 // then import it to the actual nonlinear map
188 MultiVectorPtr_Type linearX = Teuchos::rcp(new MultiVector_Type(linearMap, X->getNumVectors()));
189
190 read(GroupName, "Values", linearMap->getNodeNumElements(), linearMap->getGlobalNumElements(),
191 H5T_NATIVE_DOUBLE, linearX->getDataNonConst(0).get());
192
193 X->importFromVector(linearX);
194
195}
196
197// ==========================================================================
198template <class SC, class LO, class GO, class NO>
199void HDF5Toolbox<SC, LO, GO, NO>::readIntVectorProperties(const std::string& GroupName,
200 int& GlobalLength)
201{
202 TEUCHOS_TEST_FOR_EXCEPTION(!isContained(GroupName),std::runtime_error, "requested group " << GroupName << " not found");
203
204 std::string Label;
205 read(GroupName, "__type__", Label);
206
207 read(GroupName, "GlobalLength", GlobalLength);
208}
209
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)
214{
215 TEUCHOS_TEST_FOR_EXCEPTION(!isOpen(),std::runtime_error,"HDF5Toolbox:: no file open yet");
216
217 // Convert MySize to hsize_t for HDF5 calls
218hsize_t MySize_t = MySize;
219
220// Compute the prefix sum across ranks to get the global offset
221GO itmp = 0;
222tpetraScanSum(comm_, &MySize, &itmp, 1);
223
224// Exclusive prefix (start index for this rank)
225hsize_t Offset_t = static_cast<hsize_t>(itmp - MySize);
226
227// === DEBUG OUTPUT ===
228// std::cout << "[Rank " << comm_->getRank() << "] "
229// << "Preparing to read dataset '" << DataSetName << "' in group '" << GroupName << "'\n"
230// << " local size (MySize) = " << MySize << "\n"
231// << " computed offset (Offset)= " << Offset_t << "\n"
232// << " total elements (sum) = " << itmp << std::endl;
233
234// Open group and dataset
235hid_t group_id = H5Gopen(file_id_, GroupName.c_str(), H5P_DEFAULT);
236if (group_id < 0) {
237 std::cerr << "[Rank " << comm_->getRank() << "] ERROR: H5Gopen failed for group '"
238 << GroupName << "'\n";
239}
240
241hid_t dataset_id = H5Dopen(group_id, DataSetName.c_str(), H5P_DEFAULT);
242if (dataset_id < 0) {
243 std::cerr << "[Rank " << comm_->getRank() << "] ERROR: H5Dopen failed for dataset '"
244 << DataSetName << "'\n";
245}
246
247// Get the dataset’s file dataspace
248hid_t filespace_id = H5Dget_space(dataset_id);
249if (filespace_id < 0) {
250 std::cerr << "[Rank " << comm_->getRank() << "] ERROR: H5Dget_space failed\n";
251}
252
253// Select hyperslab in the file for this process
254hsize_t offset[2] = { 0, Offset_t }; // row index, element offset
255hsize_t count [2] = { 1, MySize_t }; // one vector, 29 elements
256
257herr_t selStatus = H5Sselect_hyperslab(filespace_id, H5S_SELECT_SET, offset, NULL, count, NULL);
258
259if (selStatus < 0) {
260 std::cerr << "[Rank " << comm_->getRank()
261 << "] ERROR selecting hyperslab: offset=" << Offset_t
262 << " count=" << MySize_t << std::endl;
263} else {
264 std::cout << "[Rank " << comm_->getRank()
265 << "] Selected hyperslab: offset=" << Offset_t
266 << " count=" << MySize_t << std::endl;
267}
268
269// Create memory dataspace for local buffer
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";
274}
275
276// Print before reading
277// std::cout << "[Rank " << comm_->getRank()
278// << "] Reading from file hyperslab offset=" << Offset_t
279// << " count=" << MySize_t
280// << " into local buffer at " << static_cast<void*>(data)
281// << std::endl;
282
283// Perform the read
284herr_t status = H5Dread(dataset_id, type, mem_dataspace, filespace_id,
285 H5P_DEFAULT, data);
286
287if (status < 0) {
288 std::cerr << "[Rank " << comm_->getRank()
289 << "] ERROR during H5Dread (offset=" << Offset_t
290 << ", count=" << MySize_t << ")\n";
291} else {
292 std::cout << "[Rank " << comm_->getRank()
293 << "] Successfully read " << MySize_t << " elements from dataset '"
294 << DataSetName << "'\n";
295}
296
297hid_t filespace = H5Dget_space(dataset_id);
298hsize_t totalElems = H5Sget_simple_extent_npoints(filespace);
299
300// std::cout << "[Rank " << comm_->getRank()
301// << "] Dataset total elements = " << totalElems << std::endl;
302
303
304hssize_t fileCount = H5Sget_select_npoints(filespace_id);
305hssize_t memCount = H5Sget_select_npoints(mem_dataspace);
306
307// std::cout << "[Rank " << comm_->getRank() << "] "
308// << "Hyperslab file selection = " << fileCount
309// << ", memory selection = " << memCount << std::endl;
310
311// Close resources
312H5Sclose(mem_dataspace);
313H5Sclose(filespace_id);
314H5Dclose(dataset_id);
315H5Gclose(group_id);
316
317// Optional: ensure ordered output by rank
318comm_->barrier();
319std::cout.flush();
320std::cerr.flush();
321// H5Dclose(filespace_id);
322}
323
324// -------------------------------------------------------------------------
326template <class SC, class LO, class GO, class NO>
327void HDF5Toolbox<SC, LO, GO, NO>::create(const std::string FileName)
328{
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");
330
331 FileName_ = FileName;
332
333 // Set up file access property list with parallel I/O access
334 plist_id_ = H5Pcreate(H5P_FILE_ACCESS);
335// #ifdef HAVE_MPI
336 {
337 // Tell HDF5 what MPI communicator to use for parallel file access
338 // for the above property list.
339 //
340 // HAVE_MPI is defined, so we know that Trilinos was built with
341 // MPI. However, we don't know whether Comm_ wraps an MPI
342 // communicator. Comm_ could very well be a serial communicator.
343 MPI_Comm mpiComm = MPI_COMM_NULL;
344
345 Teuchos::RCP<const Teuchos::MpiComm<int>> mpiWrapper =
346 Teuchos::rcp_dynamic_cast<const Teuchos::MpiComm<int>>(comm_, false);
347
348 if (!mpiWrapper.is_null()) {
349 mpiComm = *(mpiWrapper->getRawMpiComm());
350 }
351 else {
352 // Try Serial communicator next
353 Teuchos::RCP<const Teuchos::SerialComm<int>> serialWrapper =
354 Teuchos::rcp_dynamic_cast<const Teuchos::SerialComm<int>>(comm_, false);
355
356 if (!serialWrapper.is_null()) {
357 // Comm_ is a Teuchos::SerialComm<int>
358 // Use MPI_COMM_SELF for serial HDF5 access
359 mpiComm = MPI_COMM_SELF;
360 }
361 else {
362 // Unknown communicator subclass
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 "
367 "MPI_Comm from it.";
368 TEUCHOS_TEST_FOR_EXCEPTION(true,std::runtime_error,errMsg);
369 }
370 }
371
372 // By this point, mpiComm should be something other than
373 // MPI_COMM_NULL. Otherwise, Comm_ wraps MPI_COMM_NULL.
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 "
378 "communicator.";
379 TEUCHOS_TEST_FOR_EXCEPTION(true,std::runtime_error,errMsg);
380 }
381
382 // Tell HDF5 what MPI communicator to use for parallel file access
383 // for the above property list. For details, see e.g.,
384 //
385 // http://www.hdfgroup.org/HDF5/doc/UG/08_TheFile.html
386 //
387 // [last accessed 06 Oct 2011]
388 H5Pset_fapl_mpio(plist_id_, mpiComm, MPI_INFO_NULL);
389 }
390// #endif
391
392#if 0
393 unsigned int boh = H5Z_FILTER_MAX;
394 H5Pset_filter(plist_id_, H5Z_FILTER_DEFLATE, H5Z_FILTER_MAX, 0, &boh);
395#endif
396
397 // create the file collectively and release property list identifier.
398 file_id_ = H5Fcreate(FileName.c_str(), H5F_ACC_TRUNC, H5P_DEFAULT,
399 plist_id_);
400 H5Pclose(plist_id_);
401
402 isOpen_ = true;
403}
404
405
406// -------------------------------------------------------------------------
408
409template <class SC, class LO, class GO, class NO>
410bool HDF5Toolbox<SC, LO, GO, NO>::isContained(std::string Name, std::string GroupName)
411{
412 TEUCHOS_TEST_FOR_EXCEPTION(!isOpen(),std::runtime_error,"HDF5Toolbox:: no file open yet");
413
414 FindDataset_t data;
415 data.name = Name;
416 data.found = false;
417
418 // recursively look for groups
419 size_t pos = Name.find("/");
420 if (pos != std::string::npos)
421 {
422 std::string NewGroupName = Name.substr(0, pos);
423 if (GroupName != "")
424 NewGroupName = GroupName + "/" + NewGroupName;
425 std::string NewName = Name.substr(pos + 1);
426 return isContained(NewName, NewGroupName);
427 }
428
429 GroupName = "/" + GroupName;
430
431 //int idx_f =
432 H5Giterate(file_id_, GroupName.c_str(), NULL, FindDataset, (void*)&data);
433
434 return(data.found);
435}
436// -------------------------------------------------------------------------
437
438// -------------------------------------------------------------------------
440template <class SC, class LO, class GO, class NO>
441void HDF5Toolbox<SC, LO, GO, NO>::createGroup(const std::string& GroupName)
442{
443 hid_t group_id = H5Gcreate(file_id_, GroupName.c_str(), H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
444 H5Gclose(group_id);
445}
446// -------------------------------------------------------------------------
447
448
449
450// ==========================================================================
451// ==========================================================================
452
453template <class SC, class LO, class GO, class NO>
454void HDF5Toolbox<SC, LO, GO, NO>::write(const std::string& GroupName, const std::string& DataSetName,
455 double what)
456{
457 if (!isContained(GroupName))
458 createGroup(GroupName);
459
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);
464
465 herr_t status = H5Dwrite(dset_id, H5T_NATIVE_DOUBLE, H5S_ALL,
466 filespace_id, H5P_DEFAULT, &what);
467
468 // Close/release resources.
469 H5Dclose(dset_id);
470 H5Gclose(group_id);
471 H5Sclose(filespace_id);
472}
473
474// ==========================================================================
475
476template <class SC, class LO, class GO, class NO>
477void HDF5Toolbox<SC, LO, GO, NO>::write(const std::string& GroupName, const std::string& DataSetName,
478 int what)
479{
480 if (!isContained(GroupName))
481 createGroup(GroupName);
482
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);
487
488 herr_t status = H5Dwrite(dset_id, H5T_NATIVE_INT, H5S_ALL, filespace_id,
489 H5P_DEFAULT, &what);
490
491 // Close/release resources.
492 H5Dclose(dset_id);
493 H5Gclose(group_id);
494 H5Sclose(filespace_id);
495}
496
497
498// ==========================================================================
499template <class SC, class LO, class GO, class NO>
500void HDF5Toolbox<SC, LO, GO, NO>::write(const std::string& GroupName,
501 const std::string& DataSetName,
502 const std::string& data)
503{
504 if (!isContained(GroupName))
505 createGroup(GroupName);
506
507 hsize_t len = 1;
508
509 hid_t group_id = H5Gopen(file_id_, GroupName.c_str(), H5P_DEFAULT);
510
511 hid_t dataspace_id = H5Screate_simple(1, &len, NULL);
512
513 hid_t atype = H5Tcopy(H5T_C_S1);
514 H5Tset_size(atype, data.size() + 1);
515
516 hid_t dataset_id = H5Dcreate(group_id, DataSetName.c_str(), atype,
517 dataspace_id, H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
518
519 H5Dwrite(dataset_id, atype, H5S_ALL, H5S_ALL,H5P_DEFAULT, data.c_str());
520
521 // Close/release resources.
522 H5Dclose(dataset_id);
523 H5Gclose(group_id);
524}
525// ==========================================================================
526// ==========================================================================
527
528// ==========================================================================
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)
531{
532 TEUCHOS_TEST_FOR_EXCEPTION(!isContained(GroupName),std::runtime_error, "requested group " + GroupName + " not found");
533
534 // Create group in the root group using absolute name.
535 hid_t group_id = H5Gopen(file_id_, GroupName.c_str(), H5P_DEFAULT);
536
537 hid_t filespace_id = H5Screate(H5S_SCALAR);
538 hid_t dset_id = H5Dopen(group_id, DataSetName.c_str(), H5P_DEFAULT);
539
540 herr_t status = H5Dread(dset_id, H5T_NATIVE_INT, H5S_ALL, filespace_id,
541 H5P_DEFAULT, &data);
542
543 H5Sclose(filespace_id);
544 H5Dclose(dset_id);
545 H5Gclose(group_id);
546}
547
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,
551 std::string& data)
552{
553 TEUCHOS_TEST_FOR_EXCEPTION(!isContained(GroupName),std::runtime_error, "requested group " + GroupName + " not found");
554
555 hid_t group_id = H5Gopen(file_id_, GroupName.c_str(), H5P_DEFAULT);
556
557 hid_t dataset_id = H5Dopen(group_id, DataSetName.c_str(), H5P_DEFAULT);
558
559 hid_t datatype_id = H5Dget_type(dataset_id);
560// size_t typesize_id = H5Tget_size(datatype_id);
561 H5T_class_t typeclass_id = H5Tget_class(datatype_id);
562
563 if(typeclass_id != H5T_STRING)
564 TEUCHOS_TEST_FOR_EXCEPTION(true,std::runtime_error, "requested group " + GroupName + " is not a std::string");
565
566 char data2[160];
567 H5Dread(dataset_id, datatype_id, H5S_ALL, H5S_ALL,H5P_DEFAULT, data2) ;
568
569 data = data2;
570
571 H5Dclose(dataset_id);
572 H5Gclose(group_id);
573}
574// ------------------------------------------
575template <class SC, class LO, class GO, class NO>
576void HDF5Toolbox<SC, LO, GO, NO>::open(const std::string FileName, int AccessType)
577{
578 TEUCHOS_TEST_FOR_EXCEPTION(isOpen(),std::runtime_error,"HDF5Toolbox:: no file open yet");
579
580 FileName_ = FileName;
581
582 // Set up file access property list with parallel I/O access
583 plist_id_ = H5Pcreate(H5P_FILE_ACCESS);
584
585#ifdef HAVE_MPI
586 MPI_Comm mpiComm = MPI_COMM_WORLD;
587
588 Teuchos::RCP<const Teuchos::MpiComm<int>> mpiWrapper =
589 Teuchos::rcp_dynamic_cast<const Teuchos::MpiComm<int>>(comm_, false);
590
591 if (!mpiWrapper.is_null()) {
592 mpiComm = *(mpiWrapper->getRawMpiComm());
593 }
594
595 H5Pset_fapl_mpio(plist_id_, mpiComm, MPI_INFO_NULL);
596#endif
597
598 // create the file collectively and release property list identifier.
599 file_id_ = H5Fopen(FileName.c_str(), AccessType, plist_id_);
600 H5Pclose(plist_id_);
601
602 isOpen_ = true;
603}
604
605
606// ----------------------------------------------
607template <class SC, class LO, class GO, class NO>
608void HDF5Toolbox<SC, LO, GO, NO>::tpetraScanSum(const Teuchos::RCP<const Teuchos::Comm<int>>& comm,
609 const GO* sendbuf,
610 GO* recvbuf,
611 int count)
612{
613#ifdef HAVE_MPI
614 // Try to downcast to MPI communicator
615 Teuchos::RCP<const Teuchos::MpiComm<int>> mpiComm =
616 Teuchos::rcp_dynamic_cast<const Teuchos::MpiComm<int>>(comm, false);
617
618 if (!mpiComm.is_null()) {
619 // Use MPI_Scan directly
620 MPI_Comm rawComm = *(mpiComm->getRawMpiComm());
621 MPI_Scan(sendbuf, recvbuf, count, MPI_INT, MPI_SUM, rawComm);
622 return;
623 }
624#endif
625
626 // Serial (or non-MPI) fallback
627 for (int i = 0; i < count; ++i) {
628 recvbuf[i] = sendbuf[i];
629 }
630}
631}
632#endif
void open(const std::string FileName, int AccessType=H5F_ACC_RDWR)
Open specified file with given access type.
Definition HDF5Toolbox_def.hpp:576
void create(const std::string FileName)
Create a new file.
Definition HDF5Toolbox_def.hpp:327
void createGroup(const std::string &GroupName)
Create group GroupName.
Definition HDF5Toolbox_def.hpp:441
bool isContained(std::string Name, std::string GroupName="")
Checking if the dataset Name is contained in the group GroupName.
Definition HDF5Toolbox_def.hpp:410
void write(const std::string &GroupName, const MultiVectorConstPtr_Type X, bool writeTranspose=false)
Write/export a vector X to the HDF5 file under the group name GroupName.
Definition HDF5Toolbox_def.hpp:35
bool isOpen() const
Return true if a file has already been opened using Open()/Create()
Definition HDF5Toolbox_decl.hpp:127
Definition Map_decl.hpp:30
Adaptive Mesh Refinement.
Definition AdaptiveMeshRefinement_decl.hpp:36
Definition HDF5Toolbox_def.hpp:12