parallel_hdf5_write_dataset.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c), 2017, Adrien Devresse
3  *
4  * Distributed under the Boost Software License, Version 1.0.
5  * (See accompanying file LICENSE_1_0.txt or copy at
6  * http://www.boost.org/LICENSE_1_0.txt)
7  *
8  */
9 #include <iostream>
10 #include <string>
11 #include <vector>
12 
13 #include <mpi.h>
14 
15 #include <highfive/H5DataSet.hpp>
16 #include <highfive/H5DataSpace.hpp>
17 #include <highfive/H5File.hpp>
18 
19 const std::string FILE_NAME("parallel_dataset_example.h5");
20 const std::string DATASET_NAME("dset");
21 
22 //
23 // simple example to write a dataset with Parallel HDF5 with MPI-IO
24 //
25 // The dataset is written from several MPI node in parallel
26 //
27 //
28 int main(int argc, char** argv) {
29  int mpi_rank, mpi_size;
30 
31  // initialize MPI
32  MPI_Init(&argc, &argv);
33  MPI_Comm_size(MPI_COMM_WORLD, &mpi_size);
34  MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
35 
36  using namespace HighFive;
37  try {
38 
39  // open a new file with the MPI IO driver for parallel Read/Write
41  MPIOFileDriver(MPI_COMM_WORLD, MPI_INFO_NULL));
42 
43  // we define the size of our dataset to
44  // lines : total number of mpi_rank
45  // columns : 2
46  std::vector<size_t> dims(2);
47  dims[0] = std::size_t(mpi_size);
48  dims[1] = 2;
49 
50  // Create the dataset
51  DataSet dataset =
52  file.createDataSet<double>(DATASET_NAME, DataSpace(dims));
53 
54  // Each node want to write its own rank two time in
55  // its associated row
56  int data[1][2] = {{mpi_rank, mpi_rank}};
57 
58  // write it to the associated mpi_rank
59  dataset.select({std::size_t(mpi_rank), 0}, {1, 2}).write(data);
60 
61  } catch (Exception& err) {
62  // catch and print any HDF5 error
63  std::cerr << err.what() << std::endl;
64  MPI_Abort(MPI_COMM_WORLD, 1);
65  }
66 
67  MPI_Finalize();
68  return 0; // successfully terminated
69 }
int main(int argc, char **argv)
static const int ReadWrite
Open flag: Read Write access.
Definition: H5File.hpp:32
static const int Truncate
Open flag: Truncate a file if already existing.
Definition: H5File.hpp:34
MPIIO Driver for Parallel HDF5.
File class.
Definition: H5File.hpp:25
const std::string DATASET_NAME("dset")
const char * what() const override
get the current exception error message
Definition: H5Exception.hpp:34
FILE * file
Selection select(const std::vector< size_t > &offset, const std::vector< size_t > &count, const std::vector< size_t > &stride=std::vector< size_t >()) const
Basic HighFive Exception class.
Definition: H5Exception.hpp:23
const std::string FILE_NAME("parallel_dataset_example.h5")
DataSet createDataSet(const std::string &dataset_name, const DataSpace &space, const DataType &type, const DataSetCreateProps &createProps=DataSetCreateProps(), const DataSetAccessProps &accessProps=DataSetAccessProps())
createDataSet Create a new dataset in the current file of datatype type and of size space ...
char ** argv
static const int Create
Open flag: Create non existing file.
Definition: H5File.hpp:40


lvr2
Author(s): Thomas Wiemann , Sebastian Pütz , Alexander Mock , Lars Kiesow , Lukas Kalbertodt , Tristan Igelbrink , Johan M. von Behren , Dominik Feldschnieders , Alexander Löhr
autogenerated on Mon Feb 28 2022 22:46:08