QMCPACK
test_hdf_hyperslab.cpp
Go to the documentation of this file.
1 
2 //////////////////////////////////////////////////////////////////////////////////////
3 // This file is distributed under the University of Illinois/NCSA Open Source License.
4 // See LICENSE file in top directory for details.
5 //
6 // Copyright (c) 2018 Jeongnim Kim and QMCPACK developers.
7 //
8 // File developed by: Luke Shulenburger, lshulen@sandia.gov, Sandia National Laboratories
9 //
10 // File created by: Luke Shulenburger, lshulen@sandia.gov, Sandia National Laboratories
11 //////////////////////////////////////////////////////////////////////////////////////
12 
13 
14 #include "catch.hpp"
15 #include <iostream>
16 #include <vector>
17 #include <complex>
18 #include "OhmmsPETE/OhmmsVector.h"
19 #include "OhmmsPETE/OhmmsMatrix.h"
20 #include "OhmmsPETE/OhmmsArray.h"
22 #include "hdf/hdf_archive.h"
23 
24 using namespace qmcplusplus;
25 
26 // do this in two different ways.
27 // 1. directly create a hyperslab_proxy
28 // 2. use readSlabSelection which will create the hyperslab_proxy for you
29 TEST_CASE("hdf_read_partial", "[hdf]")
30 {
31  hdf_archive hd;
32  bool okay = hd.create("test_read_partial.hdf");
33  REQUIRE(okay);
34 
35  Matrix<double> allData(3, 4);
36  Matrix<std::complex<float>> allData_cplx(3, 4);
37  for (int i = 0; i < 3; i++)
38  {
39  for (int j = 0; j < 4; j++)
40  {
41  allData(i, j) = i + j * 0.1;
42  allData_cplx(i, j) = std::complex<float>(i, j * 0.1);
43  }
44  }
45 
46  hd.write(allData, "matrix");
47 
48  const auto& const_allData_cplx = allData_cplx;
49  hd.write(const_allData_cplx, "matrix_cplx_float");
50  hd.close();
51 
52  hdf_archive hd2;
53  okay = hd2.open("test_read_partial.hdf");
54  REQUIRE(okay);
55 
56  // test getShape
57  std::vector<int> datashape;
58  okay = hd2.getShape<double>("matrix", datashape);
59  REQUIRE(okay);
60  REQUIRE(datashape.size() == 2);
61  REQUIRE(datashape[0] == 3);
62  REQUIRE(datashape[1] == 4);
63 
64  okay = hd2.getShape<std::complex<float>>("matrix_cplx_float", datashape);
65  REQUIRE(okay);
66  REQUIRE(datashape.size() == 2);
67  REQUIRE(datashape[0] == 3);
68  REQUIRE(datashape[1] == 4);
69 
70  //treat std::complex<float> as an array[2]
71  okay = hd2.getShape<float>("matrix_cplx_float", datashape);
72  REQUIRE(okay);
73  REQUIRE(datashape.size() == 3);
74  REQUIRE(datashape[0] == 3);
75  REQUIRE(datashape[1] == 4);
76  REQUIRE(datashape[2] == 2);
77 
78  // method 1 (direct utilization of hyperslab_proxy in client code)
79  Matrix<double> outbuffer1(1, 4);
80  Matrix<double> outbuffer2(3, 1);
81  Matrix<double> outbuffer3(1, 1);
82  Matrix<double> outbuffer4;
83 
84  std::array<size_t, 2> dims_unused;
85  dims_unused[0] = 3;
86  dims_unused[1] = 4;
87  std::array<size_t, 2> dims_local;
88  dims_local[0] = 1;
89  dims_local[1] = 4;
90  std::array<size_t, 2> offsets;
91  offsets[0] = 1;
92  offsets[1] = 0;
93 
94  hyperslab_proxy<Matrix<double>, 2> pxy1(outbuffer1, dims_unused, dims_local, offsets);
95  hd2.read(pxy1, "matrix");
96  for (int i = 0; i < 4; i++)
97  {
98  CHECK(outbuffer1(0, i) == Approx(allData(1, i)));
99  }
100 
101  dims_local[0] = 3;
102  dims_local[1] = 1;
103  offsets[0] = 0;
104  offsets[1] = 2;
105  hyperslab_proxy<Matrix<double>, 2> pxy2(outbuffer2, dims_unused, dims_local, offsets);
106  hd2.read(pxy2, "matrix");
107  for (int i = 0; i < 3; i++)
108  CHECK(outbuffer2(i, 0) == Approx(allData(i, 2)));
109 
110  // set dims_unused to sero and to be detect
111  dims_unused[0] = 0;
112  dims_unused[1] = 0;
113 
114  dims_local[0] = 1;
115  dims_local[1] = 1;
116  offsets[0] = 2;
117  offsets[1] = 0;
118  hyperslab_proxy<Matrix<double>, 2> pxy3(outbuffer3, dims_unused, dims_local, offsets);
119  hd2.read(pxy3, "matrix");
120  CHECK(outbuffer3(0, 0) == Approx(allData(2, 0)));
121 
122  // mostly the same as outbuffer2 but outbuffer4 resized by hyperslab_proxy
123  dims_local[0] = 3;
124  dims_local[1] = 1;
125  offsets[0] = 0;
126  offsets[1] = 2;
127  hyperslab_proxy<Matrix<double>, 2> pxy4(outbuffer4, dims_unused, dims_local, offsets);
128  hd2.read(pxy4, "matrix");
129  REQUIRE(outbuffer4.rows() == 3);
130  REQUIRE(outbuffer4.cols() == 1);
131  for (int i = 0; i < 3; i++)
132  CHECK(outbuffer2(i, 0) == Approx(allData(i, 2)));
133 
134  // method 2 here
135  std::vector<double> locob1;
136  Matrix<double> locob2(3, 1);
137  Matrix<double> locob3(1, 1);
138  std::array<int, 2> readSpec{1, -1};
139  hd2.readSlabSelection(locob1, readSpec, "matrix");
140  REQUIRE(locob1.size() == 4);
141  for (int i = 0; i < 4; i++)
142  {
143  CHECK(locob1[i] == Approx(allData(1, i)));
144  }
145 
146  readSpec[0] = -1;
147  readSpec[1] = 2;
148  hd2.readSlabSelection(locob2, readSpec, "matrix");
149  for (int i = 0; i < 3; i++)
150  {
151  CHECK(locob2.data()[i] == Approx(allData(i, 2)));
152  CHECK(locob2(i) == Approx(allData(i, 2)));
153  }
154 
155  readSpec[0] = 2;
156  readSpec[1] = 0;
157  hd2.readSlabSelection(locob3, readSpec, "matrix");
158  CHECK(locob3.data()[0] == Approx(allData(2, 0)));
159  hd2.close();
160 }
void write(T &data, const std::string &aname)
write the data to the group aname and check status runtime error is issued on I/O error ...
Definition: hdf_archive.h:259
bool open(const std::filesystem::path &fname, unsigned flags=H5F_ACC_RDWR)
open a file
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
class to use file space hyperslab with a serialized container
Definition: hdf_hyperslab.h:35
TEST_CASE("complex_helper", "[type_traits]")
void close()
close all the open groups and file
Definition: hdf_archive.cpp:38
class to handle hdf file
Definition: hdf_archive.h:51
size_type cols() const
Definition: OhmmsMatrix.h:78
void readSlabSelection(T &data, const std::array< IT, RANK > &readSpec, const std::string &aname)
read a portion of the data from the group aname and check status runtime error is issued on I/O error...
Definition: hdf_archive.h:345
bool getShape(const std::string &aname, std::vector< int > &sizes_out)
read the shape of multidimensional filespace from the group aname this function can be used to query ...
Definition: hdf_archive.h:231
REQUIRE(std::filesystem::exists(filename))
size_type rows() const
Definition: OhmmsMatrix.h:77
Declaraton of Vector<T,Alloc> Manage memory through Alloc directly and allow referencing an existing ...
bool create(const std::filesystem::path &fname, unsigned flags=H5F_ACC_TRUNC)
create a file
CHECK(log_values[0]==ComplexApprox(std::complex< double >{ 5.603777579195571, -6.1586603331188225 }))
void read(T &data, const std::string &aname)
read the data from the group aname and check status runtime error is issued on I/O error ...
Definition: hdf_archive.h:306