2015-09-16 21:16:07 +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 2014 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
|
2015-09-16 21:16:07 +00:00
|
|
|
// Copyright 2014 UT-Battelle, LLC.
|
|
|
|
// Copyright 2014 Los Alamos National Security.
|
|
|
|
//
|
2017-09-20 21:33:44 +00:00
|
|
|
// Under the terms of Contract DE-NA0003525 with NTESS,
|
2015-09-16 21:16:07 +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 <iostream>
|
|
|
|
|
|
|
|
#include <vtkm/Math.h>
|
|
|
|
#include <vtkm/cont/ArrayHandle.h>
|
2015-12-02 18:55:49 +00:00
|
|
|
#include <vtkm/cont/RuntimeDeviceInformation.h>
|
|
|
|
|
2015-09-16 21:16:07 +00:00
|
|
|
#include <vtkm/worklet/DispatcherMapField.h>
|
|
|
|
#include <vtkm/worklet/WorkletMapField.h>
|
|
|
|
|
2017-05-18 14:51:24 +00:00
|
|
|
#include <vtkm/cont/TryExecute.h>
|
2015-09-16 21:16:07 +00:00
|
|
|
#include <vtkm/cont/cuda/DeviceAdapterCuda.h>
|
2016-09-28 15:38:31 +00:00
|
|
|
#include <vtkm/cont/serial/DeviceAdapterSerial.h>
|
2017-05-18 14:51:24 +00:00
|
|
|
#include <vtkm/cont/tbb/DeviceAdapterTBB.h>
|
2016-08-22 19:45:45 +00:00
|
|
|
|
2017-08-10 17:46:17 +00:00
|
|
|
using FloatVec3 = vtkm::Vec<vtkm::Float32, 3>;
|
|
|
|
using Uint8Vec4 = vtkm::Vec<vtkm::UInt8, 4>;
|
2015-09-16 21:16:07 +00:00
|
|
|
|
|
|
|
struct GenerateSurfaceWorklet : public vtkm::worklet::WorkletMapField
|
|
|
|
{
|
|
|
|
vtkm::Float32 t;
|
2017-05-18 14:29:41 +00:00
|
|
|
GenerateSurfaceWorklet(vtkm::Float32 st)
|
|
|
|
: t(st)
|
|
|
|
{
|
|
|
|
}
|
2015-09-16 21:16:07 +00:00
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
typedef void ControlSignature(FieldIn<>, FieldOut<>, FieldOut<>);
|
|
|
|
typedef void ExecutionSignature(_1, _2, _3);
|
2015-09-16 21:16:07 +00:00
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
template <typename T>
|
2017-05-26 17:53:28 +00:00
|
|
|
VTKM_EXEC void operator()(const vtkm::Vec<T, 3>& input,
|
|
|
|
vtkm::Vec<T, 3>& output,
|
2017-05-18 14:29:41 +00:00
|
|
|
vtkm::Vec<vtkm::UInt8, 4>& color) const
|
2015-09-16 21:16:07 +00:00
|
|
|
{
|
|
|
|
output[0] = input[0];
|
2017-05-18 14:29:41 +00:00
|
|
|
output[1] = 0.25f * vtkm::Sin(input[0] * 10.f + t) * vtkm::Cos(input[2] * 10.f + t);
|
2015-09-16 21:16:07 +00:00
|
|
|
output[2] = input[2];
|
|
|
|
|
|
|
|
color[0] = 0;
|
2015-11-03 14:10:39 +00:00
|
|
|
color[1] = static_cast<vtkm::UInt8>(160 + (96 * vtkm::Sin(input[0] * 10.f + t)));
|
|
|
|
color[2] = static_cast<vtkm::UInt8>(160 + (96 * vtkm::Cos(input[2] * 5.f + t)));
|
2015-09-16 21:16:07 +00:00
|
|
|
color[3] = 255;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-08-22 19:45:45 +00:00
|
|
|
struct RunGenerateSurfaceWorklet
|
2015-09-16 21:16:07 +00:00
|
|
|
{
|
2017-05-18 14:29:41 +00:00
|
|
|
template <typename DeviceAdapterTag>
|
|
|
|
bool operator()(DeviceAdapterTag) const
|
2015-09-16 21:16:07 +00:00
|
|
|
{
|
2016-08-22 19:45:45 +00:00
|
|
|
//At this point we know we have runtime support
|
2017-08-10 17:46:17 +00:00
|
|
|
using DeviceTraits = vtkm::cont::DeviceAdapterTraits<DeviceAdapterTag>;
|
2015-09-16 21:16:07 +00:00
|
|
|
|
2017-08-10 17:46:17 +00:00
|
|
|
using DispatcherType =
|
|
|
|
vtkm::worklet::DispatcherMapField<GenerateSurfaceWorklet, DeviceAdapterTag>;
|
2015-12-02 18:55:49 +00:00
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
std::cout << "Running a worklet on device adapter: " << DeviceTraits::GetName() << std::endl;
|
2016-08-22 19:45:45 +00:00
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
GenerateSurfaceWorklet worklet(0.05f);
|
|
|
|
DispatcherType(worklet).Invoke(this->In, this->Out, this->Color);
|
2016-08-22 19:45:45 +00:00
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
return true;
|
2015-09-16 21:16:07 +00:00
|
|
|
}
|
2016-08-22 19:45:45 +00:00
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
vtkm::cont::ArrayHandle<FloatVec3> In;
|
|
|
|
vtkm::cont::ArrayHandle<FloatVec3> Out;
|
|
|
|
vtkm::cont::ArrayHandle<Uint8Vec4> Color;
|
2015-09-16 21:16:07 +00:00
|
|
|
};
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
template <typename T>
|
|
|
|
std::vector<vtkm::Vec<T, 3>> make_testData(int size)
|
2015-09-16 21:16:07 +00:00
|
|
|
{
|
2017-05-18 14:29:41 +00:00
|
|
|
std::vector<vtkm::Vec<T, 3>> data;
|
|
|
|
data.reserve(static_cast<std::size_t>(size * size));
|
|
|
|
for (int i = 0; i < size; ++i)
|
|
|
|
{
|
|
|
|
for (int j = 0; j < size; ++j)
|
2015-09-16 21:16:07 +00:00
|
|
|
{
|
2017-05-26 17:53:28 +00:00
|
|
|
data.push_back(vtkm::Vec<T, 3>(
|
|
|
|
2.f * static_cast<T>(i / size) - 1.f, 0.f, 2.f * static_cast<T>(j / size) - 1.f));
|
2015-09-16 21:16:07 +00:00
|
|
|
}
|
2017-05-18 14:29:41 +00:00
|
|
|
}
|
2015-09-16 21:16:07 +00:00
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2016-08-22 19:45:45 +00:00
|
|
|
//This is the list of devices to compile in support for. The order of the
|
|
|
|
//devices determines the runtime preference.
|
2017-05-26 17:53:28 +00:00
|
|
|
struct DevicesToTry : vtkm::ListTagBase<vtkm::cont::DeviceAdapterTagCuda,
|
|
|
|
vtkm::cont::DeviceAdapterTagTBB,
|
|
|
|
vtkm::cont::DeviceAdapterTagSerial>
|
2017-05-18 14:29:41 +00:00
|
|
|
{
|
|
|
|
};
|
2015-09-16 21:16:07 +00:00
|
|
|
|
|
|
|
int main(int, char**)
|
|
|
|
{
|
2017-05-18 14:29:41 +00:00
|
|
|
std::vector<FloatVec3> data = make_testData<vtkm::Float32>(1024);
|
2015-09-16 21:16:07 +00:00
|
|
|
|
|
|
|
//make array handles for the data
|
2016-08-22 19:45:45 +00:00
|
|
|
|
|
|
|
// TryExecutes takes a functor and a list of devices. It then tries to run
|
|
|
|
// the functor for each device (in the order given in the list) until the
|
|
|
|
// execution succeeds. This allows you to compile in support for multiple
|
|
|
|
// devices which have runtime requirements ( GPU / HW Accelerator ) and
|
|
|
|
// correctly choose the best device at runtime.
|
|
|
|
//
|
|
|
|
// The functor parentheses operator should take exactly one argument, which is
|
|
|
|
// the DeviceAdapterTag to use. The functor should return true if the execution
|
|
|
|
// succeeds.
|
|
|
|
//
|
|
|
|
// This function also optionally takes a vtkm::cont::RuntimeDeviceTracker, which
|
|
|
|
// will monitor for certain failures across calls to TryExecute and skip trying
|
|
|
|
// devices with a history of failure.
|
|
|
|
RunGenerateSurfaceWorklet task;
|
|
|
|
task.In = vtkm::cont::make_ArrayHandle(data);
|
|
|
|
vtkm::cont::TryExecute(task, DevicesToTry());
|
2015-09-16 21:16:07 +00:00
|
|
|
}
|