UniqueBorderPartition.hpp 7.62 KB
Newer Older
1
2
3
4
5
6
7
8
#pragma once

#include <cassert>
#include <set>

#include <dune/common/unused.hh>
#include <dune/grid/common/datahandleif.hh>

9
10
11
12
13
14
namespace Dune
{
  // forward declaration
  template <int dim> class UGGrid;
}

15
16
17
18
19
20
21
22
namespace AMDiS
{
  /// \brief Determine for each border entity which processor owns it
  /**
   * All entities must be uniquely owned by exactly one processor, but they can
   * exist on multiple processors. For interior, overlap, and ghost entities the
   * assignment is trivial: interior: owner, otherwise not owner.
   *
23
24
   * For border entities (codim != 0) the ownership is not known a priori and must
   * be communicated. Here we assign the entity to the processor with the lowest rank.
25
26
   **/
  template <class Grid>
27
28
  class UniqueBorderPartition
      : public Dune::CommDataHandleIF<UniqueBorderPartition<Grid>, int>
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  {
    using IdSet = typename Grid::GlobalIdSet;
    using IdType = typename IdSet::IdType;

  public:
    using EntitySet = std::set<IdType>;

  public:
    /// \brief Construct a UniqueBorderPartition DataHandle to be used in a GridView
    /// communicator.
    /**
     * \param rank            The own processor rank
     * \param idSet           The id set of entity ids to store in borderEntities,
     *                        typically the grid globalIdSet.
     *
     * NOTE: Since idSet is stored by reference it must not go out of scope
     * until all calls to \ref gather and \ref scatter are finished.
     **/
47
    UniqueBorderPartition(int rank, Grid const& grid)
48
      : myrank_(rank)
49
      , idSet_(grid.globalIdSet())
50
51
    {}

52
53
    // Communicate all entities
    bool contains(int /*dim*/, int /*codim*/) const { return true; }
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

    // communicate exactly one integer, the rank
    bool fixedSize(int /*dim*/, int /*codim*/) const { return true; }

    // Always contains one int, the rank
    template <class Entity>
    std::size_t size(Entity const& e) const { return 1; }

    template <class MessageBuffer, class Entity>
    void gather(MessageBuffer& buff, Entity const& e) const
    {
      buff.write(myrank_);
    }

    template <class MessageBuffer, class Entity>
    void scatter(MessageBuffer& buff, Entity const& e, std::size_t n)
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
    {
      scatterImpl(buff, e, n, int_t<Entity::codimension>{});
    }

    /// Returns whether id is owned by this rank
    bool contains(IdType const& id) const
    {
      return notOwner_.count(id) == 0;
    }

    /// Number of iterations to perform the communication in order to collect all neighboring entities
    int numIterations() const
    {
      return 1;
    }

  private:

    template <class MessageBuffer, class Entity, int cd>
    void scatterImpl(MessageBuffer& buff, Entity const& e, std::size_t n, int_t<cd>)
    {
      DUNE_UNUSED_PARAMETER(n); // n == 1
      assert(n == 1);

      int rank = 0;
      buff.read(rank);

      // insert only border entities that are owned by other processors, i.e. rank > myrank
      // Those entities are not owned by this rank.
      if (rank > myrank_)
        notOwner_.insert(idSet_.id(e));
    }

    template <class MessageBuffer, class Entity>
    void scatterImpl(MessageBuffer& buff, Entity const& e, std::size_t n, int_t<0>)
    {
      DUNE_UNUSED_PARAMETER(n); // n == 1
      assert(n == 1);

      int rank = 0;
      buff.read(rank);

      // insert only border entities that are owned by other processors, i.e. rank > myrank
      // Those entities are not owned by this rank.
      if (rank > myrank_) {
        for (int codim = 1; codim <= Grid::dimension; ++codim) {
          for (int i = 0; i < int(e.subEntities(codim)); ++i) {
            notOwner_.insert(idSet_.subId(e, i, codim));
          }
        }
      }
    }

  private:
    int myrank_;
    EntitySet notOwner_;
    IdSet const& idSet_;
  };


