Sort all includes in src/gromacs
[alexxy/gromacs.git] / src / gromacs / analysisdata / analysisdata.cpp
index 0734bb48787f8e91d02721bf6507d1736eac4f59..300b2bd56f6fbf94c989bec8c6338c2386f6df53 100644 (file)
 /*
+ * This file is part of the GROMACS molecular simulation package.
  *
- *                This source code is part of
+ * Copyright (c) 2010,2011,2012,2013,2014, by the GROMACS development team, led by
+ * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
+ * and including many others, as listed in the AUTHORS file in the
+ * top-level source directory and at http://www.gromacs.org.
  *
- *                 G   R   O   M   A   C   S
+ * GROMACS is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1
+ * of the License, or (at your option) any later version.
  *
- *          GROningen MAchine for Chemical Simulations
+ * GROMACS is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
  *
- * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
- * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
- * Copyright (c) 2001-2009, The GROMACS development team,
- * check out http://www.gromacs.org for more information.
-
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
- * of the License, or (at your option) any later version.
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with GROMACS; if not, see
+ * http://www.gnu.org/licenses, or write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
  *
- * If you want to redistribute modifications, please consider that
- * scientific software is very special. Version control is crucial -
- * bugs must be traceable. We will be happy to consider code for
- * inclusion in the official distribution, but derived work must not
- * be called official GROMACS. Details are found in the README & COPYING
- * files - if they are missing, get the official version at www.gromacs.org.
+ * If you want to redistribute modifications to GROMACS, please
+ * consider that scientific software is very special. Version
+ * control is crucial - bugs must be traceable. We will be happy to
+ * consider code for inclusion in the official distribution, but
+ * derived work must not be called official GROMACS. Details are found
+ * in the README & COPYING files - if they are missing, get the
+ * official version at http://www.gromacs.org.
  *
  * To help us fund GROMACS development, we humbly ask that you cite
- * the papers on the package - you can find them in the top README file.
- *
- * For more info, check our website at http://www.gromacs.org
+ * the research papers on the package. Check out http://www.gromacs.org.
  */
 /*! \internal \file
  * \brief
  * Implements classes in analysisdata.h.
  *
- * \author Teemu Murtola <teemu.murtola@cbr.su.se>
+ * \author Teemu Murtola <teemu.murtola@gmail.com>
  * \ingroup module_analysisdata
  */
-#include "gromacs/analysisdata/analysisdata.h"
+#include "gmxpre.h"
 
-#include <algorithm>
-#include <memory>
+#include "analysisdata.h"
 
 #include "gromacs/analysisdata/dataframe.h"
-#include "gromacs/fatalerror/exceptions.h"
-#include "gromacs/fatalerror/gmxassert.h"
-
-#include "abstractdata-impl.h"
-#include "analysisdata-impl.h"
+#include "gromacs/analysisdata/datastorage.h"
+#include "gromacs/analysisdata/paralleloptions.h"
+#include "gromacs/utility/exceptions.h"
+#include "gromacs/utility/gmxassert.h"
+#include "gromacs/utility/uniqueptr.h"
 
 namespace gmx
 {
 
 /********************************************************************
- * AnalysisData::Impl
- ********************************************************************/
-
-static bool
-frame_index_gtr(AnalysisDataFrame *a, AnalysisDataFrame *b)
-{
-    return a->_index > b->_index;
-}
-
-
-AnalysisData::Impl::Impl(AnalysisData *data)
-    : _data(*data), _pstart(0)
-{
-}
-
+ * AnalysisDataHandleImpl
+ */
 
-AnalysisData::Impl::~Impl()
+namespace internal
 {
-    HandleList::const_iterator i;
-    for (i = _handles.begin(); i != _handles.end(); ++i)
-    {
-        delete *i;
-    }
-
-    FrameList::const_iterator j;
-    for (j = _pending.begin(); j != _pending.end(); ++j)
-    {
-        delete *j;
-    }
-}
 
-
-void
-AnalysisData::Impl::addPendingFrame(AnalysisDataFrame *fr)
+/*! \internal \brief
+ * Private implementation class for AnalysisDataHandle.
+ *
+ * \ingroup module_analysisdata
+ */
+class AnalysisDataHandleImpl
 {
-    GMX_ASSERT(fr->_index >= _data.frameCount(),
-               "addPendingFrame() called for too old frame");
-    size_t pindex = fr->_index - _data.frameCount();
-    if (pindex == 0)
-    {
-        // Just store our frame if it is the next one.
-        _data.storeNextFrame(fr->_x, fr->_dx, fr->_y, fr->_dy, fr->_present);
-        incrementPStart();
-    }
-    else
-    {
-        if (pindex >= _pending.size())
-        {
-            // TODO: We need to wait until earlier frames are ready...
-        }
-        // TODO: This is not thread-safe.
-        pindex += _pstart;
-        if (pindex > _pending.size())
+    public:
+        //! Creates a handle associated with the given data object.
+        explicit AnalysisDataHandleImpl(AnalysisData *data)
+            : data_(*data), currentFrame_(NULL)
         {
-            pindex -= _pending.size();
         }
 
-        int ncol = _data.columnCount();
-        _pending[pindex]->_x     = fr->_x;
-        _pending[pindex]->_dx    = fr->_dx;
-        for (int i = 0; i < ncol; ++i)
-        {
-            _pending[pindex]->_y[i]       = fr->_y[i];
-            _pending[pindex]->_dy[i]      = fr->_dy[i];
-            _pending[pindex]->_present[i] = fr->_present[i];
-        }
-        _pending[pindex]->_index = fr->_index;
-    }
-    processPendingFrames();
-}
-
-
-void
-AnalysisData::Impl::processPendingFrames()
-{
-    while (_pending[_pstart]->_index != -1)
-    {
-        AnalysisDataFrame *fr = _pending[_pstart];
+        //! The data object that this handle belongs to.
+        AnalysisData             &data_;
+        //! Current storage frame object, or NULL if no current frame.
+        AnalysisDataStorageFrame *currentFrame_;
+};
 
-        _data.storeNextFrame(fr->_x, fr->_dx, fr->_y, fr->_dy, fr->_present);
-        fr->_index = -1;
-        incrementPStart();
-    }
-}
+}   // namespace internal
 
