Add DispatcherMapField.

This is a simple version of a dispatcher, but an important one.

Note that there is an issue brought up with UnitTestWorkletMapField in
that there needs to be better ways to specify worklet argument types.
This commit is contained in:
Kenneth Moreland 2014-10-21 13:10:00 -06:00
parent 53a454fe77
commit 80809a8f0f
5 changed files with 242 additions and 2 deletions

@ -326,7 +326,7 @@ template<typename T>
VTKM_EXEC_CONT_EXPORT
T TestValue(vtkm::Id index, T, vtkm::TypeTraitsRealTag)
{
return T(0.01*index + 0.001);
return T(0.01*index + 1.001);
}
/// Many tests involve getting and setting values in some index-based structure

@ -19,6 +19,7 @@
##============================================================================
set(headers
DispatcherMapField.h
WorkletMapField.h
)
@ -29,4 +30,4 @@ vtkm_declare_headers(${headers})
#-----------------------------------------------------------------------------
# add_subdirectory(testing)
add_subdirectory(testing)

@ -0,0 +1,93 @@
//============================================================================
// 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_worklet_Dispatcher_MapField_h
#define vtk_m_worklet_Dispatcher_MapField_h
#include <vtkm/cont/DeviceAdapter.h>
#include <vtkm/worklet/WorkletMapField.h>
#include <vtkm/worklet/internal/DispatcherBase.h>
namespace vtkm {
namespace worklet {
/// \brief Dispatcher for worklets that inherit from \c WorkletMapField.
///
template<typename WorkletType,
typename Device = VTKM_DEFAULT_DEVICE_ADAPTER_TAG>
class DispatcherMapField :
public vtkm::worklet::internal::DispatcherBase<
DispatcherMapField<WorkletType,Device>,
WorkletType,
vtkm::worklet::WorkletMapField,
Device>
{
typedef vtkm::worklet::internal::DispatcherBase<
DispatcherMapField<WorkletType,Device>,
WorkletType,
vtkm::worklet::WorkletMapField,
Device> Superclass;
public:
VTKM_CONT_EXPORT
DispatcherMapField(const WorkletType &worklet = WorkletType())
: Superclass(worklet) { }
template<typename Invocation>
VTKM_CONT_EXPORT
void DoInvoke(const Invocation &invocation) const
{
// The parameter for the input domain is stored in the Invocation. (It is
// also in the worklet, but it is safer to get it from the Invocation
// in case some other dispatch operation had to modify it.)
static const vtkm::IdComponent InputDomainIndex =
Invocation::InputDomainIndex;
// ParameterInterface (from Invocation) is a FunctionInterface type
// containing types for all objects passed to the Invoke method (with
// some dynamic casting performed so objects like DynamicArrayHandle get
// cast to ArrayHandle).
typedef typename Invocation::ParameterInterface ParameterInterface;
// This is the type for the input domain (derived from the last two things
// we got from the Invocation).
typedef typename ParameterInterface::
template ParameterType<InputDomainIndex>::type InputDomainType;
// We can pull the input domain parameter (the data specifying the input
// domain) from the invocation object.
InputDomainType inputDomain =
invocation.Parameters.template GetParameter<InputDomainIndex>();
// For a DispatcherMapField, the inputDomain must be an ArrayHandle (or
// a DynamicArrayHandle that gets cast to one). The size of the domain
// (number of threads/worklet instances) is equal to the size of the
// array.
vtkm::Id numInstances = inputDomain.GetNumberOfValues();
// A MapField is a pretty straightforward dispatch. Once we know the number
// of invocations, the superclass can take care of the rest.
this->BasicInvoke(invocation, numInstances);
}
};
}
} // namespace vtkm::worklet
#endif //vtk_m_worklet_Dispatcher_MapField_h

@ -0,0 +1,25 @@
##============================================================================
## 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.
##============================================================================
set(unit_tests
UnitTestWorkletMapField.cxx
)
vtkm_unit_tests(SOURCES ${unit_tests})

@ -0,0 +1,121 @@
//============================================================================
// 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.
//============================================================================
#include <vtkm/worklet/DispatcherMapField.h>
#include <vtkm/worklet/WorkletMapField.h>
#include <vtkm/cont/ArrayHandle.h>
#include <vtkm/cont/DynamicArrayHandle.h>
#include <vtkm/cont/testing/Testing.h>
namespace {
static const vtkm::Id ARRAY_SIZE = 10;
class TestWorklet : public vtkm::worklet::WorkletMapField
{
public:
typedef void ControlSignature(FieldIn, FieldOut);
typedef _2 ExecutionSignature(_1, WorkIndex);
template<typename T>
T operator()(T x, vtkm::Id workIndex) const
{
if (x != TestValue(workIndex, T()) + T(100))
{
this->RaiseError("Got wrong input value.");
}
return x - T(100);
}
};
struct DoTestWorklet
{
template<typename T>
VTKM_CONT_EXPORT
void operator()(T) const
{
std::cout << "Set up data." << std::endl;
T inputArray[ARRAY_SIZE];
for (vtkm::Id index = 0; index < ARRAY_SIZE; index++)
{
inputArray[index] = TestValue(index, T()) + T(100);
}
vtkm::cont::ArrayHandle<T> inputHandle =
vtkm::cont::make_ArrayHandle(inputArray, ARRAY_SIZE);
vtkm::cont::ArrayHandle<T> outputHandle;
std::cout << "Create and run dispatcher." << std::endl;
vtkm::worklet::DispatcherMapField<TestWorklet> dispatcher;
dispatcher.Invoke(inputHandle, outputHandle);
std::cout << "Check result." << std::endl;
CheckPortal(outputHandle.GetPortalConstControl());
// The following test is commented out because as of this writing
// (10-21-2014) there is an issue with getting unexpected types when
// casting dynamic arrays. In particular, this issue is with using dynamic
// arrays. We know that both arrays will always be the same type, but the
// arrays are cast independently. Thus, the compiler will generate code for
// odd combinations that are incompatibile with each other. Thus, we need a
// way to better specify the types expected by the worklet function. I can
// think of two general ways (there might be more).
//
// 1. Specify the expected type in the ControlSignature. This would
// probably be a template argument of the tag with a list of basic types
// that could be in the array. The dynamic array casting would then take
// that into account and only try those specified types. That should be
// fairly straightforward to implement and handle many cases. However, it
// still has the problem that all dynamic arrays are cast independently.
// Thus, for example, if you have a worklet that can operate on vectors of
// any size, you will likely get a compile error when trying to operate on
// two vectors of different size when you expected them to be the same.
// This particular general case might actually be quite rare, so in that
// case the user has the onus to create a default template that handles
// this exceptional case with failure.
//
// 2. Have a mechanism to identify when the type of two things is expected
// to be the same. I'm not sure what the programming interface for that
// would look though.
//
// std::cout << "Repeat with dynamic arrays." << std::endl;
// // Clear out output array.
// outputHandle = vtkm::cont::ArrayHandle<T>();
// vtkm::cont::DynamicArrayHandle inputDynamic(inputHandle);
// vtkm::cont::DynamicArrayHandle outputDynamic(outputHandle);
// dispatcher.Invoke(inputDynamic, outputDynamic);
// CheckPortal(outputHandle.GetPortalConstControl());
}
};
void TestWorkletMapField()
{
vtkm::testing::Testing::TryTypes(DoTestWorklet(), vtkm::TypeListTagCommon());
}
} // anonymous namespace
int UnitTestWorkletMapField(int, char *[])
{
return vtkm::cont::testing::Testing::Run(TestWorkletMapField);
}