Commit 47f98741 authored by Robert Maynard's avatar Robert Maynard
Browse files

Extend the array handle testing to be done on a per device basis.

Now we test ArrayHandle with all the vtkm types to make sure all the
transfer logic / access logic works for all T types.
parent 74033a5e
......@@ -18,84 +18,17 @@
// this software.
//============================================================================
#define VTKM_DEVICE_ADAPTER VTKM_DEVICE_ADAPTER_CUDA
#define VTKM_DEVICE_ADAPTER VTKM_DEVICE_ADAPTER_ERROR
#define BOOST_SP_DISABLE_THREADS
#include <vtkm/cont/cuda/DeviceAdapterCuda.h>
#include <vtkm/cont/ArrayHandle.h>
#include <vtkm/worklet/DispatcherMapField.h>
#include <vtkm/worklet/WorkletMapField.h>
#include <vtkm/cont/testing/TestingArrayHandles.h>
#include <vtkm/cont/cuda/internal/testing/Testing.h>
namespace ut_handle_explicit{
const vtkm::Id ARRAY_SIZE = 300;
struct PassThrough : public vtkm::worklet::WorkletMapField
{
typedef void ControlSignature(FieldIn<>, FieldOut<>);
typedef _2 ExecutionSignature(_1);
template<class ValueType>
VTKM_EXEC_EXPORT
ValueType operator()(const ValueType &inValue) const
{ return inValue; }
};
template< typename ValueType >
struct CountingTest
{
void operator()(const ValueType v) const
{
std::vector< ValueType > inputVector(ARRAY_SIZE);
for(vtkm::Id i=0; i < ARRAY_SIZE; ++i)
{
inputVector[static_cast<vtkm::UInt32>(i)] = v + i;
}
vtkm::cont::ArrayHandle< ValueType > input =
vtkm::cont::make_ArrayHandle( inputVector );
vtkm::cont::ArrayHandle< ValueType > result;
vtkm::worklet::DispatcherMapField< ut_handle_explicit::PassThrough > dispatcher;
dispatcher.Invoke(input, result);
//verify that the control portal works
for(vtkm::Id i=static_cast<vtkm::Id>(v); i < ARRAY_SIZE; ++i)
{
const ValueType result_v = result.GetPortalConstControl().Get(i);
const ValueType correct_value = v + ValueType(i);
VTKM_TEST_ASSERT(result_v == correct_value, "Counting Handle Failed");
}
}
};
template <typename T>
void RunCountingTest(const T t)
{
CountingTest<T> tests;
tests(t);
}
void TestArrayHandle()
{
RunCountingTest( vtkm::Id(42) );
RunCountingTest( vtkm::Float32(3) );
// RunCountingTest( vtkm::Vec< vtkm::Float32, 3>() );
}
} // ut_handle_explicit namespace
int UnitTestCudaArrayHandle(int, char *[])
{
return vtkm::cont::cuda::internal::Testing::Run(
ut_handle_explicit::TestArrayHandle);
int result = vtkm::cont::testing::TestingArrayHandles
<vtkm::cont::DeviceAdapterTagCuda>::Run();
return vtkm::cont::cuda::internal::Testing::CheckCudaBeforeExit(result);
}
......@@ -20,6 +20,7 @@
set(unit_tests
UnitTestDeviceAdapterTBB.cxx
UnitTestTBBArrayHandle.cxx
UnitTestTBBArrayHandleFancy.cxx
)
vtkm_unit_tests(TBB SOURCES ${unit_tests})
//============================================================================
// 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 Sandia Corporation.
// Copyright 2014 UT-Battelle, LLC.
// Copyright 2014 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.
//============================================================================
#define VTKM_DEVICE_ADAPTER VTKM_DEVICE_ADAPTER_ERROR
#include <vtkm/cont/tbb/DeviceAdapterTBB.h>
#include <vtkm/cont/testing/TestingArrayHandles.h>
int UnitTestTBBArrayHandle(int, char *[])
{
return vtkm::cont::testing::TestingArrayHandles
<vtkm::cont::DeviceAdapterTagTBB>::Run();
}
......@@ -21,6 +21,7 @@
set(headers
MakeTestDataSet.h
Testing.h
TestingArrayHandles.h
TestingDeviceAdapter.h
TestingFancyArrayHandles.h
)
......
//============================================================================
// 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 Sandia Corporation.
// Copyright 2014 UT-Battelle, LLC.
// Copyright 2014 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_cont_testing_TestingArrayHandles_h
#define vtk_m_cont_testing_TestingArrayHandles_h
#include <vtkm/cont/ArrayHandle.h>
#include <vtkm/TypeTraits.h>
#include <vtkm/worklet/DispatcherMapField.h>
#include <vtkm/worklet/WorkletMapField.h>
#include <vtkm/cont/testing/Testing.h>
#include <algorithm>
namespace vtkm {
namespace cont {
namespace testing {
namespace array_handle_testing
{
template<typename T>
void CheckArray(const vtkm::cont::ArrayHandle<T> &handle)
{
CheckPortal(handle.GetPortalConstControl());
}
}
/// This class has a single static member, Run, that tests that all Fancy Array
/// Handles work with the given DeviceAdapter
///
template<class DeviceAdapterTag>
struct TestingArrayHandles
{
struct PassThrough : public vtkm::worklet::WorkletMapField
{
typedef void ControlSignature(FieldIn<>, FieldOut<>);
typedef _2 ExecutionSignature(_1);
template<class ValueType>
VTKM_EXEC_EXPORT
ValueType operator()(const ValueType &inValue) const
{ return inValue; }
};
template<typename T, typename ExecutionPortalType>
struct AssignTestValue
{
ExecutionPortalType Portal;
AssignTestValue(ExecutionPortalType p): Portal(p) {}
VTKM_EXEC_EXPORT
void operator()(vtkm::Id index,
const vtkm::exec::internal::ErrorMessageBuffer &) const
{
this->Portal.Set(index, TestValue(index, T()) );
}
VTKM_CONT_EXPORT void SetErrorMessageBuffer(
const vtkm::exec::internal::ErrorMessageBuffer &) { }
};
template<typename T, typename ExecutionPortalType>
struct InplaceFunctor
{
ExecutionPortalType Portal;
InplaceFunctor(ExecutionPortalType p): Portal(p) {}
VTKM_EXEC_EXPORT
void operator()(vtkm::Id index,
const vtkm::exec::internal::ErrorMessageBuffer &) const
{
this->Portal.Set(index, this->Portal.Get(index)+ T(1));
}
VTKM_CONT_EXPORT void SetErrorMessageBuffer(
const vtkm::exec::internal::ErrorMessageBuffer &) { }
};
private:
static const vtkm::Id ARRAY_SIZE = 100000;
typedef vtkm::cont::DeviceAdapterAlgorithm<DeviceAdapterTag> Algorithm;
typedef vtkm::worklet::DispatcherMapField<PassThrough,
DeviceAdapterTag> DispatcherPassThrough;
struct VerifyEmptyArrays
{
template<typename T>
VTKM_CONT_EXPORT void operator()(T) const
{
std::cout << "Try operations on empty arrays." << std::endl;
// After each operation, reinitialize array in case something gets
// allocated.
vtkm::cont::ArrayHandle<T> arrayHandle = vtkm::cont::ArrayHandle<T>();
VTKM_TEST_ASSERT(arrayHandle.GetNumberOfValues() == 0,
"Uninitialized array does not report zero values.");
arrayHandle = vtkm::cont::ArrayHandle<T>();
VTKM_TEST_ASSERT(
arrayHandle.GetPortalConstControl().GetNumberOfValues() == 0,
"Uninitialized array does not give portal with zero values.");
arrayHandle = vtkm::cont::ArrayHandle<T>();
arrayHandle.Shrink(0);
arrayHandle = vtkm::cont::ArrayHandle<T>();
arrayHandle.ReleaseResourcesExecution();
arrayHandle = vtkm::cont::ArrayHandle<T>();
arrayHandle.ReleaseResources();
arrayHandle = vtkm::cont::ArrayHandle<T>();
arrayHandle.PrepareForOutput(ARRAY_SIZE, DeviceAdapterTag());
}
};
struct VerifyUserAllocatedHandle
{
template<typename T>
VTKM_CONT_EXPORT void operator()(T) const
{
T array[ARRAY_SIZE];
for (vtkm::Id index = 0; index < ARRAY_SIZE; index++)
{
array[index] = TestValue(index, T());
}
vtkm::cont::internal::Storage<T,vtkm::cont::StorageTagBasic>
arrayStorage(array, ARRAY_SIZE);
vtkm::cont::ArrayHandle<T> arrayHandle(arrayStorage);
VTKM_TEST_ASSERT(arrayHandle.GetNumberOfValues() == ARRAY_SIZE,
"ArrayHandle has wrong number of entries.");
std::cout << "Check array with user provided memory." << std::endl;
array_handle_testing::CheckArray(arrayHandle);
std::cout << "Check out execution array behavior." << std::endl;
{ //as input
typename vtkm::cont::ArrayHandle<T>::template
ExecutionTypes<DeviceAdapterTag>::PortalConst
executionPortal;
executionPortal =
arrayHandle.PrepareForInput(DeviceAdapterTag());
//use a worklet to verify the input transfer worked properly
vtkm::cont::ArrayHandle<T> result;
DispatcherPassThrough().Invoke(arrayHandle, result);
array_handle_testing::CheckArray(result);
}
std::cout << "Check out inplace." << std::endl;
{ //as inplace
typename vtkm::cont::ArrayHandle<T>::template
ExecutionTypes<DeviceAdapterTag>::Portal
executionPortal;
executionPortal =
arrayHandle.PrepareForInPlace(DeviceAdapterTag());
//use a worklet to verify the inplace transfer worked properly
vtkm::cont::ArrayHandle<T> result;
DispatcherPassThrough().Invoke(arrayHandle, result);
array_handle_testing::CheckArray(result);
}
std::cout << "Check out output." << std::endl;
{ //as output with same length as user provided. This should work
//as no new memory needs to be allocated
typename vtkm::cont::ArrayHandle<T>::template
ExecutionTypes<DeviceAdapterTag>::Portal
executionPortal;
executionPortal =
arrayHandle.PrepareForOutput(ARRAY_SIZE,
DeviceAdapterTag());
//we can't verify output contents as those aren't fetched, we
//can just make sure the allocation didn't throw an exception
}
{ //as output with a length larger than the memory provided by the user
//this should fail
typedef typename vtkm::cont::ArrayHandle<T>::template
ExecutionTypes<DeviceAdapterTag>::Portal
ExecutionPortalType;
bool gotException = false;
try
{
//you should not be able to allocate a size larger than the
//user provided and get the results
arrayHandle.PrepareForOutput(ARRAY_SIZE*2,DeviceAdapterTag());
arrayHandle.GetPortalControl();
}
catch (vtkm::cont::Error &error)
{
gotException = true;
}
VTKM_TEST_ASSERT(gotException,
"PrepareForOutput should fail when asked to "\
"re-allocate user provided memory.");
}
}
};
struct VerifyVTKMAllocatedHandle
{
template<typename T>
VTKM_CONT_EXPORT void operator()(T) const
{
vtkm::cont::ArrayHandle<T> arrayHandle;
VTKM_TEST_ASSERT(arrayHandle.GetNumberOfValues() == 0,
"ArrayHandle has wrong number of entries.");
{
typedef typename vtkm::cont::ArrayHandle<T>::template
ExecutionTypes<DeviceAdapterTag>::Portal
ExecutionPortalType;
ExecutionPortalType executionPortal =
arrayHandle.PrepareForOutput(ARRAY_SIZE*2,
DeviceAdapterTag());
//we drop down to manually scheduling so that we don't need
//need to bring in array handle counting
AssignTestValue<T, ExecutionPortalType> functor(executionPortal);
Algorithm::Schedule(functor, ARRAY_SIZE*2);
}
VTKM_TEST_ASSERT(arrayHandle.GetNumberOfValues() == ARRAY_SIZE*2,
"Array not allocated correctly.");
CheckArray(arrayHandle);
std::cout << "Try shrinking the array." << std::endl;
arrayHandle.Shrink(ARRAY_SIZE);
VTKM_TEST_ASSERT(arrayHandle.GetNumberOfValues() == ARRAY_SIZE,
"Array size did not shrink correctly.");
CheckArray(arrayHandle);
std::cout << "Try reallocating array." << std::endl;
arrayHandle.Allocate(ARRAY_SIZE*2);
VTKM_TEST_ASSERT(arrayHandle.GetNumberOfValues() == ARRAY_SIZE*2,
"Array size did not allocate correctly.");
// No point in checking values. This method can invalidate them.
std::cout << "Try in place operation." << std::endl;
{
typedef typename vtkm::cont::ArrayHandle<T>::template
ExecutionTypes<DeviceAdapterTag>::Portal
ExecutionPortalType;
ExecutionPortalType executionPortal =
arrayHandle.PrepareForInPlace(DeviceAdapterTag());
//in place can't be done through the dispatcher
//instead we have to drop down to manually scheduling
InplaceFunctor<T, ExecutionPortalType> functor(executionPortal);
Algorithm::Schedule(functor,ARRAY_SIZE*2);
}
typename vtkm::cont::ArrayHandle<T>::PortalConstControl controlPortal =
arrayHandle.GetPortalConstControl();
for (vtkm::Id index = 0; index < ARRAY_SIZE; index++)
{
VTKM_TEST_ASSERT(test_equal(controlPortal.Get(index),
TestValue(index, T()) + T(1)),
"Did not get result from in place operation.");
}
}
};
struct TryArrayHandleType
{
void operator()() const
{
vtkm::testing::Testing::TryAllTypes(VerifyEmptyArrays());
vtkm::testing::Testing::TryAllTypes(VerifyUserAllocatedHandle());
// TestingArrayHandles<DeviceAdapterTag>::VerifyVTKMAllocatedHandle<T>()();
}
};
public:
static VTKM_CONT_EXPORT int Run()
{
return vtkm::cont::testing::Testing::Run(TryArrayHandleType());
}
};
}
}
} // namespace vtkm::cont::testing
#endif //vtk_m_cont_testing_TestingArrayHandles_h
......@@ -18,210 +18,12 @@
// this software.
//============================================================================
//This sets up the ArrayHandle semantics to allocate pointers and share memory
//between control and execution.
#define VTKM_STORAGE VTKM_STORAGE_BASIC
#define VTKM_DEVICE_ADAPTER VTKM_DEVICE_ADAPTER_SERIAL
#include <vtkm/cont/ArrayHandle.h>
#include <vtkm/TypeTraits.h>
#include <vtkm/cont/testing/Testing.h>
#include <algorithm>
namespace {
const vtkm::Id ARRAY_SIZE = 10;
template<typename T>
void CheckArray(const vtkm::cont::ArrayHandle<T> &handle)
{
CheckPortal(handle.GetPortalConstControl());
}
struct TryArrayHandleType
{
template<typename T>
void operator()(T) const
{
VerifyEmptyArrays(T());
VerifyUserAllocatedHandle(T());
VerifyVTKMAllocatedHandle(T());
}
template<typename T>
void VerifyEmptyArrays(T) const
{
std::cout << "Try operations on empty arrays." << std::endl;
// After each operation, reinitialize array in case something gets
// allocated.
vtkm::cont::ArrayHandle<T> arrayHandle = vtkm::cont::ArrayHandle<T>();
VTKM_TEST_ASSERT(arrayHandle.GetNumberOfValues() == 0,
"Uninitialized array does not report zero values.");
arrayHandle = vtkm::cont::ArrayHandle<T>();
VTKM_TEST_ASSERT(
arrayHandle.GetPortalConstControl().GetNumberOfValues() == 0,
"Uninitialized array does not give portal with zero values.");
arrayHandle = vtkm::cont::ArrayHandle<T>();
arrayHandle.Shrink(0);
arrayHandle = vtkm::cont::ArrayHandle<T>();
arrayHandle.ReleaseResourcesExecution();
arrayHandle = vtkm::cont::ArrayHandle<T>();
arrayHandle.ReleaseResources();
arrayHandle = vtkm::cont::ArrayHandle<T>();
arrayHandle.PrepareForOutput(ARRAY_SIZE, VTKM_DEFAULT_DEVICE_ADAPTER_TAG());
}
template<typename T>
void VerifyUserAllocatedHandle(T) const
{
T array[ARRAY_SIZE];
for (vtkm::Id index = 0; index < ARRAY_SIZE; index++)
{
array[index] = TestValue(index, T());
}
vtkm::cont::internal::Storage<T,vtkm::cont::StorageTagBasic>
arrayStorage(array, ARRAY_SIZE);
vtkm::cont::ArrayHandle<T> arrayHandle(arrayStorage);
VTKM_TEST_ASSERT(arrayHandle.GetNumberOfValues() == ARRAY_SIZE,
"ArrayHandle has wrong number of entries.");
std::cout << "Check array with user provided memory." << std::endl;
CheckArray(arrayHandle);
std::cout << "Check out execution array behavior." << std::endl;
{ //as input
typename vtkm::cont::ArrayHandle<T>::template
ExecutionTypes<VTKM_DEFAULT_DEVICE_ADAPTER_TAG>::PortalConst
executionPortal;
executionPortal =
arrayHandle.PrepareForInput(VTKM_DEFAULT_DEVICE_ADAPTER_TAG());
CheckPortal(executionPortal);
}
{ //as inplace
typename vtkm::cont::ArrayHandle<T>::template
ExecutionTypes<VTKM_DEFAULT_DEVICE_ADAPTER_TAG>::Portal
executionPortal;
executionPortal =
arrayHandle.PrepareForInPlace(VTKM_DEFAULT_DEVICE_ADAPTER_TAG());
CheckPortal(executionPortal);
}
{ //as output with same length as user provided. This should work
//as no new memory needs to be allocated
typename vtkm::cont::ArrayHandle<T>::template
ExecutionTypes<VTKM_DEFAULT_DEVICE_ADAPTER_TAG>::Portal
executionPortal;
executionPortal =
arrayHandle.PrepareForOutput(ARRAY_SIZE,
VTKM_DEFAULT_DEVICE_ADAPTER_TAG());
CheckPortal(executionPortal);
}
{ //as output with a length larger than the memory provided by the user
//this should fail
typedef typename vtkm::cont::ArrayHandle<T>::template
ExecutionTypes<VTKM_DEFAULT_DEVICE_ADAPTER_TAG>::Portal
ExecutionPortalType;
bool gotException = false;
try
{
//you should not be able to allocate
ExecutionPortalType executionPortal =
arrayHandle.PrepareForOutput(ARRAY_SIZE*2,
VTKM_DEFAULT_DEVICE_ADAPTER_TAG());
CheckPortal(executionPortal);
}
catch (vtkm::cont::Error &error)
{
gotException = true;
}
VTKM_TEST_ASSERT(gotException,
"PrepareForOutput should fail when asked to "\
"re-allocate user provided memory.");
}
}
template<typename T>
void VerifyVTKMAllocatedHandle(T) const
{
vtkm::cont::ArrayHandle<T> arrayHandle;
VTKM_TEST_ASSERT(arrayHandle.GetNumberOfValues() == 0,
"ArrayHandle has wrong number of entries.");
{
typedef typename vtkm::cont::ArrayHandle<T>::template
ExecutionTypes<VTKM_DEFAULT_DEVICE_ADAPTER_TAG>::Portal
ExecutionPortalType;
ExecutionPortalType executionPortal =
arrayHandle.PrepareForOutput(ARRAY_SIZE*2,
VTKM_DEFAULT_DEVICE_ADAPTER_TAG());
for (vtkm::Id index = 0;
index < executionPortal.GetNumberOfValues();
index++)
{
executionPortal.Set(index, TestValue(index, T()));
}
}
VTKM_TEST_ASSERT(arrayHandle.GetNumberOfValues() == ARRAY_SIZE*2,