+/********************************************************************
+ * AnalysisData::Impl
+ */
 
-void
-AnalysisData::Impl::incrementPStart()
+/*! \internal \brief
+ * Private implementation class for AnalysisData.
+ *
+ * \ingroup module_analysisdata
+ */
+class AnalysisData::Impl
 {
-    size_t val = _pstart;
-
-    ++val;
-    if (val >= _pending.size())
-    {
-        val -= _pending.size();
-    }
-    _pstart = val;
-}
-
+    public:
+        //! Smart pointer type to manage a data handle implementation.
+        typedef gmx_unique_ptr<internal::AnalysisDataHandleImpl>::type
+            HandlePointer;
+        //! Shorthand for a list of data handles.
+        typedef std::vector<HandlePointer> HandleList;
+
+        //! Storage implementation.
+        AnalysisDataStorage     storage_;
+        /*! \brief
+         * List of handles for this data object.
+         *
+         * Note that AnalysisDataHandle objects also contain (raw) pointers
+         * to these objects.
+         */
+        HandleList              handles_;
+};
 
 /********************************************************************
  * AnalysisData
  */
 
 AnalysisData::AnalysisData()
-    : _impl(new Impl(this))
+    : impl_(new Impl)
 {
 }
 
 
 AnalysisData::~AnalysisData()
 {
-    delete _impl;
 }
 
 
 void
