vtk-m2/vtkm/worklet/testing/UnitTestStreamingSine.cxx

130 lines
5.1 KiB
C++
Raw Normal View History

2016-08-03 21:34:43 +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 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
2016-08-03 21:34:43 +00:00
// Copyright 2014 UT-Battelle, LLC.
// Copyright 2014 Los Alamos National Security.
//
// Under the terms of Contract DE-NA0003525 with NTESS,
2016-08-03 21:34:43 +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/ArrayHandleStreaming.h>
2016-09-15 23:46:09 +00:00
#include <vtkm/cont/DeviceAdapterAlgorithm.h>
2016-08-03 21:34:43 +00:00
#include <vtkm/cont/testing/Testing.h>
2017-05-18 14:51:24 +00:00
#include <vtkm/worklet/DispatcherMapField.h>
#include <vtkm/worklet/DispatcherStreamingMapField.h>
2016-08-03 21:34:43 +00:00
#include <vector>
namespace vtkm
{
namespace worklet
{
class SineWorklet : public vtkm::worklet::WorkletMapField
{
public:
using ControlSignature = void(FieldIn<>, FieldOut<>);
using ExecutionSignature = _2(_1, WorkIndex);
2016-08-03 21:34:43 +00:00
2017-05-18 14:29:41 +00:00
template <typename T>
VTKM_EXEC T operator()(T x, vtkm::Id& index) const
{
return (static_cast<T>(index) + vtkm::Sin(x));
2016-08-03 21:34:43 +00:00
}
};
}
}
// Utility method to print input, output, and reference arrays
template <class T1, class T2, class T3>
2016-09-29 20:33:21 +00:00
void compareArrays(T1& a1, T2& a2, T3& a3, char const* text)
{
for (vtkm::Id i = 0; i < a1.GetNumberOfValues(); ++i)
{
2017-05-18 14:29:41 +00:00
std::cout << a1.GetPortalConstControl().Get(i) << " " << a2.GetPortalConstControl().Get(i)
<< " " << a3.GetPortalConstControl().Get(i) << std::endl;
VTKM_TEST_ASSERT(
2017-05-18 14:29:41 +00:00
test_equal(a2.GetPortalConstControl().Get(i), a3.GetPortalConstControl().Get(i), 0.01f),
text);
}
}
2016-08-03 21:34:43 +00:00
void TestStreamingSine()
{
2016-09-15 23:46:09 +00:00
// Test the streaming worklet
std::cout << "Testing streaming worklet:" << std::endl;
2016-08-03 21:34:43 +00:00
2017-05-18 14:29:41 +00:00
const vtkm::Id N = 25;
const vtkm::Id NBlocks = 4;
vtkm::cont::ArrayHandle<vtkm::Float32> input, output, reference, summation;
std::vector<vtkm::Float32> data(N), test(N);
2016-09-15 23:46:09 +00:00
vtkm::Float32 testSum = 0.0f;
2017-05-18 14:29:41 +00:00
for (vtkm::UInt32 i = 0; i < N; i++)
2016-08-03 21:34:43 +00:00
{
data[i] = static_cast<vtkm::Float32>(i);
test[i] = static_cast<vtkm::Float32>(i) + static_cast<vtkm::Float32>(vtkm::Sin(data[i]));
2016-09-15 23:46:09 +00:00
testSum += test[i];
2016-08-03 21:34:43 +00:00
}
input = vtkm::cont::make_ArrayHandle(data);
2018-02-22 13:29:13 +00:00
using DeviceAlgorithms = vtkm::cont::DeviceAdapterAlgorithm<VTKM_DEFAULT_DEVICE_ADAPTER_TAG>;
2016-08-03 21:34:43 +00:00
vtkm::worklet::SineWorklet sineWorklet;
2017-05-18 14:29:41 +00:00
vtkm::worklet::DispatcherStreamingMapField<vtkm::worklet::SineWorklet> dispatcher(sineWorklet);
2016-08-03 21:34:43 +00:00
dispatcher.SetNumberOfBlocks(NBlocks);
dispatcher.Invoke(input, output);
reference = vtkm::cont::make_ArrayHandle(test);
compareArrays(input, output, reference, "Wrong result for streaming sine worklet");
2016-08-03 21:34:43 +00:00
vtkm::Float32 referenceSum, streamSum;
// Test the streaming exclusive scan
std::cout << "Testing streaming exclusive scan: " << std::endl;
referenceSum = DeviceAlgorithms::ScanExclusive(input, summation);
streamSum = DeviceAlgorithms::StreamingScanExclusive(4, input, output);
2017-05-18 14:29:41 +00:00
VTKM_TEST_ASSERT(test_equal(streamSum, referenceSum, 0.01f),
"Wrong sum for streaming exclusive scan");
compareArrays(input, output, summation, "Wrong result for streaming exclusive scan");
// Test the streaming exclusive scan with binary operator
std::cout << "Testing streaming exnclusive scan with binary operator: " << std::endl;
2017-05-18 14:29:41 +00:00
vtkm::Float32 initValue = 0.0;
referenceSum = DeviceAlgorithms::ScanExclusive(input, summation, vtkm::Maximum(), initValue);
2017-05-18 14:29:41 +00:00
streamSum =
DeviceAlgorithms::StreamingScanExclusive(4, input, output, vtkm::Maximum(), initValue);
VTKM_TEST_ASSERT(test_equal(streamSum, referenceSum, 0.01f),
"Wrong sum for streaming exclusive scan with binary operator");
compareArrays(
input, output, summation, "Wrong result for streaming exclusive scan with binary operator");
2016-09-29 21:06:34 +00:00
// Test the streaming reduce
std::cout << "Testing streaming reduce: " << std::endl;
referenceSum = DeviceAlgorithms::Reduce(input, 0.0f);
streamSum = DeviceAlgorithms::StreamingReduce(4, input, 0.0f);
std::cout << "Result: " << streamSum << " " << referenceSum << std::endl;
VTKM_TEST_ASSERT(test_equal(streamSum, referenceSum, 0.01f), "Wrong sum for streaming reduce");
// Test the streaming reduce with binary operator
std::cout << "Testing streaming reduce with binary operator: " << std::endl;
referenceSum = DeviceAlgorithms::Reduce(input, 0.0f, vtkm::Maximum());
streamSum = DeviceAlgorithms::StreamingReduce(4, input, 0.0f, vtkm::Maximum());
std::cout << "Result: " << streamSum << " " << referenceSum << std::endl;
2017-05-18 14:29:41 +00:00
VTKM_TEST_ASSERT(test_equal(streamSum, referenceSum, 0.01f),
"Wrong sum for streaming reduce with binary operator");
2016-08-03 21:34:43 +00:00
}
2017-05-18 14:29:41 +00:00
int UnitTestStreamingSine(int, char* [])
2016-08-03 21:34:43 +00:00
{
return vtkm::cont::testing::Testing::Run(TestStreamingSine);
}