Add the implementation of RuntimeDeviceTracker to cont library

This allows code to include the RuntimeDeviceTracker without depending
on the device-specific adapters (I think).

Also changed the implementation to use a shared_ptr for the state so you
can pass it around and share the state easier.
This commit is contained in:
Kenneth Moreland 2017-02-22 14:51:57 -07:00
parent b9d3206ea6
commit 814b2db18c
3 changed files with 179 additions and 53 deletions

@ -86,6 +86,7 @@ set(sources
CoordinateSystem.cxx
DynamicArrayHandle.cxx
Field.cxx
RuntimeDeviceTracker.cxx
internal/SimplePolymorphicContainer.cxx
StorageBasic.cxx
)

@ -0,0 +1,123 @@
//============================================================================
// 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 2016 Sandia Corporation.
// Copyright 2016 UT-Battelle, LLC.
// Copyright 2016 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/cont/RuntimeDeviceTracker.h>
#include <vtkm/cont/DeviceAdapter.h>
#include <vtkm/cont/DeviceAdapterListTag.h>
#include <vtkm/cont/ErrorBadValue.h>
#include <vtkm/cont/serial/DeviceAdapterSerial.h>
#include <vtkm/cont/cuda/DeviceAdapterCuda.h>
#include <vtkm/cont/tbb/DeviceAdapterTBB.h>
#include <cstring>
#include <sstream>
#define VTKM_MAX_DEVICE_ADAPTER_ID 8
namespace vtkm {
namespace cont {
namespace detail {
struct RuntimeDeviceTrackerInternals
{
bool RuntimeValid[VTKM_MAX_DEVICE_ADAPTER_ID];
};
}
VTKM_CONT
RuntimeDeviceTracker::RuntimeDeviceTracker()
: Internals(new detail::RuntimeDeviceTrackerInternals)
{
this->Reset();
}
VTKM_CONT
RuntimeDeviceTracker::~RuntimeDeviceTracker()
{ }
VTKM_CONT
void RuntimeDeviceTracker::CheckDevice(
vtkm::cont::DeviceAdapterId deviceId,
const vtkm::cont::DeviceAdapterNameType &deviceName) const
{
if ((deviceId < 0) || (deviceId >= VTKM_MAX_DEVICE_ADAPTER_ID))
{
std::stringstream message;
message << "Device '" << deviceName << "' has invalid ID of " << deviceId;
throw vtkm::cont::ErrorBadValue(message.str());
}
}
VTKM_CONT
bool RuntimeDeviceTracker::CanRunOnImpl(
vtkm::cont::DeviceAdapterId deviceId,
const vtkm::cont::DeviceAdapterNameType &deviceName) const
{
this->CheckDevice(deviceId, deviceName);
return this->Internals->RuntimeValid[deviceId];
}
VTKM_CONT
void RuntimeDeviceTracker::SetDeviceState(
vtkm::cont::DeviceAdapterId deviceId,
const vtkm::cont::DeviceAdapterNameType &deviceName,
bool state)
{
this->CheckDevice(deviceId, deviceName);
this->Internals->RuntimeValid[deviceId] = state;
}
namespace {
struct VTKM_NEVER_EXPORT RuntimeDeviceTrackerResetFunctor
{
vtkm::cont::RuntimeDeviceTracker Tracker;
VTKM_CONT
RuntimeDeviceTrackerResetFunctor(
const vtkm::cont::RuntimeDeviceTracker &tracker)
: Tracker(tracker)
{ }
template<typename Device>
VTKM_CONT
void operator()(Device)
{
this->Tracker.ResetDevice(Device());
}
};
}
VTKM_CONT
void RuntimeDeviceTracker::Reset()
{
std::memset(this->Internals->RuntimeValid, 0, VTKM_MAX_DEVICE_ADAPTER_ID);
RuntimeDeviceTrackerResetFunctor functor(*this);
vtkm::ListForEach(functor, VTKM_DEFAULT_DEVICE_ADAPTER_LIST_TAG());
}
}
} // namespace vtkm::cont

