Merge topic 'deprecate-reduce-by-key-count'

d77c5812c Deprecate the GetCounts() method in Keys objects

Acked-by: Kitware Robot <kwrobot@kitware.com>
Merge-request: !3183
This commit is contained in:
Kenneth Moreland 2024-01-29 17:24:52 +00:00 committed by Kitware Robot
commit 4807417358
8 changed files with 48 additions and 31 deletions

@ -0,0 +1,12 @@
# Deprecate the GetCounts() method in Keys objects
The `vtkm::worklet::Keys` object held a `SortedValuesMap` array, an
`Offsets` array, a `Counts` array, and (optionally) a `UniqueKeys` array.
Of these, the `Counts` array is redundant because the counts are trivially
computed by subtracting adjacent entries in the offsets array. This pattern
shows up a lot in VTK-m, and most places we have moved to removing the
counts and just using the offsets.
This change removes the `Count` array from the `Keys` object. Where the
count is needed internally, adjacent offsets are subtracted. The deprecated
`GetCounts` method is implemented by copying values into a new array.

@ -42,7 +42,8 @@ public:
const vtkm::exec::internal::ReduceByKeyLookupBase<P1, P2>& keyLookup)
: Superclass(threadIndex, inIndex, visitIndex, outIndex)
, ValueOffset(keyLookup.Offsets.Get(inIndex))
, NumberOfValues(keyLookup.Counts.Get(inIndex))
, NumberOfValues(static_cast<vtkm::IdComponent>(keyLookup.Offsets.Get(inIndex + 1) -
keyLookup.Offsets.Get(inIndex)))
{
}

@ -35,15 +35,11 @@ struct ReduceByKeyLookupBase
IdPortalType SortedValuesMap;
IdPortalType Offsets;
IdComponentPortalType Counts;
VTKM_EXEC_CONT
ReduceByKeyLookupBase(const IdPortalType& sortedValuesMap,
const IdPortalType& offsets,
const IdComponentPortalType& counts)
ReduceByKeyLookupBase(const IdPortalType& sortedValuesMap, const IdPortalType& offsets)
: SortedValuesMap(sortedValuesMap)
, Offsets(offsets)
, Counts(counts)
{
}
@ -68,9 +64,8 @@ struct ReduceByKeyLookup : ReduceByKeyLookupBase<IdPortalType, IdComponentPortal
VTKM_EXEC_CONT
ReduceByKeyLookup(const KeyPortalType& uniqueKeys,
const IdPortalType& sortedValuesMap,
const IdPortalType& offsets,
const IdComponentPortalType& counts)
: ReduceByKeyLookupBase<IdPortalType, IdComponentPortalType>(sortedValuesMap, offsets, counts)
const IdPortalType& offsets)
: ReduceByKeyLookupBase<IdPortalType, IdComponentPortalType>(sortedValuesMap, offsets)
, UniqueKeys(uniqueKeys)
{
}

