This file is indexed.

/usr/include/dune/grid/test/checkcomcorrectness.hh is in libdune-grid-dev 2.5.1-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#ifndef DUNE_GRID_TEST_CHECKCOMMCORRECTNESS_HH
#define DUNE_GRID_TEST_CHECKCOMMCORRECTNESS_HH

#include <config.h>

#include <iostream>
#include <fstream>
#include <sstream>
#include <unordered_set>
#include <unordered_map>
#include <unistd.h>

#include <dune/common/hash.hh>
#include <dune/common/float_cmp.hh>
#include <dune/geometry/dimension.hh>
#include <dune/grid/common/capabilities.hh>
#include <dune/grid/common/datahandleif.hh>
#include <dune/grid/common/gridenums.hh>
#include <dune/grid/common/mcmgmapper.hh>

namespace Dune {

  std::size_t hash_value(const PartitionType& pt)
  {
    return static_cast<std::size_t>(pt);
  }

}

DUNE_DEFINE_HASH(DUNE_HASH_TEMPLATE_ARGS(),DUNE_HASH_TYPE(Dune::PartitionType))

namespace Dune {

  namespace GridCheck {

      struct SymmetryVerifyingDataHandle
        : public Dune::CommDataHandleIF<SymmetryVerifyingDataHandle,std::size_t>
      {

        bool contains(int dim, int codim) const
        {
          return codim == _codim;
        }

        bool fixedsize(int dim, int codim) const
        {
          return _fixed_size;
        }

        template<typename E>
        std::size_t size(const E& e) const
        {
          return _writes_per_cell;
        }

        template<typename Buf, typename E>
        void gather(Buf& buf, const E& e) const
        {
          for (std::size_t i = 0; i < _writes_per_cell; ++i)
            buf.write(std::size_t(42));
          _writes += _writes_per_cell;
        }

        template<typename Buf, typename E>
        void scatter(Buf& buf, const E& e, std::size_t n) const
        {
          assert(_writes_per_cell == n);
          for (std::size_t i = 0; i < _writes_per_cell; ++i)
            {
              std::size_t tmp = 0;
              buf.read(tmp);
              assert(tmp == 42);
            }
          _reads += _writes_per_cell;
        }

        SymmetryVerifyingDataHandle(int codim, bool fixed_size, std::size_t writes_per_cell)
          : _codim(codim)
          , _fixed_size(fixed_size)
          , _writes_per_cell(writes_per_cell)
          , _reads(0)
          , _writes(0)
        {}

        const int _codim;
        const bool _fixed_size;
        const std::size_t _writes_per_cell;
        mutable std::size_t _reads;
        mutable std::size_t _writes;

      };

      template<int dim>
      struct CodimLayout
      {

        bool contains(Dune::GeometryType gt) const
        {
          return gt.dim() == dim - _codim;
          }

        int _codim;

      };

