From b4a4534dbe9f3aa3b763f3e5be0ed177e1377931 Mon Sep 17 00:00:00 2001 From: Kenneth Moreland Date: Tue, 26 May 2015 22:32:10 -0600 Subject: [PATCH] Introductory pyexpander macros for Math functions --- CMakeLists.txt | 2 +- vtkm/CMakeLists.txt | 3 ++ vtkm/Math.h | 86 ++++++++++++++++++++++++++++++++++ vtkm/Math.h.in | 109 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 199 insertions(+), 1 deletion(-) create mode 100644 vtkm/Math.h create mode 100644 vtkm/Math.h.in diff --git a/CMakeLists.txt b/CMakeLists.txt index fdbbdfdfd..df60c1487 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -139,7 +139,7 @@ vtkm_install_headers( # * Meta programming language find_package(BoostHeaders ${VTKm_REQUIRED_BOOST_VERSION} REQUIRED) -#####find_package(Pyexpander) +find_package(Pyexpander) #----------------------------------------------------------------------------- # Add subdirectories diff --git a/vtkm/CMakeLists.txt b/vtkm/CMakeLists.txt index 3460b8aa2..efa6da2ad 100644 --- a/vtkm/CMakeLists.txt +++ b/vtkm/CMakeLists.txt @@ -24,6 +24,7 @@ set(headers CellType.h Extent.h ListTag.h + Math.h Pair.h RegularConnectivity.h RegularStructure.h @@ -33,6 +34,8 @@ set(headers VecTraits.h ) +vtkm_pyexpander_generated_file(Math.h) + vtkm_declare_headers(${headers}) #----------------------------------------------------------------------------- diff --git a/vtkm/Math.h b/vtkm/Math.h new file mode 100644 index 000000000..608cf8472 --- /dev/null +++ b/vtkm/Math.h @@ -0,0 +1,86 @@ +//============================================================================= +// +// 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 2015 Sandia Corporation. +// Copyright 2015 UT-Battelle, LLC. +// Copyright 2015 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. +// +//============================================================================= +// **** DO NOT EDIT THIS FILE!!! **** +// This file is automatically generated by FunctionInterfaceDetailPre.h.in + +#ifndef vtk_m_Math_h +#define vtk_m_Math_h + +#include + +#ifndef VTKM_CUDA +#include +#endif + +#define VTKM_SYS_MATH_FUNCTION_32(func) func ## f +#define VTKM_SYS_MATH_FUNCTION_64(func) func + + + + +namespace vtkm { + +/// Compute the square root of \p x. +/// +VTKM_EXEC_CONT_EXPORT +vtkm::Float32 Sqrt(vtkm::Float32 x) { + return VTKM_SYS_MATH_FUNCTION_32(sqrt)(x); +} +VTKM_EXEC_CONT_EXPORT +vtkm::Float64 Sqrt(vtkm::Float64 x) { + return VTKM_SYS_MATH_FUNCTION_64(sqrt)(x); +} +template +vtkm::Vec Sqrt(const vtkm::Vec x) { + vtkm::Vec result; + for (vtkm::IdComponent index = 0; index < N; index++) + { + result[index] = vtkm::Sqrt(x[index]); + } + return result; +} +template +VTKM_EXEC_CONT_EXPORT +vtkm::Vec Sqrt(const vtkm::Vec x) { + return vtkm::Vec(vtkm::Sqrt(x[0]), + vtkm::Sqrt(x[1]), + vtkm::Sqrt(x[2]), + vtkm::Sqrt(x[3])); +} +template +VTKM_EXEC_CONT_EXPORT +vtkm::Vec Sqrt(const vtkm::Vec x) { + return vtkm::Vec(vtkm::Sqrt(x[0]), + vtkm::Sqrt(x[1]), + vtkm::Sqrt(x[2])); +} +template +VTKM_EXEC_CONT_EXPORT +vtkm::Vec Sqrt(const vtkm::Vec x) { + return vtkm::Vec(vtkm::Sqrt(x[0]), + vtkm::Sqrt(x[1])); +} + + +} // namespace vtkm + +#endif //vtk_m_Math_h diff --git a/vtkm/Math.h.in b/vtkm/Math.h.in new file mode 100644 index 000000000..33150a9c8 --- /dev/null +++ b/vtkm/Math.h.in @@ -0,0 +1,109 @@ +//============================================================================= +// +// 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 2015 Sandia Corporation. +// Copyright 2015 UT-Battelle, LLC. +// Copyright 2015 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. +// +//============================================================================= +$# This file uses the pyexpander macro processing utility to build the +$# FunctionInterface facilities that use a variable number of arguments. +$# Information, documentation, and downloads for pyexpander can be found at: +$# +$# http://pyexpander.sourceforge.net/ +$# +$# To build the source code, execute the following (after installing +$# pyexpander, of course): +$# +$# expander.py Math.h.in > Math.h +$# +$# Ignore the following comment. It is meant for the generated file. +// **** DO NOT EDIT THIS FILE!!! **** +// This file is automatically generated by FunctionInterfaceDetailPre.h.in + +#ifndef vtk_m_Math_h +#define vtk_m_Math_h + +#include + +#ifndef VTKM_CUDA +#include +#endif + +#define VTKM_SYS_MATH_FUNCTION_32(func) func ## f +#define VTKM_SYS_MATH_FUNCTION_64(func) func + +$py( +def unary_function(name, type, expression): + return '''VTKM_EXEC_CONT_EXPORT +{1} {0}({1} x) {{ + return {2}; +}} +'''.format(name, type, expression) + +def unary_Vec_function(vtkmname): + return '''template +vtkm::Vec {0}(const vtkm::Vec x) {{ + vtkm::Vec result; + for (vtkm::IdComponent index = 0; index < N; index++) + {{ + result[index] = vtkm::{0}(x[index]); + }} + return result; +}} +template +VTKM_EXEC_CONT_EXPORT +vtkm::Vec {0}(const vtkm::Vec x) {{ + return vtkm::Vec(vtkm::{0}(x[0]), + vtkm::{0}(x[1]), + vtkm::{0}(x[2]), + vtkm::{0}(x[3])); +}} +template +VTKM_EXEC_CONT_EXPORT +vtkm::Vec {0}(const vtkm::Vec x) {{ + return vtkm::Vec(vtkm::{0}(x[0]), + vtkm::{0}(x[1]), + vtkm::{0}(x[2])); +}} +template +VTKM_EXEC_CONT_EXPORT +vtkm::Vec {0}(const vtkm::Vec x) {{ + return vtkm::Vec(vtkm::{0}(x[0]), + vtkm::{0}(x[1])); +}} +'''.format(vtkmname) + +def unary_math_function(vtkmname, sysname): + return unary_function(vtkmname, + 'vtkm::Float32', + 'VTKM_SYS_MATH_FUNCTION_32(' + sysname + ')(x)') + \ + unary_function(vtkmname, + 'vtkm::Float64', + 'VTKM_SYS_MATH_FUNCTION_64(' + sysname + ')(x)') + \ + unary_Vec_function(vtkmname) +) +$extend(unary_math_function) + +namespace vtkm { + +/// Compute the square root of \p x. +/// +$unary_math_function('Sqrt', 'sqrt') + +} // namespace vtkm + +#endif //vtk_m_Math_h