Viskores  1.0
AverageByKey.h
Go to the documentation of this file.
1 //============================================================================
2 // The contents of this file are covered by the Viskores license. See
3 // LICENSE.txt for details.
4 //
5 // By contributing to this file, all contributors agree to the Developer
6 // Certificate of Origin Version 1.1 (DCO 1.1) as stated in DCO.txt.
7 //============================================================================
8 
9 //============================================================================
10 // Copyright (c) Kitware, Inc.
11 // All rights reserved.
12 // See LICENSE.txt for details.
13 //
14 // This software is distributed WITHOUT ANY WARRANTY; without even
15 // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
16 // PURPOSE. See the above copyright notice for more information.
17 //============================================================================
18 #ifndef viskores_worklet_AverageByKey_h
19 #define viskores_worklet_AverageByKey_h
20 
21 #include <viskores/VecTraits.h>
25 #include <viskores/worklet/Keys.h>
27 
28 namespace viskores
29 {
30 namespace worklet
31 {
32 
34 {
36  {
37  using ControlSignature = void(KeysIn keys, ValuesIn valuesIn, ReducedValuesOut averages);
38  using ExecutionSignature = void(_2, _3);
39  using InputDomain = _1;
40 
41  template <typename ValuesVecType, typename OutType>
42  VISKORES_EXEC void operator()(const ValuesVecType& valuesIn, OutType& sum) const
43  {
44  sum = valuesIn[0];
45  for (viskores::IdComponent index = 1; index < valuesIn.GetNumberOfComponents(); ++index)
46  {
47  sum += valuesIn[index];
48  }
49 
50  // To get the average, we (of course) divide the sum by the amount of values, which is
51  // returned from valuesIn.GetNumberOfComponents(). To do this, we need to cast the number of
52  // components (returned as a viskores::IdComponent) to a FieldType. This is a little more complex
53  // than it first seems because FieldType might be a Vec type or a Vec-like type that cannot
54  // be constructed. To do this safely, we will do a component-wise divide.
55  using VTraits = viskores::VecTraits<OutType>;
56  using ComponentType = typename VTraits::ComponentType;
57  ComponentType divisor = static_cast<ComponentType>(valuesIn.GetNumberOfComponents());
58  for (viskores::IdComponent cIndex = 0; cIndex < VTraits::GetNumberOfComponents(sum); ++cIndex)
59  {
60  VTraits::SetComponent(sum, cIndex, VTraits::GetComponent(sum, cIndex) / divisor);
61  }
62  }
63  };
64 
70  template <typename InArrayType, typename OutArrayType>
71  VISKORES_CONT static void Run(const viskores::worklet::internal::KeysBase& keys,
72  const InArrayType& inValues,
73  const OutArrayType& outAverages)
74  {
76 
78  dispatcher.Invoke(keys, inValues, outAverages);
79  }
80 
86  template <typename ValueType, typename InValuesStorage>
88  const viskores::worklet::internal::KeysBase& keys,
90  {
91 
93  Run(keys, inValues, outAverages);
94  return outAverages;
95  }
96 
97  struct ExtractMean
98  {
99  template <typename ValueType>
100  VISKORES_EXEC ValueType
102  {
103  return state.Mean();
104  }
105  };
106 
118  template <class KeyType,
119  class ValueType,
120  class KeyInStorage,
121  class KeyOutStorage,
122  class ValueInStorage,
123  class ValueOutStorage>
124  VISKORES_CONT static void Run(
129  {
131 
132  auto results = viskores::worklet::DescriptiveStatistics::Run(keyArray, valueArray);
133  // Extract results to outputKeyArray and outputValueArray
134  outputKeyArray = results.GetFirstArray();
135  // TODO: DescriptiveStatistics should write its output to a SOA instead of an AOS.
136  // An ArrayHandle of a weird struct by itself is not useful in any general algorithm.
137  // In fact, using DescriptiveStatistics at all seems like way overkill. It computes
138  // all sorts of statistics, and we then throw them all away except for mean.
139  auto resultsMean =
140  viskores::cont::make_ArrayHandleTransform(results.GetSecondArray(), ExtractMean{});
141  viskores::cont::ArrayCopyDevice(resultsMean, outputValueArray);
142  }
143 };
144 }
145 } // viskores::worklet
146 
147 #endif //viskores_worklet_AverageByKey_h
viskores::worklet::AverageByKey::Run
static void Run(const viskores::cont::ArrayHandle< KeyType, KeyInStorage > &keyArray, const viskores::cont::ArrayHandle< ValueType, ValueInStorage > &valueArray, viskores::cont::ArrayHandle< KeyType, KeyOutStorage > &outputKeyArray, viskores::cont::ArrayHandle< ValueType, ValueOutStorage > &outputValueArray)
Compute average values based on an array of keys.
Definition: AverageByKey.h:124
ArrayHandle.h
DescriptiveStatistics.h
VISKORES_LOG_SCOPE
#define VISKORES_LOG_SCOPE(level,...)
Definition: Logging.h:219
viskores::worklet::AverageByKey::AverageWorklet::InputDomain
_1 InputDomain
Definition: AverageByKey.h:39
viskores::worklet::AverageByKey::AverageWorklet::operator()
void operator()(const ValuesVecType &valuesIn, OutType &sum) const
Definition: AverageByKey.h:42
viskores::worklet::AverageByKey::Run
static void Run(const viskores::worklet::internal::KeysBase &keys, const InArrayType &inValues, const OutArrayType &outAverages)
Compute average values based on a set of Keys.
Definition: AverageByKey.h:71
viskores::worklet::DescriptiveStatistics::StatState
Definition: DescriptiveStatistics.h:34
viskores::worklet::AverageByKey::AverageWorklet
Definition: AverageByKey.h:35
viskores::worklet::WorkletReduceByKey::KeysIn
A control signature tag for input keys.
Definition: WorkletReduceByKey.h:81
Keys.h
viskores::worklet::DescriptiveStatistics::StatState::Mean
T Mean() const
Definition: DescriptiveStatistics.h:141
viskores::cont::ArrayHandle
Manages an array-worth of data.
Definition: ArrayHandle.h:313
viskores::IdComponent
viskores::Int32 IdComponent
Base type to use to index small lists.
Definition: Types.h:202
viskores::worklet::WorkletReduceByKey::ReducedValuesOut
A control signature tag for reduced output values.
Definition: WorkletReduceByKey.h:147
viskores::worklet::WorkletReduceByKey::ValuesIn
A control signature tag for input values associated with the keys.
Definition: WorkletReduceByKey.h:96
viskores::worklet::DispatcherReduceByKey
Dispatcher for worklets that inherit from WorkletReduceByKey.
Definition: DispatcherReduceByKey.h:35
viskores::worklet::AverageByKey::AverageWorklet::ExecutionSignature
void(_2, _3) ExecutionSignature
Definition: AverageByKey.h:38
VISKORES_CONT
#define VISKORES_CONT
Definition: ExportMacros.h:65
viskores::worklet::AverageByKey
Definition: AverageByKey.h:33
viskores
Groups connected points that have the same field value.
Definition: Atomic.h:27
WorkletReduceByKey.h
ArrayCopyDevice.h
viskores::VecTraits
Traits that can be queried to treat any type as a Vec.
Definition: VecTraits.h:69
viskores::worklet::AverageByKey::ExtractMean
Definition: AverageByKey.h:97
viskores::worklet::DescriptiveStatistics::Run
static StatState< FieldType > Run(const viskores::cont::ArrayHandle< FieldType, Storage > &field)
Calculate various summary statistics for the input ArrayHandle.
Definition: DescriptiveStatistics.h:234
viskores::worklet::AverageByKey::Run
static viskores::cont::ArrayHandle< ValueType > Run(const viskores::worklet::internal::KeysBase &keys, const viskores::cont::ArrayHandle< ValueType, InValuesStorage > &inValues)
Compute average values based on a set of Keys.
Definition: AverageByKey.h:87
viskores::cont::ArrayCopyDevice
void ArrayCopyDevice(const viskores::cont::ArrayHandle< InValueType, InStorage > &source, viskores::cont::ArrayHandle< OutValueType, OutStorage > &destination)
Does a deep copy from one array to another array.
Definition: ArrayCopyDevice.h:83
viskores::cont::LogLevel::Perf
@ Perf
General timing data and algorithm flow information, such as filter execution, worklet dispatches,...
viskores::cont::make_ArrayHandleTransform
viskores::cont::ArrayHandleTransform< HandleType, FunctorType > make_ArrayHandleTransform(HandleType handle, FunctorType functor)
make_ArrayHandleTransform is convenience function to generate an ArrayHandleTransform.
Definition: ArrayHandleTransform.h:495
viskores::worklet::WorkletReduceByKey
Base class for worklets that group elements by keys.
Definition: WorkletReduceByKey.h:62
VISKORES_EXEC
#define VISKORES_EXEC
Definition: ExportMacros.h:59
VecTraits.h
viskores::worklet::AverageByKey::ExtractMean::operator()
ValueType operator()(const viskores::worklet::DescriptiveStatistics::StatState< ValueType > &state) const
Definition: AverageByKey.h:101
viskores::worklet::AverageByKey::AverageWorklet::ControlSignature
void(KeysIn keys, ValuesIn valuesIn, ReducedValuesOut averages) ControlSignature
Definition: AverageByKey.h:37