opm-grid
ElementChunks.hpp
1 /*
2  Copyright 2025 SINTEF Digital
3 
4  This file is part of the Open Porous Media project (OPM).
5 
6  OPM is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  OPM is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with OPM. If not, see <http://www.gnu.org/licenses/>.
18 */
19 #ifndef OPM_ELEMENT_CHUNKS_HEADER
20 #define OPM_ELEMENT_CHUNKS_HEADER
21 
22 #include <opm/grid/utility/createThreadIterators.hpp>
23 
24 #include <cstddef>
25 #include <iterator>
26 #include <utility>
27 #include <vector>
28 
29 namespace Opm
30 {
31 
75 template <class GridView, class PartitionSet>
77 {
78 private:
79  using Iter = decltype(std::begin(elements(std::declval<const GridView&>(), PartitionSet())));
80  using Storage = std::vector<Iter>;
81  using StorageIter = decltype(Storage().cbegin());
82 
83 public:
84  ElementChunks(const GridView& gv,
85  const PartitionSet included_partition,
86  const std::size_t num_chunks)
87  {
88  grid_chunk_iterators_
89  = Opm::createChunkIterators(elements(gv, included_partition), gv.size(0), num_chunks);
90  }
91 
92  struct Chunk
93  {
94  Chunk(const Iter& i1, const Iter& i2) : pi_(i1, i2) {}
95  auto begin() const { return pi_.first; }
96  auto end() const { return pi_.second; }
97  std::pair<Iter, Iter> pi_;
98  };
99 
100  struct ChunkIterator : public StorageIter
101  {
102  explicit ChunkIterator(const StorageIter& itit) : StorageIter(itit) {}
103  Chunk operator*() const
104  {
105  const StorageIter it = *this;
106  return Chunk{*it, *(it+1)};
107  }
108  };
109 
110  auto begin() const
111  {
112  return ChunkIterator(grid_chunk_iterators_.begin());
113  }
114  auto end() const
115  {
116  // Note the decrement, we want a StorageIterator pointing to
117  // the last element in grid_chunk_iterators_, not the
118  // one-beyond-last-element iterator.
119  return ChunkIterator(--grid_chunk_iterators_.end());
120  }
121  auto size() const
122  {
123  return grid_chunk_iterators_.size() - 1;
124  }
125 private:
126  Storage grid_chunk_iterators_;
127 };
128 
129 
130 } // namespace Opm
131 
132 #endif // OPM_ELEMENT_CHUNKS_HEADER
Definition: ElementChunks.hpp:92
auto createChunkIterators(const Range &r, const std::size_t num_elem, const std::size_t num_chunks)
Create a vector containing a spread of iterators into the elements of the range, to facilitate for ex...
Definition: createThreadIterators.hpp:48
Holds the implementation of the CpGrid as a pimple.
Definition: CellQuadrature.cpp:71
Definition: ElementChunks.hpp:100
Class to simplify creating parallel yet performant threaded loops over a grid.
Definition: ElementChunks.hpp:76