More precise analysisdata histogram accumulation
[alexxy/gromacs.git] / src / gromacs / analysisdata / framelocaldata.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 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.
8  *
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.
13  *
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.
18  *
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.
23  *
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.
31  *
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.
34  */
35 /*! \internal \file
36  * \brief
37  * Defines gmx::AnalysisDataFrameLocalData and supporting types.
38  *
39  * \author Teemu Murtola <teemu.murtola@gmail.com>
40  * \ingroup module_analysisdata
41  */
42 #ifndef GMX_ANALYSISDATA_FRAMELOCALDATA_H
43 #define GMX_ANALYSISDATA_FRAMELOCALDATA_H
44
45 #include <algorithm>
46 #include <numeric>
47 #include <vector>
48
49 #include "gromacs/analysisdata/paralleloptions.h"
50 #include "gromacs/utility/arrayref.h"
51 #include "gromacs/utility/gmxassert.h"
52
53 namespace gmx
54 {
55
56 //! \addtogroup module_analysisdata
57 //! \{
58
59 /*! \internal
60  * \brief
61  * Handle to a single data set within frame-local data array.
62  *
63  * Methods in this class do not throw.
64  *
65  * \see AnalysisDataFrameLocalData
66  */
67 template<typename ValueType>
68 class AnalysisDataFrameLocalDataSetHandle
69 {
70     public:
71         //! Constructs a handle from an array of values.
72         explicit AnalysisDataFrameLocalDataSetHandle(ArrayRef<ValueType> values)
73             : values_(values)
74         {
75         }
76
77         //! Clears all values in the data set.
78         void clear()
79         {
80             std::fill(values_.begin(), values_.end(), ValueType());
81         }
82
83         //! Accesses a single value in the data set.
84         ValueType &value(int column)
85         {
86             GMX_ASSERT(column >= 0 && column < static_cast<int>(values_.size()),
87                        "Invalid column index");
88             return values_[column];
89         }
90
91     private:
92         ArrayRef<ValueType>  values_;
93 };
94
95 /*! \internal
96  * \brief
97  * Handle to a single frame data within frame-local data array.
98  *
99  * Methods in this class do not throw.
100  *
101  * \see AnalysisDataFrameLocalData
102  */
103 template<typename ValueType>
104 class AnalysisDataFrameLocalDataHandle
105 {
106     public:
107         //! Shorthand for the internal array of values.
108         typedef std::vector<ValueType> ValueArray;
109         //! Shorthand for a handle to a single data set.
110         typedef AnalysisDataFrameLocalDataSetHandle<ValueType> DataSetHandle;
111
112         //! Constructs a handle from specified frame data.
113         AnalysisDataFrameLocalDataHandle(const std::vector<int> *dataSetIndices,
114                                          ValueArray             *values)
115             : dataSetIndices_(dataSetIndices), values_(values)
116         {
117         }
118
119         //! Returns the number of data sets in the array.
120         int dataSetCount() const
121         {
122             return dataSetIndices_->size() - 1;
123         }
124         //! Clears all values in the frame.
125         void clear()
126         {
127             std::fill(values_->begin(), values_->end(), ValueType());
128         }
129
130         //! Returns a handle for a single data set.
131         DataSetHandle dataSet(int dataSet)
132         {
133             GMX_ASSERT(dataSet >= 0 && dataSet < dataSetCount(),
134                        "Invalid data set index");
135             const int firstIndex = (*dataSetIndices_)[dataSet];
136             const int lastIndex  = (*dataSetIndices_)[dataSet + 1];
137             typename ValueArray::iterator begin = values_->begin() + firstIndex;
138             typename ValueArray::iterator end   = values_->begin() + lastIndex;
139             return DataSetHandle(arrayRefFromVector<ValueType>(begin, end));
140         }
141         //! Accesses a single value in the frame.
142         ValueType &value(int dataSet, int column)
143         {
144             GMX_ASSERT(dataSet >= 0 && dataSet < dataSetCount(),
145                        "Invalid data set index");
146             const int firstIndex = (*dataSetIndices_)[dataSet];
147             GMX_ASSERT(column >= 0
148                        && column < (*dataSetIndices_)[dataSet+1] - firstIndex,
149                        "Invalid column index");
150             return (*values_)[firstIndex + column];
151         }
152
153     private:
154         const std::vector<int> *dataSetIndices_;
155         ValueArray             *values_;
156 };
157
158 /*! \internal \brief
159  * Container for an array of frame-local values that supports parallel data
160  * processing.
161  *
162  * \tparam ValueType Type of values to store.
163  *
164  * This class provides a convenient interface to create an array of frame-local
165  * data for use in analysis data modules that support parallel processing.
166  * The object is initialized by setting the desired dimensionality with
167  * setDataSetCount() and setColumnCount(), followed by a call to init(),
168  * typically in AnalysisDataModuleInterface::parallelDataStarted(),
169  *
170  * After initialization, frameData() can be used to access the data for a given
171  * frame, independently from other frames.  This works if the assumptions about
172  * parallelism hold: if `N` is the parallelization factor given for init() with
173  * AnalysisDataParallelOptions::parallelizationFactor(), then frame `i+N` must
174  * not be accessed before all processing for frame `i` is finished.
175  * Technically, the data for different frames is kept in a ring buffer of size
176  * `N`.
177  *
178  * The data for a frame is not cleared after it is reused for a new frame (but
179  * is initially cleared).  This allows using the data for accumulating values
180  * over all frames in a lock-free manner.
181  *
182  * frameDataSet() is provided for convenience when only a single data set
183  * needs to be accessed (typically in AnalysisDataModuleInterface::pointsAdded()).
184  *
185  * Methods in this class do not throw except where indicated.
186  *
187  * \see AnalysisDataFrameLocalData
188  */
189 template<typename ValueType>
190 class AnalysisDataFrameLocalData
191 {
192     public:
193         //! Shorthand for the internal array of values for a frame.
194         typedef std::vector<ValueType> ValueArray;
195         //! Shorthand for a handle to a single frame.
196         typedef AnalysisDataFrameLocalDataHandle<ValueType> FrameHandle;
197         //! Shorthand for a handle to a single data set.
198         typedef AnalysisDataFrameLocalDataSetHandle<ValueType> DataSetHandle;
199
200         //! Constructs an empty container with a single data set.
201         AnalysisDataFrameLocalData()
202         {
203             dataSetColumns_.resize(2);
204         }
205
206         //! Whether init() has been called.
207         bool isInitialized() const { return !values_.empty(); }
208         /*! \brief
209          * Returns number of independent data frames in this object.
210          *
211          * This supports looping over all the frame arrays to, e.g., sum them
212          * up at the end in accumulation scenarios.
213          */
214         int frameCount() const { return values_.size(); }
215
216         /*! \brief
217          * Sets the number of data sets stored for each frame.
218          *
219          * \throws std::bad_alloc if out of memory.
220          *
221          * If not called, there is a single data set in the object.
222          * Cannot be called after init().
223          */
224         void setDataSetCount(int dataSetCount)
225         {
226             GMX_RELEASE_ASSERT(!isInitialized(),
227                                "Cannot change value count after init()");
228             GMX_RELEASE_ASSERT(dataSetCount >= 0,
229                                "Invalid data set count");
230             dataSetColumns_.resize(dataSetCount + 1);
231         }
232         /*! \brief
233          * Sets the number of columns stored for a data set.
234          *
235          * Must be called for each data set that needs to have values,
236          * otherwise there will be zero columns for that data set.
237          * Cannot be called after init().
238          */
239         void setColumnCount(int dataSet, int columnCount)
240         {
241             GMX_RELEASE_ASSERT(!isInitialized(),
242                                "Cannot change value count after init()");
243             GMX_RELEASE_ASSERT(dataSet >= 0 && dataSet < static_cast<int>(dataSetColumns_.size()) - 1,
244                                "Invalid data set index");
245             GMX_RELEASE_ASSERT(columnCount >= 0,
246                                "Invalid column count");
247             dataSetColumns_[dataSet + 1] = columnCount;
248         }
249
250         /*! \brief
251          * Initializes the storage to support specified parallelism.
252          *
253          * \throws std::bad_alloc if out of memory.
254          */
255         void init(const AnalysisDataParallelOptions &opt)
256         {
257             GMX_RELEASE_ASSERT(!isInitialized(), "init() called multiple times");
258             std::partial_sum(dataSetColumns_.begin(), dataSetColumns_.end(),
259                              dataSetColumns_.begin());
260             values_.resize(opt.parallelizationFactor());
261             typename std::vector<ValueArray>::iterator i;
262             for (i = values_.begin(); i != values_.end(); ++i)
263             {
264                 i->resize(dataSetColumns_.back());
265             }
266         }
267
268         //! Returns a handle to access data for a frame.
269         FrameHandle frameData(int frameIndex)
270         {
271             GMX_ASSERT(frameIndex >= 0, "Invalid frame index");
272             GMX_ASSERT(isInitialized(), "Cannot access data before init()");
273             return FrameHandle(&dataSetColumns_,
274                                &values_[frameIndex % values_.size()]);
275         }
276         //! Returns a handle to access a single data set within a frame.
277         DataSetHandle frameDataSet(int frameIndex, int dataSet)
278         {
279             return frameData(frameIndex).dataSet(dataSet);
280         }
281
282     private:
283         /*! \brief
284          * Index to find data sets within a per-frame array in `values_`.
285          *
286          * The first entry is always zero, followed by one entry for each data
287          * set.  Before init(), the data set entries hold the numbers set with
288          * setColumnCount().  After init(), the data set entries hold the
289          * indices of the first column for that data set in the per-frame
290          * arrays in `values_`.
291          */
292         std::vector<int>         dataSetColumns_;
293         /*! \brief
294          * Data array for each frame.
295          *
296          * This is a ring buffer whose size is specified by the desired
297          * parallelism level.  For each frame, there is a single array of
298          * values, where the individual data sets are indexed with
299          * `dataSetColumns_`.
300          */
301         std::vector<ValueArray>  values_;
302 };
303
304 //! \}
305
306 } // namespace gmx
307
308 #endif