votca 2024-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// Third party includes
30#include <H5Cpp.h>
31
32// Local VOTCA includes
33#include "checkpoint_utils.h"
34
35#define CPT_MEM_FROM_STRUCT(m, s) HOFFSET(s, m)
36#define CPT_MEMBER(m, s) HOFFSET(s, m)
37
38namespace votca {
39namespace xtp {
40using namespace checkpoint_utils;
41
42class CptTable {
43 public:
44 CptTable() = default;
45 CptTable(const std::string& name, const std::size_t& rowSize,
46 const std::size_t& nRows)
47 : name_(name),
48 rowStructure_(rowSize),
49 nRows_(nRows),
50 props_(H5::DSetCreatPropList(H5::DSetCreatPropList::DEFAULT)){};
51
52 CptTable(const std::string& name, const std::size_t& rowSize,
53 const CptLoc& loc)
54 : name_(name), loc_(loc), inited_(true), rowStructure_(rowSize) {
55
56 dataset_ = loc_.openDataSet(name_);
57 dp_ = dataset_.getSpace();
58 hsize_t dims[2];
59 dp_.getSimpleExtentDims(dims, nullptr);
60 nRows_ = dims[0];
61 }
62
63 template <typename U>
64 void addCol(const std::string& name, const size_t& offset);
65
66 void initialize(const CptLoc& loc, bool compact) {
67 // create the dataspace...
68 if (inited_) {
69 std::stringstream message;
70
71 message << "Checkpoint tables cannot be reinitialized. " << name_
72 << " has either already been initialized or already exists."
73 << std::endl;
74
75 throw std::runtime_error(message.str());
76 }
77 dims_[0] = nRows_;
78 dims_[1] = 1;
79
80 dp_ = H5::DataSpace(2, dims_);
81 loc_ = loc;
82
83 if (compact) {
84 props_.setLayout(H5D_layout_t::H5D_COMPACT);
85 }
86
87 try {
88 dataset_ = loc_.createDataSet(name_.c_str(), rowStructure_, dp_, props_);
89 } catch (H5::Exception&) {
90 std::stringstream message;
91 message << "Could not write " << name_ << " from " << loc_.getFileName();
92 throw std::runtime_error(message.str());
93 }
94
95 inited_ = true;
96 }
97
98 void write(void* buffer, const std::size_t& startIdx,
99 const std::size_t& endIdx) {
100
101 if (!inited_) {
102 std::stringstream message;
103 message << "Checkpoint table uninitialized." << std::endl;
104 throw std::runtime_error(message.str());
105 }
106
107 hsize_t s = (hsize_t)(startIdx);
108 hsize_t e = (hsize_t)(endIdx);
109 hsize_t l = e - s;
110
111 hsize_t fStart[2] = {s, 0};
112 hsize_t fCount[2] = {l, 1};
113
114 hsize_t mStart[2] = {s, 0};
115 hsize_t mCount[2] = {l, 1};
116
117 hsize_t mDim[2] = {l, 1};
118
119 H5::DataSpace mspace(2, mDim);
120
121 dp_.selectHyperslab(H5S_SELECT_SET, fCount, fStart);
122 mspace.selectHyperslab(H5S_SELECT_SET, mCount, mStart);
123 try {
124 dataset_.write(buffer, rowStructure_, mspace, dp_);
125 } catch (H5::Exception&) {
126 std::stringstream message;
127 message << "Could not write " << name_ << " from " << loc_.getFileName();
128 throw std::runtime_error(message.str());
129 }
130 }
131
132 void writeToRow(void* buffer, const std::size_t idx) {
133 write(buffer, idx, idx + 1);
134 }
135
136 template <typename T>
137 void write(std::vector<T>& dataVec) {
138 write(dataVec.data(), 0, dataVec.size());
139 }
140
141 void read(void* buffer, const std::size_t& startIdx,
142 const std::size_t& endIdx) {
143
144 if (!inited_) {
145 std::stringstream message;
146 message << "Checkpoint table uninitialized." << std::endl;
147 throw std::runtime_error(message.str());
148 }
149
150 hsize_t s = (hsize_t)(startIdx);
151 hsize_t e = (hsize_t)(endIdx);
152 hsize_t l = e - s;
153
154 hsize_t fStart[2] = {s, 0};
155 hsize_t fCount[2] = {l, 1};
156
157 hsize_t mStart[2] = {s, 0};
158 hsize_t mCount[2] = {l, 1};
159
160 hsize_t mDim[2] = {l, 1};
161
162 H5::DataSpace mspace(2, mDim);
163
164 dp_.selectHyperslab(H5S_SELECT_SET, fCount, fStart);
165 mspace.selectHyperslab(H5S_SELECT_SET, mCount, mStart);
166 try {
167 dataset_.read(buffer, rowStructure_, mspace, dp_);
168 } catch (H5::Exception&) {
169 std::stringstream message;
170 message << "Could not read " << name_ << " from " << loc_.getFileName();
171 throw std::runtime_error(message.str());
172 }
173 }
174
175 void readFromRow(void* buffer, const std::size_t& idx) {
176 read(buffer, idx, idx + 1);
177 }
178
179 template <typename T>
180 void read(std::vector<T>& dataVec) {
181 read(dataVec.data(), 0, dataVec.size());
182 }
183
184 std::size_t numRows() { return nRows_; }
185
186 static const std::size_t MaxStringSize = 512;
187
188 private:
189 std::string name_;
191 bool inited_ = false;
192 H5::CompType rowStructure_;
193 std::size_t nRows_;
194 hsize_t dims_[2];
195 H5::DataSpace dp_;
196 H5::DataSet dataset_;
197 H5::DSetCreatPropList props_;
198};
199
200template <typename U>
201inline void CptTable::addCol(const std::string& name, const size_t& offset) {
202 static_assert(
203 std::is_fundamental<U>::value,
204 "Columns can only be added for fundamental types and 'const char*'");
205 rowStructure_.insertMember(name, offset, *InferDataType<U>::get());
206}
207
208template <>
209inline void CptTable::addCol<std::string>(const std::string& name,
210 const size_t& offset) {
211 rowStructure_.insertMember(name, offset, *InferDataType<std::string>::get());
212}
213
214} // namespace xtp
215} // namespace votca
216#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