2017-02-22 21:51:57 +00:00
|
|
|
//============================================================================
|
|
|
|
// 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.
|
|
|
|
//
|
2017-09-20 21:33:44 +00:00
|
|
|
// Copyright 2016 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
|
2017-02-22 21:51:57 +00:00
|
|
|
// Copyright 2016 UT-Battelle, LLC.
|
|
|
|
// Copyright 2016 Los Alamos National Security.
|
|
|
|
//
|
2017-09-20 21:33:44 +00:00
|
|
|
// Under the terms of Contract DE-NA0003525 with NTESS,
|
2017-02-22 21:51:57 +00:00
|
|
|
// 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>
|
2018-10-30 13:49:41 +00:00
|
|
|
#include <vtkm/cont/internal/DeviceAdapterError.h>
|
2017-02-22 21:51:57 +00:00
|
|
|
|
|
|
|
#include <vtkm/cont/cuda/DeviceAdapterCuda.h>
|
2017-05-18 14:51:24 +00:00
|
|
|
#include <vtkm/cont/serial/DeviceAdapterSerial.h>
|
2017-02-22 21:51:57 +00:00
|
|
|
#include <vtkm/cont/tbb/DeviceAdapterTBB.h>
|
|
|
|
|
2017-02-23 16:19:13 +00:00
|
|
|
#include <algorithm>
|
2017-11-15 00:42:48 +00:00
|
|
|
#include <map>
|
|
|
|
#include <mutex>
|
2017-02-22 21:51:57 +00:00
|
|
|
#include <sstream>
|
2017-11-15 00:42:48 +00:00
|
|
|
#include <thread>
|
2017-02-22 21:51:57 +00:00
|
|
|
|
2018-08-28 21:14:49 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
struct VTKM_NEVER_EXPORT GetDeviceNameFunctor
|
|
|
|
{
|
|
|
|
vtkm::cont::DeviceAdapterNameType* Names;
|
|
|
|
|
|
|
|
VTKM_CONT
|
|
|
|
GetDeviceNameFunctor(vtkm::cont::DeviceAdapterNameType* names)
|
|
|
|
: Names(names)
|
|
|
|
{
|
|
|
|
std::fill_n(this->Names, VTKM_MAX_DEVICE_ADAPTER_ID, "InvalidDeviceId");
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Device>
|
|
|
|
VTKM_CONT void operator()(Device device)
|
|
|
|
{
|
|
|
|
auto id = device.GetValue();
|
|
|
|
|
|
|
|
if (id > 0 && id < VTKM_MAX_DEVICE_ADAPTER_ID)
|
|
|
|
{
|
|
|
|
this->Names[id] = vtkm::cont::DeviceAdapterTraits<Device>::GetName();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end anon namespace
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
namespace vtkm
|
|
|
|
{
|
|
|
|
namespace cont
|
|
|
|
{
|
2017-02-22 21:51:57 +00:00
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
namespace detail
|
|
|
|
{
|
2017-02-22 21:51:57 +00:00
|
|
|
|
|
|
|
struct RuntimeDeviceTrackerInternals
|
|
|
|
{
|
|
|
|
bool RuntimeValid[VTKM_MAX_DEVICE_ADAPTER_ID];
|
2018-08-28 21:14:49 +00:00
|
|
|
DeviceAdapterNameType DeviceNames[VTKM_MAX_DEVICE_ADAPTER_ID];
|
2017-02-22 21:51:57 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
VTKM_CONT
|
|
|
|
RuntimeDeviceTracker::RuntimeDeviceTracker()
|
|
|
|
: Internals(new detail::RuntimeDeviceTrackerInternals)
|
|
|
|
{
|
2018-08-28 21:14:49 +00:00
|
|
|
GetDeviceNameFunctor functor(this->Internals->DeviceNames);
|
|
|
|
vtkm::ListForEach(functor, VTKM_DEFAULT_DEVICE_ADAPTER_LIST_TAG());
|
|
|
|
|
2017-02-22 21:51:57 +00:00
|
|
|
this->Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
VTKM_CONT
|
|
|
|
RuntimeDeviceTracker::~RuntimeDeviceTracker()
|
2017-05-18 14:29:41 +00:00
|
|
|
{
|
|
|
|
}
|
2017-02-22 21:51:57 +00:00
|
|
|
|
|
|
|
VTKM_CONT
|
2018-08-29 20:47:51 +00:00
|
|
|
void RuntimeDeviceTracker::CheckDevice(vtkm::cont::DeviceAdapterId deviceId) const
|
2017-02-22 21:51:57 +00:00
|
|
|
{
|
2018-07-25 15:11:05 +00:00
|
|
|
if (!deviceId.IsValueValid())
|
2017-02-22 21:51:57 +00:00
|
|
|
{
|
|
|
|
std::stringstream message;
|
2018-08-29 20:47:51 +00:00
|
|
|
message << "Device '" << deviceId.GetName() << "' has invalid ID of "
|
|
|
|
<< (int)deviceId.GetValue();
|
2017-02-22 21:51:57 +00:00
|
|
|
throw vtkm::cont::ErrorBadValue(message.str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VTKM_CONT
|
2018-08-29 20:47:51 +00:00
|
|
|
bool RuntimeDeviceTracker::CanRunOnImpl(vtkm::cont::DeviceAdapterId deviceId) const
|
2017-02-22 21:51:57 +00:00
|
|
|
{
|
2018-08-29 20:47:51 +00:00
|
|
|
this->CheckDevice(deviceId);
|
2018-07-25 15:11:05 +00:00
|
|
|
return this->Internals->RuntimeValid[deviceId.GetValue()];
|
2017-02-22 21:51:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VTKM_CONT
|
2018-08-29 20:47:51 +00:00
|
|
|
void RuntimeDeviceTracker::SetDeviceState(vtkm::cont::DeviceAdapterId deviceId, bool state)
|
2017-02-22 21:51:57 +00:00
|
|
|
{
|
2018-08-29 20:47:51 +00:00
|
|
|
this->CheckDevice(deviceId);
|
2018-07-25 15:11:05 +00:00
|
|
|
this->Internals->RuntimeValid[deviceId.GetValue()] = state;
|
2017-02-22 21:51:57 +00:00
|
|
|
}
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
namespace
|
|
|
|
{
|
2017-02-22 21:51:57 +00:00
|
|
|
|
|
|
|
struct VTKM_NEVER_EXPORT RuntimeDeviceTrackerResetFunctor
|
|
|
|
{
|
|
|
|
vtkm::cont::RuntimeDeviceTracker Tracker;
|
|
|
|
|
|
|
|
VTKM_CONT
|
2017-05-18 14:29:41 +00:00
|
|
|
RuntimeDeviceTrackerResetFunctor(const vtkm::cont::RuntimeDeviceTracker& tracker)
|
2017-02-22 21:51:57 +00:00
|
|
|
: Tracker(tracker)
|
2017-05-18 14:29:41 +00:00
|
|
|
{
|
|
|
|
}
|
2017-02-22 21:51:57 +00:00
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
template <typename Device>
|
2018-07-25 15:11:05 +00:00
|
|
|
VTKM_CONT void operator()(Device device)
|
2017-02-22 21:51:57 +00:00
|
|
|
{
|
2018-07-25 15:11:05 +00:00
|
|
|
this->Tracker.ResetDevice(device);
|
2017-02-22 21:51:57 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
VTKM_CONT
|
|
|
|
void RuntimeDeviceTracker::Reset()
|
|
|
|
{
|
2017-02-23 16:19:13 +00:00
|
|
|
std::fill_n(this->Internals->RuntimeValid, VTKM_MAX_DEVICE_ADAPTER_ID, false);
|
2017-02-22 21:51:57 +00:00
|
|
|
|
|
|
|
RuntimeDeviceTrackerResetFunctor functor(*this);
|
|
|
|
vtkm::ListForEach(functor, VTKM_DEFAULT_DEVICE_ADAPTER_LIST_TAG());
|
|
|
|
}
|
|
|
|
|
2017-02-23 16:19:13 +00:00
|
|
|
VTKM_CONT
|
2017-05-18 14:29:41 +00:00
|
|
|
vtkm::cont::RuntimeDeviceTracker RuntimeDeviceTracker::DeepCopy() const
|
2017-02-23 16:19:13 +00:00
|
|
|
{
|
|
|
|
vtkm::cont::RuntimeDeviceTracker dest;
|
|
|
|
dest.DeepCopy(*this);
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
|
|
|
VTKM_CONT
|
2017-05-18 14:29:41 +00:00
|
|
|
void RuntimeDeviceTracker::DeepCopy(const vtkm::cont::RuntimeDeviceTracker& src)
|
2017-02-23 16:19:13 +00:00
|
|
|
{
|
2017-05-26 17:53:28 +00:00
|
|
|
std::copy_n(
|
|
|
|
src.Internals->RuntimeValid, VTKM_MAX_DEVICE_ADAPTER_ID, this->Internals->RuntimeValid);
|
2017-02-23 16:19:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VTKM_CONT
|
2018-08-29 20:47:51 +00:00
|
|
|
void RuntimeDeviceTracker::ForceDeviceImpl(vtkm::cont::DeviceAdapterId deviceId, bool runtimeExists)
|
2017-02-23 16:19:13 +00:00
|
|
|
{
|
|
|
|
if (!runtimeExists)
|
|
|
|
{
|
|
|
|
std::stringstream message;
|
2018-08-29 20:47:51 +00:00
|
|
|
message << "Cannot force to device '" << deviceId.GetName()
|
2017-02-23 16:19:13 +00:00
|
|
|
<< "' because that device is not available on this system";
|
|
|
|
throw vtkm::cont::ErrorBadValue(message.str());
|
|
|
|
}
|
2018-08-29 20:47:51 +00:00
|
|
|
this->CheckDevice(deviceId);
|
2017-02-23 16:19:13 +00:00
|
|
|
|
|
|
|
std::fill_n(this->Internals->RuntimeValid, VTKM_MAX_DEVICE_ADAPTER_ID, false);
|
|
|
|
|
2018-07-25 15:11:05 +00:00
|
|
|
this->Internals->RuntimeValid[deviceId.GetValue()] = runtimeExists;
|
2017-02-23 16:19:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
VTKM_CONT
|
|
|
|
vtkm::cont::RuntimeDeviceTracker GetGlobalRuntimeDeviceTracker()
|
|
|
|
{
|
2018-08-01 17:30:22 +00:00
|
|
|
#if defined(VTKM_CLANG) && (__apple_build_version__ < 8000000)
|
2017-11-15 00:42:48 +00:00
|
|
|
static std::mutex mtx;
|
|
|
|
static std::map<std::thread::id, vtkm::cont::RuntimeDeviceTracker> globalTrackers;
|
|
|
|
std::thread::id this_id = std::this_thread::get_id();
|
|
|
|
|
|
|
|
std::unique_lock<std::mutex> lock(mtx);
|
|
|
|
auto iter = globalTrackers.find(this_id);
|
|
|
|
if (iter != globalTrackers.end())
|
|
|
|
{
|
|
|
|
return iter->second;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
vtkm::cont::RuntimeDeviceTracker tracker;
|
|
|
|
globalTrackers[this_id] = tracker;
|
|
|
|
return tracker;
|
|
|
|
}
|
2018-08-01 17:30:22 +00:00
|
|
|
#else
|
|
|
|
return runtimeDeviceTracker;
|
|
|
|
#endif
|
2017-02-23 16:19:13 +00:00
|
|
|
}
|
2018-08-28 21:14:49 +00:00
|
|
|
|
|
|
|
VTKM_CONT
|
|
|
|
DeviceAdapterNameType RuntimeDeviceTracker::GetDeviceName(DeviceAdapterId device) const
|
|
|
|
{
|
|
|
|
auto id = device.GetValue();
|
|
|
|
|
|
|
|
if (id < 0)
|
|
|
|
{
|
|
|
|
switch (id)
|
|
|
|
{
|
|
|
|
case VTKM_DEVICE_ADAPTER_ERROR:
|
|
|
|
return vtkm::cont::DeviceAdapterTraits<vtkm::cont::DeviceAdapterTagError>::GetName();
|
|
|
|
case VTKM_DEVICE_ADAPTER_UNDEFINED:
|
2018-08-28 21:52:35 +00:00
|
|
|
return vtkm::cont::DeviceAdapterTraits<vtkm::cont::DeviceAdapterTagUndefined>::GetName();
|
2018-08-28 21:14:49 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (id >= VTKM_MAX_DEVICE_ADAPTER_ID)
|
|
|
|
{
|
|
|
|
switch (id)
|
|
|
|
{
|
|
|
|
case VTKM_DEVICE_ADAPTER_ANY:
|
2018-08-28 21:52:35 +00:00
|
|
|
return vtkm::cont::DeviceAdapterTraits<vtkm::cont::DeviceAdapterTagAny>::GetName();
|
2018-08-28 21:14:49 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else // id is valid:
|
|
|
|
{
|
|
|
|
return this->Internals->DeviceNames[id];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Device 0 is invalid:
|
|
|
|
return this->Internals->DeviceNames[0];
|
|
|
|
}
|
2017-02-22 21:51:57 +00:00
|
|
|
}
|
|
|
|
} // namespace vtkm::cont
|