  /// Specialization for UGGrid that can not communicate over edges.
  template <int dim>
  class UniqueBorderPartition<Dune::UGGrid<dim>>
      : public Dune::CommDataHandleIF<UniqueBorderPartition<Dune::UGGrid<dim>>, int>
  {
    using Grid = Dune::UGGrid<dim>;
    using IdSet = typename Grid::LocalIdSet;
    using IdType = typename IdSet::IdType;

  public:
    using EntitySet = std::set<IdType>;

  public:
    /// \brief Construct a UniqueBorderPartition DataHandle to be used in a GridView
    /// communicator.
    /**
     * \param rank            The own processor rank
     * \param idSet           The id set of entity ids to store in borderEntities,
     *                        typically the grid globalIdSet.
     *
     * NOTE: Since idSet is stored by reference it must not go out of scope
     * until all calls to \ref gather and \ref scatter are finished.
     **/
    UniqueBorderPartition(int rank, Grid const& grid)
      : myrank_(rank)
      , idSet_(grid.localIdSet())
    {}

    // Communicate all entities
    bool contains(int /*dim*/, int codim) const { return true; }

    // see size()
    bool fixedSize(int /*dim*/, int codim) const { return codim != 0; }

    // for codim=0 elements communicate data for all subEntities, otherwise communicate just the owner rank
    template <class Entity>
    std::size_t size(Entity const& e) const
    {
      if (Entity::codimension != 0)
        return 1;

      std::size_t s = 0;
      for (int codim = 1; codim <= Grid::dimension; ++codim)
        s += e.subEntities(codim);
      return s;
    }

    template <class MessageBuffer, class Entity>
    void gather(MessageBuffer& buff, Entity const& e) const
    {
      gatherImpl(buff, e, int_t<Entity::codimension>{});
    }

    template <class MessageBuffer, class Entity>
    void scatter(MessageBuffer& buff, Entity const& e, std::size_t n)
    {
      scatterImpl(buff, e, n, int_t<Entity::codimension>{});
    }

    /// Returns whether id is in EntitySet
    bool contains(IdType const& id) const
    {
      return entityToRank_[id] == myrank_;
    }

    /// Number of iterations to perform the communication in order to collect all neighboring entities
    int numIterations() const
    {
      return Grid::dimension;
    }

  private:
    template <class MessageBuffer, class Entity, int cd>
    void gatherImpl(MessageBuffer& buff, Entity const& e, int_t<cd>) const
    {
      int& rank = entityToRank_[idSet_.id(e)];
      rank = std::max(rank, myrank_);
      buff.write(rank);
    }

    template <class MessageBuffer, class Entity>
    void gatherImpl(MessageBuffer& buff, Entity const& e, int_t<0>) const
    {
      // maybe use global unique numbering (?)
      for (int codim = 1; codim <= Grid::dimension; ++codim) {
        for (int i = 0; i < int(e.subEntities(codim)); ++i) {
          int& rank = entityToRank_[idSet_.subId(e, i, codim)];
          rank = std::max(rank, myrank_);
          buff.write(rank);
        }
      }
    }

    template <class MessageBuffer, class Entity, int cd>
    void scatterImpl(MessageBuffer& buff, Entity const& e, std::size_t n, int_t<cd>)
225
226
227
228
229
230
    {
      DUNE_UNUSED_PARAMETER(n); // n == 1
      assert(n == 1);

      int rank = 0;
      buff.read(rank);
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249

      int& storedRank = entityToRank_[idSet_.id(e)];
      storedRank = std::max(rank, storedRank);
    }

    template <class MessageBuffer, class Entity>
    void scatterImpl(MessageBuffer& buff, Entity const& e, std::size_t n, int_t<0>)
    {
      std::size_t j = 0;
      for (int codim = 1; codim <= Grid::dimension; ++codim) {
        for (int i = 0; i < int(e.subEntities(codim)); ++i, ++j) {
          assert(j < n);
          int rank = 0;
          buff.read(rank);

          int& storedRank = entityToRank_[idSet_.subId(e, i, codim)];
          storedRank = std::max(rank, storedRank);
        }
      }
250
251
252
253
    }

  private:
    int myrank_;
254
    mutable std::map<IdType, int> entityToRank_;
255
256
257
258
    IdSet const& idSet_;
  };

} // end namespace AMDiS
259