From ec0791e941d1be0bc2befe2e43b953dc19e03cc0 Mon Sep 17 00:00:00 2001 From: Allison Vacanti Date: Fri, 1 Jun 2018 14:01:23 -0400 Subject: [PATCH] Add a swap implementation that works on all backend. Calling std::swap isn't legal from CUDA code, but the new vtkm::Swap method is safe. It currently does a naive swap when compiling CUDA code, and falls back to an ADL swap --- docs/changelog/swap.md | 7 +++++ vtkm/CMakeLists.txt | 1 + vtkm/Swap.h | 51 +++++++++++++++++++++++++++++++++++++ vtkm/rendering/Wireframer.h | 11 ++++---- 4 files changed, 65 insertions(+), 5 deletions(-) create mode 100644 docs/changelog/swap.md create mode 100644 vtkm/Swap.h diff --git a/docs/changelog/swap.md b/docs/changelog/swap.md new file mode 100644 index 000000000..cd1822c76 --- /dev/null +++ b/docs/changelog/swap.md @@ -0,0 +1,7 @@ +# Add a CUDA-safe `vtkm::Swap` method. + +Added a swap implementation that is safe to call from all backends. + +It is not legal to call std functions from CUDA code, and the new +`vtkm::Swap` implements a naive swap when compiled under NVCC while +falling back to a std/ADL swap otherwise. diff --git a/vtkm/CMakeLists.txt b/vtkm/CMakeLists.txt index 4ecb43234..be9ff8b80 100644 --- a/vtkm/CMakeLists.txt +++ b/vtkm/CMakeLists.txt @@ -46,6 +46,7 @@ set(headers RangeId.h RangeId3.h StaticAssert.h + Swap.h TopologyElementTag.h Transform3D.h TypeListTag.h diff --git a/vtkm/Swap.h b/vtkm/Swap.h new file mode 100644 index 000000000..b870248d0 --- /dev/null +++ b/vtkm/Swap.h @@ -0,0 +1,51 @@ +//============================================================================ +// 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 2018 National Technology & Engineering Solutions of Sandia, LLC (NTESS). +// Copyright 2018 UT-Battelle, LLC. +// Copyright 2018 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. +//============================================================================ + +#ifndef vtk_m_Swap_h +#define vtk_m_Swap_h + +#include + +#include + +namespace vtkm +{ + +/// Performs a swap operation. Safe to call from cuda code. +#ifdef __CUDACC__ +template +VTKM_EXEC_CONT void Swap(T& a, T& b) +{ + T tmp = a; + a = b; + b = tmp; +} +#else +template +VTKM_EXEC_CONT void Swap(T& a, T& b) +{ + using namespace std; + swap(a, b); +} +#endif + +} // end namespace vtkm + +#endif //vtk_m_Swap_h diff --git a/vtkm/rendering/Wireframer.h b/vtkm/rendering/Wireframer.h index 43cb767ed..ba3d977a7 100644 --- a/vtkm/rendering/Wireframer.h +++ b/vtkm/rendering/Wireframer.h @@ -23,6 +23,7 @@ #include #include +#include #include #include #include @@ -219,16 +220,16 @@ public: bool transposed = vtkm::Abs(y2 - y1) > vtkm::Abs(x2 - x1); if (transposed) { - std::swap(x1, y1); - std::swap(x2, y2); + vtkm::Swap(x1, y1); + vtkm::Swap(x2, y2); } // Ensure we are always going from left to right if (x1 > x2) { - std::swap(x1, x2); - std::swap(y1, y2); - std::swap(z1, z2); + vtkm::Swap(x1, x2); + vtkm::Swap(y1, y2); + vtkm::Swap(z1, z2); } vtkm::Float32 dx = x2 - x1;