2017-02-07 17:53:53 +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.
|
|
|
|
//
|
2017-09-20 21:33:44 +00:00
|
|
|
// Copyright 2017 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
|
2017-02-07 17:53:53 +00:00
|
|
|
// Copyright 2017 UT-Battelle, LLC.
|
|
|
|
// Copyright 2017 Los Alamos National Security.
|
|
|
|
//
|
2017-09-20 21:33:44 +00:00
|
|
|
// Under the terms of Contract DE-NA0003525 with NTESS,
|
2017-02-07 17:53:53 +00:00
|
|
|
// 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.
|
|
|
|
//============================================================================
|
|
|
|
|
|
|
|
#include <vtkm/cont/ArrayHandle.h>
|
|
|
|
#include <vtkm/cont/ArrayHandleDiscard.h>
|
|
|
|
#include <vtkm/cont/DeviceAdapterAlgorithm.h>
|
|
|
|
|
|
|
|
#include <vtkm/cont/serial/internal/ArrayManagerExecutionSerial.h>
|
|
|
|
#include <vtkm/cont/serial/internal/DeviceAdapterAlgorithmSerial.h>
|
|
|
|
#include <vtkm/cont/serial/internal/DeviceAdapterTagSerial.h>
|
|
|
|
|
|
|
|
#include <vtkm/cont/testing/Testing.h>
|
|
|
|
|
|
|
|
#include <vtkm/BinaryOperators.h>
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
namespace UnitTestArrayHandleDiscardDetail
|
|
|
|
{
|
|
|
|
|
|
|
|
template <typename ValueType>
|
|
|
|
struct Test
|
|
|
|
{
|
2018-02-27 14:25:25 +00:00
|
|
|
static constexpr vtkm::Id ARRAY_SIZE = 100;
|
|
|
|
static constexpr vtkm::Id NUM_KEYS = 3;
|
2017-02-07 17:53:53 +00:00
|
|
|
|
|
|
|
using DeviceTag = vtkm::cont::DeviceAdapterTagSerial;
|
|
|
|
using Algorithm = vtkm::cont::DeviceAdapterAlgorithm<DeviceTag>;
|
|
|
|
using Handle = vtkm::cont::ArrayHandle<ValueType>;
|
|
|
|
using DiscardHandle = vtkm::cont::ArrayHandleDiscard<ValueType>;
|
|
|
|
using OutputPortal = typename Handle::PortalControl;
|
|
|
|
using ReduceOp = vtkm::Add;
|
|
|
|
|
|
|
|
// Test discard arrays by using the ReduceByKey algorithm. Two regular arrays
|
|
|
|
// handles are provided as inputs, but the keys_output array is a discard
|
|
|
|
// array handle. The values_output array should still be populated correctly.
|
|
|
|
void TestReduceByKey()
|
|
|
|
{
|
|
|
|
// The reduction operator:
|
|
|
|
ReduceOp op;
|
|
|
|
|
|
|
|
// Prepare inputs / reference data:
|
|
|
|
ValueType keyData[ARRAY_SIZE];
|
|
|
|
ValueType valueData[ARRAY_SIZE];
|
|
|
|
ValueType refData[NUM_KEYS];
|
|
|
|
std::fill(refData, refData + NUM_KEYS, ValueType(0));
|
|
|
|
for (vtkm::Id i = 0; i < ARRAY_SIZE; ++i)
|
|
|
|
{
|
|
|
|
const vtkm::Id key = i % NUM_KEYS;
|
|
|
|
keyData[i] = static_cast<ValueType>(key);
|
|
|
|
valueData[i] = static_cast<ValueType>(i * 2);
|
|
|
|
refData[key] = op(refData[key], valueData[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare array handles:
|
|
|
|
Handle keys = vtkm::cont::make_ArrayHandle(keyData, ARRAY_SIZE);
|
|
|
|
Handle values = vtkm::cont::make_ArrayHandle(valueData, ARRAY_SIZE);
|
|
|
|
DiscardHandle output_keys;
|
|
|
|
Handle output_values;
|
|
|
|
|
|
|
|
Algorithm::SortByKey(keys, values);
|
|
|
|
Algorithm::ReduceByKey(keys, values, output_keys, output_values, op);
|
|
|
|
|
|
|
|
OutputPortal outputs = output_values.GetPortalControl();
|
|
|
|
|
|
|
|
VTKM_TEST_ASSERT(outputs.GetNumberOfValues() == NUM_KEYS,
|
|
|
|
"Unexpected number of output values from ReduceByKey.");
|
|
|
|
|
|
|
|
for (vtkm::Id i = 0; i < NUM_KEYS; ++i)
|
|
|
|
{
|
2017-05-18 14:29:41 +00:00
|
|
|
VTKM_TEST_ASSERT(outputs.Get(i) == refData[i], "Unexpected output value after ReduceByKey.");
|
2017-02-07 17:53:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TestPrepareExceptions()
|
|
|
|
{
|
|
|
|
DiscardHandle handle;
|
|
|
|
handle.Allocate(50);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
handle.PrepareForInput(DeviceTag());
|
|
|
|
}
|
2017-02-23 15:13:30 +00:00
|
|
|
catch (vtkm::cont::ErrorBadValue&)
|
2017-02-07 17:53:53 +00:00
|
|
|
{
|
|
|
|
// Expected failure.
|
|
|
|
}
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
handle.PrepareForInPlace(DeviceTag());
|
|
|
|
}
|
2017-02-23 15:13:30 +00:00
|
|
|
catch (vtkm::cont::ErrorBadValue&)
|
2017-02-07 17:53:53 +00:00
|
|
|
{
|
|
|
|
// Expected failure.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shouldn't fail:
|
|
|
|
handle.PrepareForOutput(ARRAY_SIZE, DeviceTag());
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator()()
|
|
|
|
{
|
|
|
|
TestReduceByKey();
|
|
|
|
TestPrepareExceptions();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void TestArrayHandleDiscard()
|
|
|
|
{
|
|
|
|
Test<vtkm::UInt8>()();
|
|
|
|
Test<vtkm::Int16>()();
|
|
|
|
Test<vtkm::Int32>()();
|
|
|
|
Test<vtkm::Int64>()();
|
|
|
|
Test<vtkm::Float32>()();
|
|
|
|
Test<vtkm::Float64>()();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // end namespace UnitTestArrayHandleDiscardDetail
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
int UnitTestArrayHandleDiscard(int, char* [])
|
2017-02-07 17:53:53 +00:00
|
|
|
{
|
|
|
|
using namespace UnitTestArrayHandleDiscardDetail;
|
|
|
|
return vtkm::cont::testing::Testing::Run(TestArrayHandleDiscard);
|
|
|
|
}
|