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