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 }
H5File.hpp
H5DataSet.hpp
FILE_NAME
const std::string FILE_NAME("parallel_dataset_example.h5")
H5DataSpace.hpp
HighFive::SliceTraits::select
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
Definition: H5Slice_traits_misc.hpp:66
main
int main(int argc, char **argv)
Definition: parallel_hdf5_write_dataset.cpp:28
HighFive::File::Create
static const int Create
Open flag: Create non existing file.
Definition: H5File.hpp:40
DATASET_NAME
const std::string DATASET_NAME("dset")
HighFive::File
File class.
Definition: H5File.hpp:25
argc
int argc
Definition: tests_high_five_parallel.cpp:27
file
FILE * file
Definition: arithmeticencoder.cpp:77
HighFive::DataSpace
Definition: H5DataSpace.hpp:30
HighFive::Exception::what
const char * what() const override
get the current exception error message
Definition: H5Exception.hpp:34
HighFive::Exception
Basic HighFive Exception class.
Definition: H5Exception.hpp:23
HighFive::File::ReadWrite
static const int ReadWrite
Open flag: Read Write access.
Definition: H5File.hpp:32
HighFive::MPIOFileDriver
MPIIO Driver for Parallel HDF5.
Definition: H5FileDriver.hpp:29
argv
char ** argv
Definition: tests_high_five_parallel.cpp:28
HighFive::DataSet
Definition: H5DataSet.hpp:27
HighFive::File::Truncate
static const int Truncate
Open flag: Truncate a file if already existing.
Definition: H5File.hpp:34
HighFive
Definition: H5Annotate_traits.hpp:14


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 Wed Mar 2 2022 00:37:24