ExecutionObjectBase.h 3.25 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
//============================================================================
//  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.
//============================================================================
20 21
#ifndef vtk_m_cont_ExecutionObjectBase_h
#define vtk_m_cont_ExecutionObjectBase_h
22

23
#include <vtkm/Types.h>
24 25 26 27 28 29 30 31 32 33 34 35 36

#include <vtkm/cont/DeviceAdapter.h>

#if ((VTKM_DEVICE_ADAPTER > 0) && (VTKM_DEVICE_ADAPTER < VTKM_MAX_DEVICE_ADAPTER_ID))
// Use the default device adapter tag for testing whether execution objects are valid.
#define VTK_M_DEVICE_ADAPTER_TO_TEST_EXEC_OBJECT VTKM_DEFAULT_DEVICE_ADAPTER_TAG
#else
// The default device adapter is invalid. Perhaps the error device adapter is being used.
// In this case, try the serial device adapter instead. It should always be valid.
#include <vtkm/cont/serial/DeviceAdapterSerial.h>
#define VTK_M_DEVICE_ADAPTER_TO_TEST_EXEC_OBJECT ::vtkm::cont::DeviceAdapterTagSerial
#endif

37 38 39 40
namespace vtkm
{
namespace cont
{
41

42
/// Base \c ExecutionObjectBase for execution objects to inherit from so that
43
/// you can use an arbitrary object as a parameter in an execution environment
44 45 46 47 48 49 50
/// function. Any subclass of \c ExecutionObjectBase must implement a
/// \c PrepareForExecution method that takes a device adapter tag and returns
/// an object for that device.
///
struct ExecutionObjectBase
{
};
51

52
namespace internal
53
{
54 55 56 57 58 59 60 61 62 63 64 65 66

namespace detail
{

struct CheckPrepareForExecution
{
  template <typename T>
  static auto check(T* p)
    -> decltype(p->PrepareForExecution(VTK_M_DEVICE_ADAPTER_TO_TEST_EXEC_OBJECT()),
                std::true_type());

  template <typename T>
  static auto check(...) -> std::false_type;
67
};
68 69 70 71 72 73 74 75 76 77 78 79 80 81

} // namespace detail

template <typename T>
using IsExecutionObjectBase =
  std::is_base_of<vtkm::cont::ExecutionObjectBase, typename std::decay<T>::type>;

template <typename T>
struct HasPrepareForExecution
  : decltype(detail::CheckPrepareForExecution::check<typename std::decay<T>::type>(nullptr))
{
};

} // namespace internal
82 83 84
}
} // namespace vtkm::cont

85 86 87 88 89 90 91 92
/// Checks that the argument is a proper execution object.
///
#define VTKM_IS_EXECUTION_OBJECT(execObject)                                                       \
  static_assert(::vtkm::cont::internal::IsExecutionObjectBase<execObject>::value,                  \
                "Provided type is not a subclass of vtkm::cont::ExecutionObjectBase.");            \
  static_assert(::vtkm::cont::internal::HasPrepareForExecution<execObject>::value,                 \
                "Provided type does not have requisite PrepareForExecution method.")

93
#endif //vtk_m_cont_ExecutionObjectBase_h