tests_high_five_parallel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c), 2017, Adrien Devresse <adrien.devresse@epfl.ch>
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 
10 #include <cstdlib>
11 #include <iostream>
12 #include <string>
13 #include <typeinfo>
14 #include <vector>
15 
16 #include <highfive/H5File.hpp>
17 #include <highfive/H5DataSet.hpp>
18 #include <highfive/H5DataSpace.hpp>
19 #include <highfive/H5Group.hpp>
20 
21 #define BOOST_TEST_MAIN HighFiveTest
22 #include <boost/mpl/list.hpp>
23 #include <boost/test/included/unit_test.hpp>
24 
25 using namespace HighFive;
26 
27 int argc = boost::unit_test::framework::master_test_suite().argc;
28 char** argv = boost::unit_test::framework::master_test_suite().argv;
29 
30 struct MpiFixture {
31  MpiFixture() { MPI_Init(&argc, &argv); }
32 
33  ~MpiFixture() { MPI_Finalize(); }
34 };
35 
37 
38 typedef boost::mpl::list<int, unsigned int, long, unsigned long, unsigned char,
39  char, float, double, long long, unsigned long long>
41 
42 template <typename T>
43 struct ContentGenerate {
44  ContentGenerate(T init_val = T(0), T inc_val = T(1) + T(1) / T(10))
45  : _init(init_val), _inc(inc_val) {}
46 
47  T operator()() {
48  T ret = _init;
49  _init += _inc;
50  return ret;
51  }
52 
53  T _init, _inc;
54 };
55 
56 template <>
57 struct ContentGenerate<char> {
58  ContentGenerate() : _init('a') {}
59 
60  char operator()() {
61  char ret = _init;
62  if (++_init >= ('a' + 26))
63  _init = 'a';
64  return ret;
65  }
66 
67  char _init;
68 };
69 
70 template <>
71 struct ContentGenerate<std::string> {
73 
74  std::string operator()() {
76  std::string random_string;
77  const size_t size_string = std::rand() % 1000;
78  random_string.resize(size_string);
79  std::generate(random_string.begin(), random_string.end(), gen);
80  return random_string;
81  }
82 };
83 
84 template <typename T>
86 
87  int mpi_rank, mpi_size;
88  MPI_Comm_size(MPI_COMM_WORLD, &mpi_size);
89  MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
90 
91  typedef typename std::vector<T> Vector;
92 
93  std::ostringstream filename;
94  filename << "h5_rw_select_parallel_test_" << typeid(T).name() << "_test.h5";
95 
96  const size_t size_x = mpi_size;
97  const size_t offset_x = mpi_rank, count_x = mpi_size - mpi_rank;
98 
99  const std::string DATASET_NAME("dset");
100 
101  Vector values(size_x);
102 
103  ContentGenerate<T> generator;
104  std::generate(values.begin(), values.end(), generator);
105 
106  // Create a new file using the default property lists.
108  MPIOFileDriver(MPI_COMM_WORLD, MPI_INFO_NULL));
109 
110  DataSet dataset =
111  file.createDataSet<T>(DATASET_NAME, DataSpace::From(values));
112 
113  dataset.write(values);
114 
115  file.flush();
116 
117  // read it back
118  Vector result;
119  std::vector<size_t> offset;
120  offset.push_back(offset_x);
121  std::vector<size_t> size;
122  size.push_back(count_x);
123 
124  Selection slice = dataset.select(offset, size);
125 
126  BOOST_CHECK_EQUAL(slice.getSpace().getDimensions()[0], size_x);
127  BOOST_CHECK_EQUAL(slice.getMemSpace().getDimensions()[0], count_x);
128 
129  slice.read(result);
130 
131  BOOST_CHECK_EQUAL(result.size(), count_x);
132 
133  for (size_t i = offset_x; i < count_x; ++i) {
134  // std::cout << result[i] << " ";
135  BOOST_CHECK_EQUAL(values[i + offset_x], result[i]);
136  }
137 }
138 
140 
141  selectionArraySimpleTestParallel<T>();
142 }
DataSpace getSpace() const
getSpace
void read(T &array) const
ContentGenerate(T init_val=T(0), T inc_val=T(1)+T(1)/T(10))
static DataSpace From(const ScalarValue &scalar_value)
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
BOOST_AUTO_TEST_CASE_TEMPLATE(selectionArraySimple, T, numerical_test_types)
DataSpace getMemSpace() const
getMemSpace
BOOST_GLOBAL_FIXTURE(MpiFixture)
FILE * file
boost::mpl::list< int, unsigned int, long, unsigned long, unsigned char, char, float, double, long long, unsigned long long > numerical_test_types
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
void selectionArraySimpleTestParallel()
Selection: represent a view on a slice/part of a dataset.
Definition: H5Selection.hpp:27
const size_t size_x
std::vector< size_t > getDimensions() const
getDimensions
boost::mpl::list< int, unsigned int, long, unsigned long, unsigned char, char, float, double, long long, unsigned long long, complex > numerical_test_types
char ** argv
static const int Create
Open flag: Create non existing file.
Definition: H5File.hpp:40
const std::string DATASET_NAME("dset")


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:09