@ -19,6 +19,7 @@
#include <vtkm/cont/ArrayHandlePermutation.h>
#include <vtkm/cont/Logging.h>
#include <vtkm/Deprecated.h>
#include <vtkm/Hash.h>
#include <vtkm/exec/internal/ReduceByKeyLookup.h>
@ -52,7 +53,7 @@ public:
~KeysBase() = default;
VTKM_CONT
vtkm::Id GetInputRange() const { return this->Counts.GetNumberOfValues(); }
vtkm::Id GetInputRange() const { return this->Offsets.GetNumberOfValues() - 1; }
VTKM_CONT
vtkm::cont::ArrayHandle<vtkm::Id> GetSortedValuesMap() const { return this->SortedValuesMap; }
@ -60,8 +61,9 @@ public:
VTKM_CONT
vtkm::cont::ArrayHandle<vtkm::Id> GetOffsets() const { return this->Offsets; }
VTKM_DEPRECATED(2.2, "Use the `GetOffsets()` array in an `ArrayHandleOffsetsToNumComponents`.")
VTKM_CONT
vtkm::cont::ArrayHandle<vtkm::IdComponent> GetCounts() const { return this->Counts; }
vtkm::cont::ArrayHandle<vtkm::IdComponent> GetCounts() const;
VTKM_CONT
vtkm::Id GetNumberOfValues() const { return this->SortedValuesMap.GetNumberOfValues(); }
@ -74,15 +76,14 @@ public:
vtkm::cont::Token& token) const
{
return ExecLookup(this->SortedValuesMap.PrepareForInput(device, token),
this->Offsets.PrepareForInput(device, token),
this->Counts.PrepareForInput(device, token));
this->Offsets.PrepareForInput(device, token));
}
VTKM_CONT
bool operator==(const vtkm::worklet::internal::KeysBase& other) const
{
return ((this->SortedValuesMap == other.SortedValuesMap) && (this->Offsets == other.Offsets) &&
(this->Counts == other.Counts));
(this->Offsets == other.Offsets));
}
VTKM_CONT
@ -96,7 +97,6 @@ protected:
vtkm::cont::ArrayHandle<vtkm::Id> SortedValuesMap;
vtkm::cont::ArrayHandle<vtkm::Id> Offsets;
vtkm::cont::ArrayHandle<vtkm::IdComponent> Counts;
};
} // namespace internal
@ -184,16 +184,14 @@ public:
{
return ExecLookup(this->UniqueKeys.PrepareForInput(device, token),
this->SortedValuesMap.PrepareForInput(device, token),
this->Offsets.PrepareForInput(device, token),
this->Counts.PrepareForInput(device, token));
this->Offsets.PrepareForInput(device, token));
}
VTKM_CONT
bool operator==(const vtkm::worklet::Keys<KeyType>& other) const
{
return ((this->UniqueKeys == other.UniqueKeys) &&
(this->SortedValuesMap == other.SortedValuesMap) && (this->Offsets == other.Offsets) &&
(this->Counts == other.Counts));
(this->SortedValuesMap == other.SortedValuesMap) && (this->Offsets == other.Offsets));
}
VTKM_CONT

@ -87,16 +87,17 @@ VTKM_CONT void Keys<T>::BuildArraysInternal(KeyArrayType& keys, vtkm::cont::Devi
vtkm::cont::Algorithm::SortByKey(device, keys, this->SortedValuesMap);
// Find the unique keys and the number of values per key.
vtkm::cont::ArrayHandle<vtkm::IdComponent> counts;
vtkm::cont::Algorithm::ReduceByKey(device,
keys,
vtkm::cont::ArrayHandleConstant<vtkm::IdComponent>(1, numKeys),
this->UniqueKeys,
this->Counts,
counts,
vtkm::Sum());
// Get the offsets from the counts with a scan.
vtkm::cont::Algorithm::ScanExtended(
device, vtkm::cont::make_ArrayHandleCast(this->Counts, vtkm::Id()), this->Offsets);
device, vtkm::cont::make_ArrayHandleCast(counts, vtkm::Id()), this->Offsets);
VTKM_ASSERT(numKeys ==
vtkm::cont::ArrayGetValue(this->Offsets.GetNumberOfValues() - 1, this->Offsets));
@ -116,16 +117,17 @@ VTKM_CONT void Keys<T>::BuildArraysInternalStable(const KeyArrayType& keys,
auto sortedKeys = vtkm::cont::make_ArrayHandlePermutation(this->SortedValuesMap, keys);
// Find the unique keys and the number of values per key.
vtkm::cont::ArrayHandle<vtkm::IdComponent> counts;
vtkm::cont::Algorithm::ReduceByKey(device,
sortedKeys,
vtkm::cont::ArrayHandleConstant<vtkm::IdComponent>(1, numKeys),
this->UniqueKeys,
this->Counts,
counts,
vtkm::Sum());
// Get the offsets from the counts with a scan.
vtkm::cont::Algorithm::ScanExtended(
device, vtkm::cont::make_ArrayHandleCast(this->Counts, vtkm::Id()), this->Offsets);
device, vtkm::cont::make_ArrayHandleCast(counts, vtkm::Id()), this->Offsets);
VTKM_ASSERT(numKeys ==
vtkm::cont::ArrayGetValue(this->Offsets.GetNumberOfValues() - 1, this->Offsets));

