2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012,2013,2014, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
35 /*! \libinternal \file
37 * Declares gmx::AnalysisDataStorage.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
41 * \ingroup module_analysisdata
43 #ifndef GMX_ANALYSISDATA_DATASTORAGE_H
44 #define GMX_ANALYSISDATA_DATASTORAGE_H
48 #include "gromacs/analysisdata/dataframe.h"
49 #include "gromacs/utility/common.h"
50 #include "gromacs/utility/gmxassert.h"
51 #include "gromacs/utility/real.h"
56 class AbstractAnalysisData;
57 class AnalysisDataFrameHeader;
58 class AnalysisDataFrameRef;
59 class AnalysisDataModuleManager;
60 class AnalysisDataParallelOptions;
62 class AnalysisDataStorage;
66 class AnalysisDataStorageImpl;
67 class AnalysisDataStorageFrameData;
68 } // namespace internal
70 /*! \libinternal \brief
71 * Allows assigning values for a data frame in AnalysisDataStorage.
73 * This class implements the necessary methods to add new data into the
74 * storage. AnalysisDataStorage::startFrame() returns an object of this type,
75 * which can be used to add one or more point sets to that data frame.
76 * When all data has been added, finishFrame() needs to be called.
79 * \ingroup module_analysisdata
81 class AnalysisDataStorageFrame
84 /*! \brief Frees the frame object.
86 * Should not be called outside AnalysisDataStorage.
88 ~AnalysisDataStorageFrame();
91 * Select data set that all other methods operate on.
93 * \param[in] index Zero-based data set index to select.
95 * With multipoint data, a single point set can only contain values in
97 * With non-multipoint data, arbitrary sequences of selectDataSet() and
98 * setValue() are supported. The full frame is notified to the modules
99 * once it is finished.
103 void selectDataSet(int index);
105 //! Returns number of columns for the frame.
106 int columnCount() const { return columnCount_; }
109 * Sets value for a column.
111 * \param[in] column Zero-based column index.
112 * \param[in] value Value to set for the column.
113 * \param[in] bPresent Present flag to set for the column.
115 * If called multiple times for a column (within one point set for
116 * multipoint data), old values are overwritten.
120 void setValue(int column, real value, bool bPresent = true)
122 GMX_ASSERT(column >= 0 && column < columnCount(),
123 "Invalid column index");
124 values_[currentOffset_ + column].setValue(value, bPresent);
125 bPointSetInProgress_ = true;
128 * Sets value for a column.
130 * \param[in] column Zero-based column index.
131 * \param[in] value Value to set for the column.
132 * \param[in] error Error estimate to set for the column.
133 * \param[in] bPresent Present flag to set for the column.
135 * If called multiple times for a column (within one point set for
136 * multipoint data), old values are overwritten.
140 void setValue(int column, real value, real error, bool bPresent = true)
142 GMX_ASSERT(column >= 0 && column < columnCount(),
143 "Invalid column index");
144 values_[currentOffset_ + column].setValue(value, error, bPresent);
145 bPointSetInProgress_ = true;
148 * Access value for a column.
150 * \param[in] column Zero-based column index.
152 * Should only be called after the column value has been set using
153 * setValue(); assigning a value to \c value(i) does not mark the
158 real &value(int column)
160 GMX_ASSERT(column >= 0 && column < columnCount(),
161 "Invalid column index");
162 return values_[currentOffset_ + column].value();
165 * Access value for a column.
167 * \param[in] column Zero-based column index.
169 * Should only be called after the column value has been set using
174 real value(int column) const
176 GMX_ASSERT(column >= 0 && column < columnCount(),
177 "Invalid column index");
178 return values_[currentOffset_ + column].value();
181 * Mark point set as finished for multipoint data.
183 * Must be called after each point set for multipoint data, including
184 * the last (i.e., no values must be set between the last call to this
185 * method and AnalysisDataStorage::finishFrame()).
186 * Must not be called for non-multipoint data.
188 * After this method has been called, all values appear as not set.
190 * May call AnalysisDataModuleManager::notifyPointsAdd() and
191 * AnalysisDataModuleManager::notifyParallelPointsAdd(), and may throw
192 * any exception these methods throw.
194 void finishPointSet();
196 * Finish storing a frame.
198 * Must be called exactly once for each frame returned by startFrame(),
199 * after the corresponding call.
200 * The frame object must not be accessed after the call.
202 * Calls notification methods in AnalysisDataModuleManager, and may
203 * throw any exceptions these methods throw.
210 * Create a new storage frame.
212 * \param[in] data Data object for which the frame is for
213 * (used for data set and column counts).
215 explicit AnalysisDataStorageFrame(const AbstractAnalysisData &data);
217 //! Clear all column values from the frame.
220 //! Implementation data.
221 internal::AnalysisDataStorageFrameData *data_;
222 //! Values for the currently in-progress point set.
223 std::vector<AnalysisDataValue> values_;
225 //! Index of the currently active dataset.
227 //! Offset of the first value in \a values_ for the current data set.
229 //! Number of columns in the current data set.
232 //! Whether any values have been set in the current point set.
233 bool bPointSetInProgress_;
235 //! Needed for access to the constructor.
236 friend class internal::AnalysisDataStorageImpl;
237 //! Needed for managing the frame the object points to.
238 friend class internal::AnalysisDataStorageFrameData;
240 GMX_DISALLOW_COPY_AND_ASSIGN(AnalysisDataStorageFrame);
243 /*! \libinternal \brief
244 * Helper class that implements storage of data.
246 * This class implements a standard way of storing data to avoid implementing
247 * storage in each class derived from AbstractAnalysisData separately.
248 * To use this class in a class derived from AbstractAnalysisData, a member
249 * variable of this type should be declared and the pure virtual methods
250 * forwarded to frameCount(), tryGetDataFrame() and requestStorage().
251 * Storage properties should be set up, and then startDataStorage() or
252 * startParallelDataStorage() called.
253 * New frames can then be added using startFrame(), currentFrame() and
254 * finishFrame() methods. When all frames are ready, finishDataStorage() must
255 * be called. These methods (and AnalysisDataStorageFrame::finishPointSet())
256 * take the responsibility of calling all the notification methods in
257 * AnalysisDataModuleManager,
260 * Proper multi-threaded implementation.
263 * \ingroup module_analysisdata
265 class AnalysisDataStorage
268 //! Constructs a storage object.
269 AnalysisDataStorage();
270 ~AnalysisDataStorage();
273 * Returns the number of ready frames.
275 * This method is designed such that calls to
276 * AbstractAnalysisData::frameCount() can be directly forwarded to this
277 * method. See that method for more documentation.
279 * If this method returns N, this means that the first N frames have
282 * \see AbstractAnalysisData::frameCount()
284 int frameCount() const;
286 * Implements access to data frames.
288 * This method is designed such that calls to
289 * AbstractAnalysisData::tryGetDataFrameInternal() can be directly
290 * forwarded to this method. See that method for more documentation.
292 * A valid reference for a frame will be returned after finishFrame()
293 * has been called for that frame.
295 * \see AbstractAnalysisData::tryGetDataFrameInternal()
297 AnalysisDataFrameRef tryGetDataFrame(int index) const;
299 * Implements storage requests.
301 * This method is designed such that calls to
302 * AbstractAnalysisData::requestStorageInternal() can be directly
303 * forwarded to this method. See that method for more documentation.
305 * \see AbstractAnalysisData::requestStorageInternal()
307 bool requestStorage(int nframes);
310 * Start storing data.
312 * \param[in] data AbstractAnalysisData object containing this
314 * \param modules Module manager for \p data.
315 * \exception std::bad_alloc if storage allocation fails.
317 * Typically called as \c startDataStorage(this, &moduleManager())
318 * from a member of \p data when the data is ready to be started.
319 * The storage object will take responsibility of calling all
320 * module notification methods in AnalysisDataModuleManager using
323 * Lifetime of \p data and \p modules must exceed the lifetime of the
325 * (typically, the storage object will be a member in \p data).
327 * Calls AnalysisDataModuleManager::notifyDataStart(), and throws any
328 * exceptions this method throws.
330 void startDataStorage(AbstractAnalysisData *data,
331 AnalysisDataModuleManager *modules);
333 * Start storing data in parallel.
335 * \param[in] data AbstractAnalysisData object containing this
337 * \param[in] options Parallelization options to use.
338 * \param modules Module manager for \p data.
339 * \exception std::bad_alloc if storage allocation fails.
341 * Should be called instead of startDataStorage() if the data will be
342 * produced in parallel. Works as startDataStorage(), but additionally
343 * initializes the storage and the attached modules to prepare for
344 * out-of-order data frames.
346 * Calls AnalysisDataModuleManager::notifyParallelDataStart(), and
347 * throws any exceptions this method throws.
349 void startParallelDataStorage(
350 AbstractAnalysisData *data,
351 AnalysisDataModuleManager *modules,
352 const AnalysisDataParallelOptions &options);
354 * Starts storing a new frame.
356 * \param[in] header Header for the new frame.
357 * \retval Frame object corresponding to the started frame.
358 * \exception std::bad_alloc if storage reallocation fails
359 * (only possible if storage of all frames has been requested).
360 * \exception APIError if frame is too far in the future.
362 * The returned object will be valid until the corresponding
363 * finishFrame() call.
365 * Must be called exactly once for each frame index.
367 * Currently, the implementation only works if the new frame is not too
369 * If \c i is the index of the last frame such that all frames from
370 * 0, ..., \c i have been finished, then \p header().index() should be
371 * at most \c 2*parallelizationFactor-1 larger than \c i, where
372 * parallelizationFactor is the parallelization factor passed to
373 * setParallelOptions().
374 * Throws APIError if this constraint is violated.
376 * Calls AnalysisDataModuleManager::notifyFrameStart() (in certain
377 * cases) and AnalysisDataModuleManager::notifyParallelFrameStart(),
378 * and throws any exceptions these methods throw.
380 AnalysisDataStorageFrame &startFrame(const AnalysisDataFrameHeader &header);
382 * Convenience method to start storing a new frame.
384 * Identical to \c startFrame(AnalysisDataFrameHeader(index, x, dx));
386 AnalysisDataStorageFrame &startFrame(int index, real x, real dx);
388 * Obtain a frame object for an in-progress frame.
390 * \param[in] index Frame index.
391 * \retval Frame object corresponding to \p index.
393 * startFrame() should have been called for the frame with index
394 * \p index, and finishFrame() should not yet have been called.
395 * Returns the same object as returned by the original startFrame()
396 * call for the same index.
400 AnalysisDataStorageFrame ¤tFrame(int index);
402 * Convenience method for finishing a data frame.
404 * \param[in] index Frame index.
406 * Identical to \c currentFrame(index).finishFrame().
408 * \see AnalysisDataStorageFrame::finishFrame()
410 void finishFrame(int index);
412 * Finishes storing data.
414 * Calls AnalysisDataModuleManager::notifyDataFinish(), and throws any
415 * exceptions this method throws.
417 void finishDataStorage();
420 typedef internal::AnalysisDataStorageImpl Impl;
422 PrivateImplPointer<Impl> impl_;