2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2010,2011,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.
37 * Implements gmx::AnalysisDataModuleManager.
39 * \author Teemu Murtola <teemu.murtola@gmail.com>
40 * \ingroup module_analysisdata
44 #include "gromacs/analysisdata/datamodulemanager.h"
48 #include "gromacs/analysisdata/abstractdata.h"
49 #include "gromacs/analysisdata/dataframe.h"
50 #include "gromacs/analysisdata/datamodule.h"
51 #include "gromacs/analysisdata/paralleloptions.h"
52 #include "gromacs/utility/exceptions.h"
53 #include "gromacs/utility/gmxassert.h"
58 /********************************************************************
59 * AnalysisDataModuleManager::Impl
63 * Private implementation class for AnalysisDataModuleManager.
65 * \ingroup module_analysisdata
67 class AnalysisDataModuleManager::Impl
70 //! Stores information about an attached module.
73 //! Initializes the module information.
74 explicit ModuleInfo(AnalysisDataModulePointer module)
75 : module(module), bParallel(false)
79 //! Pointer to the actual module.
80 AnalysisDataModulePointer module;
81 //! Whether the module supports parallel processing.
85 //! Shorthand for list of modules added to the data.
86 typedef std::vector<ModuleInfo> ModuleList;
88 //! Describes the current state of the notification methods.
91 eNotStarted, //!< Initial state (nothing called).
92 eInData, //!< notifyDataStart() called, no frame in progress.
93 eInFrame, //!< notifyFrameStart() called, but notifyFrameFinish() not.
94 eFinished //!< notifyDataFinish() called.
100 * Checks whether a module is compatible with a given data property.
102 * \param[in] module Module to check.
103 * \param[in] property Property to check.
104 * \param[in] bSet Value of the property to check against.
105 * \throws APIError if \p module is not compatible with the data.
107 void checkModuleProperty(const AnalysisDataModuleInterface &module,
108 DataProperty property, bool bSet) const;
110 * Checks whether a module is compatible with the data properties.
112 * \param[in] module Module to check.
113 * \throws APIError if \p module is not compatible with the data.
115 * Does not currently check the actual data (e.g., missing values), but
116 * only the dimensionality and other preset properties of the data.
118 void checkModuleProperties(const AnalysisDataModuleInterface &module) const;
121 * Present data already added to the data object to a module.
123 * \param[in] data Data object to read data from.
124 * \param[in] module Module to present the data to.
125 * \throws APIError if \p module is not compatible with the data.
126 * \throws APIError if all data is not available through
128 * \throws unspecified Any exception thrown by \p module in its data
129 * notification methods.
131 * Uses getDataFrame() in \p data to access all data in the object, and
132 * calls the notification functions in \p module as if the module had
133 * been registered to the data object when the data was added.
135 void presentData(AbstractAnalysisData *data,
136 AnalysisDataModuleInterface *module);
138 //! List of modules added to the data.
140 //! Properties of the owning data for module checking.
141 bool bDataProperty_[eDataPropertyNR];
142 //! true if all modules support missing data.
144 //! true if there are modules that do not support parallel processing.
145 bool bSerialModules_;
146 //! true if there are modules that support parallel processing.
147 bool bParallelModules_;
150 * Current state of the notification methods.
152 * This is used together with \a currIndex_ for sanity checks on the
153 * input data; invalid call sequences trigger asserts.
154 * The state of these variables does not otherwise affect the behavior
155 * of this class; this is the reason they can be changed in const
158 //! Whether notifyDataStart() has been called.
159 mutable State state_;
160 //! Index of currently active frame or the next frame if not in frame.
161 mutable int currIndex_;
164 AnalysisDataModuleManager::Impl::Impl()
165 : bAllowMissing_(true), bSerialModules_(false), bParallelModules_(false),
166 state_(eNotStarted), currIndex_(0)
168 // This must be in sync with how AbstractAnalysisData is actually
170 for (int i = 0; i < eDataPropertyNR; ++i)
172 bDataProperty_[i] = false;
177 AnalysisDataModuleManager::Impl::checkModuleProperty(
178 const AnalysisDataModuleInterface &module,
179 DataProperty property, bool bSet) const
182 const int flags = module.flags();
185 case eMultipleDataSets:
186 if (bSet && !(flags & AnalysisDataModuleInterface::efAllowMultipleDataSets))
191 case eMultipleColumns:
192 if (bSet && !(flags & AnalysisDataModuleInterface::efAllowMulticolumn))
198 if ((bSet && !(flags & AnalysisDataModuleInterface::efAllowMultipoint))
199 || (!bSet && (flags & AnalysisDataModuleInterface::efOnlyMultipoint)))
205 GMX_RELEASE_ASSERT(false, "Invalid data property enumeration");
209 GMX_THROW(APIError("Data module not compatible with data object properties"));
214 AnalysisDataModuleManager::Impl::checkModuleProperties(
215 const AnalysisDataModuleInterface &module) const
217 for (int i = 0; i < eDataPropertyNR; ++i)
219 checkModuleProperty(module, static_cast<DataProperty>(i), bDataProperty_[i]);
224 AnalysisDataModuleManager::Impl::presentData(AbstractAnalysisData *data,
225 AnalysisDataModuleInterface *module)
227 if (state_ == eNotStarted)
231 GMX_RELEASE_ASSERT(state_ != eInFrame,
232 "Cannot apply a modules in mid-frame");
233 module->dataStarted(data);
234 const bool bCheckMissing = bAllowMissing_
235 && !(module->flags() & AnalysisDataModuleInterface::efAllowMissing);
236 for (int i = 0; i < data->frameCount(); ++i)
238 AnalysisDataFrameRef frame = data->getDataFrame(i);
239 GMX_RELEASE_ASSERT(frame.isValid(), "Invalid data frame returned");
240 // TODO: Check all frames before doing anything for slightly better
241 // exception behavior.
242 if (bCheckMissing && !frame.allPresent())
244 GMX_THROW(APIError("Missing data not supported by a module"));
246 module->frameStarted(frame.header());
247 for (int j = 0; j < frame.pointSetCount(); ++j)
249 module->pointsAdded(frame.pointSet(j));
251 module->frameFinished(frame.header());
253 if (state_ == eFinished)
255 module->dataFinished();
259 /********************************************************************
260 * AnalysisDataModuleManager
263 AnalysisDataModuleManager::AnalysisDataModuleManager()
268 AnalysisDataModuleManager::~AnalysisDataModuleManager()
273 AnalysisDataModuleManager::dataPropertyAboutToChange(DataProperty property, bool bSet)
275 GMX_RELEASE_ASSERT(impl_->state_ == Impl::eNotStarted,
276 "Cannot change data properties after data has been started");
277 if (impl_->bDataProperty_[property] != bSet)
279 Impl::ModuleList::const_iterator i;
280 for (i = impl_->modules_.begin(); i != impl_->modules_.end(); ++i)
282 impl_->checkModuleProperty(*i->module, property, bSet);
284 impl_->bDataProperty_[property] = bSet;
289 AnalysisDataModuleManager::addModule(AbstractAnalysisData *data,
290 AnalysisDataModulePointer module)
292 impl_->checkModuleProperties(*module);
293 // TODO: Ensure that the system does not end up in an inconsistent state by
294 // adding a module in mid-data during parallel processing (probably best to
295 // prevent alltogether).
296 GMX_RELEASE_ASSERT(impl_->state_ != Impl::eInFrame,
297 "Cannot add a data module in mid-frame");
298 impl_->presentData(data, module.get());
300 if (!(module->flags() & AnalysisDataModuleInterface::efAllowMissing))
302 impl_->bAllowMissing_ = false;
304 impl_->modules_.push_back(Impl::ModuleInfo(module));
308 AnalysisDataModuleManager::applyModule(AbstractAnalysisData *data,
309 AnalysisDataModuleInterface *module)
311 impl_->checkModuleProperties(*module);
312 GMX_RELEASE_ASSERT(impl_->state_ == Impl::eFinished,
313 "Data module can only be applied to ready data");
314 impl_->presentData(data, module);
319 AnalysisDataModuleManager::hasSerialModules() const
321 GMX_ASSERT(impl_->state_ != Impl::eNotStarted,
322 "Module state not accessible before data is started");
323 return impl_->bSerialModules_;
328 AnalysisDataModuleManager::notifyDataStart(AbstractAnalysisData *data)
330 GMX_RELEASE_ASSERT(impl_->state_ == Impl::eNotStarted,
331 "notifyDataStart() called more than once");
332 for (int d = 0; d < data->dataSetCount(); ++d)
334 GMX_RELEASE_ASSERT(data->columnCount(d) > 0,
335 "Data column count is not set");
337 impl_->state_ = Impl::eInData;
338 impl_->bSerialModules_ = !impl_->modules_.empty();
339 impl_->bParallelModules_ = false;
341 Impl::ModuleList::const_iterator i;
342 for (i = impl_->modules_.begin(); i != impl_->modules_.end(); ++i)
344 // This should not fail, since addModule() and
345 // dataPropertyAboutToChange() already do the checks, but kept here to
346 // catch potential bugs (perhaps it would be best to assert on failure).
347 impl_->checkModuleProperties(*i->module);
348 i->module->dataStarted(data);
354 AnalysisDataModuleManager::notifyParallelDataStart(
355 AbstractAnalysisData *data,
356 const AnalysisDataParallelOptions &options)
358 GMX_RELEASE_ASSERT(impl_->state_ == Impl::eNotStarted,
359 "notifyDataStart() called more than once");
360 for (int d = 0; d < data->dataSetCount(); ++d)
362 GMX_RELEASE_ASSERT(data->columnCount(d) > 0,
363 "Data column count is not set");
365 impl_->state_ = Impl::eInData;
366 impl_->bSerialModules_ = false;
367 impl_->bParallelModules_ = false;
369 Impl::ModuleList::iterator i;
370 for (i = impl_->modules_.begin(); i != impl_->modules_.end(); ++i)
372 // This should not fail, since addModule() and
373 // dataPropertyAboutToChange() already do the checks, but kept here to
374 // catch potential bugs (perhaps it would be best to assert on failure).
375 impl_->checkModuleProperties(*i->module);
376 i->bParallel = i->module->parallelDataStarted(data, options);
379 impl_->bParallelModules_ = true;
383 impl_->bSerialModules_ = true;
390 AnalysisDataModuleManager::notifyFrameStart(const AnalysisDataFrameHeader &header) const
392 GMX_ASSERT(impl_->state_ == Impl::eInData, "Invalid call sequence");
393 GMX_ASSERT(header.index() == impl_->currIndex_, "Out of order frames");
394 impl_->state_ = Impl::eInFrame;
396 if (impl_->bSerialModules_)
398 Impl::ModuleList::const_iterator i;
399 for (i = impl_->modules_.begin(); i != impl_->modules_.end(); ++i)
403 i->module->frameStarted(header);
410 AnalysisDataModuleManager::notifyParallelFrameStart(
411 const AnalysisDataFrameHeader &header) const
413 if (impl_->bParallelModules_)
415 Impl::ModuleList::const_iterator i;
416 for (i = impl_->modules_.begin(); i != impl_->modules_.end(); ++i)
420 i->module->frameStarted(header);
428 AnalysisDataModuleManager::notifyPointsAdd(const AnalysisDataPointSetRef &points) const
430 GMX_ASSERT(impl_->state_ == Impl::eInFrame, "notifyFrameStart() not called");
431 // TODO: Add checks for column spans (requires passing the information
432 // about the column counts from somewhere).
433 //GMX_ASSERT(points.lastColumn() < columnCount(points.dataSetIndex()),
434 // "Invalid columns");
435 GMX_ASSERT(points.frameIndex() == impl_->currIndex_,
436 "Points do not correspond to current frame");
437 if (impl_->bSerialModules_)
439 if (!impl_->bAllowMissing_ && !points.allPresent())
441 GMX_THROW(APIError("Missing data not supported by a module"));
444 Impl::ModuleList::const_iterator i;
445 for (i = impl_->modules_.begin(); i != impl_->modules_.end(); ++i)
449 i->module->pointsAdded(points);
457 AnalysisDataModuleManager::notifyParallelPointsAdd(
458 const AnalysisDataPointSetRef &points) const
460 // TODO: Add checks for column spans (requires passing the information
461 // about the column counts from somewhere).
462 //GMX_ASSERT(points.lastColumn() < columnCount(points.dataSetIndex()),
463 // "Invalid columns");
464 if (impl_->bParallelModules_)
466 if (!impl_->bAllowMissing_ && !points.allPresent())
468 GMX_THROW(APIError("Missing data not supported by a module"));
471 Impl::ModuleList::const_iterator i;
472 for (i = impl_->modules_.begin(); i != impl_->modules_.end(); ++i)
476 i->module->pointsAdded(points);
484 AnalysisDataModuleManager::notifyFrameFinish(const AnalysisDataFrameHeader &header) const
486 GMX_ASSERT(impl_->state_ == Impl::eInFrame, "notifyFrameStart() not called");
487 GMX_ASSERT(header.index() == impl_->currIndex_,
488 "Header does not correspond to current frame");
489 // TODO: Add a check for the frame count in the source data including this
491 impl_->state_ = Impl::eInData;
494 if (impl_->bSerialModules_)
496 Impl::ModuleList::const_iterator i;
497 for (i = impl_->modules_.begin(); i != impl_->modules_.end(); ++i)
501 i->module->frameFinished(header);
509 AnalysisDataModuleManager::notifyParallelFrameFinish(
510 const AnalysisDataFrameHeader &header) const
512 if (impl_->bParallelModules_)
514 Impl::ModuleList::const_iterator i;
515 for (i = impl_->modules_.begin(); i != impl_->modules_.end(); ++i)
519 i->module->frameFinished(header);
527 AnalysisDataModuleManager::notifyDataFinish() const
529 GMX_RELEASE_ASSERT(impl_->state_ == Impl::eInData, "Invalid call sequence");
530 impl_->state_ = Impl::eFinished;
532 Impl::ModuleList::const_iterator i;
533 for (i = impl_->modules_.begin(); i != impl_->modules_.end(); ++i)
535 i->module->dataFinished();