2017-05-18 14:29:41 +00:00
|
|
|
//============================================================================
|
2014-02-11 17:34:56 +00:00
|
|
|
// Copyright (c) Kitware, Inc.
|
|
|
|
// All rights reserved.
|
|
|
|
// See LICENSE.txt for details.
|
2019-04-15 23:24:21 +00:00
|
|
|
//
|
2014-02-11 17:34:56 +00:00
|
|
|
// 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.
|
|
|
|
//============================================================================
|
2016-09-28 15:38:31 +00:00
|
|
|
#ifndef vtk_m_cont_serial_internal_DeviceAdapterAlgorithmSerial_h
|
|
|
|
#define vtk_m_cont_serial_internal_DeviceAdapterAlgorithmSerial_h
|
2014-02-11 17:34:56 +00:00
|
|
|
|
|
|
|
#include <vtkm/cont/ArrayHandle.h>
|
2015-06-10 13:01:55 +00:00
|
|
|
#include <vtkm/cont/ArrayHandleZip.h>
|
2014-09-08 20:59:11 +00:00
|
|
|
#include <vtkm/cont/ArrayPortalToIterators.h>
|
2014-11-25 14:14:04 +00:00
|
|
|
#include <vtkm/cont/DeviceAdapterAlgorithm.h>
|
2014-02-11 17:34:56 +00:00
|
|
|
#include <vtkm/cont/ErrorExecution.h>
|
|
|
|
#include <vtkm/cont/internal/DeviceAdapterAlgorithmGeneral.h>
|
2016-09-28 15:38:31 +00:00
|
|
|
#include <vtkm/cont/serial/internal/DeviceAdapterTagSerial.h>
|
2014-02-11 17:34:56 +00:00
|
|
|
|
2015-08-27 17:50:32 +00:00
|
|
|
#include <vtkm/BinaryOperators.h>
|
|
|
|
|
2016-09-12 14:30:03 +00:00
|
|
|
#include <vtkm/exec/serial/internal/TaskTiling.h>
|
2014-02-11 17:34:56 +00:00
|
|
|
|
|
|
|
#include <algorithm>
|
2017-10-10 20:11:33 +00:00
|
|
|
#include <iterator>
|
2014-02-11 17:34:56 +00:00
|
|
|
#include <numeric>
|
2017-10-10 20:11:33 +00:00
|
|
|
#include <type_traits>
|
2014-02-11 17:34:56 +00:00
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
namespace vtkm
|
|
|
|
{
|
|
|
|
namespace cont
|
|
|
|
{
|
2014-02-11 17:34:56 +00:00
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
template <>
|
|
|
|
struct DeviceAdapterAlgorithm<vtkm::cont::DeviceAdapterTagSerial>
|
|
|
|
: vtkm::cont::internal::DeviceAdapterAlgorithmGeneral<
|
|
|
|
DeviceAdapterAlgorithm<vtkm::cont::DeviceAdapterTagSerial>,
|
|
|
|
vtkm::cont::DeviceAdapterTagSerial>
|
2014-02-11 17:34:56 +00:00
|
|
|
{
|
|
|
|
private:
|
2017-06-23 18:50:34 +00:00
|
|
|
using Device = vtkm::cont::DeviceAdapterTagSerial;
|
2014-02-11 17:34:56 +00:00
|
|
|
|
2017-10-10 20:11:33 +00:00
|
|
|
// MSVC likes complain about narrowing type conversions in std::copy and
|
|
|
|
// provides no reasonable way to disable the warning. As a work-around, this
|
|
|
|
// template calls std::copy if and only if the types match, otherwise falls
|
|
|
|
// back to a iterative casting approach. Since std::copy can only really
|
|
|
|
// optimize same-type copies, this shouldn't affect performance.
|
|
|
|
template <typename InIter, typename OutIter>
|
2018-07-11 22:45:30 +00:00
|
|
|
static void DoCopy(InIter src, InIter srcEnd, OutIter dst, std::false_type)
|
2017-10-10 20:11:33 +00:00
|
|
|
{
|
|
|
|
using OutputType = typename std::iterator_traits<OutIter>::value_type;
|
|
|
|
while (src != srcEnd)
|
|
|
|
{
|
|
|
|
*dst = static_cast<OutputType>(*src);
|
|
|
|
++src;
|
|
|
|
++dst;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename InIter, typename OutIter>
|
2018-07-11 22:45:30 +00:00
|
|
|
static void DoCopy(InIter src, InIter srcEnd, OutIter dst, std::true_type)
|
2017-10-10 20:11:33 +00:00
|
|
|
{
|
|
|
|
std::copy(src, srcEnd, dst);
|
|
|
|
}
|
|
|
|
|
2014-02-11 17:34:56 +00:00
|
|
|
public:
|
2017-10-10 16:42:13 +00:00
|
|
|
template <typename T, typename U, class CIn, class COut>
|
|
|
|
VTKM_CONT static void Copy(const vtkm::cont::ArrayHandle<T, CIn>& input,
|
|
|
|
vtkm::cont::ArrayHandle<U, COut>& output)
|
|
|
|
{
|
2019-09-03 15:53:39 +00:00
|
|
|
VTKM_LOG_SCOPE_FUNCTION(vtkm::cont::LogLevel::Perf);
|
|
|
|
|
2017-10-10 16:42:13 +00:00
|
|
|
const vtkm::Id inSize = input.GetNumberOfValues();
|
|
|
|
auto inputPortal = input.PrepareForInput(DeviceAdapterTagSerial());
|
|
|
|
auto outputPortal = output.PrepareForOutput(inSize, DeviceAdapterTagSerial());
|
|
|
|
|
2017-10-31 17:35:13 +00:00
|
|
|
if (inSize <= 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-10 20:11:33 +00:00
|
|
|
using InputType = decltype(inputPortal.Get(0));
|
|
|
|
using OutputType = decltype(outputPortal.Get(0));
|
|
|
|
|
|
|
|
DoCopy(vtkm::cont::ArrayPortalToIteratorBegin(inputPortal),
|
|
|
|
vtkm::cont::ArrayPortalToIteratorEnd(inputPortal),
|
|
|
|
vtkm::cont::ArrayPortalToIteratorBegin(outputPortal),
|
|
|
|
std::is_same<InputType, OutputType>());
|
2017-10-10 16:42:13 +00:00
|
|
|
}
|
|
|
|
|
2017-09-19 15:08:10 +00:00
|
|
|
template <typename T, typename U, class CIn, class CStencil, class COut>
|
|
|
|
VTKM_CONT static void CopyIf(const vtkm::cont::ArrayHandle<T, CIn>& input,
|
|
|
|
const vtkm::cont::ArrayHandle<U, CStencil>& stencil,
|
|
|
|
vtkm::cont::ArrayHandle<T, COut>& output)
|
|
|
|
{
|
2019-09-03 15:53:39 +00:00
|
|
|
VTKM_LOG_SCOPE_FUNCTION(vtkm::cont::LogLevel::Perf);
|
|
|
|
|
2017-09-19 15:08:10 +00:00
|
|
|
::vtkm::NotZeroInitialized unary_predicate;
|
|
|
|
CopyIf(input, stencil, output, unary_predicate);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename U, class CIn, class CStencil, class COut, class UnaryPredicate>
|
|
|
|
VTKM_CONT static void CopyIf(const vtkm::cont::ArrayHandle<T, CIn>& input,
|
|
|
|
const vtkm::cont::ArrayHandle<U, CStencil>& stencil,
|
|
|
|
vtkm::cont::ArrayHandle<T, COut>& output,
|
|
|
|
UnaryPredicate predicate)
|
|
|
|
{
|
2019-09-03 15:53:39 +00:00
|
|
|
VTKM_LOG_SCOPE_FUNCTION(vtkm::cont::LogLevel::Perf);
|
|
|
|
|
2017-09-19 15:08:10 +00:00
|
|
|
vtkm::Id inputSize = input.GetNumberOfValues();
|
|
|
|
VTKM_ASSERT(inputSize == stencil.GetNumberOfValues());
|
|
|
|
|
|
|
|
auto inputPortal = input.PrepareForInput(DeviceAdapterTagSerial());
|
|
|
|
auto stencilPortal = stencil.PrepareForInput(DeviceAdapterTagSerial());
|
|
|
|
auto outputPortal = output.PrepareForOutput(inputSize, DeviceAdapterTagSerial());
|
|
|
|
|
|
|
|
vtkm::Id readPos = 0;
|
|
|
|
vtkm::Id writePos = 0;
|
|
|
|
|
|
|
|
for (; readPos < inputSize; ++readPos)
|
|
|
|
{
|
|
|
|
if (predicate(stencilPortal.Get(readPos)))
|
|
|
|
{
|
|
|
|
outputPortal.Set(writePos, inputPortal.Get(readPos));
|
|
|
|
++writePos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
output.Shrink(writePos);
|
|
|
|
}
|
|
|
|
|
2017-10-10 16:42:13 +00:00
|
|
|
template <typename T, typename U, class CIn, class COut>
|
|
|
|
VTKM_CONT static bool CopySubRange(const vtkm::cont::ArrayHandle<T, CIn>& input,
|
|
|
|
vtkm::Id inputStartIndex,
|
|
|
|
vtkm::Id numberOfElementsToCopy,
|
|
|
|
vtkm::cont::ArrayHandle<U, COut>& output,
|
|
|
|
vtkm::Id outputIndex = 0)
|
|
|
|
{
|
2019-09-03 15:53:39 +00:00
|
|
|
VTKM_LOG_SCOPE_FUNCTION(vtkm::cont::LogLevel::Perf);
|
|
|
|
|
2017-10-10 16:42:13 +00:00
|
|
|
const vtkm::Id inSize = input.GetNumberOfValues();
|
2017-10-10 19:30:49 +00:00
|
|
|
|
|
|
|
// Check if the ranges overlap and fail if they do.
|
|
|
|
if (input == output && ((outputIndex >= inputStartIndex &&
|
|
|
|
outputIndex < inputStartIndex + numberOfElementsToCopy) ||
|
|
|
|
(inputStartIndex >= outputIndex &&
|
|
|
|
inputStartIndex < outputIndex + numberOfElementsToCopy)))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-10-10 16:42:13 +00:00
|
|
|
if (inputStartIndex < 0 || numberOfElementsToCopy < 0 || outputIndex < 0 ||
|
|
|
|
inputStartIndex >= inSize)
|
|
|
|
{ //invalid parameters
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//determine if the numberOfElementsToCopy needs to be reduced
|
|
|
|
if (inSize < (inputStartIndex + numberOfElementsToCopy))
|
|
|
|
{ //adjust the size
|
|
|
|
numberOfElementsToCopy = (inSize - inputStartIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
const vtkm::Id outSize = output.GetNumberOfValues();
|
|
|
|
const vtkm::Id copyOutEnd = outputIndex + numberOfElementsToCopy;
|
|
|
|
if (outSize < copyOutEnd)
|
|
|
|
{ //output is not large enough
|
|
|
|
if (outSize == 0)
|
|
|
|
{ //since output has nothing, just need to allocate to correct length
|
|
|
|
output.Allocate(copyOutEnd);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ //we currently have data in this array, so preserve it in the new
|
|
|
|
//resized array
|
|
|
|
vtkm::cont::ArrayHandle<U, COut> temp;
|
|
|
|
temp.Allocate(copyOutEnd);
|
|
|
|
CopySubRange(output, 0, outSize, temp);
|
|
|
|
output = temp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto inputPortal = input.PrepareForInput(DeviceAdapterTagSerial());
|
|
|
|
auto outputPortal = output.PrepareForInPlace(DeviceAdapterTagSerial());
|
|
|
|
auto inIter = vtkm::cont::ArrayPortalToIteratorBegin(inputPortal);
|
|
|
|
auto outIter = vtkm::cont::ArrayPortalToIteratorBegin(outputPortal);
|
|
|
|
|
2017-10-10 20:11:33 +00:00
|
|
|
using InputType = decltype(inputPortal.Get(0));
|
|
|
|
using OutputType = decltype(outputPortal.Get(0));
|
|
|
|
|
|
|
|
DoCopy(inIter + inputStartIndex,
|
|
|
|
inIter + inputStartIndex + numberOfElementsToCopy,
|
|
|
|
outIter + outputIndex,
|
|
|
|
std::is_same<InputType, OutputType>());
|
2017-10-10 16:42:13 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
template <typename T, typename U, class CIn>
|
|
|
|
VTKM_CONT static U Reduce(const vtkm::cont::ArrayHandle<T, CIn>& input, U initialValue)
|
2015-05-04 19:53:35 +00:00
|
|
|
{
|
2019-09-03 15:53:39 +00:00
|
|
|
VTKM_LOG_SCOPE_FUNCTION(vtkm::cont::LogLevel::Perf);
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
return Reduce(input, initialValue, vtkm::Add());
|
2015-05-04 19:53:35 +00:00
|
|
|
}
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
template <typename T, typename U, class CIn, class BinaryFunctor>
|
2017-05-26 17:53:28 +00:00
|
|
|
VTKM_CONT static U Reduce(const vtkm::cont::ArrayHandle<T, CIn>& input,
|
|
|
|
U initialValue,
|
2017-05-18 14:29:41 +00:00
|
|
|
BinaryFunctor binary_functor)
|
2015-05-04 19:53:35 +00:00
|
|
|
{
|
2019-09-03 15:53:39 +00:00
|
|
|
VTKM_LOG_SCOPE_FUNCTION(vtkm::cont::LogLevel::Perf);
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
internal::WrappedBinaryOperator<U, BinaryFunctor> wrappedOp(binary_functor);
|
2017-08-10 20:13:55 +00:00
|
|
|
auto inputPortal = input.PrepareForInput(Device());
|
2015-05-04 19:53:35 +00:00
|
|
|
return std::accumulate(vtkm::cont::ArrayPortalToIteratorBegin(inputPortal),
|
2017-05-26 17:53:28 +00:00
|
|
|
vtkm::cont::ArrayPortalToIteratorEnd(inputPortal),
|
|
|
|
initialValue,
|
2015-06-11 13:52:16 +00:00
|
|
|
wrappedOp);
|
2015-05-04 19:53:35 +00:00
|
|
|
}
|
|
|
|
|
2017-05-26 17:53:28 +00:00
|
|
|
template <typename T,
|
|
|
|
typename U,
|
|
|
|
class KIn,
|
|
|
|
class VIn,
|
|
|
|
class KOut,
|
|
|
|
class VOut,
|
2017-05-18 14:29:41 +00:00
|
|
|
class BinaryFunctor>
|
|
|
|
VTKM_CONT static void ReduceByKey(const vtkm::cont::ArrayHandle<T, KIn>& keys,
|
|
|
|
const vtkm::cont::ArrayHandle<U, VIn>& values,
|
|
|
|
vtkm::cont::ArrayHandle<T, KOut>& keys_output,
|
|
|
|
vtkm::cont::ArrayHandle<U, VOut>& values_output,
|
|
|
|
BinaryFunctor binary_functor)
|
2015-05-04 19:53:35 +00:00
|
|
|
{
|
2019-09-03 15:53:39 +00:00
|
|
|
VTKM_LOG_SCOPE_FUNCTION(vtkm::cont::LogLevel::Perf);
|
|
|
|
|
2017-08-10 20:13:55 +00:00
|
|
|
auto keysPortalIn = keys.PrepareForInput(Device());
|
|
|
|
auto valuesPortalIn = values.PrepareForInput(Device());
|
2015-05-04 19:53:35 +00:00
|
|
|
const vtkm::Id numberOfKeys = keys.GetNumberOfValues();
|
2017-10-09 14:08:57 +00:00
|
|
|
|
|
|
|
VTKM_ASSERT(numberOfKeys == values.GetNumberOfValues());
|
|
|
|
if (numberOfKeys == 0)
|
|
|
|
{
|
|
|
|
keys_output.Shrink(0);
|
|
|
|
values_output.Shrink(0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-10 20:13:55 +00:00
|
|
|
auto keysPortalOut = keys_output.PrepareForOutput(numberOfKeys, Device());
|
|
|
|
auto valuesPortalOut = values_output.PrepareForOutput(numberOfKeys, Device());
|
2015-05-04 19:53:35 +00:00
|
|
|
|
|
|
|
vtkm::Id writePos = 0;
|
|
|
|
vtkm::Id readPos = 0;
|
|
|
|
|
|
|
|
T currentKey = keysPortalIn.Get(readPos);
|
|
|
|
U currentValue = valuesPortalIn.Get(readPos);
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
for (++readPos; readPos < numberOfKeys; ++readPos)
|
|
|
|
{
|
|
|
|
while (readPos < numberOfKeys && currentKey == keysPortalIn.Get(readPos))
|
2015-05-04 19:53:35 +00:00
|
|
|
{
|
2015-06-22 15:55:02 +00:00
|
|
|
currentValue = binary_functor(currentValue, valuesPortalIn.Get(readPos));
|
2015-05-04 19:53:35 +00:00
|
|
|
++readPos;
|
2017-05-18 14:29:41 +00:00
|
|
|
}
|
2015-05-04 19:53:35 +00:00
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
if (readPos < numberOfKeys)
|
|
|
|
{
|
2015-05-04 19:53:35 +00:00
|
|
|
keysPortalOut.Set(writePos, currentKey);
|
|
|
|
valuesPortalOut.Set(writePos, currentValue);
|
|
|
|
++writePos;
|
|
|
|
|
|
|
|
currentKey = keysPortalIn.Get(readPos);
|
|
|
|
currentValue = valuesPortalIn.Get(readPos);
|
|
|
|
}
|
2017-05-18 14:29:41 +00:00
|
|
|
}
|
2015-05-04 19:53:35 +00:00
|
|
|
|
|
|
|
//now write out the last set of values
|
|
|
|
keysPortalOut.Set(writePos, currentKey);
|
|
|
|
valuesPortalOut.Set(writePos, currentValue);
|
|
|
|
|
|
|
|
//now we need to shrink to the correct number of keys/values
|
|
|
|
//writePos is zero-based so add 1 to get correct length
|
2017-05-18 14:29:41 +00:00
|
|
|
keys_output.Shrink(writePos + 1);
|
|
|
|
values_output.Shrink(writePos + 1);
|
2015-05-04 19:53:35 +00:00
|
|
|
}
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
template <typename T, class CIn, class COut, class BinaryFunctor>
|
|
|
|
VTKM_CONT static T ScanInclusive(const vtkm::cont::ArrayHandle<T, CIn>& input,
|
|
|
|
vtkm::cont::ArrayHandle<T, COut>& output,
|
|
|
|
BinaryFunctor binary_functor)
|
2015-04-29 19:48:25 +00:00
|
|
|
{
|
2019-09-03 15:53:39 +00:00
|
|
|
VTKM_LOG_SCOPE_FUNCTION(vtkm::cont::LogLevel::Perf);
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
internal::WrappedBinaryOperator<T, BinaryFunctor> wrappedBinaryOp(binary_functor);
|
2015-04-30 13:01:52 +00:00
|
|
|
|
2015-04-29 19:48:25 +00:00
|
|
|
vtkm::Id numberOfValues = input.GetNumberOfValues();
|
|
|
|
|
2017-08-10 20:13:55 +00:00
|
|
|
auto inputPortal = input.PrepareForInput(Device());
|
|
|
|
auto outputPortal = output.PrepareForOutput(numberOfValues, Device());
|
2015-04-29 19:48:25 +00:00
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
if (numberOfValues <= 0)
|
|
|
|
{
|
|
|
|
return vtkm::TypeTraits<T>::ZeroInitialization();
|
|
|
|
}
|
2015-04-29 19:48:25 +00:00
|
|
|
|
|
|
|
std::partial_sum(vtkm::cont::ArrayPortalToIteratorBegin(inputPortal),
|
2015-04-30 13:01:52 +00:00
|
|
|
vtkm::cont::ArrayPortalToIteratorEnd(inputPortal),
|
2017-05-26 17:53:28 +00:00
|
|
|
vtkm::cont::ArrayPortalToIteratorBegin(outputPortal),
|
|
|
|
wrappedBinaryOp);
|
2015-04-29 19:48:25 +00:00
|
|
|
|
|
|
|
// Return the value at the last index in the array, which is the full sum.
|
|
|
|
return outputPortal.Get(numberOfValues - 1);
|
|
|
|
}
|
|
|
|
|
2018-01-30 16:57:13 +00:00
|
|
|
template <typename T, class CIn, class COut>
|
|
|
|
VTKM_CONT static T ScanInclusive(const vtkm::cont::ArrayHandle<T, CIn>& input,
|
|
|
|
vtkm::cont::ArrayHandle<T, COut>& output)
|
|
|
|
{
|
2019-09-03 15:53:39 +00:00
|
|
|
VTKM_LOG_SCOPE_FUNCTION(vtkm::cont::LogLevel::Perf);
|
|
|
|
|
2018-01-30 16:57:13 +00:00
|
|
|
return ScanInclusive(input, output, vtkm::Sum());
|
|
|
|
}
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
template <typename T, class CIn, class COut, class BinaryFunctor>
|
|
|
|
VTKM_CONT static T ScanExclusive(const vtkm::cont::ArrayHandle<T, CIn>& input,
|
|
|
|
vtkm::cont::ArrayHandle<T, COut>& output,
|
2017-05-26 17:53:28 +00:00
|
|
|
BinaryFunctor binaryFunctor,
|
|
|
|
const T& initialValue)
|
2014-02-11 17:34:56 +00:00
|
|
|
{
|
2019-09-03 15:53:39 +00:00
|
|
|
VTKM_LOG_SCOPE_FUNCTION(vtkm::cont::LogLevel::Perf);
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
internal::WrappedBinaryOperator<T, BinaryFunctor> wrappedBinaryOp(binaryFunctor);
|
2015-08-27 17:50:32 +00:00
|
|
|
|
2014-02-11 17:34:56 +00:00
|
|
|
vtkm::Id numberOfValues = input.GetNumberOfValues();
|
|
|
|
|
2017-08-10 20:13:55 +00:00
|
|
|
auto inputPortal = input.PrepareForInput(Device());
|
|
|
|
auto outputPortal = output.PrepareForOutput(numberOfValues, Device());
|
2014-02-11 17:34:56 +00:00
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
if (numberOfValues <= 0)
|
|
|
|
{
|
|
|
|
return initialValue;
|
|
|
|
}
|
2014-02-11 17:34:56 +00:00
|
|
|
|
2015-10-12 19:19:54 +00:00
|
|
|
// Shift right by one, by iterating backwards. We are required to iterate
|
|
|
|
//backwards so that the algorithm works correctly when the input and output
|
|
|
|
//are the same array, otherwise you just propagate the first element
|
|
|
|
//to all elements
|
|
|
|
//Note: We explicitly do not use std::copy_backwards for good reason.
|
|
|
|
//The ICC compiler has been found to improperly optimize the copy_backwards
|
|
|
|
//into a standard copy, causing the above issue.
|
2015-08-27 17:50:32 +00:00
|
|
|
T lastValue = inputPortal.Get(numberOfValues - 1);
|
2017-05-18 14:29:41 +00:00
|
|
|
for (vtkm::Id i = (numberOfValues - 1); i >= 1; --i)
|
2016-03-15 20:15:11 +00:00
|
|
|
{
|
2017-05-18 14:29:41 +00:00
|
|
|
outputPortal.Set(i, inputPortal.Get(i - 1));
|
2016-03-15 20:15:11 +00:00
|
|
|
}
|
2015-08-27 17:50:32 +00:00
|
|
|
outputPortal.Set(0, initialValue);
|
|
|
|
|
|
|
|
std::partial_sum(vtkm::cont::ArrayPortalToIteratorBegin(outputPortal),
|
|
|
|
vtkm::cont::ArrayPortalToIteratorEnd(outputPortal),
|
2017-05-26 17:53:28 +00:00
|
|
|
vtkm::cont::ArrayPortalToIteratorBegin(outputPortal),
|
|
|
|
wrappedBinaryOp);
|
2015-08-27 17:50:32 +00:00
|
|
|
|
|
|
|
return wrappedBinaryOp(outputPortal.Get(numberOfValues - 1), lastValue);
|
|
|
|
}
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
template <typename T, class CIn, class COut>
|
|
|
|
VTKM_CONT static T ScanExclusive(const vtkm::cont::ArrayHandle<T, CIn>& input,
|
|
|
|
vtkm::cont::ArrayHandle<T, COut>& output)
|
2015-08-27 17:50:32 +00:00
|
|
|
{
|
2019-09-03 15:53:39 +00:00
|
|
|
VTKM_LOG_SCOPE_FUNCTION(vtkm::cont::LogLevel::Perf);
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
return ScanExclusive(input, output, vtkm::Sum(), vtkm::TypeTraits<T>::ZeroInitialization());
|
2014-02-11 17:34:56 +00:00
|
|
|
}
|
|
|
|
|
2016-09-12 14:30:03 +00:00
|
|
|
VTKM_CONT_EXPORT static void ScheduleTask(vtkm::exec::serial::internal::TaskTiling1D& functor,
|
|
|
|
vtkm::Id size);
|
|
|
|
VTKM_CONT_EXPORT static void ScheduleTask(vtkm::exec::serial::internal::TaskTiling3D& functor,
|
|
|
|
vtkm::Id3 size);
|
2014-02-11 17:34:56 +00:00
|
|
|
|
2016-09-12 14:30:03 +00:00
|
|
|
template <class FunctorType>
|
|
|
|
VTKM_CONT static inline void Schedule(FunctorType functor, vtkm::Id size)
|
2014-02-11 17:34:56 +00:00
|
|
|
{
|
2019-09-03 15:53:39 +00:00
|
|
|
VTKM_LOG_SCOPE_FUNCTION(vtkm::cont::LogLevel::Perf);
|
|
|
|
|
2016-09-12 14:30:03 +00:00
|
|
|
vtkm::exec::serial::internal::TaskTiling1D kernel(functor);
|
|
|
|
ScheduleTask(kernel, size);
|
2014-02-11 17:34:56 +00:00
|
|
|
}
|
|
|
|
|
2016-09-12 14:30:03 +00:00
|
|
|
template <class FunctorType>
|
|
|
|
VTKM_CONT static inline void Schedule(FunctorType functor, vtkm::Id3 size)
|
2014-02-11 17:34:56 +00:00
|
|
|
{
|
2019-09-03 15:53:39 +00:00
|
|
|
VTKM_LOG_SCOPE_FUNCTION(vtkm::cont::LogLevel::Perf);
|
|
|
|
|
2016-09-12 14:30:03 +00:00
|
|
|
vtkm::exec::serial::internal::TaskTiling3D kernel(functor);
|
|
|
|
ScheduleTask(kernel, size);
|
2014-02-11 17:34:56 +00:00
|
|
|
}
|
|
|
|
|
2015-06-04 19:34:17 +00:00
|
|
|
private:
|
2017-05-26 17:53:28 +00:00
|
|
|
template <typename Vin,
|
|
|
|
typename I,
|
|
|
|
typename Vout,
|
|
|
|
class StorageVin,
|
|
|
|
class StorageI,
|
2017-05-18 14:29:41 +00:00
|
|
|
class StorageVout>
|
|
|
|
VTKM_CONT static void Scatter(vtkm::cont::ArrayHandle<Vin, StorageVin>& values,
|
|
|
|
vtkm::cont::ArrayHandle<I, StorageI>& index,
|
|
|
|
vtkm::cont::ArrayHandle<Vout, StorageVout>& values_out)
|
2015-06-04 17:59:04 +00:00
|
|
|
{
|
2019-09-03 15:53:39 +00:00
|
|
|
VTKM_LOG_SCOPE_FUNCTION(vtkm::cont::LogLevel::Perf);
|
|
|
|
|
2015-06-19 19:30:03 +00:00
|
|
|
const vtkm::Id n = values.GetNumberOfValues();
|
2017-05-18 14:29:41 +00:00
|
|
|
VTKM_ASSERT(n == index.GetNumberOfValues());
|
2015-06-04 17:59:04 +00:00
|
|
|
|
2017-08-10 20:13:55 +00:00
|
|
|
auto valuesPortal = values.PrepareForInput(Device());
|
|
|
|
auto indexPortal = index.PrepareForInput(Device());
|
|
|
|
auto valuesOutPortal = values_out.PrepareForOutput(n, Device());
|
2015-06-04 17:59:04 +00:00
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
for (vtkm::Id i = 0; i < n; i++)
|
2015-06-19 19:30:03 +00:00
|
|
|
{
|
2017-05-18 14:29:41 +00:00
|
|
|
valuesOutPortal.Set(i, valuesPortal.Get(indexPortal.Get(i)));
|
2015-06-19 19:30:03 +00:00
|
|
|
}
|
2015-06-04 17:59:04 +00:00
|
|
|
}
|
|
|
|
|
2015-06-04 19:34:17 +00:00
|
|
|
private:
|
2015-06-08 16:40:44 +00:00
|
|
|
/// Reorder the value array along with the sorting algorithm
|
2017-05-18 14:29:41 +00:00
|
|
|
template <typename T, typename U, class StorageT, class StorageU, class BinaryCompare>
|
|
|
|
VTKM_CONT static void SortByKeyDirect(vtkm::cont::ArrayHandle<T, StorageT>& keys,
|
|
|
|
vtkm::cont::ArrayHandle<U, StorageU>& values,
|
|
|
|
BinaryCompare binary_compare)
|
2015-06-04 17:59:04 +00:00
|
|
|
{
|
2019-09-03 15:53:39 +00:00
|
|
|
VTKM_LOG_SCOPE_FUNCTION(vtkm::cont::LogLevel::Perf);
|
|
|
|
|
2015-06-19 19:30:03 +00:00
|
|
|
//combine the keys and values into a ZipArrayHandle
|
|
|
|
//we than need to specify a custom compare function wrapper
|
|
|
|
//that only checks for key side of the pair, using the custom compare
|
|
|
|
//functor that the user passed in
|
2017-08-10 20:13:55 +00:00
|
|
|
auto zipHandle = vtkm::cont::make_ArrayHandleZip(keys, values);
|
2017-05-18 14:29:41 +00:00
|
|
|
Sort(zipHandle, internal::KeyCompare<T, U, BinaryCompare>(binary_compare));
|
2015-06-04 17:59:04 +00:00
|
|
|
}
|
|
|
|
|
2015-06-04 19:34:17 +00:00
|
|
|
public:
|
2017-05-18 14:29:41 +00:00
|
|
|
template <typename T, typename U, class StorageT, class StorageU>
|
|
|
|
VTKM_CONT static void SortByKey(vtkm::cont::ArrayHandle<T, StorageT>& keys,
|
|
|
|
vtkm::cont::ArrayHandle<U, StorageU>& values)
|
2015-06-04 18:18:59 +00:00
|
|
|
{
|
2019-09-03 15:53:39 +00:00
|
|
|
VTKM_LOG_SCOPE_FUNCTION(vtkm::cont::LogLevel::Perf);
|
|
|
|
|
2015-06-19 19:30:03 +00:00
|
|
|
SortByKey(keys, values, std::less<T>());
|
2015-06-04 18:18:59 +00:00
|
|
|
}
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
template <typename T, typename U, class StorageT, class StorageU, class BinaryCompare>
|
|
|
|
VTKM_CONT static void SortByKey(vtkm::cont::ArrayHandle<T, StorageT>& keys,
|
|
|
|
vtkm::cont::ArrayHandle<U, StorageU>& values,
|
|
|
|
const BinaryCompare& binary_compare)
|
2015-06-04 17:59:04 +00:00
|
|
|
{
|
2019-09-03 15:53:39 +00:00
|
|
|
VTKM_LOG_SCOPE_FUNCTION(vtkm::cont::LogLevel::Perf);
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
internal::WrappedBinaryOperator<bool, BinaryCompare> wrappedCompare(binary_compare);
|
2018-02-22 16:34:08 +00:00
|
|
|
constexpr bool larger_than_64bits = sizeof(U) > sizeof(vtkm::Int64);
|
2017-09-21 14:33:17 +00:00
|
|
|
if (larger_than_64bits)
|
2015-06-19 19:30:03 +00:00
|
|
|
{
|
|
|
|
/// More efficient sort:
|
|
|
|
/// Move value indexes when sorting and reorder the value array at last
|
2017-08-10 20:13:55 +00:00
|
|
|
vtkm::cont::ArrayHandle<vtkm::Id> indexArray;
|
|
|
|
vtkm::cont::ArrayHandle<U, StorageU> valuesScattered;
|
2015-06-19 19:30:03 +00:00
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
Copy(ArrayHandleIndex(keys.GetNumberOfValues()), indexArray);
|
2015-06-19 19:30:03 +00:00
|
|
|
SortByKeyDirect(keys, indexArray, wrappedCompare);
|
|
|
|
Scatter(values, indexArray, valuesScattered);
|
2017-05-18 14:29:41 +00:00
|
|
|
Copy(valuesScattered, values);
|
2015-06-19 19:30:03 +00:00
|
|
|
}
|
|
|
|
else
|
2015-06-19 17:01:49 +00:00
|
|
|
{
|
2015-06-19 19:30:03 +00:00
|
|
|
SortByKeyDirect(keys, values, wrappedCompare);
|
2015-06-19 17:01:49 +00:00
|
|
|
}
|
2015-06-04 17:59:04 +00:00
|
|
|
}
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
template <typename T, class Storage>
|
|
|
|
VTKM_CONT static void Sort(vtkm::cont::ArrayHandle<T, Storage>& values)
|
2014-02-11 17:34:56 +00:00
|
|
|
{
|
2019-09-03 15:53:39 +00:00
|
|
|
VTKM_LOG_SCOPE_FUNCTION(vtkm::cont::LogLevel::Perf);
|
|
|
|
|
2015-06-19 17:01:49 +00:00
|
|
|
Sort(values, std::less<T>());
|
2014-02-11 17:34:56 +00:00
|
|
|
}
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
template <typename T, class Storage, class BinaryCompare>
|
|
|
|
VTKM_CONT static void Sort(vtkm::cont::ArrayHandle<T, Storage>& values,
|
|
|
|
BinaryCompare binary_compare)
|
2014-02-11 17:34:56 +00:00
|
|
|
{
|
2019-09-03 15:53:39 +00:00
|
|
|
VTKM_LOG_SCOPE_FUNCTION(vtkm::cont::LogLevel::Perf);
|
|
|
|
|
2017-08-10 20:13:55 +00:00
|
|
|
auto arrayPortal = values.PrepareForInPlace(Device());
|
|
|
|
vtkm::cont::ArrayPortalToIterators<decltype(arrayPortal)> iterators(arrayPortal);
|
2015-06-10 13:01:55 +00:00
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
internal::WrappedBinaryOperator<bool, BinaryCompare> wrappedCompare(binary_compare);
|
2015-06-10 13:01:55 +00:00
|
|
|
std::sort(iterators.GetBegin(), iterators.GetEnd(), wrappedCompare);
|
2014-02-11 17:34:56 +00:00
|
|
|
}
|
|
|
|
|
2017-09-18 16:16:12 +00:00
|
|
|
template <typename T, class Storage>
|
|
|
|
VTKM_CONT static void Unique(vtkm::cont::ArrayHandle<T, Storage>& values)
|
|
|
|
{
|
2019-09-03 15:53:39 +00:00
|
|
|
VTKM_LOG_SCOPE_FUNCTION(vtkm::cont::LogLevel::Perf);
|
|
|
|
|
2017-09-18 16:16:12 +00:00
|
|
|
Unique(values, std::equal_to<T>());
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, class Storage, class BinaryCompare>
|
|
|
|
VTKM_CONT static void Unique(vtkm::cont::ArrayHandle<T, Storage>& values,
|
|
|
|
BinaryCompare binary_compare)
|
|
|
|
{
|
2019-09-03 15:53:39 +00:00
|
|
|
VTKM_LOG_SCOPE_FUNCTION(vtkm::cont::LogLevel::Perf);
|
|
|
|
|
2017-09-18 16:16:12 +00:00
|
|
|
auto arrayPortal = values.PrepareForInPlace(Device());
|
|
|
|
vtkm::cont::ArrayPortalToIterators<decltype(arrayPortal)> iterators(arrayPortal);
|
|
|
|
internal::WrappedBinaryOperator<bool, BinaryCompare> wrappedCompare(binary_compare);
|
|
|
|
|
|
|
|
auto end = std::unique(iterators.GetBegin(), iterators.GetEnd(), wrappedCompare);
|
|
|
|
values.Shrink(static_cast<vtkm::Id>(end - iterators.GetBegin()));
|
|
|
|
}
|
|
|
|
|
2016-10-19 22:42:58 +00:00
|
|
|
VTKM_CONT static void Synchronize()
|
2014-02-11 17:34:56 +00:00
|
|
|
{
|
|
|
|
// Nothing to do. This device is serial and has no asynchronous operations.
|
|
|
|
}
|
|
|
|
};
|
2016-09-12 14:30:03 +00:00
|
|
|
|
|
|
|
template <>
|
|
|
|
class DeviceTaskTypes<vtkm::cont::DeviceAdapterTagSerial>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
template <typename WorkletType, typename InvocationType>
|
2018-01-18 18:51:30 +00:00
|
|
|
static vtkm::exec::serial::internal::TaskTiling1D MakeTask(WorkletType& worklet,
|
|
|
|
InvocationType& invocation,
|
2016-09-12 14:30:03 +00:00
|
|
|
vtkm::Id,
|
|
|
|
vtkm::Id globalIndexOffset = 0)
|
|
|
|
{
|
|
|
|
return vtkm::exec::serial::internal::TaskTiling1D(worklet, invocation, globalIndexOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename WorkletType, typename InvocationType>
|
2018-01-18 18:51:30 +00:00
|
|
|
static vtkm::exec::serial::internal::TaskTiling3D MakeTask(WorkletType& worklet,
|
|
|
|
InvocationType& invocation,
|
2016-09-12 14:30:03 +00:00
|
|
|
vtkm::Id3,
|
|
|
|
vtkm::Id globalIndexOffset = 0)
|
|
|
|
{
|
|
|
|
return vtkm::exec::serial::internal::TaskTiling3D(worklet, invocation, globalIndexOffset);
|
|
|
|
}
|
|
|
|
};
|
2014-02-11 17:34:56 +00:00
|
|
|
}
|
|
|
|
} // namespace vtkm::cont
|
|
|
|
|
2016-09-28 15:38:31 +00:00
|
|
|
#endif //vtk_m_cont_serial_internal_DeviceAdapterAlgorithmSerial_h
|