votca 2025.1-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 std::lock_guard<std::recursive_mutex> lock(checkpoint_utils::Hdf5Mutex());
62
63 dataset_ = loc_.openDataSet(name_);
64 dp_ = dataset_.getSpace();
65 hsize_t dims[2];
66 dp_.getSimpleExtentDims(dims, nullptr);
67 nRows_ = dims[0];
68 }
69
70 template <typename U>
71 void addCol(const std::string& name, const size_t& offset);
72
73 void initialize(const CptLoc& loc, bool compact) {
74 std::lock_guard<std::recursive_mutex> lock(checkpoint_utils::Hdf5Mutex());
75
76 // create the dataspace...
77 if (inited_) {
78 std::stringstream message;
79
80 message << "Checkpoint tables cannot be reinitialized. " << name_
81 << " has either already been initialized or already exists."
82 << std::endl;
83
84 throw std::runtime_error(message.str());
85 }
86 dims_[0] = nRows_;
87 dims_[1] = 1;
88
89 dp_ = H5::DataSpace(2, dims_);
90 loc_ = loc;
91
92 if (compact) {
93 props_.setLayout(H5D_layout_t::H5D_COMPACT);
94 }
95
96 try {
97 dataset_ = loc_.createDataSet(name_.c_str(), rowStructure_, dp_, props_);
98 } catch (H5::Exception&) {
99 std::stringstream message;
100 message << "Could not write " << name_ << " from " << loc_.getFileName();
101 throw std::runtime_error(message.str());
102 }
103
104 inited_ = true;
105 }
106
107 void write(void* buffer, const std::size_t& startIdx,
108 const std::size_t& endIdx) {
109 std::lock_guard<std::recursive_mutex> lock(checkpoint_utils::Hdf5Mutex());
110
111 if (!inited_) {
112 std::stringstream message;
113 message << "Checkpoint table uninitialized." << std::endl;
114 throw std::runtime_error(message.str());
115 }
116
117 hsize_t s = (hsize_t)(startIdx);
118 hsize_t e = (hsize_t)(endIdx);
119 hsize_t l = e - s;
120
121 hsize_t fStart[2] = {s, 0};
122 hsize_t fCount[2] = {l, 1};
123
124 hsize_t mStart[2] = {s, 0};
125 hsize_t mCount[2] = {l, 1};
126
127 hsize_t mDim[2] = {l, 1};
128
129 H5::DataSpace mspace(2, mDim);
130
131 dp_.selectHyperslab(H5S_SELECT_SET, fCount, fStart);
132 mspace.selectHyperslab(H5S_SELECT_SET, mCount, mStart);
133 try {
134 dataset_.write(buffer, rowStructure_, mspace, dp_);
135 } catch (H5::Exception&) {
136 std::stringstream message;
137 message << "Could not write " << name_ << " from " << loc_.getFileName();
138 throw std::runtime_error(message.str());
139 }
140 }
141
142 void writeToRow(void* buffer, const std::size_t idx) {
143 write(buffer, idx, idx + 1);
144 }
145
146 template <typename T>
147 void write(std::vector<T>& dataVec) {
148 write(dataVec.data(), 0, dataVec.size());
149 }
150
151 void read(void* buffer, const std::size_t& startIdx,
152 const std::size_t& endIdx) {
153 std::lock_guard<std::recursive_mutex> lock(checkpoint_utils::Hdf5Mutex());
154
155 if (!inited_) {
156 std::stringstream message;
157 message << "Checkpoint table uninitialized." << std::endl;
158 throw std::runtime_error(message.str());
159 }
160
161 hsize_t s = (hsize_t)(startIdx);
162 hsize_t e = (hsize_t)(endIdx);
163 hsize_t l = e - s;
164
165 hsize_t fStart[2] = {s, 0};
166 hsize_t fCount[2] = {l, 1};
167
168 hsize_t mStart[2] = {s, 0};
169 hsize_t mCount[2] = {l, 1};
170
171 hsize_t mDim[2] = {l, 1};
172
173 H5::DataSpace mspace(2, mDim);
174
175 dp_.selectHyperslab(H5S_SELECT_SET, fCount, fStart);
176 mspace.selectHyperslab(H5S_SELECT_SET, mCount, mStart);
177 try {
178 dataset_.read(buffer, rowStructure_, mspace, dp_);
179 } catch (H5::Exception&) {
180 std::stringstream message;
181 message << "Could not read " << name_ << " from " << loc_.getFileName();
182 throw std::runtime_error(message.str());
183 }
184 }
185
186 void readFromRow(void* buffer, const std::size_t& idx) {
187 read(buffer, idx, idx + 1);
188 }
189
190 template <typename T>
191 void read(std::vector<T>& dataVec) {
192 read(dataVec.data(), 0, dataVec.size());
193 }
194
195 std::size_t numRows() { return nRows_; }
196
197 static const std::size_t MaxStringSize = 512;
198
199 private:
200 std::string name_;
202 bool inited_ = false;
203 H5::CompType rowStructure_;
204 std::size_t nRows_;
205 hsize_t dims_[2];
206 H5::DataSpace dp_;
207 H5::DataSet dataset_;
208 H5::DSetCreatPropList props_;
209};
210
211template <typename U>
212inline void CptTable::addCol(const std::string& name, const size_t& offset) {
213 static_assert(
214 std::is_fundamental<U>::value,
215 "Columns can only be added for fundamental types and 'const char*'");
216 rowStructure_.insertMember(name, offset, *InferDataType<U>::get());
217}
218
219template <>
220inline void CptTable::addCol<std::string>(const std::string& name,
221 const size_t& offset) {
222 rowStructure_.insertMember(name, offset, *InferDataType<std::string>::get());
223}
224
225} // namespace xtp
226} // namespace votca
227
228#if defined(__clang__)
229#elif defined(__GNUC__)
230#pragma GCC diagnostic pop
231#endif
232
233#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)
std::recursive_mutex & Hdf5Mutex()
H5::Group CptLoc
Provides a means for comparing floating point numbers.
Definition basebead.h:33