2015-06-18 17:07:31 +00:00
|
|
|
//============================================================================
|
|
|
|
// Copyright (c) Kitware, Inc.
|
|
|
|
// All rights reserved.
|
|
|
|
// See LICENSE.txt for details.
|
|
|
|
// This software is distributed WITHOUT ANY WARRANTY; without even
|
|
|
|
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
// PURPOSE. See the above copyright notice for more information.
|
|
|
|
//
|
|
|
|
// Copyright 2014 Sandia Corporation.
|
|
|
|
// Copyright 2014 UT-Battelle, LLC.
|
|
|
|
// Copyright 2014 Los Alamos National Security.
|
|
|
|
//
|
|
|
|
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
|
|
|
|
// the U.S. Government retains certain rights in this software.
|
|
|
|
//
|
|
|
|
// Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
|
|
|
|
// Laboratory (LANL), the U.S. Government retains certain rights in
|
|
|
|
// this software.
|
|
|
|
//============================================================================
|
|
|
|
#ifndef vtk_m_worklet_AverageByKey_h
|
|
|
|
#define vtk_m_worklet_AverageByKey_h
|
|
|
|
|
2015-07-20 21:01:13 +00:00
|
|
|
#include <vtkm/BinaryPredicates.h>
|
|
|
|
#include <vtkm/VecTraits.h>
|
|
|
|
|
2015-06-18 17:07:31 +00:00
|
|
|
#include <vtkm/cont/ArrayHandle.h>
|
2015-08-28 13:16:45 +00:00
|
|
|
#include <vtkm/cont/ArrayHandleConstant.h>
|
2015-09-15 04:11:09 +00:00
|
|
|
#include <vtkm/cont/ArrayHandleIndex.h>
|
2015-06-18 17:07:31 +00:00
|
|
|
#include <vtkm/cont/ArrayHandlePermutation.h>
|
2015-06-24 18:52:41 +00:00
|
|
|
#include <vtkm/cont/ArrayHandleZip.h>
|
2017-05-18 14:51:24 +00:00
|
|
|
#include <vtkm/cont/DeviceAdapterAlgorithm.h>
|
2015-07-20 21:01:13 +00:00
|
|
|
|
2015-06-18 17:07:31 +00:00
|
|
|
#include <vtkm/worklet/DispatcherMapField.h>
|
|
|
|
#include <vtkm/worklet/WorkletMapField.h>
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
namespace vtkm
|
|
|
|
{
|
|
|
|
namespace worklet
|
|
|
|
{
|
2015-06-18 17:07:31 +00:00
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
struct DivideWorklet : public vtkm::worklet::WorkletMapField
|
2015-08-28 13:16:45 +00:00
|
|
|
{
|
2015-06-25 14:52:02 +00:00
|
|
|
typedef void ControlSignature(FieldIn<>, FieldIn<>, FieldOut<>);
|
|
|
|
typedef void ExecutionSignature(_1, _2, _3);
|
2015-06-18 17:07:31 +00:00
|
|
|
|
2015-08-28 13:16:45 +00:00
|
|
|
template <class ValueType>
|
2017-05-18 14:29:41 +00:00
|
|
|
VTKM_EXEC void operator()(const ValueType& v, const vtkm::Id& count, ValueType& vout) const
|
2015-06-29 21:14:50 +00:00
|
|
|
{
|
|
|
|
typedef typename VecTraits<ValueType>::ComponentType ComponentType;
|
2017-05-18 14:29:41 +00:00
|
|
|
vout = v * ComponentType(1. / static_cast<double>(count));
|
2015-06-29 21:14:50 +00:00
|
|
|
}
|
2015-08-28 13:16:45 +00:00
|
|
|
|
|
|
|
template <class T1, class T2>
|
2017-05-18 14:29:41 +00:00
|
|
|
VTKM_EXEC void operator()(const T1&, const vtkm::Id&, T2&) const
|
|
|
|
{
|
|
|
|
}
|
2015-06-18 17:07:31 +00:00
|
|
|
};
|
|
|
|
|
2017-05-26 17:53:28 +00:00
|
|
|
template <class KeyType,
|
|
|
|
class ValueType,
|
|
|
|
class KeyInStorage,
|
|
|
|
class KeyOutStorage,
|
|
|
|
class ValueInStorage,
|
|
|
|
class ValueOutStorage,
|
|
|
|
class DeviceAdapter>
|
2017-05-18 14:29:41 +00:00
|
|
|
void AverageByKey(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,
|
2015-08-28 13:16:45 +00:00
|
|
|
DeviceAdapter)
|
2015-06-18 17:07:31 +00:00
|
|
|
{
|
2015-06-24 18:52:41 +00:00
|
|
|
typedef vtkm::cont::DeviceAdapterAlgorithm<DeviceAdapter> Algorithm;
|
2015-08-28 13:16:45 +00:00
|
|
|
typedef vtkm::cont::ArrayHandle<ValueType, ValueInStorage> ValueInArray;
|
|
|
|
typedef vtkm::cont::ArrayHandle<vtkm::Id> IdArray;
|
|
|
|
typedef vtkm::cont::ArrayHandle<ValueType> ValueArray;
|
2015-06-18 17:07:31 +00:00
|
|
|
|
|
|
|
// sort the indexed array
|
2015-09-15 04:11:09 +00:00
|
|
|
vtkm::cont::ArrayHandleIndex indexArray(keyArray.GetNumberOfValues());
|
2015-06-24 18:52:41 +00:00
|
|
|
IdArray indexArraySorted;
|
|
|
|
vtkm::cont::ArrayHandle<KeyType> keyArraySorted;
|
2015-06-18 17:07:31 +00:00
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
Algorithm::Copy(keyArray, keyArraySorted); // keep the input key array unchanged
|
|
|
|
Algorithm::Copy(indexArray, indexArraySorted);
|
|
|
|
Algorithm::SortByKey(keyArraySorted, indexArraySorted, vtkm::SortLess());
|
2015-06-18 17:07:31 +00:00
|
|
|
|
2015-06-24 18:52:41 +00:00
|
|
|
// generate permultation array based on the indexes
|
2017-05-18 14:29:41 +00:00
|
|
|
typedef vtkm::cont::ArrayHandlePermutation<IdArray, ValueInArray> PermutatedValueArray;
|
|
|
|
PermutatedValueArray valueArraySorted =
|
|
|
|
vtkm::cont::make_ArrayHandlePermutation(indexArraySorted, valueArray);
|
2015-06-18 17:07:31 +00:00
|
|
|
|
2015-06-25 14:38:34 +00:00
|
|
|
// reduce both sumArray and countArray by key
|
2015-06-24 18:52:41 +00:00
|
|
|
typedef vtkm::cont::ArrayHandleConstant<vtkm::Id> ConstIdArray;
|
|
|
|
ConstIdArray constOneArray(1, valueArray.GetNumberOfValues());
|
|
|
|
IdArray countArray;
|
|
|
|
ValueArray sumArray;
|
2017-05-18 14:29:41 +00:00
|
|
|
vtkm::cont::ArrayHandleZip<PermutatedValueArray, ConstIdArray> inputZipHandle(valueArraySorted,
|
|
|
|
constOneArray);
|
|
|
|
vtkm::cont::ArrayHandleZip<ValueArray, IdArray> outputZipHandle(sumArray, countArray);
|
2015-06-18 17:07:31 +00:00
|
|
|
|
2017-05-26 17:53:28 +00:00
|
|
|
Algorithm::ReduceByKey(
|
|
|
|
keyArraySorted, inputZipHandle, outputKeyArray, outputZipHandle, vtkm::Add());
|
2015-06-18 17:07:31 +00:00
|
|
|
|
|
|
|
// get average
|
2017-05-18 14:29:41 +00:00
|
|
|
DispatcherMapField<DivideWorklet, DeviceAdapter>().Invoke(sumArray, countArray, outputValueArray);
|
2015-06-18 17:07:31 +00:00
|
|
|
}
|
2015-06-19 18:23:20 +00:00
|
|
|
}
|
2015-08-28 13:16:45 +00:00
|
|
|
} // vtkm::worklet
|
2015-06-18 17:07:31 +00:00
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
#endif //vtk_m_worklet_AverageByKey_h
|