MeshStructureFreudenthal2D.h 11.1 KB
Newer Older
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
//============================================================================
//  Copyright (c) Kitware, Inc.
//  All rights reserved.
//  See LICENSE.txt for details.
//  This software is distributed WITHOUT ANY WARRANTY; without even
//  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
//  PURPOSE.  See the above copyright notice for more information.
//
//  Copyright 2014 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
//  Copyright 2014 UT-Battelle, LLC.
//  Copyright 2014 Los Alamos National Security.
//
//  Under the terms of Contract DE-NA0003525 with NTESS,
//  the U.S. Government retains certain rights in this software.
//
//  Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
//  Laboratory (LANL), the U.S. Government retains certain rights in
//  this software.
//============================================================================
// Copyright (c) 2018, The Regents of the University of California, through
// Lawrence Berkeley National Laboratory (subject to receipt of any required approvals
// from the U.S. Dept. of Energy).  All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// (1) Redistributions of source code must retain the above copyright notice, this
//     list of conditions and the following disclaimer.
//
// (2) Redistributions in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other materials provided with the distribution.
//
// (3) Neither the name of the University of California, Lawrence Berkeley National
//     Laboratory, U.S. Dept. of Energy nor the names of its contributors may be
//     used to endorse or promote products derived from this software without
//     specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
// OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
// OF THE POSSIBILITY OF SUCH DAMAGE.
//
//=============================================================================
//
//  This code is an extension of the algorithm presented in the paper:
//  Parallel Peak Pruning for Scalable SMP Contour Tree Computation.
//  Hamish Carr, Gunther Weber, Christopher Sewell, and James Ahrens.
//  Proceedings of the IEEE Symposium on Large Data Analysis and Visualization
//  (LDAV), October 2016, Baltimore, Maryland.
//
//  The PPP2 algorithm and software were jointly developed by
//  Hamish Carr (University of Leeds), Gunther H. Weber (LBNL), and
//  Oliver Ruebel (LBNL)
//==============================================================================

63
64
#ifndef vtkm_worklet_contourtree_augmented_mesh_dem_MeshStructureFreudenthal2D_h
#define vtkm_worklet_contourtree_augmented_mesh_dem_MeshStructureFreudenthal2D_h
65
66
67
68

#include <vtkm/Pair.h>
#include <vtkm/Types.h>
#include <vtkm/cont/ArrayHandle.h>
Robert Maynard's avatar
Robert Maynard committed
69
#include <vtkm/worklet/contourtree_augmented/Types.h>
70
#include <vtkm/worklet/contourtree_augmented/mesh_dem/MeshStructure2D.h>
Robert Maynard's avatar
Robert Maynard committed
71
#include <vtkm/worklet/contourtree_augmented/mesh_dem_meshtypes/freudenthal_2D/Types.h>
72
73
74
75
76
77


