vtk-m/vtkm/cont/testing/UnitTestStorageImplicit.cxx
Kenneth Moreland e62091a619 Add ability to "allocate" implicit storage
Previously, it was not possible to call Allocate or Shrink on an
implicit storage. The reason for this is that the implicit storage does
not represent any real memory and any attempt to modify it is wrong.

However, there are some rare cases where ArrayHandle will attempt to
"allocate" the storage even when behaving in a read-only manner. The use
case this is being created for is when an ArrayHandleImplicit first
calls ReleaseResources and then calls ReleaseResourcesExecution (or
anything else that tries to get a control-side portal). In this case,
the ReleaseResources makes the control side portal invalid and the
ReleaseResourcesExecution attempts to make it valid again by allocating
the storage to size 0. This change solves the problem by allowing the
implicit storage to be "allocated" to something smaller than originally
created.
2018-06-05 17:07:44 -05:00

143 lines
4.2 KiB
C++

//============================================================================
// 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).
// Copyright 2014 UT-Battelle, LLC.
// Copyright 2014 Los Alamos National Security.
//
// Under the terms of Contract DE-NA0003525 with NTESS,
// 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_STORAGE VTKM_STORAGE_ERROR
#include <vtkm/Types.h>
#include <vtkm/VecTraits.h>
#include <vtkm/cont/ArrayHandle.h>
#include <vtkm/cont/StorageImplicit.h>
#include <vtkm/cont/internal/IteratorFromArrayPortal.h>
#include <vtkm/cont/testing/Testing.h>
namespace
{
const vtkm::Id ARRAY_SIZE = 10;
template <typename T>
struct TestImplicitStorage
{
using ValueType = T;
ValueType Temp;
VTKM_EXEC_CONT
TestImplicitStorage()
: Temp(1)
{
}
VTKM_EXEC_CONT
vtkm::Id GetNumberOfValues() const { return ARRAY_SIZE; }
VTKM_EXEC_CONT
ValueType Get(vtkm::Id vtkmNotUsed(index)) const { return Temp; }
};
template <typename T>
struct TemplatedTests
{
using StorageTagType = vtkm::cont::StorageTagImplicit<TestImplicitStorage<T>>;
using StorageType = vtkm::cont::internal::Storage<T, StorageTagType>;
using ValueType = typename StorageType::ValueType;
using PortalType = typename StorageType::PortalType;
using IteratorType = typename PortalType::IteratorType;
void BasicAllocation()
{
StorageType arrayStorage;
// The implicit portal defined for this test always returns ARRAY_SIZE for the
// number of values. We should get that.
VTKM_TEST_ASSERT(arrayStorage.GetNumberOfValues() == ARRAY_SIZE,
"Implicit Storage GetNumberOfValues returned wrong size.");
// Make sure you can allocate and shrink to any value <= the reported portal size.
arrayStorage.Allocate(ARRAY_SIZE / 2);
VTKM_TEST_ASSERT(arrayStorage.GetNumberOfValues() == ARRAY_SIZE / 2,
"Cannot re-Allocate array to half size.");
arrayStorage.Allocate(0);
VTKM_TEST_ASSERT(arrayStorage.GetNumberOfValues() == 0, "Cannot re-Allocate array to zero.");
arrayStorage.Allocate(ARRAY_SIZE);
VTKM_TEST_ASSERT(arrayStorage.GetNumberOfValues() == ARRAY_SIZE,
"Cannot re-Allocate array to original size.");
arrayStorage.Shrink(ARRAY_SIZE / 2);
VTKM_TEST_ASSERT(arrayStorage.GetNumberOfValues() == ARRAY_SIZE / 2,
"Cannot Shrink array to half size.");
arrayStorage.Shrink(0);
VTKM_TEST_ASSERT(arrayStorage.GetNumberOfValues() == 0, "Cannot Shrink array to zero.");
arrayStorage.Shrink(ARRAY_SIZE);
VTKM_TEST_ASSERT(arrayStorage.GetNumberOfValues() == ARRAY_SIZE,
"Cannot Shrink array to original size.");
//verify that calling ReleaseResources doesn't throw an exception
arrayStorage.ReleaseResources();
//verify that you can allocate after releasing resources.
arrayStorage.Allocate(ARRAY_SIZE);
}
void BasicAccess()
{
TestImplicitStorage<T> portal;
vtkm::cont::ArrayHandle<T, StorageTagType> implictHandle(portal);
VTKM_TEST_ASSERT(implictHandle.GetNumberOfValues() == ARRAY_SIZE, "handle has wrong size");
VTKM_TEST_ASSERT(implictHandle.GetPortalConstControl().Get(0) == T(1),
"portals first values should be 1");
}
void operator()()
{
BasicAllocation();
BasicAccess();
}
};
struct TestFunctor
{
template <typename T>
void operator()(T) const
{
TemplatedTests<T> tests;
tests();
}
};
void TestStorageBasic()
{
vtkm::testing::Testing::TryTypes(TestFunctor());
}
} // Anonymous namespace
int UnitTestStorageImplicit(int, char* [])
{
return vtkm::cont::testing::Testing::Run(TestStorageBasic);
}