VTK-m  2.0
AverageByKey.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 #ifndef vtk_m_worklet_AverageByKey_h
11 #define vtk_m_worklet_AverageByKey_h
12 
13 #include <vtkm/VecTraits.h>
15 #include <vtkm/cont/ArrayHandle.h>
17 #include <vtkm/worklet/Keys.h>
19 
20 namespace vtkm
21 {
22 namespace worklet
23 {
24 
26 {
28  {
29  using ControlSignature = void(KeysIn keys, ValuesIn valuesIn, ReducedValuesOut averages);
30  using ExecutionSignature = void(_2, _3);
31  using InputDomain = _1;
32 
33  template <typename ValuesVecType, typename OutType>
34  VTKM_EXEC void operator()(const ValuesVecType& valuesIn, OutType& sum) const
35  {
36  sum = valuesIn[0];
37  for (vtkm::IdComponent index = 1; index < valuesIn.GetNumberOfComponents(); ++index)
38  {
39  sum += valuesIn[index];
40  }
41 
42  // To get the average, we (of course) divide the sum by the amount of values, which is
43  // returned from valuesIn.GetNumberOfComponents(). To do this, we need to cast the number of
44  // components (returned as a vtkm::IdComponent) to a FieldType. This is a little more complex
45  // than it first seems because FieldType might be a Vec type or a Vec-like type that cannot
46  // be constructed. To do this safely, we will do a component-wise divide.
47  using VTraits = vtkm::VecTraits<OutType>;
48  using ComponentType = typename VTraits::ComponentType;
49  ComponentType divisor = static_cast<ComponentType>(valuesIn.GetNumberOfComponents());
50  for (vtkm::IdComponent cIndex = 0; cIndex < VTraits::GetNumberOfComponents(sum); ++cIndex)
51  {
52  VTraits::SetComponent(sum, cIndex, VTraits::GetComponent(sum, cIndex) / divisor);
53  }
54  }
55  };
56 
62  template <typename InArrayType, typename OutArrayType>
63  VTKM_CONT static void Run(const vtkm::worklet::internal::KeysBase& keys,
64  const InArrayType& inValues,
65  const OutArrayType& outAverages)
66  {
67  VTKM_LOG_SCOPE(vtkm::cont::LogLevel::Perf, "AverageByKey::Run");
68 
70  dispatcher.Invoke(keys, inValues, outAverages);
71  }
72 
78  template <typename ValueType, typename InValuesStorage>
80  const vtkm::worklet::internal::KeysBase& keys,
82  {
83 
85  Run(keys, inValues, outAverages);
86  return outAverages;
87  }
88 
89  struct ExtractMean
90  {
91  template <typename ValueType>
92  VTKM_EXEC ValueType
94  {
95  return state.Mean();
96  }
97  };
98 
110  template <class KeyType,
111  class ValueType,
112  class KeyInStorage,
113  class KeyOutStorage,
114  class ValueInStorage,
115  class ValueOutStorage>
120  {
121  VTKM_LOG_SCOPE(vtkm::cont::LogLevel::Perf, "AverageByKey::Run");
122 
123  auto results = vtkm::worklet::DescriptiveStatistics::Run(keyArray, valueArray);
124  // Extract results to outputKeyArray and outputValueArray
125  outputKeyArray = results.GetFirstArray();
126  // TODO: DescriptiveStatistics should write its output to a SOA instead of an AOS.
127  // An ArrayHandle of a weird struct by itself is not useful in any general algorithm.
128  // In fact, using DescriptiveStatistics at all seems like way overkill. It computes
129  // all sorts of statistics, and we then throw them all away except for mean.
130  auto resultsMean =
131  vtkm::cont::make_ArrayHandleTransform(results.GetSecondArray(), ExtractMean{});
132  vtkm::cont::ArrayCopyDevice(resultsMean, outputValueArray);
133  }
134 };
135 }
136 } // vtkm::worklet
137 
138 #endif //vtk_m_worklet_AverageByKey_h
vtkm::cont::ArrayCopyDevice
VTKM_CONT void ArrayCopyDevice(const vtkm::cont::ArrayHandle< InValueType, InStorage > &source, vtkm::cont::ArrayHandle< OutValueType, OutStorage > &destination)
Does a deep copy from one array to another array.
Definition: ArrayCopyDevice.h:75
VTKM_LOG_SCOPE
#define VTKM_LOG_SCOPE(level,...)
Definition: Logging.h:265
vtkm::cont::ArrayHandle
Manages an array-worth of data.
Definition: ArrayHandle.h:283
ArrayHandle.h
DescriptiveStatistics.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::IdComponent
vtkm::Int32 IdComponent
Represents a component ID (index of component in a vector).
Definition: Types.h:168
vtkm::worklet::WorkletReduceByKey::ValuesIn
A control signature tag for input values.
Definition: WorkletReduceByKey.h:70
Keys.h
vtkm::worklet::AverageByKey::Run
static VTKM_CONT void Run(const vtkm::cont::ArrayHandle< KeyType, KeyInStorage > &keyArray, const vtkm::cont::ArrayHandle< ValueType, ValueInStorage > &valueArray, vtkm::cont::ArrayHandle< KeyType, KeyOutStorage > &outputKeyArray, vtkm::cont::ArrayHandle< ValueType, ValueOutStorage > &outputValueArray)
Compute average values based on an array of keys.
Definition: AverageByKey.h:116
vtkm::worklet::DescriptiveStatistics::Run
static VTKM_CONT StatState< FieldType > Run(const vtkm::cont::ArrayHandle< FieldType, Storage > &field)
Calculate various summary statistics for the input ArrayHandle.
Definition: DescriptiveStatistics.h:186
vtkm::worklet::AverageByKey::AverageWorklet
Definition: AverageByKey.h:27
vtkm::worklet::AverageByKey::Run
static VTKM_CONT void Run(const vtkm::worklet::internal::KeysBase &keys, const InArrayType &inValues, const OutArrayType &outAverages)
Compute average values based on a set of Keys.
Definition: AverageByKey.h:63
vtkm::worklet::WorkletReduceByKey::ReducedValuesOut
A control signature tag for reduced output values.
Definition: WorkletReduceByKey.h:150
vtkm::worklet::AverageByKey::AverageWorklet::operator()
VTKM_EXEC void operator()(const ValuesVecType &valuesIn, OutType &sum) const
Definition: AverageByKey.h:34
vtkm::worklet::AverageByKey::AverageWorklet::InputDomain
_1 InputDomain
Definition: AverageByKey.h:31
WorkletReduceByKey.h
vtkm::cont::make_ArrayHandleTransform
VTKM_CONT vtkm::cont::ArrayHandleTransform< HandleType, FunctorType > make_ArrayHandleTransform(HandleType handle, FunctorType functor)
make_ArrayHandleTransform is convenience function to generate an ArrayHandleTransform.
Definition: ArrayHandleTransform.h:474
ArrayCopyDevice.h
vtkm::worklet::WorkletReduceByKey
Definition: WorkletReduceByKey.h:42
vtkm::worklet::DescriptiveStatistics::StatState
Definition: DescriptiveStatistics.h:26
vtkm::worklet::AverageByKey::AverageWorklet::ExecutionSignature
void(_2, _3) ExecutionSignature
Definition: AverageByKey.h:30
vtkm::worklet::AverageByKey::Run
static VTKM_CONT vtkm::cont::ArrayHandle< ValueType > Run(const vtkm::worklet::internal::KeysBase &keys, const vtkm::cont::ArrayHandle< ValueType, InValuesStorage > &inValues)
Compute average values based on a set of Keys.
Definition: AverageByKey.h:79
VTKM_CONT
#define VTKM_CONT
Definition: ExportMacros.h:57
vtkm::worklet::WorkletReduceByKey::KeysIn
A control signature tag for input keys.
Definition: WorkletReduceByKey.h:56
vtkm::worklet::DescriptiveStatistics::StatState::Mean
VTKM_EXEC_CONT T Mean() const
Definition: DescriptiveStatistics.h:113
vtkm::worklet::DispatcherReduceByKey
Dispatcher for worklets that inherit from WorkletReduceByKey.
Definition: DispatcherReduceByKey.h:27
vtkm::worklet::AverageByKey::ExtractMean::operator()
VTKM_EXEC ValueType operator()(const vtkm::worklet::DescriptiveStatistics::StatState< ValueType > &state) const
Definition: AverageByKey.h:93
vtkm::VecTraits
The VecTraits class gives several static members that define how to use a given type as a vector.
Definition: VecTraits.h:66
vtkm::worklet::AverageByKey::ExtractMean
Definition: AverageByKey.h:89
VecTraits.h
vtkm::cont::LogLevel::Perf
@ Perf
General timing data and algorithm flow information, such as filter execution, worklet dispatches,...
vtkm::worklet::AverageByKey
Definition: AverageByKey.h:25
vtkm::worklet::AverageByKey::AverageWorklet::ControlSignature
void(KeysIn keys, ValuesIn valuesIn, ReducedValuesOut averages) ControlSignature
Definition: AverageByKey.h:29