WorkletReduceByKey.h 5.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
//============================================================================
//  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 2016 Sandia Corporation.
//  Copyright 2016 UT-Battelle, LLC.
//  Copyright 2016 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_WorkletReduceByKey_h
#define vtk_m_worklet_WorkletReduceByKey_h

#include <vtkm/worklet/internal/WorkletBase.h>

25 26 27
#include <vtkm/cont/arg/TransportTagKeyedValuesIn.h>
#include <vtkm/cont/arg/TransportTagKeyedValuesInOut.h>
#include <vtkm/cont/arg/TransportTagKeyedValuesOut.h>
28
#include <vtkm/cont/arg/TransportTagKeysIn.h>
29
#include <vtkm/cont/arg/TypeCheckTagArray.h>
30 31 32 33
#include <vtkm/cont/arg/TypeCheckTagKeys.h>

#include <vtkm/exec/internal/ReduceByKeyLookup.h>

34 35 36
#include <vtkm/exec/arg/FetchTagArrayDirectIn.h>
#include <vtkm/exec/arg/FetchTagArrayDirectInOut.h>
#include <vtkm/exec/arg/FetchTagArrayDirectOut.h>
37 38
#include <vtkm/exec/arg/FetchTagKeysIn.h>
#include <vtkm/exec/arg/ThreadIndicesReduceByKey.h>
39
#include <vtkm/exec/arg/ValueCount.h>
40 41 42 43 44 45 46 47 48

namespace vtkm {
namespace worklet {

class WorkletReduceByKey : public vtkm::worklet::internal::WorkletBase
{
public:
  /// \brief A control signature tag for input keys.
  ///
49
  /// A \c WorkletReduceByKey operates by collecting all identical keys and
50 51 52 53 54 55 56 57 58 59 60 61 62
  /// then executing the worklet on each unique key. This tag specifies a
  /// \c Keys object that defines and manages these keys.
  ///
  /// A \c WorkletReduceByKey should have exactly one \c KeysIn tag in its \c
  /// ControlSignature, and the \c InputDomain should point to it.
  ///
  struct KeysIn : vtkm::cont::arg::ControlSignatureTagBase
  {
    using TypeCheckTag = vtkm::cont::arg::TypeCheckTagKeys;
    using TransportTag = vtkm::cont::arg::TransportTagKeysIn;
    using FetchTag = vtkm::exec::arg::FetchTagKeysIn;
  };

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
  /// \brief A control signature tag for input values.
  ///
  /// A \c WorkletReduceByKey operates by collecting all values associated with
  /// identical keys and then giving the worklet a Vec-like object containing
  /// all values with a matching key. This tag specifies an \c ArrayHandle
  /// object that holds the values.
  ///
  template<typename TypeList = AllTypes>
  struct ValuesIn : vtkm::cont::arg::ControlSignatureTagBase
  {
    using TypeCheckTag = vtkm::cont::arg::TypeCheckTagArray<TypeList>;
    using TransportTag = vtkm::cont::arg::TransportTagKeyedValuesIn;
    using FetchTag = vtkm::exec::arg::FetchTagArrayDirectIn;
  };

  /// \brief A control signature tag for input/output values.
  ///
  /// A \c WorkletReduceByKey operates by collecting all values associated with
  /// identical keys and then giving the worklet a Vec-like object containing
  /// all values with a matching key. This tag specifies an \c ArrayHandle
  /// object that holds the values.
  ///
85 86
  /// This tag might not work with scatter operations.
  ///
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
  template<typename TypeList = AllTypes>
  struct ValuesInOut : vtkm::cont::arg::ControlSignatureTagBase
  {
    using TypeCheckTag = vtkm::cont::arg::TypeCheckTagArray<TypeList>;
    using TransportTag = vtkm::cont::arg::TransportTagKeyedValuesInOut;
    using FetchTag = vtkm::exec::arg::FetchTagArrayDirectIn;
  };

  /// \brief A control signature tag for output values.
  ///
  /// A \c WorkletReduceByKey operates by collecting all values associated with
  /// identical keys and then giving the worklet a Vec-like object containing
  /// all values with a matching key. This tag specifies an \c ArrayHandle
  /// object that holds the values.
  ///
102 103
  /// This tag might not work with scatter operations.
  ///
104 105 106 107 108 109 110 111
  template<typename TypeList = AllTypes>
  struct ValuesOut : vtkm::cont::arg::ControlSignatureTagBase
  {
    using TypeCheckTag = vtkm::cont::arg::TypeCheckTagArray<TypeList>;
    using TransportTag = vtkm::cont::arg::TransportTagKeyedValuesOut;
    using FetchTag = vtkm::exec::arg::FetchTagArrayDirectIn;
  };

112 113 114 115 116 117 118 119 120
  /// \brief The \c ExecutionSignature tag to get the number of values.
  ///
  /// A \c WorkletReduceByKey operates by collecting all values associated with
  /// identical keys and then giving the worklet a Vec-like object containing all
  /// values with a matching key. This \c ExecutionSignature tag provides the
  /// number of values associated with the key and given in the Vec-like objects.
  ///
  struct ValueCount : vtkm::exec::arg::ValueCount {  };

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
  /// Reduce by key worklets use the related thread indices class.
  ///
  VTKM_SUPPRESS_EXEC_WARNINGS
  template<typename T,
           typename OutToInArrayType,
           typename VisitArrayType,
           typename InputDomainType>
  VTKM_EXEC
  vtkm::exec::arg::ThreadIndicesReduceByKey
  GetThreadIndices(const T& threadIndex,
                   const OutToInArrayType& outToIn,
                   const VisitArrayType& visit,
                   const InputDomainType &inputDomain,
                   const T& globalThreadIndexOffset=0) const
  {
    return vtkm::exec::arg::ThreadIndicesReduceByKey(
          threadIndex,
          outToIn.Get(threadIndex),
          visit.Get(threadIndex),
          inputDomain,
          globalThreadIndexOffset);
  }
};

}
} // namespace vtkm::worklet

#endif //vtk_m_worklet_WorkletReduceByKey_h