-AnalysisData::setColumns(int ncol, bool multipoint)
+AnalysisData::setDataSetCount(int dataSetCount)
 {
-    GMX_RELEASE_ASSERT(ncol > 0, "Number of columns must be positive");
-    GMX_RELEASE_ASSERT(_impl->_handles.empty(),
+    GMX_RELEASE_ASSERT(impl_->handles_.empty(),
                        "Cannot change data dimensionality after creating handles");
-    setColumnCount(ncol);
-    setMultipoint(multipoint);
+    AbstractAnalysisData::setDataSetCount(dataSetCount);
 }
 
 
-AnalysisDataHandle *
-AnalysisData::startData(AnalysisDataParallelOptions opt)
+void
+AnalysisData::setColumnCount(int dataSet, int columnCount)
 {
-    if (_impl->_handles.empty())
-    {
-        startDataStore();
-    }
-    else if (isMultipoint())
-    {
-        GMX_THROW(NotImplementedError("Parallelism not supported for multipoint data"));
-    }
+    GMX_RELEASE_ASSERT(impl_->handles_.empty(),
+                       "Cannot change data dimensionality after creating handles");
+    AbstractAnalysisData::setColumnCount(dataSet, columnCount);
+}
+
+
+void
+AnalysisData::setMultipoint(bool bMultipoint)
+{
+    GMX_RELEASE_ASSERT(impl_->handles_.empty(),
+                       "Cannot change data type after creating handles");
+    AbstractAnalysisData::setMultipoint(bMultipoint);
+}
 
-    std::auto_ptr<AnalysisDataHandle> handle(new AnalysisDataHandle(this));
-    _impl->_handles.push_back(handle.get());
 
-    size_t oldSize = _impl->_pending.size();
-    _impl->_pending.resize(2 * _impl->_handles.size() - 1);
-    Impl::FrameList::iterator i;
-    for (i = _impl->_pending.begin() + oldSize; i != _impl->_pending.end(); ++i)
+int
+AnalysisData::frameCount() const
+{
+    return impl_->storage_.frameCount();
+}
+
+
+AnalysisDataHandle
+AnalysisData::startData(const AnalysisDataParallelOptions &opt)
+{
+    GMX_RELEASE_ASSERT(impl_->handles_.size() < static_cast<unsigned>(opt.parallelizationFactor()),
+                       "Too many calls to startData() compared to provided options");
+    if (impl_->handles_.empty())
     {
-        *i = new AnalysisDataFrame(columnCount());
-        (*i)->_index = -1;
+        impl_->storage_.startParallelDataStorage(this, &moduleManager(), opt);
     }
 
-    return handle.release();
+    Impl::HandlePointer handle(new internal::AnalysisDataHandleImpl(this));
+    impl_->handles_.push_back(move(handle));
+    return AnalysisDataHandle(impl_->handles_.back().get());
 }
 
 
 void
-AnalysisData::finishData(AnalysisDataHandle *handle)
+AnalysisData::finishData(AnalysisDataHandle handle)
 {
     Impl::HandleList::iterator i;
 
-    i = std::find(_impl->_handles.begin(), _impl->_handles.end(), handle);
-    GMX_RELEASE_ASSERT(i != _impl->_handles.end(),
+    for (i = impl_->handles_.begin(); i != impl_->handles_.end(); ++i)
+    {
+        if (i->get() == handle.impl_)
+        {
+            break;
+        }
+    }
+    GMX_RELEASE_ASSERT(i != impl_->handles_.end(),
                        "finishData() called for an unknown handle");
 
-    _impl->_handles.erase(i);
-    delete handle;
+    impl_->handles_.erase(i);
 
-    if (_impl->_handles.empty())
+    if (impl_->handles_.empty())
     {
-        notifyDataFinish();
+        impl_->storage_.finishDataStorage();
     }
 }
 
 
-/********************************************************************
- * AnalysisDataHandle::Impl
- */
-
-AnalysisDataHandle::Impl::Impl(AnalysisData *data)
-    : _data(*data)
+AnalysisDataFrameRef
+AnalysisData::tryGetDataFrameInternal(int index) const
 {
-    if (!_data.isMultipoint())
-    {
-        _frame.reset(new AnalysisDataFrame(_data.columnCount()));
-    }
+    return impl_->storage_.tryGetDataFrame(index);
 }
 
 
-AnalysisDataHandle::Impl::~Impl()
+bool
+AnalysisData::requestStorageInternal(int nframes)
 {
+    return impl_->storage_.requestStorage(nframes);
 }
 
 
@@ -248,116 +218,103 @@ AnalysisDataHandle::Impl::~Impl()
  * AnalysisDataHandle
  */
 
-AnalysisDataHandle::AnalysisDataHandle(AnalysisData *data)
-    : _impl(new Impl(data))
+AnalysisDataHandle::AnalysisDataHandle()
+    : impl_(NULL)
 {
 }
 
 
-AnalysisDataHandle::~AnalysisDataHandle()
+AnalysisDataHandle::AnalysisDataHandle(internal::AnalysisDataHandleImpl *impl)
+    : impl_(impl)
 {
-    delete _impl;
 }
 
 
 void
 AnalysisDataHandle::startFrame(int index, real x, real dx)
 {
-    if (_impl->_data.isMultipoint())
-    {
-        _impl->_data.notifyFrameStart(AnalysisDataFrameHeader(index, x, dx));
-    }
-    else
-    {
-        _impl->_frame->_index = index;
-        _impl->_frame->_x  = x;
-        _impl->_frame->_dx = dx;
-        for (int i = 0; i < _impl->_data.columnCount(); ++i)
-        {
-            _impl->_frame->_y[i]  = 0.0;
-            _impl->_frame->_dy[i] = 0.0;
-            _impl->_frame->_present[i] = false;
-        }
-    }
+    GMX_RELEASE_ASSERT(impl_ != NULL, "Invalid data handle used");
+    GMX_RELEASE_ASSERT(impl_->currentFrame_ == NULL,
+                       "startFrame() called twice without calling finishFrame()");
+    impl_->currentFrame_ =
+        &impl_->data_.impl_->storage_.startFrame(index, x, dx);
 }
 
 
 void
-AnalysisDataHandle::addPoint(int col, real y, real dy, bool present)
+AnalysisDataHandle::selectDataSet(int index)
 {
-    if (_impl->_data.isMultipoint())
-    {
-        _impl->_data.notifyPointsAdd(col, 1, &y, &dy, &present);
-    }
-    else
-    {
-        GMX_ASSERT(!_impl->_frame->_present[col],
-                   "Data for a column set multiple times");
-        _impl->_frame->_y[col] = y;
-        _impl->_frame->_dy[col] = dy;
-        _impl->_frame->_present[col] = present;
-    }
+    GMX_RELEASE_ASSERT(impl_ != NULL, "Invalid data handle used");
+    GMX_RELEASE_ASSERT(impl_->currentFrame_ != NULL,
+                       "selectDataSet() called without calling startFrame()");
+    impl_->currentFrame_->selectDataSet(index);
 }
 
 
 void
-AnalysisDataHandle::addPoints(int firstcol, int n,
-                              const real *y, const real *dy,
-                              const bool *present)
+AnalysisDataHandle::setPoint(int column, real value, bool bPresent)
 {
-    if (_impl->_data.isMultipoint())
-    {
-        _impl->_data.notifyPointsAdd(firstcol, n, y, dy, present);
-    }
-    else
-    {
-        for (int i = 0; i < n; ++i)
-        {
-            addPoint(firstcol + i, y[i], dy ? dy[i] : 0.0,
-                     present ? present[i] : true);
-        }
-    }
+    GMX_RELEASE_ASSERT(impl_ != NULL, "Invalid data handle used");
+    GMX_RELEASE_ASSERT(impl_->currentFrame_ != NULL,
+                       "setPoint() called without calling startFrame()");
+    impl_->currentFrame_->setValue(column, value, bPresent);
 }
 
 
 void
-AnalysisDataHandle::finishFrame()
+AnalysisDataHandle::setPoint(int column, real value, real error, bool bPresent)
 {
-    if (_impl->_data.isMultipoint())
-    {
-        _impl->_data.notifyFrameFinish();
-    }
-    else
+    GMX_RELEASE_ASSERT(impl_ != NULL, "Invalid data handle used");
+    GMX_RELEASE_ASSERT(impl_->currentFrame_ != NULL,
+                       "setPoint() called without calling startFrame()");
+    impl_->currentFrame_->setValue(column, value, error, bPresent);
+}
+
+
+void
+AnalysisDataHandle::setPoints(int firstColumn, int count, const real *values)
+{
+    GMX_RELEASE_ASSERT(impl_ != NULL, "Invalid data handle used");
+    GMX_RELEASE_ASSERT(impl_->currentFrame_ != NULL,
+                       "setPoints() called without calling startFrame()");
+    for (int i = 0; i < count; ++i)
     {
-        _impl->_data._impl->addPendingFrame(_impl->_frame.get());
+        impl_->currentFrame_->setValue(firstColumn + i, values[i]);
     }
 }
 
 
 void
-AnalysisDataHandle::addFrame(int index, real x, const real *y, const real *dy,
-                             const bool *present)
+AnalysisDataHandle::finishPointSet()
 {
-    addFrame(index, x, 0.0, y, dy, present);
+    GMX_RELEASE_ASSERT(impl_ != NULL, "Invalid data handle used");
+    GMX_RELEASE_ASSERT(impl_->data_.isMultipoint(),
+                       "finishPointSet() called for non-multipoint data");
+    GMX_RELEASE_ASSERT(impl_->currentFrame_ != NULL,
+                       "finishPointSet() called without calling startFrame()");
+    impl_->currentFrame_->finishPointSet();
 }
 
 
 void
-AnalysisDataHandle::addFrame(int index, real x, real dx,
-                             const real *y, const real *dy,
-                             const bool *present)
+AnalysisDataHandle::finishFrame()
 {
-    startFrame(index, x, dx);
-    addPoints(0, _impl->_data.columnCount(), y, dy, present);
-    finishFrame();
+    GMX_RELEASE_ASSERT(impl_ != NULL, "Invalid data handle used");
+    GMX_RELEASE_ASSERT(impl_->currentFrame_ != NULL,
+                       "finishFrame() called without calling startFrame()");
+    AnalysisDataStorageFrame *frame = impl_->currentFrame_;
+    impl_->currentFrame_ = NULL;
+    frame->finishFrame();
 }
 
 
 void
 AnalysisDataHandle::finishData()
 {
-    // Calls delete this
-    _impl->_data.finishData(this);
+    GMX_RELEASE_ASSERT(impl_ != NULL, "Invalid data handle used");
+    // Deletes the implementation pointer.
+    impl_->data_.finishData(*this);
+    impl_ = NULL;
 }
 
 } // namespace gmx