@ -27,3 +27,15 @@ VTK_M_KEYS_EXPORT(vtkm::IdComponent);
#endif
#undef VTK_M_KEYS_EXPORT
// Putting this deprecated implementation here because I am too lazy to create
// a separate source file just for a deprecated method.
#include <vtkm/cont/ArrayCopyDevice.h>
#include <vtkm/cont/ArrayHandleOffsetsToNumComponents.h>
vtkm::cont::ArrayHandle<vtkm::IdComponent> vtkm::worklet::internal::KeysBase::GetCounts() const
{
vtkm::cont::ArrayHandle<vtkm::IdComponent> counts;
vtkm::cont::ArrayCopyDevice(
vtkm::cont::make_ArrayHandleOffsetsToNumComponents(this->GetOffsets()), counts);
return counts;
}

@ -20,25 +20,24 @@ namespace
static constexpr vtkm::Id ARRAY_SIZE = 1033;
static constexpr vtkm::Id NUM_UNIQUE = ARRAY_SIZE / 10;
template <typename KeyPortal, typename IdPortal, typename IdComponentPortal>
template <typename KeyPortal, typename IdPortal>
void CheckKeyReduce(const KeyPortal& originalKeys,
const KeyPortal& uniqueKeys,
const IdPortal& sortedValuesMap,
const IdPortal& offsets,
const IdComponentPortal& counts)
const IdPortal& offsets)
{
using KeyType = typename KeyPortal::ValueType;
vtkm::Id originalSize = originalKeys.GetNumberOfValues();
vtkm::Id uniqueSize = uniqueKeys.GetNumberOfValues();
VTKM_TEST_ASSERT(originalSize == sortedValuesMap.GetNumberOfValues(), "Inconsistent array size.");
VTKM_TEST_ASSERT(uniqueSize == offsets.GetNumberOfValues() - 1, "Inconsistent array size.");
VTKM_TEST_ASSERT(uniqueSize == counts.GetNumberOfValues(), "Inconsistent array size.");
for (vtkm::Id uniqueIndex = 0; uniqueIndex < uniqueSize; uniqueIndex++)
{
KeyType key = uniqueKeys.Get(uniqueIndex);
vtkm::Id offset = offsets.Get(uniqueIndex);
vtkm::IdComponent groupCount = counts.Get(uniqueIndex);
vtkm::IdComponent groupCount =
static_cast<vtkm::IdComponent>(offsets.Get(uniqueIndex + 1) - offset);
for (vtkm::IdComponent groupIndex = 0; groupIndex < groupCount; groupIndex++)
{
vtkm::Id originalIndex = sortedValuesMap.Get(offset + groupIndex);
@ -69,8 +68,7 @@ void TryKeyType(KeyType)
CheckKeyReduce(keyArray.ReadPortal(),
keys.GetUniqueKeys().ReadPortal(),
keys.GetSortedValuesMap().ReadPortal(),
keys.GetOffsets().ReadPortal(),
keys.GetCounts().ReadPortal());
keys.GetOffsets().ReadPortal());
}
void TestKeys()

@ -131,7 +131,6 @@ void TryKeyType(KeyType)
vtkm::worklet::Keys<KeyType> keys(sortedKeys);
vtkm::cont::printSummary_ArrayHandle(keys.GetUniqueKeys(), std::cout);
vtkm::cont::printSummary_ArrayHandle(keys.GetOffsets(), std::cout);
vtkm::cont::printSummary_ArrayHandle(keys.GetCounts(), std::cout);
vtkm::cont::ArrayHandle<KeyType> valuesToModify;
valuesToModify.Allocate(ARRAY_SIZE);