1 //=============================================================================
2 //
3 //  Copyright (c) Kitware, Inc.
4 //  All rights reserved.
5 //  See LICENSE.txt for details.
6 //
7 //  This software is distributed WITHOUT ANY WARRANTY; without even
8 //  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
9 //  PURPOSE.  See the above copyright notice for more information.
10 //
11 //  Copyright 2017 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
12 //  Copyright 2017 UT-Battelle, LLC.
13 //  Copyright 2017 Los Alamos National Security.
14 //
15 //  Under the terms of Contract DE-NA0003525 with NTESS,
16 //  the U.S. Government retains certain rights in this software.
17 //  Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
18 //  Laboratory (LANL), the U.S. Government retains certain rights in
19 //  this software.
20 //
21 //=============================================================================
22 #ifndef vtk_m_worklet_ScatterPermutation_h
23 #define vtk_m_worklet_ScatterPermutation_h
24 
25 #include <vtkm/cont/ArrayHandle.h>
26 #include <vtkm/cont/ArrayHandleConstant.h>
27 
28 namespace vtkm
29 {
30 namespace worklet
31 {
32 
33 /// \brief A scatter that maps input to output based on a permutation array.
34 ///
35 /// The \c Scatter classes are responsible for defining how much output is
36 /// generated based on some sized input. \c ScatterPermutation is similar to
37 /// \c ScatterCounting but can have lesser memory usage for some cases.
38 /// The constructor takes an array of ids, where each entry maps the
39 /// corresponding output to an input. The ids can be in any order and there
40 /// can be duplicates. Note that even with duplicates the VistIndex is always 0.
41 ///
42 template <typename PermutationStorage = VTKM_DEFAULT_STORAGE_TAG>
43 class ScatterPermutation
44 {
45 private:
46   using PermutationArrayHandle = vtkm::cont::ArrayHandle<vtkm::Id, PermutationStorage>;
47 
48 public:
49   using OutputToInputMapType = PermutationArrayHandle;
50   using VisitArrayType = vtkm::cont::ArrayHandleConstant<vtkm::IdComponent>;
51 
ScatterPermutation(const PermutationArrayHandle & permutation)52   ScatterPermutation(const PermutationArrayHandle& permutation)
53     : Permutation(permutation)
54   {
55   }
56 
57   VTKM_CONT
58   template <typename RangeType>
GetOutputRange(RangeType)59   vtkm::Id GetOutputRange(RangeType) const
60   {
61     return this->Permutation.GetNumberOfValues();
62   }
63 
64   template <typename RangeType>
GetOutputToInputMap(RangeType)65   VTKM_CONT OutputToInputMapType GetOutputToInputMap(RangeType) const
66   {
67     return this->Permutation;
68   }
69 
GetOutputToInputMap()70   VTKM_CONT OutputToInputMapType GetOutputToInputMap() const { return this->Permutation; }
71 
72   VTKM_CONT
GetVisitArray(vtkm::Id inputRange)73   VisitArrayType GetVisitArray(vtkm::Id inputRange) const { return VisitArrayType(0, inputRange); }
74 
75   VTKM_CONT
GetVisitArray(vtkm::Id3 inputRange)76   VisitArrayType GetVisitArray(vtkm::Id3 inputRange) const
77   {
78     return this->GetVisitArray(inputRange[0] * inputRange[1] * inputRange[2]);
79   }
80 
81 private:
82   PermutationArrayHandle Permutation;
83 };
84 }
85 } // vtkm::worklet
86 
87 #endif // vtk_m_worklet_ScatterPermutation_h
88