      template<typename GV>
      struct CommunicationTestDataHandle
        : public Dune::CommDataHandleIF<CommunicationTestDataHandle<GV>,
                                  typename GV::template Codim<0>::Geometry::GlobalCoordinate>
      {

        bool contains(int dim, int codim) const
        {
          return codim == _codim;
        }

        bool fixedsize(int dim, int codim) const
        {
          return true;
        }

        template<typename E>
        std::size_t size(const E& e) const
        {
          return 1;
        }

        template<typename Buf, typename E>
        void gather(Buf& buf, const E& e) const
        {
          assert(_allowed_writes.count(e.partitionType()) > 0);
          auto center = e.geometry().center();
          buf.write(e.geometry().center());
          if (_gv.comm().rank() == 0)
            std::cout << "Gathering from entity " << _mapper.index(e) << " at " << e.geometry().center() << std::endl;
          ++_writes[_mapper.index(e)];
          center -= _coords[_mapper.index(e)];
          assert(Dune::FloatCmp::eq(center.two_norm(),0.0));
        }

        template<typename Buf, typename E>
        void scatter(Buf& buf, const E& e, std::size_t n) const
        {
          assert(_allowed_reads.count(e.partitionType()) > 0);
          typename E::Geometry::GlobalCoordinate data;
          buf.read(data);
          ++_reads[_mapper.index(e)];
          auto center = e.geometry().center();
          data -= e.geometry().center();
          assert(Dune::FloatCmp::eq(data.two_norm(),0.0));
          center -= _coords[_mapper.index(e)];
          assert(Dune::FloatCmp::eq(center.two_norm(),0.0));
        }

        template<int cd, typename Check>
        void verify(Dune::Codim<cd> codim, Check check)
        {
          assert(codim == _codim);
          for (const auto& e : entities(_gv,codim))
            {
              if (_gv.comm().rank() == 0)
                std::cout << "Entity of codim " << cd
                          << " at " << e.geometry().center()
                          << " with partition " << PartitionName(e.partitionType())
                          << " and " << _writes[_mapper.index(e)] << " / " << _reads[_mapper.index(e)] << " writes / reads"
                          << std::endl;
              check(
                e.partitionType(),
                _allowed_writes.count(e.partitionType()) > 0,
                _allowed_reads.count(e.partitionType()) > 0,
                _writes[_mapper.index(e)],
                _reads[_mapper.index(e)]
                );
            }
        }

        CommunicationTestDataHandle(GV gv, int codim, const std::unordered_set<Dune::PartitionType>& allowed_writes, const std::unordered_set<Dune::PartitionType>& allowed_reads, const std::vector<typename GV::template Codim<0>::Geometry::GlobalCoordinate>& coords)
          : _gv(gv)
          , _codim(codim)
          , _mapper(gv,{_codim})
          , _allowed_writes(allowed_writes)
          , _allowed_reads(allowed_reads)
          , _reads(_mapper.size(),0)
          , _writes(_mapper.size(),0)
          , _coords(coords)
        {}

        GV _gv;
        const int _codim;
        Dune::MultipleCodimMultipleGeomTypeMapper<GV,CodimLayout> _mapper;
        const std::unordered_set<Dune::PartitionType> _allowed_writes;
        const std::unordered_set<Dune::PartitionType> _allowed_reads;
        mutable std::vector<std::size_t> _reads;
        mutable std::vector<std::size_t> _writes;
        const std::vector<typename GV::template Codim<0>::Geometry::GlobalCoordinate>& _coords;

      };