namespace vtkm
{
namespace worklet
{
Robert Maynard's avatar
Robert Maynard committed
78
namespace contourtree_augmented
79
80
81
82
{

// Worklet for computing the sort indices from the sort order
template <typename DeviceAdapter>
83
class MeshStructureFreudenthal2D : public mesh_dem::MeshStructure2D<DeviceAdapter>
84
85
{
public:
86
87
88
89
90
  using sortIndicesPortalType =
    typename IdArrayType::template ExecutionTypes<DeviceAdapter>::PortalConst;
  using edgeBoundaryDetectionMasksPortalType =
    typename m2d_freudenthal::edgeBoundaryDetectionMasksType::template ExecutionTypes<
      DeviceAdapter>::PortalConst;
91
92
93

  // Default constucture. Needed for the CUDA built to work
  VTKM_EXEC_CONT
94
95
  MeshStructureFreudenthal2D()
    : mesh_dem::MeshStructure2D<DeviceAdapter>()
96
    , getMax(false)
97
    , nIncidentEdges(m2d_freudenthal::N_INCIDENT_EDGES)
98
99
100
  {
  }

luz.paz's avatar
luz.paz committed
101
  // Main constructor used in the code
102
103
104
105
106
107
108
109
  MeshStructureFreudenthal2D(
    vtkm::Id nrows,
    vtkm::Id ncols,
    vtkm::Int32 nincident_edges,
    bool getmax,
    const IdArrayType& sortIndices,
    const m2d_freudenthal::edgeBoundaryDetectionMasksType& edgeBoundaryDetectionMasksIn)
    : mesh_dem::MeshStructure2D<DeviceAdapter>(nrows, ncols)
110
111
112
113
114
115
116
117
118
    , getMax(getmax)
    , nIncidentEdges(nincident_edges)
  {
    sortIndicesPortal = sortIndices.PrepareForInput(DeviceAdapter());
    edgeBoundaryDetectionMasksPortal =
      edgeBoundaryDetectionMasksIn.PrepareForInput(DeviceAdapter());
  }

  VTKM_EXEC
119
  constexpr vtkm::Id GetMaxNumberOfNeighbours() const { return m2d_freudenthal::N_INCIDENT_EDGES; }
120
121
122
123
124
125
126
127

  VTKM_EXEC
  inline vtkm::Id GetNeighbourIndex(vtkm::Id vertex, vtkm::Id edgeNo) const
  { // GetNeighbourIndex
    switch (edgeNo)
    {
      case 0:
        return vertex + 1;
128
      //break; // row    , col + 1
129
130
      case 1:
        return vertex + this->nCols + 1;
131
      //break; // row + 1, col + 1
132
133
      case 2:
        return vertex + this->nCols;
134
      //break; // row + 1, col
135
136
      case 3:
        return vertex - 1;
137
      //break; // row    , col - 1
138
139
      case 4:
        return vertex - this->nCols - 1;
140
      //break; // row - 1, col - 1
141
142
      case 5:
        return vertex - this->nCols;
143
      //break; // row - 1, col
144
      default:
145
146
        VTKM_ASSERT(false);
        return vertex;
147
148
149
150
    }
  } // GetNeighbourIndex


151
152
153
154
155
156
157
158
159
160
161
// Disable conversion warnings for Add, Subtract, Multiply, Divide on GCC only.
// GCC creates false positive warnings for signed/unsigned char* operations.
// This occurs because the values are implicitly casted up to int's for the
// operation, and than  casted back down to char's when return.
// This causes a false positive warning, even when the values is within
// the value types range
#if (defined(VTKM_GCC) || defined(VTKM_CLANG))
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
#endif // gcc || clang

162
163
164
  // sets outgoing paths for saddles
  VTKM_EXEC
  inline vtkm::Id GetExtremalNeighbour(vtkm::Id vertex) const
165
166
167
  {
    using namespace m2d_freudenthal;
    // operator()
168
169
170
171
172
173
    // convert to a sort index
    vtkm::Id sortIndex = sortIndicesPortal.Get(vertex);

    // get the row and column
    vtkm::Id row = this->vertexRow(vertex);
    vtkm::Id col = this->vertexColumn(vertex);
174
175
176
    vtkm::Int8 boundaryConfig = ((col == 0) ? leftBit : 0) |
      ((col == this->nCols - 1) ? rightBit : 0) | ((row == 0) ? topBit : 0) |
      ((row == this->nRows - 1) ? bottomBit : 0);
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

    // in what follows, the boundary conditions always reset wasAscent
    for (vtkm::Id edgeNo = 0; edgeNo < this->nIncidentEdges; edgeNo++)
    { // per edge
      // ignore if at edge of data
      if (!(boundaryConfig & edgeBoundaryDetectionMasksPortal.Get(edgeNo)))
      {
        // calculate neighbour's ID and sort order
        vtkm::Id nbr = GetNeighbourIndex(vertex, edgeNo);

        // retrieve the sort index
        vtkm::Id nbrIndex = sortIndicesPortal.Get(nbr);

        // if it's not a valid destination, ignore it
        if (getMax ? (nbrIndex > sortIndex) : (nbrIndex < sortIndex))
        {
          // and save the destination
          return nbrIndex;
        }
      }
    } // per edge

    return sortIndex | TERMINAL_ELEMENT;
  } // operator()


  // NOTE/FIXME: The following also iterates over all values and could be combined with GetExtremalNeighbour(). However, the
  // results are needed at different places and splitting the two functions leads to a cleaner design
  VTKM_EXEC
  inline vtkm::Pair<vtkm::Id, vtkm::Id> GetNeighbourComponentsMaskAndDegree(
    vtkm::Id vertex,
    bool getMaxComponents) const
209
210
211
  {
    using namespace m2d_freudenthal;
    // GetNeighbourComponentsMaskAndDegree()
212
213
214
215
216
217
218
    // get data portals
    // convert to a sort index
    vtkm::Id sortIndex = sortIndicesPortal.Get(vertex);

    // get the row and column
    vtkm::Id row = this->vertexRow(vertex);
    vtkm::Id col = this->vertexColumn(vertex);
219
220
221
    vtkm::Int8 boundaryConfig = ((col == 0) ? leftBit : 0) |
      ((col == this->nCols - 1) ? rightBit : 0) | ((row == 0) ? topBit : 0) |
      ((row == this->nRows - 1) ? bottomBit : 0);
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240

    // and initialise the mask
    vtkm::Id neighbourhoodMask = 0;
    // in what follows, the boundary conditions always reset wasAscent
    for (vtkm::Id edgeNo = 0; edgeNo < N_INCIDENT_EDGES; edgeNo++)
    { // per edge
      // ignore if at edge of data
      if (!(boundaryConfig & edgeBoundaryDetectionMasksPortal.Get(edgeNo)))
      {
        // calculate neighbour's ID and sort order
        vtkm::Id nbr = GetNeighbourIndex(vertex, edgeNo);

        // retrieve the sort index
        vtkm::Id nbrIndex = sortIndicesPortal.Get(nbr);

        // if it's not a valid destination, ignore it
        if (getMaxComponents ? (nbrIndex > sortIndex) : (nbrIndex < sortIndex))
        {
          // now set the flag in the neighbourhoodMask
241
          neighbourhoodMask |= static_cast<vtkm::Id>(1) << edgeNo;
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
        }
      }
    } // per edge

    // we now know which edges are outbound, so we count to get the outdegree
    vtkm::Id outDegree = 0;
    vtkm::Id neighbourComponentMask = 0;
    // special case for local minimum
    if (neighbourhoodMask == 0x3F)
      outDegree = 1;
    else
    { // not a local minimum
      if ((neighbourhoodMask & 0x30) == 0x20)
      {
        ++outDegree;
257
        neighbourComponentMask |= static_cast<vtkm::Id>(1) << 5;
258
259
260
261
      }
      if ((neighbourhoodMask & 0x18) == 0x10)
      {
        ++outDegree;
262
        neighbourComponentMask |= static_cast<vtkm::Id>(1) << 4;
263
264
265
266
      }
      if ((neighbourhoodMask & 0x0C) == 0x08)
      {
        ++outDegree;
267
        neighbourComponentMask |= static_cast<vtkm::Id>(1) << 3;
268
269
270
271
      }
      if ((neighbourhoodMask & 0x06) == 0x04)
      {
        ++outDegree;
272
        neighbourComponentMask |= static_cast<vtkm::Id>(1) << 2;
273
274
275
276
      }
      if ((neighbourhoodMask & 0x03) == 0x02)
      {
        ++outDegree;
277
        neighbourComponentMask |= static_cast<vtkm::Id>(1) << 1;
278
279
280
281
      }
      if ((neighbourhoodMask & 0x21) == 0x01)
      {
        ++outDegree;
282
        neighbourComponentMask |= static_cast<vtkm::Id>(1) << 0;
283
284
285
286
287
288
      }
    } // not a local minimum

    return vtkm::make_Pair(neighbourComponentMask, outDegree);
  } // GetNeighbourComponentsMaskAndDegree()

289
290
291
292
#if (defined(VTKM_GCC) || defined(VTKM_CLANG))
#pragma GCC diagnostic pop
#endif // gcc || clang

293
294
295
296
297
298
299
300
private:
  sortIndicesPortalType sortIndicesPortal;
  edgeBoundaryDetectionMasksPortalType edgeBoundaryDetectionMasksPortal;
  bool getMax;
  vtkm::Id nIncidentEdges;

}; // ExecutionObjec_MeshStructure_3Dt

Robert Maynard's avatar
Robert Maynard committed
301
} // namespace contourtree_augmented
302
303
304
305
} // namespace worklet
} // namespace vtkm

#endif