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.
This commit is contained in:
Kenneth Moreland 2018-06-04 14:56:44 -05:00
parent 9d16fadfc3
commit e62091a619
2 changed files with 39 additions and 28 deletions

@ -70,6 +70,7 @@ public:
VTKM_CONT VTKM_CONT
Storage(const PortalConstType& portal = PortalConstType()) Storage(const PortalConstType& portal = PortalConstType())
: Portal(portal) : Portal(portal)
, NumberOfValues(portal.GetNumberOfValues())
{ {
} }
@ -79,22 +80,25 @@ public:
VTKM_CONT VTKM_CONT
PortalConstType GetPortalConst() const { return this->Portal; } PortalConstType GetPortalConst() const { return this->Portal; }
VTKM_CONT VTKM_CONT
vtkm::Id GetNumberOfValues() const { return this->Portal.GetNumberOfValues(); } vtkm::Id GetNumberOfValues() const { return this->NumberOfValues; }
VTKM_CONT VTKM_CONT
void Allocate(vtkm::Id vtkmNotUsed(numberOfValues)) void Allocate(vtkm::Id numberOfValues)
{ {
throw vtkm::cont::ErrorBadValue("Implicit arrays are read-only."); VTKM_ASSERT(numberOfValues <= this->Portal.GetNumberOfValues());
this->NumberOfValues = numberOfValues;
} }
VTKM_CONT VTKM_CONT
void Shrink(vtkm::Id vtkmNotUsed(numberOfValues)) void Shrink(vtkm::Id numberOfValues)
{ {
throw vtkm::cont::ErrorBadValue("Implicit arrays are read-only."); VTKM_ASSERT(numberOfValues <= this->Portal.GetNumberOfValues());
this->NumberOfValues = numberOfValues;
} }
VTKM_CONT VTKM_CONT
void ReleaseResources() {} void ReleaseResources() {}
private: private:
PortalConstType Portal; PortalConstType Portal;
vtkm::Id NumberOfValues;
}; };
template <typename T, class ArrayPortalType, class DeviceAdapterTag> template <typename T, class ArrayPortalType, class DeviceAdapterTag>

@ -33,6 +33,8 @@
namespace namespace
{ {
const vtkm::Id ARRAY_SIZE = 10;
template <typename T> template <typename T>
struct TestImplicitStorage struct TestImplicitStorage
{ {
@ -46,14 +48,12 @@ struct TestImplicitStorage
} }
VTKM_EXEC_CONT VTKM_EXEC_CONT
vtkm::Id GetNumberOfValues() const { return 1; } vtkm::Id GetNumberOfValues() const { return ARRAY_SIZE; }
VTKM_EXEC_CONT VTKM_EXEC_CONT
ValueType Get(vtkm::Id vtkmNotUsed(index)) const { return Temp; } ValueType Get(vtkm::Id vtkmNotUsed(index)) const { return Temp; }
}; };
const vtkm::Id ARRAY_SIZE = 1;
template <typename T> template <typename T>
struct TemplatedTests struct TemplatedTests
{ {
@ -68,39 +68,46 @@ struct TemplatedTests
{ {
StorageType arrayStorage; StorageType arrayStorage;
// The implicit portal defined for this test always returns 1 for the // The implicit portal defined for this test always returns ARRAY_SIZE for the
// number of values. We should get that. // number of values. We should get that.
VTKM_TEST_ASSERT(arrayStorage.GetNumberOfValues() == 1, VTKM_TEST_ASSERT(arrayStorage.GetNumberOfValues() == ARRAY_SIZE,
"Implicit Storage GetNumberOfValues returned wrong size."); "Implicit Storage GetNumberOfValues returned wrong size.");
try // Make sure you can allocate and shrink to any value <= the reported portal size.
{ arrayStorage.Allocate(ARRAY_SIZE / 2);
arrayStorage.Allocate(ARRAY_SIZE); VTKM_TEST_ASSERT(arrayStorage.GetNumberOfValues() == ARRAY_SIZE / 2,
VTKM_TEST_ASSERT(false == true, "Implicit Storage Allocate method didn't throw error."); "Cannot re-Allocate array to half size.");
}
catch (vtkm::cont::ErrorBadValue&)
{
}
try arrayStorage.Allocate(0);
{ VTKM_TEST_ASSERT(arrayStorage.GetNumberOfValues() == 0, "Cannot re-Allocate array to zero.");
arrayStorage.Shrink(ARRAY_SIZE);
VTKM_TEST_ASSERT(true == false, arrayStorage.Allocate(ARRAY_SIZE);
"Array shrink do a larger size was possible. This can't be allowed."); VTKM_TEST_ASSERT(arrayStorage.GetNumberOfValues() == ARRAY_SIZE,
} "Cannot re-Allocate array to original size.");
catch (vtkm::cont::ErrorBadValue&)
{ 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 //verify that calling ReleaseResources doesn't throw an exception
arrayStorage.ReleaseResources(); arrayStorage.ReleaseResources();
//verify that you can allocate after releasing resources.
arrayStorage.Allocate(ARRAY_SIZE);
} }
void BasicAccess() void BasicAccess()
{ {
TestImplicitStorage<T> portal; TestImplicitStorage<T> portal;
vtkm::cont::ArrayHandle<T, StorageTagType> implictHandle(portal); vtkm::cont::ArrayHandle<T, StorageTagType> implictHandle(portal);
VTKM_TEST_ASSERT(implictHandle.GetNumberOfValues() == 1, "handle should have size 1"); VTKM_TEST_ASSERT(implictHandle.GetNumberOfValues() == ARRAY_SIZE, "handle has wrong size");
VTKM_TEST_ASSERT(implictHandle.GetPortalConstControl().Get(0) == T(1), VTKM_TEST_ASSERT(implictHandle.GetPortalConstControl().Get(0) == T(1),
"portals first values should be 1"); "portals first values should be 1");
} }