VTK-m  2.0
Mesh2D_DEM_Triangulation.h
Go to the documentation of this file.
1 //============================================================================
2 // Copyright (c) Kitware, Inc.
3 // All rights reserved.
4 // See LICENSE.txt for details.
5 //
6 // This software is distributed WITHOUT ANY WARRANTY; without even
7 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8 // PURPOSE. See the above copyright notice for more information.
9 //============================================================================
10 // Copyright (c) 2016, Los Alamos National Security, LLC
11 // All rights reserved.
12 //
13 // Copyright 2016. Los Alamos National Security, LLC.
14 // This software was produced under U.S. Government contract DE-AC52-06NA25396
15 // for Los Alamos National Laboratory (LANL), which is operated by
16 // Los Alamos National Security, LLC for the U.S. Department of Energy.
17 // The U.S. Government has rights to use, reproduce, and distribute this
18 // software. NEITHER THE GOVERNMENT NOR LOS ALAMOS NATIONAL SECURITY, LLC
19 // MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY LIABILITY FOR THE
20 // USE OF THIS SOFTWARE. If software is modified to produce derivative works,
21 // such modified software should be clearly marked, so as not to confuse it
22 // with the version available from LANL.
23 //
24 // Additionally, redistribution and use in source and binary forms, with or
25 // without modification, are permitted provided that the following conditions
26 // are met:
27 //
28 // 1. Redistributions of source code must retain the above copyright notice,
29 // this list of conditions and the following disclaimer.
30 // 2. Redistributions in binary form must reproduce the above copyright notice,
31 // this list of conditions and the following disclaimer in the documentation
32 // and/or other materials provided with the distribution.
33 // 3. Neither the name of Los Alamos National Security, LLC, Los Alamos
34 // National Laboratory, LANL, the U.S. Government, nor the names of its
35 // contributors may be used to endorse or promote products derived from
36 // this software without specific prior written permission.
37 //
38 // THIS SOFTWARE IS PROVIDED BY LOS ALAMOS NATIONAL SECURITY, LLC AND
39 // CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
40 // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
41 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL LOS ALAMOS
42 // NATIONAL SECURITY, LLC OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
43 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
44 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
45 // USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
46 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
47 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
48 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
49 //============================================================================
50 
51 // This code is based on the algorithm presented in the paper:
52 // “Parallel Peak Pruning for Scalable SMP Contour Tree Computation.”
53 // Hamish Carr, Gunther Weber, Christopher Sewell, and James Ahrens.
54 // Proceedings of the IEEE Symposium on Large Data Analysis and Visualization
55 // (LDAV), October 2016, Baltimore, Maryland.
56 
57 //=======================================================================================
58 //
59 // COMMENTS:
60 //
61 // Essentially, a vector of data values. BUT we will want them sorted to simplify
62 // processing - i.e. it's the robust way of handling simulation of simplicity
63 //
64 // On the other hand, once we have them sorted, we can discard the original data since
65 // only the sort order matters
66 //
67 // Since we've been running into memory issues, we'll start being more careful.
68 // Clearly, we can eliminate the values if we sort, but in this iteration we are
69 // deferring doing a full sort, so we need to keep the values.
70 //
71 //=======================================================================================
72 
73 #ifndef vtkm_worklet_contourtree_mesh2d_dem_triangulation_h
74 #define vtkm_worklet_contourtree_mesh2d_dem_triangulation_h
75 
76 #include <vtkm/cont/Algorithm.h>
77 #include <vtkm/cont/ArrayCopy.h>
83 
90 
91 //#define DEBUG_PRINT 1
92 //#define DEBUG_TIMING 1
93 
94 namespace vtkm
95 {
96 namespace worklet
97 {
98 namespace contourtree
99 {
100 
101 template <typename T, typename StorageType>
103 {
104 public:
105  // original data array
107 
108  // size of the mesh
110 
111  // Array with neighbourhood masks
113 
114  // constructor
116  vtkm::Id NRows,
117  vtkm::Id NCols);
118 
119  // sets all vertices to point along an outgoing edge (except extrema)
120  void SetStarts(vtkm::cont::ArrayHandle<vtkm::Id>& chains, bool descending);
121 
122  // sets outgoing paths for saddles
123  void SetSaddleStarts(ChainGraph<T, StorageType>& mergeGraph, bool descending);
124 };
125 
126 // sets outgoing paths for saddles
127 template <typename T, typename StorageType>
129  bool ascending)
130 {
131  // create the neighbourhood mask
132  neighbourhoodMask.Allocate(NumVertices);
133 
134  // For each vertex set the next vertex in the chain
135  vtkm::cont::ArrayHandleIndex vertexIndexArray(NumVertices);
136  Mesh2D_DEM_VertexStarter<T> vertexStarter(nRows, nCols, ascending);
138  vertexStarter);
139 
140  vertexStarterDispatcher.Invoke(vertexIndexArray, // input
141  values, // input (whole array)
142  chains, // output
143  neighbourhoodMask); // output
144 } // SetStarts()
145 
146 // creates input mesh
147 template <typename T, typename StorageType>
150  vtkm::Id NRows,
151  vtkm::Id NCols)
152  : values(Values)
153  , nRows(NRows)
154  , nCols(NCols)
155 {
156  NumVertices = nRows * nCols;
157 
158  // compute the number of log-jumping steps (i.e. lg_2 (NumVertices))
159  nLogSteps = 1;
160  for (vtkm::Id shifter = NumVertices; shifter > 0; shifter >>= 1)
161  nLogSteps++;
162 }
163 
164 // sets outgoing paths for saddles
165 template <typename T, typename StorageType>
167  ChainGraph<T, StorageType>& mergeGraph,
168  bool ascending)
169 {
170  // we need a temporary inverse index to change vertex IDs
174  inverseIndex.Allocate(NumVertices);
175  isCritical.Allocate(NumVertices);
176  outdegree.Allocate(NumVertices);
177 
178  vtkm::cont::ArrayHandleIndex vertexIndexArray(NumVertices);
179  Mesh2D_DEM_VertexOutdegreeStarter vertexOutdegreeStarter(nRows, nCols, ascending);
181  vertexOutdegreeStarterDispatcher(vertexOutdegreeStarter);
182 
183  vertexOutdegreeStarterDispatcher.Invoke(vertexIndexArray, // input
184  neighbourhoodMask, // input
185  mergeGraph.arcArray, // input (whole array)
186  outdegree, // output
187  isCritical); // output
188 
189  vtkm::cont::Algorithm::ScanExclusive(isCritical, inverseIndex);
190 
191  // now we can compute how many critical points we carry forward
192  vtkm::Id nCriticalPoints = vtkm::cont::ArrayGetValue(NumVertices - 1, inverseIndex) +
193  vtkm::cont::ArrayGetValue(NumVertices - 1, isCritical);
194 
195  // allocate space for the join graph vertex arrays
196  mergeGraph.AllocateVertexArrays(nCriticalPoints);
197 
198  // compact the set of vertex indices to critical ones only
199  vtkm::cont::Algorithm::CopyIf(vertexIndexArray, isCritical, mergeGraph.valueIndex);
200 
201  // we initialise the prunesTo array to "NONE"
203  vtkm::cont::Algorithm::Copy(notAssigned, mergeGraph.prunesTo);
204 
205  // copy the outdegree from our temporary array
206  // : mergeGraph.outdegree[vID] <= outdegree[mergeGraph.valueIndex[vID]]
207  vtkm::cont::Algorithm::CopyIf(outdegree, isCritical, mergeGraph.outdegree);
208 
209  // copy the chain maximum from arcArray
210  // : mergeGraph.chainExtremum[vID] = inverseIndex[mergeGraph.arcArray[mergeGraph.valueIndex[vID]]]
213 
215  tArray.Allocate(nCriticalPoints);
216  vtkm::cont::Algorithm::CopyIf(mergeGraph.arcArray, isCritical, tArray);
217  vtkm::cont::Algorithm::Copy(PermuteIndexType(tArray, inverseIndex), mergeGraph.chainExtremum);
218 
219  // and set up the active vertices - initially to identity
220  vtkm::cont::ArrayHandleIndex criticalVertsIndexArray(nCriticalPoints);
221  vtkm::cont::Algorithm::Copy(criticalVertsIndexArray, mergeGraph.activeVertices);
222 
223  // now we need to compute the firstEdge array from the outdegrees
225 
226  vtkm::Id nCriticalEdges = vtkm::cont::ArrayGetValue(nCriticalPoints - 1, mergeGraph.firstEdge) +
227  vtkm::cont::ArrayGetValue(nCriticalPoints - 1, mergeGraph.outdegree);
228 
229  // now we allocate the edge arrays
230  mergeGraph.AllocateEdgeArrays(nCriticalEdges);
231 
232  // and we have to set them, so we go back to the vertices
233  Mesh2D_DEM_SaddleStarter saddleStarter(nRows, // input
234  nCols, // input
235  ascending); // input
237  saddleStarter);
238 
240  outDegFirstEdge = vtkm::cont::make_ArrayHandleZip(mergeGraph.outdegree, mergeGraph.firstEdge);
241 
242  saddleStarterDispatcher.Invoke(criticalVertsIndexArray, // input
243  outDegFirstEdge, // input (pair)
244  mergeGraph.valueIndex, // input
245  neighbourhoodMask, // input (whole array)
246  mergeGraph.arcArray, // input (whole array)
247  inverseIndex, // input (whole array)
248  mergeGraph.edgeNear, // output (whole array)
249  mergeGraph.edgeFar, // output (whole array)
250  mergeGraph.activeEdges); // output (whole array)
251 
252  // finally, allocate and initialise the edgeSorter array
253  vtkm::cont::ArrayCopy(mergeGraph.activeEdges, mergeGraph.edgeSorter);
254 } // SetSaddleStarts()
255 }
256 }
257 }
258 
259 #endif
vtkm::worklet::contourtree::Mesh2D_DEM_Triangulation::NumVertices
vtkm::Id NumVertices
Definition: Mesh2D_DEM_Triangulation.h:109
vtkm::worklet::contourtree::ChainGraph
Definition: ChainGraph.h:127
vtkm::worklet::contourtree::ChainGraph::prunesTo
vtkm::cont::ArrayHandle< vtkm::Id > prunesTo
Definition: ChainGraph.h:148
vtkm::cont::ArrayHandle< T, StorageType >
vtkm::worklet::contourtree::Mesh2D_DEM_Triangulation::neighbourhoodMask
vtkm::cont::ArrayHandle< vtkm::Id > neighbourhoodMask
Definition: Mesh2D_DEM_Triangulation.h:112
vtkm::worklet::contourtree::ChainGraph::AllocateEdgeArrays
void AllocateEdgeArrays(vtkm::Id Size)
Definition: ChainGraph.h:231
vtkm::worklet::contourtree::ChainGraph::activeVertices
vtkm::cont::ArrayHandle< vtkm::Id > activeVertices
Definition: ChainGraph.h:164
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
ChainGraph.h
vtkm::cont::ArrayHandle::Allocate
VTKM_CONT void Allocate(vtkm::Id numberOfValues, vtkm::CopyFlag preserve, vtkm::cont::Token &token) const
Allocates an array large enough to hold the given number of values.
Definition: ArrayHandle.h:465
vtkm::worklet::contourtree::Mesh2D_DEM_Triangulation::nCols
vtkm::Id nCols
Definition: Mesh2D_DEM_Triangulation.h:109
vtkm::worklet::contourtree::ChainGraph::chainExtremum
vtkm::cont::ArrayHandle< vtkm::Id > chainExtremum
Definition: ChainGraph.h:157
Mesh2D_DEM_VertexOutdegreeStarter.h
vtkm::cont::ArrayGetValue
VTKM_CONT T ArrayGetValue(vtkm::Id id, const vtkm::cont::ArrayHandle< T, S > &data)
Obtain a small set of values from an ArrayHandle with minimal device transfers.
Definition: ArrayGetValues.h:264
Mesh2D_DEM_SaddleStarter.h
vtkm::cont::Algorithm::Copy
static VTKM_CONT bool Copy(vtkm::cont::DeviceAdapterId devId, const vtkm::cont::ArrayHandle< T, CIn > &input, vtkm::cont::ArrayHandle< U, COut > &output)
Definition: Algorithm.h:410
vtkm::Id
vtkm::Int32 Id
Represents an ID (index into arrays).
Definition: Types.h:191
ArrayCopy.h
DispatcherMapField.h
vtkm::worklet::contourtree::ChainGraph::outdegree
vtkm::cont::ArrayHandle< vtkm::Id > outdegree
Definition: ChainGraph.h:154
vtkm::worklet::contourtree::Mesh2D_DEM_Triangulation::SetSaddleStarts
void SetSaddleStarts(ChainGraph< T, StorageType > &mergeGraph, bool descending)
Definition: Mesh2D_DEM_Triangulation.h:166
ArrayHandleZip.h
vtkm::worklet::contourtree::Mesh2D_DEM_Triangulation::SetStarts
void SetStarts(vtkm::cont::ArrayHandle< vtkm::Id > &chains, bool descending)
Definition: Mesh2D_DEM_Triangulation.h:128
vtkm::worklet::contourtree::ChainGraph::AllocateVertexArrays
void AllocateVertexArrays(vtkm::Id Size)
Definition: ChainGraph.h:219
vtkm::worklet::DispatcherMapField
Dispatcher for worklets that inherit from WorkletMapField.
Definition: DispatcherMapField.h:25
vtkm::worklet::contourtree::ChainGraph::edgeFar
vtkm::cont::ArrayHandle< vtkm::Id > edgeFar
Definition: ChainGraph.h:160
vtkm::cont::ArrayHandleZip
ArrayHandleZip is a specialization of ArrayHandle.
Definition: ArrayHandleZip.h:251
Mesh2D_DEM_VertexStarter.h
Algorithm.h
vtkm::worklet::contourtree::Mesh2D_DEM_VertexOutdegreeStarter
Definition: Mesh2D_DEM_VertexOutdegreeStarter.h:87
ArrayHandleIndex.h
vtkm::worklet::contourtree::ChainGraph::edgeSorter
vtkm::cont::ArrayHandle< vtkm::Id > edgeSorter
Definition: ChainGraph.h:168
vtkm::cont::Algorithm::ScanExclusive
static VTKM_CONT T ScanExclusive(vtkm::cont::DeviceAdapterId devId, const vtkm::cont::ArrayHandle< T, CIn > &input, vtkm::cont::ArrayHandle< T, COut > &output)
Definition: Algorithm.h:816
vtkm::cont::ArrayHandlePermutation
Implicitly permutes the values in an array.
Definition: ArrayHandlePermutation.h:227
vtkm::cont::make_ArrayHandleZip
VTKM_CONT vtkm::cont::ArrayHandleZip< FirstHandleType, SecondHandleType > make_ArrayHandleZip(const FirstHandleType &first, const SecondHandleType &second)
A convenience function for creating an ArrayHandleZip.
Definition: ArrayHandleZip.h:288
vtkm::worklet::contourtree::Mesh2D_DEM_SaddleStarter
Definition: Mesh2D_DEM_SaddleStarter.h:87
vtkm::cont::ArrayCopy
void ArrayCopy(const SourceArrayType &source, DestArrayType &destination)
Does a deep copy from one array to another array.
Definition: ArrayCopy.h:142
vtkm::worklet::contourtree_augmented::IdArrayType
vtkm::cont::ArrayHandle< vtkm::Id > IdArrayType
Definition: filter/scalar_topology/worklet/contourtree_augmented/Types.h:90
vtkm::cont::Algorithm::CopyIf
static VTKM_CONT void CopyIf(vtkm::cont::DeviceAdapterId devId, const vtkm::cont::ArrayHandle< T, CIn > &input, const vtkm::cont::ArrayHandle< U, CStencil > &stencil, vtkm::cont::ArrayHandle< T, COut > &output)
Definition: Algorithm.h:435
vtkm::worklet::contourtree::Mesh2D_DEM_Triangulation::nRows
vtkm::Id nRows
Definition: Mesh2D_DEM_Triangulation.h:109
vtkm::worklet::contourtree::Mesh2D_DEM_Triangulation::nLogSteps
vtkm::Id nLogSteps
Definition: Mesh2D_DEM_Triangulation.h:109
NO_VERTEX_ASSIGNED
#define NO_VERTEX_ASSIGNED
Definition: filter/scalar_topology/worklet/contourtree/Types.h:77
vtkm::cont::ArrayHandleConstant
An array handle with a constant value.
Definition: ArrayHandleConstant.h:63
vtkm::worklet::contourtree::ChainGraph::valueIndex
vtkm::cont::ArrayHandle< vtkm::Id > valueIndex
Definition: ChainGraph.h:137
ArrayGetValues.h
vtkm::worklet::contourtree::ChainGraph::arcArray
vtkm::cont::ArrayHandle< vtkm::Id > & arcArray
Definition: ChainGraph.h:134
vtkm::worklet::contourtree::ChainGraph::firstEdge
vtkm::cont::ArrayHandle< vtkm::Id > firstEdge
Definition: ChainGraph.h:151
vtkm::worklet::contourtree::Mesh2D_DEM_Triangulation
Definition: Mesh2D_DEM_Triangulation.h:102
vtkm::worklet::contourtree::ChainGraph::activeEdges
vtkm::cont::ArrayHandle< vtkm::Id > activeEdges
Definition: ChainGraph.h:165
ArrayHandleCounting.h
vtkm::worklet::contourtree::Mesh2D_DEM_Triangulation::values
const vtkm::cont::ArrayHandle< T, StorageType > & values
Definition: Mesh2D_DEM_Triangulation.h:106
vtkm::worklet::contourtree::ChainGraph::edgeNear
vtkm::cont::ArrayHandle< vtkm::Id > edgeNear
Definition: ChainGraph.h:161
PrintVectors.h
Types.h
vtkm::worklet::contourtree::Mesh2D_DEM_VertexStarter
Definition: Mesh2D_DEM_VertexStarter.h:89
vtkm::worklet::contourtree::Mesh2D_DEM_Triangulation::Mesh2D_DEM_Triangulation
Mesh2D_DEM_Triangulation(const vtkm::cont::ArrayHandle< T, StorageType > &Values, vtkm::Id NRows, vtkm::Id NCols)
Definition: Mesh2D_DEM_Triangulation.h:148
vtkm::cont::ArrayHandleIndex
An implicit array handle containing the its own indices.
Definition: ArrayHandleIndex.h:54