      template<typename GV, int cd>
      void check_communication_correctness_do(GV gv, Codim<cd> codim)
      {
        if (gv.grid().comm().rank() == 0)
          {
            std::cout << "Checking codim " << cd << std::endl;
          }

        std::unordered_map<Dune::PartitionType,std::size_t> count;

        Dune::MultipleCodimMultipleGeomTypeMapper<GV,CodimLayout> mapper(gv,{codim});

        std::vector<
          typename GV::template Codim<0>::Geometry::GlobalCoordinate
          > coords(mapper.size());

        // start by counting entities by partition type and storing entity positions
        for (const auto& e : entities(gv,codim))
          {
            ++count[e.partitionType()];
            coords[mapper.index(e)] = e.geometry().center();
          }

        {
          SymmetryVerifyingDataHandle dh_forward(cd,false,3);
          gv.communicate(dh_forward,InteriorBorder_InteriorBorder_Interface,ForwardCommunication);

          SymmetryVerifyingDataHandle dh_backward(cd,true,3);
          gv.communicate(dh_backward,InteriorBorder_InteriorBorder_Interface,BackwardCommunication);

          if (count[BorderEntity] > 0)
            {
              assert(dh_forward._writes == dh_forward._reads);
              assert(dh_backward._writes == dh_backward._reads);

              assert(dh_forward._writes == dh_backward._writes);
              assert(dh_backward._writes == dh_backward._writes);
            }

          if (gv.grid().comm().size() == 2)
            {
              if (gv.comm().rank() == 0)
                std::cout << "MPI size == 2, checking writes (" << (dh_forward._writes / 3)
                          << ") against count of border entities (" <<  count[BorderEntity] << ")"
                          << std::endl;
              assert((dh_forward._writes / 3) == count[BorderEntity]);
            }

        }

        {
          SymmetryVerifyingDataHandle dh_forward(cd,true,7);
          gv.communicate(dh_forward,All_All_Interface,ForwardCommunication);

          SymmetryVerifyingDataHandle dh_backward(cd,false,7);
          gv.communicate(dh_backward,All_All_Interface,BackwardCommunication);

          if (count[BorderEntity] > 0)
            {
              assert(dh_forward._writes == dh_forward._reads);
              assert(dh_backward._writes == dh_backward._reads);

              assert(dh_forward._writes == dh_backward._writes);
              assert(dh_backward._writes == dh_backward._writes);
            }

        }

        using PTSet = std::unordered_set<PartitionType>;

        {

          PTSet writers({InteriorEntity,BorderEntity});
          PTSet readers({InteriorEntity,BorderEntity,OverlapEntity,FrontEntity,GhostEntity});

          CommunicationTestDataHandle<GV> dh(gv,codim,writers,readers,coords);
          gv.communicate(dh,InteriorBorder_All_Interface,ForwardCommunication);

        }

        {

          PTSet writers({InteriorEntity,BorderEntity,OverlapEntity,FrontEntity,GhostEntity});
          PTSet readers({InteriorEntity,BorderEntity});

          CommunicationTestDataHandle<GV> dh(gv,codim,writers,readers,coords);
          gv.communicate(dh,InteriorBorder_All_Interface,BackwardCommunication);

        }

        {

          PTSet writers({InteriorEntity,BorderEntity});
          PTSet readers({InteriorEntity,BorderEntity});

          CommunicationTestDataHandle<GV> dh(gv,codim,writers,readers,coords);
          gv.communicate(dh,InteriorBorder_InteriorBorder_Interface,ForwardCommunication);
          dh.verify(
            codim,
            [](PartitionType partition, bool write_allowed, bool read_allowed, std::size_t writes, std::size_t reads)
            {
              if (partition == BorderEntity)
                {
                  assert(writes > 0);
                  assert(reads > 0);
                  assert(writes == reads);
                }
              else
                {
                  assert(writes == 0);
                  assert(reads == 0);
                }
            });
        }
      }

      // Need a forward declaration here
      template<typename GV, int cd>
      void check_communication_correctness_iter(GV gv, Codim<cd> codim);

      // Statically iterate over all codimensions
      template<typename GV, int cd>
      void check_communication_correctness_iter(GV gv, Codim<cd> codim, std::true_type, std::true_type) {}
      template<typename GV, int cd>
      void check_communication_correctness_iter(GV gv, Codim<cd> codim, std::false_type, std::true_type) {}

      template<typename GV, int cd>
      void check_communication_correctness_iter(GV gv, Codim<cd> codim, std::true_type, std::false_type) {
        check_communication_correctness_do(gv, codim);
        check_communication_correctness_iter (gv, Codim<cd+1>());
      }
      template<typename GV, int cd>
      void check_communication_correctness_iter(GV gv, Codim<cd> codim, std::false_type, std::false_type) {
        check_communication_correctness_iter (gv, Codim<cd+1>());
      }

      template<typename GV, int cd>
      void check_communication_correctness_iter(GV gv, Codim<cd> codim) {
        check_communication_correctness_iter (gv, codim,
              std::integral_constant<bool, Dune::Capabilities::hasEntity<typename GV::Grid, cd>::v>(),
              std::integral_constant<bool, cd == GV::dimension + 1>());
      }

      // Start with codim 0
      template<typename GV>
      void check_communication_correctness(GV gv)
      {
        check_communication_correctness_iter (gv, Codim<0>());
      }

  } // namespace GridCheck
} // namespace Dune

#endif