threadedentityiterator.hh
Go to the documentation of this file.
1 // -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 // vi: set et ts=4 sw=4 sts=4:
3 /*
4  Copyright (C) 2014 by Andreas Lauser
5 
6  This file is part of the Open Porous Media project (OPM).
7 
8  OPM is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 2 of the License, or
11  (at your option) any later version.
12 
13  OPM is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with OPM. If not, see <http://www.gnu.org/licenses/>.
20 */
25 #ifndef EWOMS_THREADED_ENTITY_ITERATOR_HH
26 #define EWOMS_THREADED_ENTITY_ITERATOR_HH
27 
28 #include <ewoms/parallel/locks.hh>
29 
30 namespace Ewoms {
31 
38 template <class GridView, int codim>
40 {
41  typedef typename GridView::template Codim<codim>::Entity Entity;
42  typedef typename GridView::template Codim<codim>::Iterator EntityIterator;
43 public:
44  ThreadedEntityIterator(const GridView& gridView)
45  : gridView_(gridView)
46  , sequentialIt_(gridView.template begin<codim>())
47  , sequentialEnd_(gridView.template end<codim>())
48  { }
49 
50  ThreadedEntityIterator(const ThreadedEntityIterator &other) = default;
51 
52  // begin iterating over the grid in parallel
53  void beginParallel(EntityIterator& threadPrivateIt)
54  {
55  mutex_.lock();
56  threadPrivateIt = sequentialIt_;
57  if (sequentialIt_ != sequentialEnd_)
58  ++sequentialIt_;
59  mutex_.unlock();
60  }
61 
62  // returns true if the last element was reached
63  bool isFinished(const EntityIterator& threadPrivateIt) const
64  { return threadPrivateIt == sequentialEnd_; }
65 
66  // prefix increment: goes to the next element which is not yet worked on by any
67  // thread
68  void increment(EntityIterator& threadPrivateIt)
69  {
70  mutex_.lock();
71  threadPrivateIt = sequentialIt_;
72  if (sequentialIt_ != sequentialEnd_)
73  ++sequentialIt_;
74  mutex_.unlock();
75  }
76 
77 private:
78  GridView gridView_;
79  EntityIterator sequentialIt_;
80  EntityIterator sequentialEnd_;
81 
82  OmpMutex mutex_;
83 };
84 } // namespace Ewoms
85 
86 #endif
void beginParallel(EntityIterator &threadPrivateIt)
Definition: threadedentityiterator.hh:53
This file implements various objects which provide mutual exclusion capabilities for multi-threaded a...
void lock()
Definition: locks.hh:40
void unlock()
Definition: locks.hh:41
Definition: baseauxiliarymodule.hh:35
Provides an STL-iterator like interface to iterate over the enties of a GridView in OpenMP threaded a...
Definition: threadedentityiterator.hh:39
ThreadedEntityIterator(const GridView &gridView)
Definition: threadedentityiterator.hh:44
Implements a shallow wrapper around the "raw" locks provided by OpenMP.
Definition: locks.hh:35
void increment(EntityIterator &threadPrivateIt)
Definition: threadedentityiterator.hh:68
bool isFinished(const EntityIterator &threadPrivateIt) const
Definition: threadedentityiterator.hh:63