votca 2024.2-dev
Loading...
Searching...
No Matches
checkpointtable.h
Go to the documentation of this file.
1/*
2 * Copyright 2009-2020 The VOTCA Development Team (http://www.votca.org)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 *
15 */
16#pragma once
17#ifndef VOTCA_XTP_CHECKPOINTTABLE_H
18#define VOTCA_XTP_CHECKPOINTTABLE_H
19
20// Standard includes
21#include <cstddef>
22#include <cstring>
23#include <iostream>
24#include <sstream>
25#include <stdexcept>
26#include <string>
27#include <vector>
28
29#if defined(__clang__)
30#elif defined(__GNUC__)
31#pragma GCC diagnostic push
32#pragma GCC diagnostic ignored "-Wdeprecated-copy"
33#endif
34
35// Third party includes
36#include <H5Cpp.h>
37
38// Local VOTCA includes
39#include "checkpoint_utils.h"
40
41#define CPT_MEM_FROM_STRUCT(m, s) HOFFSET(s, m)
42#define CPT_MEMBER(m, s) HOFFSET(s, m)
43
44namespace votca {
45namespace xtp {
46using namespace checkpoint_utils;
47
48class CptTable {
49 public:
50 CptTable() = default;
51 CptTable(const std::string& name, const std::size_t& rowSize,
52 const std::size_t& nRows)
53 : name_(name),
54 rowStructure_(rowSize),
55 nRows_(nRows),
56 props_(H5::DSetCreatPropList(H5::DSetCreatPropList::DEFAULT)) {};
57
58 CptTable(const std::string& name, const std::size_t& rowSize,
59 const CptLoc& loc)
60 : name_(name), loc_(loc), inited_(true), rowStructure_(rowSize) {
61 dataset_ = loc_.openDataSet(name_);
62 dp_ = dataset_.getSpace();
63 hsize_t dims[2];
64 dp_.getSimpleExtentDims(dims, nullptr);
65 nRows_ = dims[0];
66 }
67
68 template <typename U>
69 void addCol(const std::string& name, const size_t& offset);
70
71 void initialize(const CptLoc& loc, bool compact) {
72 // create the dataspace...
73 if (inited_) {
74 std::stringstream message;
75
76 message << "Checkpoint tables cannot be reinitialized. " << name_
77 << " has either already been initialized or already exists."
78 << std::endl;
79
80 throw std::runtime_error(message.str());
81 }
82 dims_[0] = nRows_;
83 dims_[1] = 1;
84
85 dp_ = H5::DataSpace(2, dims_);
86 loc_ = loc;
87
88 if (compact) {
89 props_.setLayout(H5D_layout_t::H5D_COMPACT);
90 }
91
92 try {
93 dataset_ = loc_.createDataSet(name_.c_str(), rowStructure_, dp_, props_);
94 } catch (H5::Exception&) {
95 std::stringstream message;
96 message << "Could not write " << name_ << " from " << loc_.getFileName();
97 throw std::runtime_error(message.str());
98 }
99
100 inited_ = true;
101 }
102
103 void write(void* buffer, const std::size_t& startIdx,
104 const std::size_t& endIdx) {
105
106 if (!inited_) {
107 std::stringstream message;
108 message << "Checkpoint table uninitialized." << std::endl;
109 throw std::runtime_error(message.str());
110 }
111
112 hsize_t s = (hsize_t)(startIdx);
113 hsize_t e = (hsize_t)(endIdx);
114 hsize_t l = e - s;
115
116 hsize_t fStart[2] = {s, 0};
117 hsize_t fCount[2] = {l, 1};
118
119 hsize_t mStart[2] = {s, 0};
120 hsize_t mCount[2] = {l, 1};
121
122 hsize_t mDim[2] = {l, 1};
123
124 H5::DataSpace mspace(2, mDim);
125
126 dp_.selectHyperslab(H5S_SELECT_SET, fCount, fStart);
127 mspace.selectHyperslab(H5S_SELECT_SET, mCount, mStart);
128 try {
129 dataset_.write(buffer, rowStructure_, mspace, dp_);
130 } catch (H5::Exception&) {
131 std::stringstream message;
132 message << "Could not write " << name_ << " from " << loc_.getFileName();
133 throw std::runtime_error(message.str());
134 }
135 }
136
137 void writeToRow(void* buffer, const std::size_t idx) {
138 write(buffer, idx, idx + 1);
139 }
140
141 template <typename T>
142 void write(std::vector<T>& dataVec) {
143 write(dataVec.data(), 0, dataVec.size());
144 }
145
146 void read(void* buffer, const std::size_t& startIdx,
147 const std::size_t& endIdx) {
148
149 if (!inited_) {
150 std::stringstream message;
151 message << "Checkpoint table uninitialized." << std::endl;
152 throw std::runtime_error(message.str());
153 }
154
155 hsize_t s = (hsize_t)(startIdx);
156 hsize_t e = (hsize_t)(endIdx);
157 hsize_t l = e - s;
158
159 hsize_t fStart[2] = {s, 0};
160 hsize_t fCount[2] = {l, 1};
161
162 hsize_t mStart[2] = {s, 0};
163 hsize_t mCount[2] = {l, 1};
164
165 hsize_t mDim[2] = {l, 1};
166
167 H5::DataSpace mspace(2, mDim);
168
169 dp_.selectHyperslab(H5S_SELECT_SET, fCount, fStart);
170 mspace.selectHyperslab(H5S_SELECT_SET, mCount, mStart);
171 try {
172 dataset_.read(buffer, rowStructure_, mspace, dp_);
173 } catch (H5::Exception&) {
174 std::stringstream message;
175 message << "Could not read " << name_ << " from " << loc_.getFileName();
176 throw std::runtime_error(message.str());
177 }
178 }
179
180 void readFromRow(void* buffer, const std::size_t& idx) {
181 read(buffer, idx, idx + 1);
182 }
183
184 template <typename T>
185 void read(std::vector<T>& dataVec) {
186 read(dataVec.data(), 0, dataVec.size());
187 }
188
189 std::size_t numRows() { return nRows_; }
190
191 static const std::size_t MaxStringSize = 512;
192
193 private:
194 std::string name_;
196 bool inited_ = false;
197 H5::CompType rowStructure_;
198 std::size_t nRows_;
199 hsize_t dims_[2];
200 H5::DataSpace dp_;
201 H5::DataSet dataset_;
202 H5::DSetCreatPropList props_;
203};
204
205template <typename U>
206inline void CptTable::addCol(const std::string& name, const size_t& offset) {
207 static_assert(
208 std::is_fundamental<U>::value,
209 "Columns can only be added for fundamental types and 'const char*'");
210 rowStructure_.insertMember(name, offset, *InferDataType<U>::get());
211}
212
213template <>
214inline void CptTable::addCol<std::string>(const std::string& name,
215 const size_t& offset) {
216 rowStructure_.insertMember(name, offset, *InferDataType<std::string>::get());
217}
218
219} // namespace xtp
220} // namespace votca
221
222#if defined(__clang__)
223#elif defined(__GNUC__)
224#pragma GCC diagnostic pop
225#endif
226
227#endif // VOTCA_XTP_CHECKPOINTTABLE_H
H5::DSetCreatPropList props_
static const std::size_t MaxStringSize
void write(void *buffer, const std::size_t &startIdx, const std::size_t &endIdx)
void initialize(const CptLoc &loc, bool compact)
void readFromRow(void *buffer, const std::size_t &idx)
CptTable(const std::string &name, const std::size_t &rowSize, const CptLoc &loc)
void write(std::vector< T > &dataVec)
void read(std::vector< T > &dataVec)
CptTable(const std::string &name, const std::size_t &rowSize, const std::size_t &nRows)
void writeToRow(void *buffer, const std::size_t idx)
void read(void *buffer, const std::size_t &startIdx, const std::size_t &endIdx)
void addCol(const std::string &name, const size_t &offset)
H5::Group CptLoc
base class for all analysis tools
Definition basebead.h:33