VTK-m  2.0
Mesh3D_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_mesh3d_dem_triangulation_h
74 #define vtkm_worklet_contourtree_mesh3d_dem_triangulation_h
75 
77 #include <vtkm/cont/ArrayHandle.h>
82 
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  // case table information for finding neighbours
117 
118  // constructor
120  vtkm::Id NRows,
121  vtkm::Id NCols,
122  vtkm::Id NSlices);
123 
124  // sets all vertices to point along an outgoing edge (except extrema)
125  void SetStarts(vtkm::cont::ArrayHandle<vtkm::Id>& chains, bool descending);
126 
127  // sets outgoing paths for saddles
128  void SetSaddleStarts(ChainGraph<T, StorageType>& mergeGraph, bool descending);
129 };
130 
131 // creates input mesh
132 template <typename T, typename StorageType>
135  vtkm::Id NRows,
136  vtkm::Id NCols,
137  vtkm::Id NSlices)
138  : values(Values)
139  , nRows(NRows)
140  , nCols(NCols)
141  , nSlices(NSlices)
144 {
146 
147  // compute the number of log-jumping steps (i.e. lg_2 (NumVertices))
148  nLogSteps = 1;
149  for (vtkm::Id shifter = NumVertices; shifter > 0; shifter >>= 1)
150  nLogSteps++;
151 
156 }
157 
158 // sets outgoing paths for saddles
159 template <typename T, typename StorageType>
161  bool ascending)
162 {
163  // create the neighbourhood mask
164  neighbourhoodMask.Allocate(NumVertices);
165 
166  // For each vertex set the next vertex in the chain
167  vtkm::cont::ArrayHandleIndex vertexIndexArray(NumVertices);
168  Mesh3D_DEM_VertexStarter<T> vertexStarter(nRows, nCols, nSlices, ascending);
170  vertexStarter);
171 
172  vertexStarterDispatcher.Invoke(vertexIndexArray, // input
173  values, // input (whole array)
174  chains, // output
175  neighbourhoodMask); // output
176 } // SetStarts()
177 
178 // sets outgoing paths for saddles
179 template <typename T, typename StorageType>
181  ChainGraph<T, StorageType>& mergeGraph,
182  bool ascending)
183 {
184  // we need a temporary inverse index to change vertex IDs
188 
189  vtkm::cont::ArrayHandleIndex vertexIndexArray(NumVertices);
190  Mesh3D_DEM_VertexOutdegreeStarter vertexOutdegreeStarter(nRows, nCols, nSlices, ascending);
192  vertexOutdegreeStarterDispatcher(vertexOutdegreeStarter);
193 
194  vertexOutdegreeStarterDispatcher.Invoke(vertexIndexArray, // input
195  neighbourhoodMask, // input
196  mergeGraph.arcArray, // input (whole array)
197  neighbourOffsets3D, // input (whole array)
198  linkComponentCaseTable3D, // input (whole array)
199  outdegree, // output
200  isCritical); // output
201 
202  vtkm::cont::Algorithm::ScanExclusive(isCritical, inverseIndex);
203 
204  // now we can compute how many critical points we carry forward
205  vtkm::Id nCriticalPoints = vtkm::cont::ArrayGetValue(NumVertices - 1, inverseIndex) +
206  vtkm::cont::ArrayGetValue(NumVertices - 1, isCritical);
207 
208  // allocate space for the join graph vertex arrays
209  mergeGraph.AllocateVertexArrays(nCriticalPoints);
210 
211  // compact the set of vertex indices to critical ones only
212  vtkm::cont::Algorithm::CopyIf(vertexIndexArray, isCritical, mergeGraph.valueIndex);
213 
214  // we initialise the prunesTo array to "NONE"
216  vtkm::cont::Algorithm::Copy(notAssigned, mergeGraph.prunesTo);
217 
218  // copy the outdegree from our temporary array
219  // : mergeGraph.outdegree[vID] <= outdegree[mergeGraph.valueIndex[vID]]
220  vtkm::cont::Algorithm::CopyIf(outdegree, isCritical, mergeGraph.outdegree);
221 
222  // copy the chain maximum from arcArray
223  // : mergeGraph.chainExtremum[vID] = inverseIndex[mergeGraph.arcArray[mergeGraph.valueIndex[vID]]]
226 
228  tArray.Allocate(nCriticalPoints);
229  vtkm::cont::Algorithm::CopyIf(mergeGraph.arcArray, isCritical, tArray);
230  vtkm::cont::Algorithm::Copy(PermuteIndexType(tArray, inverseIndex), mergeGraph.chainExtremum);
231 
232  // and set up the active vertices - initially to identity
233  vtkm::cont::ArrayHandleIndex criticalVertsIndexArray(nCriticalPoints);
234  vtkm::cont::Algorithm::Copy(criticalVertsIndexArray, mergeGraph.activeVertices);
235 
236  // now we need to compute the firstEdge array from the outdegrees
238 
239  vtkm::Id nCriticalEdges = vtkm::cont::ArrayGetValue(nCriticalPoints - 1, mergeGraph.firstEdge) +
240  vtkm::cont::ArrayGetValue(nCriticalPoints - 1, mergeGraph.outdegree);
241 
242  // now we allocate the edge arrays
243  mergeGraph.AllocateEdgeArrays(nCriticalEdges);
244 
245  // and we have to set them, so we go back to the vertices
246  Mesh3D_DEM_SaddleStarter saddleStarter(nRows, // input
247  nCols, // input
248  nSlices, // input
249  ascending); // input
251  saddleStarter);
252 
254  outDegFirstEdge = vtkm::cont::make_ArrayHandleZip(mergeGraph.outdegree, mergeGraph.firstEdge);
255 
256  saddleStarterDispatcher.Invoke(criticalVertsIndexArray, // input
257  outDegFirstEdge, // input (pair)
258  mergeGraph.valueIndex, // input
259  neighbourhoodMask, // input (whole array)
260  mergeGraph.arcArray, // input (whole array)
261  inverseIndex, // input (whole array)
262  neighbourOffsets3D, // input (whole array)
263  linkComponentCaseTable3D, // input (whole array)
264  mergeGraph.edgeNear, // output (whole array)
265  mergeGraph.edgeFar, // output (whole array)
266  mergeGraph.activeEdges); // output (whole array)
267 
268  // finally, allocate and initialise the edgeSorter array
269  vtkm::cont::ArrayCopy(mergeGraph.activeEdges, mergeGraph.edgeSorter);
270 } // SetSaddleStarts()
271 }
272 }
273 }
274 
275 #endif
vtkm::cont::make_ArrayHandle
VTKM_CONT vtkm::cont::ArrayHandleBasic< T > make_ArrayHandle(const T *array, vtkm::Id numberOfValues, vtkm::CopyFlag copy)
A convenience function for creating an ArrayHandle from a standard C array.
Definition: ArrayHandleBasic.h:217
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 >
ArrayHandle.h
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::Mesh3D_DEM_Triangulation::SetStarts
void SetStarts(vtkm::cont::ArrayHandle< vtkm::Id > &chains, bool descending)
Definition: Mesh3D_DEM_Triangulation.h:160
vtkm::worklet::contourtree::Mesh3D_DEM_Triangulation::nRows
vtkm::Id nRows
Definition: Mesh3D_DEM_Triangulation.h:109
vtkm::worklet::contourtree::ChainGraph::chainExtremum
vtkm::cont::ArrayHandle< vtkm::Id > chainExtremum
Definition: ChainGraph.h:157
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
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::worklet::contourtree::Mesh3D_DEM_Triangulation::SetSaddleStarts
void SetSaddleStarts(ChainGraph< T, StorageType > &mergeGraph, bool descending)
Definition: Mesh3D_DEM_Triangulation.h:180
vtkm::worklet::contourtree::Mesh3D_DEM_Triangulation
Definition: Mesh3D_DEM_Triangulation.h:102
vtkm::Id
vtkm::Int32 Id
Represents an ID (index into arrays).
Definition: Types.h:191
DispatcherMapField.h
vtkm::worklet::contourtree::ChainGraph::outdegree
vtkm::cont::ArrayHandle< vtkm::Id > outdegree
Definition: ChainGraph.h:154
ArrayHandleZip.h
Mesh3D_DEM_VertexStarter.h
vtkm::worklet::contourtree::Mesh3D_DEM_VertexOutdegreeStarter
Definition: Mesh3D_DEM_VertexOutdegreeStarter.h:88
LinkComponentCaseTable3D.h
Mesh3D_DEM_VertexOutdegreeStarter.h
vtkm::worklet::contourtree::ChainGraph::AllocateVertexArrays
void AllocateVertexArrays(vtkm::Id Size)
Definition: ChainGraph.h:219
vtkm::worklet::contourtree::Mesh3D_DEM_VertexStarter
Definition: Mesh3D_DEM_VertexStarter.h:89
vtkm::worklet::contourtree::Mesh3D_DEM_Triangulation::nLogSteps
vtkm::Id nLogSteps
Definition: Mesh3D_DEM_Triangulation.h:109
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
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::Mesh3D_DEM_Triangulation::neighbourOffsets3D
vtkm::cont::ArrayHandle< vtkm::IdComponent > neighbourOffsets3D
Definition: Mesh3D_DEM_Triangulation.h:115
vtkm::worklet::contourtree::neighbourOffsets3D
const vtkm::IdComponent neighbourOffsets3D[N_INCIDENT_EDGES *2]
Definition: LinkComponentCaseTable2D.h:70
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::Mesh3D_DEM_Triangulation::NumVertices
vtkm::Id NumVertices
Definition: Mesh3D_DEM_Triangulation.h:109
vtkm::worklet::contourtree::Mesh3D_DEM_Triangulation::nSlices
vtkm::Id nSlices
Definition: Mesh3D_DEM_Triangulation.h:109
vtkm::worklet::contourtree::Mesh3D_DEM_Triangulation::nCols
vtkm::Id nCols
Definition: Mesh3D_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::Mesh3D_DEM_Triangulation::neighbourhoodMask
vtkm::cont::ArrayHandle< vtkm::Id > neighbourhoodMask
Definition: Mesh3D_DEM_Triangulation.h:112
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::Mesh3D_DEM_Triangulation::linkComponentCaseTable3D
vtkm::cont::ArrayHandle< vtkm::UInt16 > linkComponentCaseTable3D
Definition: Mesh3D_DEM_Triangulation.h:116
vtkm::worklet::contourtree::ChainGraph::firstEdge
vtkm::cont::ArrayHandle< vtkm::Id > firstEdge
Definition: ChainGraph.h:151
vtkm::CopyFlag::Off
@ Off
vtkm::worklet::contourtree::ChainGraph::activeEdges
vtkm::cont::ArrayHandle< vtkm::Id > activeEdges
Definition: ChainGraph.h:165
vtkm::worklet::contourtree::Mesh3D_DEM_Triangulation::values
const vtkm::cont::ArrayHandle< T, StorageType > & values
Definition: Mesh3D_DEM_Triangulation.h:106
ArrayHandleCounting.h
vtkm::worklet::contourtree::Mesh3D_DEM_Triangulation::Mesh3D_DEM_Triangulation
Mesh3D_DEM_Triangulation(const vtkm::cont::ArrayHandle< T, StorageType > &Values, vtkm::Id NRows, vtkm::Id NCols, vtkm::Id NSlices)
Definition: Mesh3D_DEM_Triangulation.h:133
vtkm::worklet::contourtree::ChainGraph::edgeNear
vtkm::cont::ArrayHandle< vtkm::Id > edgeNear
Definition: ChainGraph.h:161
vtkm::worklet::contourtree::Mesh3D_DEM_SaddleStarter
Definition: Mesh3D_DEM_SaddleStarter.h:88
PrintVectors.h
vtkm::worklet::contourtree::linkComponentCaseTable3D
const vtkm::UInt16 linkComponentCaseTable3D[16384]
Definition: LinkComponentCaseTable3D.h:75
Types.h
Mesh3D_DEM_SaddleStarter.h
vtkm::cont::ArrayHandleIndex
An implicit array handle containing the its own indices.
Definition: ArrayHandleIndex.h:54