2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010,2011,2012,2013, by the GROMACS development team, led by
5 * David van der Spoel, Berk Hess, Erik Lindahl, and including many
6 * others, as listed in the AUTHORS file in the top-level source
7 * 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.
37 * Implements gmx::AnalysisDataModuleManager.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_analysisdata
42 #include "gromacs/analysisdata/datamodulemanager.h"
46 #include "gromacs/analysisdata/abstractdata.h"
47 #include "gromacs/analysisdata/dataframe.h"
48 #include "gromacs/analysisdata/datamodule.h"
49 #include "gromacs/analysisdata/paralleloptions.h"
50 #include "gromacs/utility/exceptions.h"
51 #include "gromacs/utility/gmxassert.h"
56 /********************************************************************
57 * AnalysisDataModuleManager::Impl
61 * Private implementation class for AnalysisDataModuleManager.
63 * \ingroup module_analysisdata
65 class AnalysisDataModuleManager::Impl
68 //! Stores information about an attached module.
71 //! Initializes the module information.
72 explicit ModuleInfo(AnalysisDataModulePointer module)
73 : module(module), bParallel(false)
77 //! Pointer to the actual module.
78 AnalysisDataModulePointer module;
79 //! Whether the module supports parallel processing.
83 //! Shorthand for list of modules added to the data.
84 typedef std::vector<ModuleInfo> ModuleList;
86 //! Describes the current state of the notification methods.
89 eNotStarted, //!< Initial state (nothing called).
90 eInData, //!< notifyDataStart() called, no frame in progress.
91 eInFrame, //!< notifyFrameStart() called, but notifyFrameFinish() not.
92 eFinished //!< notifyDataFinish() called.
98 * Checks whether a module is compatible with a given data property.
100 * \param[in] module Module to check.
101 * \param[in] property Property to check.
102 * \param[in] bSet Value of the property to check against.
103 * \throws APIError if \p module is not compatible with the data.
105 void checkModuleProperty(const AnalysisDataModuleInterface &module,
106 DataProperty property, bool bSet) const;
108 * Checks whether a module is compatible with the data properties.
110 * \param[in] module Module to check.
111 * \throws APIError if \p module is not compatible with the data.
113 * Does not currently check the actual data (e.g., missing values), but
114 * only the dimensionality and other preset properties of the data.
116 void checkModuleProperties(const AnalysisDataModuleInterface &module) const;
119 * Present data already added to the data object to a module.
121 * \param[in] data Data object to read data from.
122 * \param[in] module Module to present the data to.
123 * \throws APIError if \p module is not compatible with the data.
124 * \throws APIError if all data is not available through
126 * \throws unspecified Any exception thrown by \p module in its data
127 * notification methods.
129 * Uses getDataFrame() in \p data to access all data in the object, and
130 * calls the notification functions in \p module as if the module had
131 * been registered to the data object when the data was added.
133 void presentData(AbstractAnalysisData *data,
134 AnalysisDataModuleInterface *module);
136 //! List of modules added to the data.
138 //! Properties of the owning data for module checking.
139 bool bDataProperty_[eDataPropertyNR];
140 //! true if all modules support missing data.
142 //! true if there are modules that do not support parallel processing.
143 bool bSerialModules_;
144 //! true if there are modules that support parallel processing.
145 bool bParallelModules_;
148 * Current state of the notification methods.
150 * This is used together with \a currIndex_ for sanity checks on the
151 * input data; invalid call sequences trigger asserts.
152 * The state of these variables does not otherwise affect the behavior
153 * of this class; this is the reason they can be changed in const
156 //! Whether notifyDataStart() has been called.
157 mutable State state_;
158 //! Index of currently active frame or the next frame if not in frame.
159 mutable int currIndex_;
162 AnalysisDataModuleManager::Impl::Impl()
163 : bAllowMissing_(true), bSerialModules_(false), bParallelModules_(false),
164 state_(eNotStarted), currIndex_(0)
166 // This must be in sync with how AbstractAnalysisData is actually
168 for (int i = 0; i < eDataPropertyNR; ++i)
170 bDataProperty_[i] = false;
175 AnalysisDataModuleManager::Impl::checkModuleProperty(
176 const AnalysisDataModuleInterface &module,
177 DataProperty property, bool bSet) const
180 const int flags = module.flags();
183 case eMultipleDataSets:
184 if (bSet && !(flags & AnalysisDataModuleInterface::efAllowMultipleDataSets))
189 case eMultipleColumns:
190 if (bSet && !(flags & AnalysisDataModuleInterface::efAllowMulticolumn))
196 if ((bSet && !(flags & AnalysisDataModuleInterface::efAllowMultipoint))
197 || (!bSet && (flags & AnalysisDataModuleInterface::efOnlyMultipoint)))
203 GMX_RELEASE_ASSERT(false, "Invalid data property enumeration");
207 GMX_THROW(APIError("Data module not compatible with data object properties"));
212 AnalysisDataModuleManager::Impl::checkModuleProperties(
213 const AnalysisDataModuleInterface &module) const
215 for (int i = 0; i < eDataPropertyNR; ++i)
217 checkModuleProperty(module, static_cast<DataProperty>(i), bDataProperty_[i]);
222 AnalysisDataModuleManager::Impl::presentData(AbstractAnalysisData *data,
223 AnalysisDataModuleInterface *module)
225 if (state_ == eNotStarted)
229 GMX_RELEASE_ASSERT(state_ != eInFrame,
230 "Cannot apply a modules in mid-frame");
231 module->dataStarted(data);
232 const bool bCheckMissing = bAllowMissing_
233 && !(module->flags() & AnalysisDataModuleInterface::efAllowMissing);
234 for (int i = 0; i < data->frameCount(); ++i)
236 AnalysisDataFrameRef frame = data->getDataFrame(i);
237 GMX_RELEASE_ASSERT(frame.isValid(), "Invalid data frame returned");
238 // TODO: Check all frames before doing anything for slightly better
239 // exception behavior.
240 if (bCheckMissing && !frame.allPresent())
242 GMX_THROW(APIError("Missing data not supported by a module"));
244 module->frameStarted(frame.header());
245 for (int j = 0; j < frame.pointSetCount(); ++j)
247 module->pointsAdded(frame.pointSet(j));
249 module->frameFinished(frame.header());
251 if (state_ == eFinished)
253 module->dataFinished();
257 /********************************************************************
258 * AnalysisDataModuleManager
261 AnalysisDataModuleManager::AnalysisDataModuleManager()
266 AnalysisDataModuleManager::~AnalysisDataModuleManager()
271 AnalysisDataModuleManager::dataPropertyAboutToChange(DataProperty property, bool bSet)
273 GMX_RELEASE_ASSERT(impl_->state_ == Impl::eNotStarted,
274 "Cannot change data properties after data has been started");
275 if (impl_->bDataProperty_[property] != bSet)
277 Impl::ModuleList::const_iterator i;
278 for (i = impl_->modules_.begin(); i != impl_->modules_.end(); ++i)
280 impl_->checkModuleProperty(*i->module, property, bSet);
282 impl_->bDataProperty_[property] = bSet;
287 AnalysisDataModuleManager::addModule(AbstractAnalysisData *data,
288 AnalysisDataModulePointer module)
290 impl_->checkModuleProperties(*module);
291 // TODO: Ensure that the system does not end up in an inconsistent state by
292 // adding a module in mid-data during parallel processing (probably best to
293 // prevent alltogether).
294 GMX_RELEASE_ASSERT(impl_->state_ != Impl::eInFrame,
295 "Cannot add a data module in mid-frame");
296 impl_->presentData(data, module.get());
298 if (!(module->flags() & AnalysisDataModuleInterface::efAllowMissing))
300 impl_->bAllowMissing_ = false;
302 impl_->modules_.push_back(Impl::ModuleInfo(module));
306 AnalysisDataModuleManager::applyModule(AbstractAnalysisData *data,
307 AnalysisDataModuleInterface *module)
309 impl_->checkModuleProperties(*module);
310 GMX_RELEASE_ASSERT(impl_->state_ == Impl::eFinished,
311 "Data module can only be applied to ready data");
312 impl_->presentData(data, module);
317 AnalysisDataModuleManager::hasSerialModules() const
319 GMX_ASSERT(impl_->state_ != Impl::eNotStarted,
320 "Module state not accessible before data is started");
321 return impl_->bSerialModules_;
326 AnalysisDataModuleManager::notifyDataStart(AbstractAnalysisData *data)
328 GMX_RELEASE_ASSERT(impl_->state_ == Impl::eNotStarted,
329 "notifyDataStart() called more than once");
330 for (int d = 0; d < data->dataSetCount(); ++d)
332 GMX_RELEASE_ASSERT(data->columnCount(d) > 0,
333 "Data column count is not set");
335 impl_->state_ = Impl::eInData;
336 impl_->bSerialModules_ = !impl_->modules_.empty();
337 impl_->bParallelModules_ = false;
339 Impl::ModuleList::const_iterator i;
340 for (i = impl_->modules_.begin(); i != impl_->modules_.end(); ++i)
342 // This should not fail, since addModule() and
343 // dataPropertyAboutToChange() already do the checks, but kept here to
344 // catch potential bugs (perhaps it would be best to assert on failure).
345 impl_->checkModuleProperties(*i->module);
346 i->module->dataStarted(data);
352 AnalysisDataModuleManager::notifyParallelDataStart(
353 AbstractAnalysisData *data,
354 const AnalysisDataParallelOptions &options)
356 GMX_RELEASE_ASSERT(impl_->state_ == Impl::eNotStarted,
357 "notifyDataStart() called more than once");
358 for (int d = 0; d < data->dataSetCount(); ++d)
360 GMX_RELEASE_ASSERT(data->columnCount(d) > 0,
361 "Data column count is not set");
363 impl_->state_ = Impl::eInData;
364 impl_->bSerialModules_ = false;
365 impl_->bParallelModules_ = false;
367 Impl::ModuleList::iterator i;
368 for (i = impl_->modules_.begin(); i != impl_->modules_.end(); ++i)
370 // This should not fail, since addModule() and
371 // dataPropertyAboutToChange() already do the checks, but kept here to
372 // catch potential bugs (perhaps it would be best to assert on failure).
373 impl_->checkModuleProperties(*i->module);
374 i->bParallel = i->module->parallelDataStarted(data, options);
377 impl_->bParallelModules_ = true;
381 impl_->bSerialModules_ = true;
388 AnalysisDataModuleManager::notifyFrameStart(const AnalysisDataFrameHeader &header) const
390 GMX_ASSERT(impl_->state_ == Impl::eInData, "Invalid call sequence");
391 GMX_ASSERT(header.index() == impl_->currIndex_, "Out of order frames");
392 impl_->state_ = Impl::eInFrame;
394 if (impl_->bSerialModules_)
396 Impl::ModuleList::const_iterator i;
397 for (i = impl_->modules_.begin(); i != impl_->modules_.end(); ++i)
401 i->module->frameStarted(header);
408 AnalysisDataModuleManager::notifyParallelFrameStart(
409 const AnalysisDataFrameHeader &header) const
411 if (impl_->bParallelModules_)
413 Impl::ModuleList::const_iterator i;
414 for (i = impl_->modules_.begin(); i != impl_->modules_.end(); ++i)
418 i->module->frameStarted(header);
426 AnalysisDataModuleManager::notifyPointsAdd(const AnalysisDataPointSetRef &points) const
428 GMX_ASSERT(impl_->state_ == Impl::eInFrame, "notifyFrameStart() not called");
429 // TODO: Add checks for column spans (requires passing the information
430 // about the column counts from somewhere).
431 //GMX_ASSERT(points.lastColumn() < columnCount(points.dataSetIndex()),
432 // "Invalid columns");
433 GMX_ASSERT(points.frameIndex() == impl_->currIndex_,
434 "Points do not correspond to current frame");
435 if (impl_->bSerialModules_)
437 if (!impl_->bAllowMissing_ && !points.allPresent())
439 GMX_THROW(APIError("Missing data not supported by a module"));
442 Impl::ModuleList::const_iterator i;
443 for (i = impl_->modules_.begin(); i != impl_->modules_.end(); ++i)
447 i->module->pointsAdded(points);
455 AnalysisDataModuleManager::notifyParallelPointsAdd(
456 const AnalysisDataPointSetRef &points) const
458 // TODO: Add checks for column spans (requires passing the information
459 // about the column counts from somewhere).
460 //GMX_ASSERT(points.lastColumn() < columnCount(points.dataSetIndex()),
461 // "Invalid columns");
462 if (impl_->bParallelModules_)
464 if (!impl_->bAllowMissing_ && !points.allPresent())
466 GMX_THROW(APIError("Missing data not supported by a module"));
469 Impl::ModuleList::const_iterator i;
470 for (i = impl_->modules_.begin(); i != impl_->modules_.end(); ++i)
474 i->module->pointsAdded(points);
482 AnalysisDataModuleManager::notifyFrameFinish(const AnalysisDataFrameHeader &header) const
484 GMX_ASSERT(impl_->state_ == Impl::eInFrame, "notifyFrameStart() not called");
485 GMX_ASSERT(header.index() == impl_->currIndex_,
486 "Header does not correspond to current frame");
487 // TODO: Add a check for the frame count in the source data including this
489 impl_->state_ = Impl::eInData;
492 if (impl_->bSerialModules_)
494 Impl::ModuleList::const_iterator i;
495 for (i = impl_->modules_.begin(); i != impl_->modules_.end(); ++i)
499 i->module->frameFinished(header);
507 AnalysisDataModuleManager::notifyParallelFrameFinish(
508 const AnalysisDataFrameHeader &header) const
510 if (impl_->bParallelModules_)
512 Impl::ModuleList::const_iterator i;
513 for (i = impl_->modules_.begin(); i != impl_->modules_.end(); ++i)
517 i->module->frameFinished(header);
525 AnalysisDataModuleManager::notifyDataFinish() const
527 GMX_RELEASE_ASSERT(impl_->state_ == Impl::eInData, "Invalid call sequence");
528 impl_->state_ = Impl::eFinished;
530 Impl::ModuleList::const_iterator i;
531 for (i = impl_->modules_.begin(); i != impl_->modules_.end(); ++i)
533 i->module->dataFinished();