2014-02-10 19:53:03 +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 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.
|
|
|
|
//============================================================================
|
|
|
|
|
|
|
|
//This sets up the ArrayHandle semantics to allocate pointers and share memory
|
|
|
|
//between control and execution.
|
2014-06-23 23:33:04 +00:00
|
|
|
#define VTKM_STORAGE VTKM_STORAGE_BASIC
|
2014-02-10 19:53:03 +00:00
|
|
|
#define VTKM_DEVICE_ADAPTER VTKM_DEVICE_ADAPTER_SERIAL
|
|
|
|
|
|
|
|
#include <vtkm/cont/ArrayHandle.h>
|
|
|
|
|
2014-10-09 14:54:56 +00:00
|
|
|
#include <vtkm/TypeTraits.h>
|
|
|
|
|
2014-02-10 19:53:03 +00:00
|
|
|
#include <vtkm/cont/testing/Testing.h>
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
2014-02-11 21:20:30 +00:00
|
|
|
namespace {
|
2014-10-09 14:54:56 +00:00
|
|
|
|
2014-10-14 16:00:34 +00:00
|
|
|
const vtkm::Id ARRAY_SIZE = 10;
|
2014-09-08 20:59:11 +00:00
|
|
|
|
2014-02-10 19:53:03 +00:00
|
|
|
template<typename T>
|
2014-10-14 16:00:34 +00:00
|
|
|
void CheckArray(const vtkm::cont::ArrayHandle<T> &handle)
|
2014-02-10 19:53:03 +00:00
|
|
|
{
|
2014-10-14 16:00:34 +00:00
|
|
|
CheckPortal(handle.GetPortalConstControl());
|
2014-02-10 19:53:03 +00:00
|
|
|
}
|
|
|
|
|
2014-10-09 14:54:56 +00:00
|
|
|
struct TryArrayHandleType
|
2014-02-10 19:53:03 +00:00
|
|
|
{
|
2014-10-09 14:54:56 +00:00
|
|
|
template<typename T>
|
|
|
|
void operator()(T) const
|
2014-02-11 21:20:30 +00:00
|
|
|
{
|
2014-10-09 14:54:56 +00:00
|
|
|
std::cout << "Create array handle." << std::endl;
|
|
|
|
T array[ARRAY_SIZE];
|
|
|
|
for (vtkm::Id index = 0; index < ARRAY_SIZE; index++)
|
|
|
|
{
|
|
|
|
array[index] = TestValue(index, T());
|
|
|
|
}
|
2014-02-10 19:53:03 +00:00
|
|
|
|
2015-04-28 00:01:05 +00:00
|
|
|
vtkm::cont::internal::Storage<T,vtkm::cont::StorageTagBasic>
|
|
|
|
arrayStorage(array, ARRAY_SIZE);
|
2014-02-10 19:53:03 +00:00
|
|
|
|
2015-04-28 00:01:05 +00:00
|
|
|
vtkm::cont::ArrayHandle<T> arrayHandle(arrayStorage);
|
2014-02-10 19:53:03 +00:00
|
|
|
|
2014-10-09 14:54:56 +00:00
|
|
|
VTKM_TEST_ASSERT(arrayHandle.GetNumberOfValues() == ARRAY_SIZE,
|
|
|
|
"ArrayHandle has wrong number of entries.");
|
2014-02-10 19:53:03 +00:00
|
|
|
|
2014-10-09 14:54:56 +00:00
|
|
|
std::cout << "Check basic array." << std::endl;
|
2014-10-14 16:00:34 +00:00
|
|
|
CheckArray(arrayHandle);
|
2014-02-10 19:53:03 +00:00
|
|
|
|
2014-10-09 14:54:56 +00:00
|
|
|
std::cout << "Check out execution array behavior." << std::endl;
|
2014-02-10 19:53:03 +00:00
|
|
|
{
|
2014-10-09 14:54:56 +00:00
|
|
|
typename vtkm::cont::ArrayHandle<T>::template
|
|
|
|
ExecutionTypes<VTKM_DEFAULT_DEVICE_ADAPTER_TAG>::PortalConst
|
|
|
|
executionPortal;
|
|
|
|
executionPortal =
|
|
|
|
arrayHandle.PrepareForInput(VTKM_DEFAULT_DEVICE_ADAPTER_TAG());
|
2014-10-14 16:00:34 +00:00
|
|
|
CheckPortal(executionPortal);
|
2014-02-10 19:53:03 +00:00
|
|
|
}
|
2014-10-09 14:54:56 +00:00
|
|
|
|
2014-02-10 19:53:03 +00:00
|
|
|
{
|
2014-10-09 14:54:56 +00:00
|
|
|
bool gotException = false;
|
|
|
|
try
|
|
|
|
{
|
|
|
|
arrayHandle.PrepareForInPlace(VTKM_DEFAULT_DEVICE_ADAPTER_TAG());
|
|
|
|
}
|
|
|
|
catch (vtkm::cont::Error &error)
|
|
|
|
{
|
|
|
|
std::cout << "Got expected error: " << error.GetMessage() << std::endl;
|
|
|
|
gotException = true;
|
|
|
|
}
|
|
|
|
VTKM_TEST_ASSERT(gotException,
|
|
|
|
"PrepareForInPlace did not fail for const array.");
|
2014-02-10 19:53:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2014-10-09 14:54:56 +00:00
|
|
|
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()));
|
|
|
|
}
|
2014-02-10 19:53:03 +00:00
|
|
|
}
|
2014-10-09 14:54:56 +00:00
|
|
|
VTKM_TEST_ASSERT(arrayHandle.GetNumberOfValues() == ARRAY_SIZE*2,
|
|
|
|
"Array not allocated correctly.");
|
2014-10-14 16:00:34 +00:00
|
|
|
CheckArray(arrayHandle);
|
2014-10-09 14:54:56 +00:00
|
|
|
|
|
|
|
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.");
|
2014-10-14 16:00:34 +00:00
|
|
|
CheckArray(arrayHandle);
|
2014-10-09 14:54:56 +00:00
|
|
|
|
2015-02-10 22:58:41 +00:00
|
|
|
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.
|
|
|
|
|
2014-10-09 14:54:56 +00:00
|
|
|
std::cout << "Try in place operation." << std::endl;
|
2014-02-10 19:53:03 +00:00
|
|
|
{
|
2014-10-09 14:54:56 +00:00
|
|
|
typedef typename vtkm::cont::ArrayHandle<T>::template
|
|
|
|
ExecutionTypes<VTKM_DEFAULT_DEVICE_ADAPTER_TAG>::Portal
|
|
|
|
ExecutionPortalType;
|
|
|
|
ExecutionPortalType executionPortal =
|
|
|
|
arrayHandle.PrepareForInPlace(VTKM_DEFAULT_DEVICE_ADAPTER_TAG());
|
|
|
|
for (vtkm::Id index = 0;
|
|
|
|
index < executionPortal.GetNumberOfValues();
|
|
|
|
index++)
|
|
|
|
{
|
|
|
|
executionPortal.Set(index, executionPortal.Get(index) + T(1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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.");
|
2014-02-10 19:53:03 +00:00
|
|
|
}
|
2015-02-09 21:54:04 +00:00
|
|
|
|
|
|
|
std::cout << "Try operations on empty arrays." << std::endl;
|
|
|
|
// After each operation, reinitialize array in case something gets
|
|
|
|
// allocated.
|
|
|
|
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());
|
2014-02-10 19:53:03 +00:00
|
|
|
}
|
2014-10-09 14:54:56 +00:00
|
|
|
};
|
2014-02-10 19:53:03 +00:00
|
|
|
|
2014-10-09 14:54:56 +00:00
|
|
|
void TestArrayHandle()
|
|
|
|
{
|
|
|
|
vtkm::testing::Testing::TryAllTypes(TryArrayHandleType());
|
2014-02-10 19:53:03 +00:00
|
|
|
}
|
|
|
|
|
2014-10-09 14:54:56 +00:00
|
|
|
} // anonymous namespace
|
2014-02-10 19:53:03 +00:00
|
|
|
|
|
|
|
int UnitTestArrayHandle(int, char *[])
|
|
|
|
{
|
|
|
|
return vtkm::cont::testing::Testing::Run(TestArrayHandle);
|
|
|
|
}
|