VTK-m  2.0
GraphConnectivity.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 
11 #ifndef vtk_m_worklet_connectivity_graph_connectivity_h
12 #define vtk_m_worklet_connectivity_graph_connectivity_h
13 
14 #include <vtkm/cont/Invoker.h>
18 
19 namespace vtkm
20 {
21 namespace worklet
22 {
23 namespace connectivity
24 {
25 namespace detail
26 {
27 class GraphGraft : public vtkm::worklet::WorkletMapField
28 {
29 public:
30  using ControlSignature = void(FieldIn start,
31  FieldIn degree,
32  WholeArrayIn ids,
33  AtomicArrayInOut comp);
34 
35  using ExecutionSignature = void(WorkIndex, _1, _2, _3, _4);
36 
37  template <typename InPortalType, typename AtomicCompInOut>
38  VTKM_EXEC void operator()(vtkm::Id index,
39  vtkm::Id start,
40  vtkm::Id degree,
41  const InPortalType& conn,
42  AtomicCompInOut& comp) const
43  {
44  for (vtkm::Id offset = start; offset < start + degree; offset++)
45  {
46  vtkm::Id neighbor = conn.Get(offset);
47 
48  // We need to reload thisComp and thatComp every iteration since
49  // they might have been changed by Unite() both as a result of
50  // attaching one tree to the other or as a result of path compression
51  // in findRoot().
52  auto thisComp = comp.Get(index);
53  auto thatComp = comp.Get(neighbor);
54 
55  // Merge the two components one way or the other, the order will
56  // be resolved by Unite().
57  UnionFind::Unite(comp, thisComp, thatComp);
58  }
59  }
60 };
61 }
62 
63 // Single pass connected component algorithm from
64 // Jaiganesh, Jayadharini, and Martin Burtscher.
65 // "A high-performance connected components implementation for GPUs."
66 // Proceedings of the 27th International Symposium on High-Performance
67 // Parallel and Distributed Computing. 2018.
69 {
70 public:
71  template <typename InputArrayType, typename OutputArrayType>
72  static void Run(const InputArrayType& numIndicesArray,
73  const InputArrayType& indexOffsetsArray,
74  const InputArrayType& connectivityArray,
75  OutputArrayType& componentsOut)
76  {
77  VTKM_IS_ARRAY_HANDLE(InputArrayType);
78  VTKM_IS_ARRAY_HANDLE(OutputArrayType);
79 
80  using Algorithm = vtkm::cont::Algorithm;
81 
82  // Initialize the parent pointer to point to the node itself. There are other
83  // ways to initialize the parent pointers, for example, a smaller or the minimal
84  // neighbor.
85  Algorithm::Copy(vtkm::cont::ArrayHandleIndex(numIndicesArray.GetNumberOfValues()),
86  componentsOut);
87 
88  vtkm::cont::Invoker invoke;
89  invoke(
90  detail::GraphGraft{}, indexOffsetsArray, numIndicesArray, connectivityArray, componentsOut);
91  invoke(PointerJumping{}, componentsOut);
92 
93  // renumber connected component to the range of [0, number of components).
94  Renumber::Run(componentsOut);
95  }
96 };
97 }
98 }
99 }
100 #endif //vtk_m_worklet_connectivity_graph_connectivity_h
vtkm::worklet::connectivity::Renumber::Run
static void Run(vtkm::cont::ArrayHandle< vtkm::Id > &componentsInOut)
Definition: InnerJoin.h:88
UnionFind.h
VTKM_EXEC
#define VTKM_EXEC
Definition: ExportMacros.h:51
vtkm
Groups connected points that have the same field value.
Definition: Atomic.h:19
vtkm::worklet::connectivity::GraphConnectivity
Definition: GraphConnectivity.h:68
vtkm::worklet::connectivity::UnionFind::Unite
static VTKM_EXEC void Unite(Parents &parents, vtkm::Id u, vtkm::Id v)
Definition: UnionFind.h:45
Invoker.h
vtkm::Id
vtkm::Int32 Id
Represents an ID (index into arrays).
Definition: Types.h:191
VTKM_IS_ARRAY_HANDLE
#define VTKM_IS_ARRAY_HANDLE(T)
Definition: ArrayHandle.h:132
vtkm::cont::Invoker
Allows launching any worklet without a dispatcher.
Definition: Invoker.h:41
InnerJoin.h
vtkm::cont::Algorithm
Definition: Algorithm.h:385
vtkm::worklet::connectivity::PointerJumping
Definition: UnionFind.h:157
vtkm::worklet::connectivity::GraphConnectivity::Run
static void Run(const InputArrayType &numIndicesArray, const InputArrayType &indexOffsetsArray, const InputArrayType &connectivityArray, OutputArrayType &componentsOut)
Definition: GraphConnectivity.h:72
CellSetDualGraph.h
vtkm::cont::ArrayHandleIndex
An implicit array handle containing the its own indices.
Definition: ArrayHandleIndex.h:54
vtkm::worklet::WorkletMapField
Base class for worklets that do a simple mapping of field arrays.
Definition: WorkletMapField.h:38