@ -20,19 +20,19 @@
#ifndef vtk_m_cont_RuntimeDeviceTracker_h
#define vtk_m_cont_RuntimeDeviceTracker_h
#include <vtkm/cont/DeviceAdapterAlgorithm.h>
#include <vtkm/cont/ErrorBadAllocation.h>
#include <vtkm/cont/RuntimeDeviceInformation.h>
#include <vtkm/cont/DeviceAdapter.h>
#include <vtkm/cont/serial/DeviceAdapterSerial.h>
#include <vtkm/cont/cuda/DeviceAdapterCuda.h>
#include <vtkm/cont/tbb/DeviceAdapterTBB.h>
#include <cstring>
namespace vtkm {
namespace cont {
namespace detail {
struct RuntimeDeviceTrackerInternals;
}
/// A class that can be used to determine if a given device adapter
/// is supported on the current machine at runtime. This is a more
/// complex version of vtkm::cont::RunimeDeviceInformation, as this can
@ -40,14 +40,16 @@ namespace cont {
/// of valid runtime devices based on that information.
///
///
class RuntimeDeviceTracker
class VTKM_ALWAYS_EXPORT RuntimeDeviceTracker
{
public:
VTKM_CONT_EXPORT
VTKM_CONT
RuntimeDeviceTracker()
{
this->Reset();
}
RuntimeDeviceTracker();
VTKM_CONT_EXPORT
VTKM_CONT
~RuntimeDeviceTracker();
/// Returns true if the given device adapter is supported on the current
/// machine.
@ -56,63 +58,63 @@ public:
VTKM_CONT
bool CanRunOn(DeviceAdapterTag) const
{
typedef vtkm::cont::DeviceAdapterTraits<DeviceAdapterTag> Traits;
return this->RuntimeValid[ Traits::GetId() ];
using Traits = vtkm::cont::DeviceAdapterTraits<DeviceAdapterTag>;
return this->CanRunOnImpl(Traits::GetId(), Traits::GetName());
}
///Report a failure to allocate memory on a device, this will flag the device
///as being unusable for all future invocations of the instance of the filter.
/// Report a failure to allocate memory on a device, this will flag the
/// device as being unusable for all future invocations of the instance of
/// the filter.
///
template<typename DeviceAdapterTag>
VTKM_CONT
void ReportAllocationFailure(DeviceAdapterTag,
const vtkm::cont::ErrorBadAllocation&)
{
typedef vtkm::cont::DeviceAdapterTraits<DeviceAdapterTag> Traits;
this->RuntimeValid[ Traits::GetId() ] = false;
using Traits = vtkm::cont::DeviceAdapterTraits<DeviceAdapterTag>;
this->SetDeviceState(Traits::GetId(), Traits::GetName(), false);
}
///Reset the tracker to its default state.
/// Reset the tracker for the given device. This will discard any updates
/// caused by reported failures
///
template<typename DeviceAdapterTag>
VTKM_CONT
void ResetDevice(DeviceAdapterTag)
{
using Traits = vtkm::cont::DeviceAdapterTraits<DeviceAdapterTag>;
vtkm::cont::RuntimeDeviceInformation<DeviceAdapterTag> runtimeDevice;
this->SetDeviceState(Traits::GetId(),
Traits::GetName(),
runtimeDevice.Exists());
}
/// Reset the tracker to its default state for default devices.
/// Will discard any updates caused by reported failures.
///
VTKM_CONT_EXPORT
VTKM_CONT
void Reset()
{
std::memset(this->RuntimeValid, 0, sizeof(bool)*8 );
//for each device determine the current runtime status at mark it
//self in the validity array
{
typedef vtkm::cont::DeviceAdapterTagCuda CudaTag;
typedef vtkm::cont::DeviceAdapterTraits<CudaTag> CudaTraits;
vtkm::cont::RuntimeDeviceInformation<CudaTag> runtimeDevice;
this->RuntimeValid[ CudaTraits::GetId() ] = runtimeDevice.Exists();
}
{
typedef vtkm::cont::DeviceAdapterTagTBB TBBTag;
typedef vtkm::cont::DeviceAdapterTraits<TBBTag> TBBTraits;
vtkm::cont::RuntimeDeviceInformation<TBBTag> runtimeDevice;
this->RuntimeValid[ TBBTraits::GetId() ] = runtimeDevice.Exists();
}
{
typedef vtkm::cont::DeviceAdapterTagSerial SerialTag;
typedef vtkm::cont::DeviceAdapterTraits<SerialTag> SerialTraits;
vtkm::cont::RuntimeDeviceInformation<SerialTag> runtimeDevice;
this->RuntimeValid[ SerialTraits::GetId() ] = runtimeDevice.Exists();
}
}
void Reset();
private:
//make the array size 8 so the sizeof the class doesn't change when
//we add more device adapters.
bool RuntimeValid[8];
std::shared_ptr<detail::RuntimeDeviceTrackerInternals> Internals;
VTKM_CONT_EXPORT
VTKM_CONT
void CheckDevice(vtkm::cont::DeviceAdapterId deviceId,
const vtkm::cont::DeviceAdapterNameType &deviceName) const;
VTKM_CONT_EXPORT
VTKM_CONT
bool CanRunOnImpl(vtkm::cont::DeviceAdapterId deviceId,
const vtkm::cont::DeviceAdapterNameType &deviceName) const;
VTKM_CONT_EXPORT
VTKM_CONT
void SetDeviceState(vtkm::cont::DeviceAdapterId deviceId,
const vtkm::cont::DeviceAdapterNameType &deviceName,
bool state);
};
}