Commit c07301a9 authored by Robert Maynard's avatar Robert Maynard

Add in primitive types to vtkm (Id, Scalar, Tuple).

This includes the configure scripts to setup if you want a 32bit or
64bit build.
parent ad0bc833
//============================================================================
// 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 2014 Sandia Corporation.
// Copyright 2014 UT-Battelle, LLC.
// Copyright 2014. 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.
//============================================================================
#ifndef vtkm_TypeTraits_h
#define vtkm_TypeTraits_h
#include <vtkm/Types.h>
namespace vtkm {
/// Tag used to identify types that store real (floating-point) numbers. A
/// TypeTraits class will typedef this class to NumericTag if it stores real
/// numbers (or vectors of real numbers).
///
struct TypeTraitsRealTag {};
/// Tag used to identify types that store integer numbers. A TypeTraits class
/// will typedef this class to NumericTag if it stores integer numbers (or
/// vectors of integers).
///
struct TypeTraitsIntegerTag {};
/// Tag used to identify 0 dimensional types (scalars). Scalars can also be
/// treated like vectors when used with VectorTraits. A TypeTraits class will
/// typedef this class to DimensionalityTag.
///
struct TypeTraitsScalarTag {};
/// Tag used to identify 1 dimensional types (vectors). A TypeTraits class will
/// typedef this class to DimensionalityTag.
///
struct TypeTraitsVectorTag {};
template<typename T> struct TypeTraits;
#ifdef VTKM_DOXYGEN_ONLY
/// The TypeTraits class provides helpful compile-time information about the
/// basic types used in VTKm (and a few others for convienience). The majority
/// of TypeTraits contents are typedefs to tags that can be used to easily
/// override behavior of called functions.
///
template<typename T>
class TypeTraits {
typedef int tag_type; // Shut up, test compile.
public:
/// \brief A tag to determing whether the type is integer or real.
///
/// This tag is either TypeTraitsRealTag or TypeTraitsIntegerTag.
typedef tag_type NumericTag;
/// \brief A tag to determine whether the type has multiple components.
///
/// This tag is either TypeTraitsScalarTag or TypeTraitsVectorTag. Scalars can
/// also be treated as vectors.
typedef tag_type DimensionalityTag;
};
#endif //VTKM_DOXYGEN_ONLY
// Const types should have the same traits as their non-const counterparts.
//
template<typename T>
struct TypeTraits<const T> : TypeTraits<T>
{ };
#define VTKM_BASIC_REAL_TYPE(T) \
template<> struct TypeTraits<T> { \
typedef TypeTraitsRealTag NumericTag; \
typedef TypeTraitsScalarTag DimensionalityTag; \
}
#define VTKM_BASIC_INTEGER_TYPE(T) \
template<> struct TypeTraits<T> { \
typedef TypeTraitsIntegerTag NumericTag; \
typedef TypeTraitsScalarTag DimensionalityTag; \
}
/// Traits for basic C++ types.
///
VTKM_BASIC_REAL_TYPE(float);
VTKM_BASIC_REAL_TYPE(double);
VTKM_BASIC_INTEGER_TYPE(char);
VTKM_BASIC_INTEGER_TYPE(unsigned char);
VTKM_BASIC_INTEGER_TYPE(short);
VTKM_BASIC_INTEGER_TYPE(unsigned short);
VTKM_BASIC_INTEGER_TYPE(int);
VTKM_BASIC_INTEGER_TYPE(unsigned int);
#if VTKM_SIZE_LONG == 8
VTKM_BASIC_INTEGER_TYPE(long);
VTKM_BASIC_INTEGER_TYPE(unsigned long);
#elif VTKM_SIZE_LONG_LONG == 8
VTKM_BASIC_INTEGER_TYPE(long long);
VTKM_BASIC_INTEGER_TYPE(unsigned long long);
#else
#error No implementation for 64-bit integer traits.
#endif
#undef VTKM_BASIC_REAL_TYPE
#undef VTKM_BASIC_INTEGER_TYPE
#define VTKM_VECTOR_TYPE(T, NTag) \
template<> struct TypeTraits<T> { \
typedef NTag NumericTag; \
typedef TypeTraitsVectorTag DimensionalityTag; \
}
/// Traits for vector types.
///
VTKM_VECTOR_TYPE(vtkm::Id3, TypeTraitsIntegerTag);
VTKM_VECTOR_TYPE(vtkm::Vector2, TypeTraitsRealTag);
VTKM_VECTOR_TYPE(vtkm::Vector3, TypeTraitsRealTag);
VTKM_VECTOR_TYPE(vtkm::Vector4, TypeTraitsRealTag);
#undef VTKM_VECTOR_TYPE
/// Traits for tuples.
///
template<typename T, int Size> struct TypeTraits<vtkm::Tuple<T, Size> > {
typedef typename TypeTraits<T>::NumericTag NumericTag;
typedef TypeTraitsVectorTag DimensionalityTag;
};
} // namespace vtkm
#endif //vtkm_TypeTraits_h
//============================================================================
// 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 2014 Sandia Corporation.
// Copyright 2014 UT-Battelle, LLC.
// Copyright 2014. 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.
//============================================================================
#ifndef vtkm_Types_h
#define vtkm_Types_h
#include <vtkm/internal/Configure.h>
#include <vtkm/internal/ExportMacros.h>
/*!
* \namespace vtkm
* \brief VTKm Toolkit.
*
* vtkm is the namespace for the VTKm Toolkit. It contains other sub namespaces,
* as well as basic data types and functions callable from all components in VTKm
* toolkit.
*
* \namespace vtkm::cont
* \brief VTKm Control Environment.
*
* vtkm::cont defines the publicly accessible API for the VTKm Control
* Environment. Users of the VTKm Toolkit can use this namespace to access the
* Control Environment.
*
* \namespace vtkm::cuda
* \brief CUDA implementation.
*
* vtkm::cuda includes the code to implement the VTKm for CUDA-based platforms.
*
* \namespace vtkm::cuda::cont
* \brief CUDA implementation for Control Environment.
*
* vtkm::cuda::cont includes the code to implement the VTKm Control Environment
* for CUDA-based platforms.
*
* \namespace vtkm::cuda::exec
* \brief CUDA implementation for Execution Environment.
*
* vtkm::cuda::exec includes the code to implement the VTKm Execution Environment
* for CUDA-based platforms.
*
* \namespace vtkm::exec
* \brief VTKm Execution Environment.
*
* vtkm::exec defines the publicly accessible API for the VTKm Execution
* Environment. Worklets typically use classes/apis defined within this
* namespace alone.
*
* \namespace vtkm::internal
* \brief VTKm Internal Environment
*
* vtkm::internal defines API which is internal and subject to frequent
* change. This should not be used for projects using VTKm. Instead it servers
* are a reference for the developers of VTKm.
*
* \namespace vtkm::math
* \brief Utility math functions
*
* vtkm::math defines the publicly accessible API for Utility Math functions.
*
* \namespace vtkm::testing
* \brief Internal testing classes
*
*/
namespace vtkm
{
//*****************************************************************************
// Typedefs for basic types.
//*****************************************************************************
/// Alignment requirements are prescribed by CUDA on device (Table B-1 in NVIDIA
/// CUDA C Programming Guide 4.0)
namespace internal {
#if VTKM_SIZE_INT == 4
typedef int Int32Type;
typedef unsigned int UInt32Type;
#else
#error Could not find a 32-bit integer.
#endif
#if VTKM_SIZE_LONG == 8
typedef long Int64Type;
typedef unsigned long UInt64Type;
#elif VTKM_SIZE_LONG_LONG == 8
typedef long long Int64Type;
typedef unsigned long long UInt64Type;
#else
#error Could not find a 64-bit integer.
#endif
//-----------------------------------------------------------------------------
template<int Size>
struct equals
{
template<typename T>
VTKM_EXEC_CONT_EXPORT bool operator()(const T& a, const T& b) const
{
return equals<Size-1>()(a,b) && a[Size-1] == b[Size-1];
}
};
template<>
struct equals<1>
{
template<typename T>
VTKM_EXEC_CONT_EXPORT bool operator()(const T& a, const T& b) const
{
return a[0] == b[0];
}
};
template<>
struct equals<2>
{
template<typename T>
VTKM_EXEC_CONT_EXPORT bool operator()(const T& a, const T& b) const
{
return a[0] == b[0] && a[1] == b[1];
}
};
template<>
struct equals<3>
{
template<typename T>
VTKM_EXEC_CONT_EXPORT bool operator()(const T& a, const T& b) const
{
return a[0] == b[0] && a[1] == b[1] && a[2] == b[2];
}
};
template<int Size>
struct assign_scalar_to_vector
{
template<typename VectorType, typename ComponentType>
VTKM_EXEC_CONT_EXPORT
void operator()(VectorType &dest, const ComponentType &src)
{
assign_scalar_to_vector<Size-1>()(dest, src);
dest[Size-1] = src;
}
};
template<>
struct assign_scalar_to_vector<1>
{
template<typename VectorType, typename ComponentType>
VTKM_EXEC_CONT_EXPORT
void operator()(VectorType &dest, const ComponentType &src)
{
dest[0] = src;
}
};
template<>
struct assign_scalar_to_vector<2>
{
template<typename VectorType, typename ComponentType>
VTKM_EXEC_CONT_EXPORT
void operator()(VectorType &dest, const ComponentType &src)
{
dest[0] = src; dest[1] = src;
}
};
template<>
struct assign_scalar_to_vector<3>
{
template<typename VectorType, typename ComponentType>
VTKM_EXEC_CONT_EXPORT
void operator()(VectorType &dest, const ComponentType &src)
{
dest[0] = src; dest[1] = src; dest[2] = src;
}
};
template<int Size>
struct copy_vector
{
template<typename T1, typename T2>
VTKM_EXEC_CONT_EXPORT void operator()(T1 &dest, const T2 &src)
{
copy_vector<Size-1>()(dest, src);
dest[Size-1] = src[Size-1];
}
};
template<>
struct copy_vector<1>
{
template<typename T1, typename T2>
VTKM_EXEC_CONT_EXPORT void operator()(T1 &dest, const T2 &src)
{
dest[0] = src[0];
}
};
template<>
struct copy_vector<2>
{
template<typename T1, typename T2>
VTKM_EXEC_CONT_EXPORT void operator()(T1 &dest, const T2 &src)
{
dest[0] = src[0]; dest[1] = src[1];
}
};
template<>
struct copy_vector<3>
{
template<typename T1, typename T2>
VTKM_EXEC_CONT_EXPORT void operator()(T1 &dest, const T2 &src)
{
dest[0] = src[0]; dest[1] = src[1]; dest[2] = src[2];
}
};
} // namespace internal
//-----------------------------------------------------------------------------
#if VTKM_SIZE_ID == 4
/// Represents an ID.
typedef internal::Int32Type Id __attribute__ ((aligned(VTKM_SIZE_ID)));
#elif VTKM_SIZE_ID == 8
/// Represents an ID.
typedef internal::Int64Type Id __attribute__ ((aligned(VTKM_SIZE_ID)));
#else
#error Unknown Id Size
#endif
#ifdef VTKM_USE_DOUBLE_PRECISION
/// Scalar corresponds to a floating point number.
typedef double Scalar __attribute__ ((aligned(VTKM_SIZE_SCALAR)));
#else //VTKM_USE_DOUBLE_PRECISION
/// Scalar corresponds to a floating point number.
typedef float Scalar __attribute__ ((aligned(VTKM_SIZE_SCALAR)));
#endif //VTKM_USE_DOUBLE_PRECISION
//-----------------------------------------------------------------------------
/// Tuple corresponds to a Size-tuple of type T
template<typename T, int Size>
class Tuple {
public:
typedef T ComponentType;
static const int NUM_COMPONENTS=Size;
VTKM_EXEC_CONT_EXPORT Tuple(){}
VTKM_EXEC_CONT_EXPORT explicit Tuple(const ComponentType& value)
{
for(int i=0; i < NUM_COMPONENTS;++i)
{
this->Components[i]=value;
}
}
VTKM_EXEC_CONT_EXPORT explicit Tuple(const ComponentType* values)
{
for(int i=0; i < NUM_COMPONENTS;++i)
{
this->Components[i]=values[i];
}
}
VTKM_EXEC_CONT_EXPORT
Tuple(const Tuple<ComponentType, Size> &src)
{
for (int i = 0; i < NUM_COMPONENTS; i++)
{
this->Components[i] = src[i];
}
}
VTKM_EXEC_CONT_EXPORT
Tuple<ComponentType, Size> &operator=(const Tuple<ComponentType, Size> &src)
{
for (int i = 0; i < NUM_COMPONENTS; i++)
{
this->Components[i] = src[i];
}
return *this;
}
VTKM_EXEC_CONT_EXPORT const ComponentType &operator[](int idx) const {
return this->Components[idx];
}
VTKM_EXEC_CONT_EXPORT ComponentType &operator[](int idx) {
return this->Components[idx];
}
VTKM_EXEC_CONT_EXPORT
bool operator==(const Tuple<T,NUM_COMPONENTS> &other) const
{
bool same = true;
for (int componentIndex=0; componentIndex<NUM_COMPONENTS; componentIndex++)
{
same &= (this->Components[componentIndex] == other[componentIndex]);
}
return same;
}
VTKM_EXEC_CONT_EXPORT
bool operator<(const Tuple<T,NUM_COMPONENTS> &other) const
{
for(vtkm::Id i=0; i < NUM_COMPONENTS; ++i)
{
//ignore equals as that represents check next value
if(this->Components[i] < other[i])
return true;
else if(other[i] < this->Components[i])
return false;
} //if all same we are not less
return false;
}
VTKM_EXEC_CONT_EXPORT
bool operator!=(const Tuple<T,NUM_COMPONENTS> &other) const
{
return !(this->operator==(other));
}
protected:
ComponentType Components[NUM_COMPONENTS];
};
//-----------------------------------------------------------------------------
// Specializations for common tuple sizes (with special names).
template<typename T>
class Tuple<T,2>{
public:
typedef T ComponentType;
static const int NUM_COMPONENTS = 2;
VTKM_EXEC_CONT_EXPORT Tuple(){}
VTKM_EXEC_CONT_EXPORT explicit Tuple(const ComponentType& value)
{
internal::assign_scalar_to_vector<NUM_COMPONENTS>()(this->Components,value);
}
VTKM_EXEC_CONT_EXPORT explicit Tuple(const ComponentType* values)
{
internal::copy_vector<NUM_COMPONENTS>()(this->Components, values);
}
VTKM_EXEC_CONT_EXPORT Tuple(ComponentType x, ComponentType y) {
this->Components[0] = x;
this->Components[1] = y;
}
VTKM_EXEC_CONT_EXPORT
Tuple(const Tuple<ComponentType, NUM_COMPONENTS> &src)
{
internal::copy_vector<NUM_COMPONENTS>()(this->Components, src.Components);
}
VTKM_EXEC_CONT_EXPORT
Tuple<ComponentType, NUM_COMPONENTS> &
operator=(const Tuple<ComponentType, NUM_COMPONENTS> &src)
{
internal::copy_vector<NUM_COMPONENTS>()(this->Components, src.Components);
return *this;
}
VTKM_EXEC_CONT_EXPORT const ComponentType &operator[](int idx) const {
return this->Components[idx];
}
VTKM_EXEC_CONT_EXPORT ComponentType &operator[](int idx) {
return this->Components[idx];
}
VTKM_EXEC_CONT_EXPORT
bool operator==(const Tuple<T,NUM_COMPONENTS> &other) const
{
return internal::equals<NUM_COMPONENTS>()(*this, other);
}
VTKM_EXEC_CONT_EXPORT
bool operator!=(const Tuple<T,NUM_COMPONENTS> &other) const
{
return !(this->operator==(other));
}
VTKM_EXEC_CONT_EXPORT
bool operator<(const Tuple<T,NUM_COMPONENTS> &other) const
{
return( (this->Components[0] < other[0]) ||
(!(other[0] < this->Components[0]) && (this->Components[1] < other[1]))
);
}
protected:
ComponentType Components[NUM_COMPONENTS];
};
/// Vector2 corresponds to a 2-tuple
typedef vtkm::Tuple<vtkm::Scalar,2>
Vector2 __attribute__ ((aligned(VTKM_ALIGNMENT_TWO_SCALAR)));
/// Id2 corresponds to a 2-dimensional index
typedef vtkm::Tuple<vtkm::Id,2> Id2 __attribute__ ((aligned(VTKM_SIZE_ID)));
template<typename T>
class Tuple<T,3>{
public:
typedef T ComponentType;
static const int NUM_COMPONENTS = 3;
VTKM_EXEC_CONT_EXPORT Tuple(){}
VTKM_EXEC_CONT_EXPORT explicit Tuple(const ComponentType& value)
{
internal::assign_scalar_to_vector<NUM_COMPONENTS>()(this->Components,value);
}
VTKM_EXEC_CONT_EXPORT explicit Tuple(const ComponentType* values)
{
internal::copy_vector<NUM_COMPONENTS>()(this->Components, values);
}
VTKM_EXEC_CONT_EXPORT
Tuple(ComponentType x, ComponentType y, ComponentType z) {
this->Components[0] = x;
this->Components[1] = y;
this->Components[2] = z;
}
VTKM_EXEC_CONT_EXPORT
Tuple(const Tuple<ComponentType, NUM_COMPONENTS> &src)
{
internal::copy_vector<NUM_COMPONENTS>()(this->Components, src.Components);
}
VTKM_EXEC_CONT_EXPORT
Tuple<ComponentType, NUM_COMPONENTS> &
operator=(const Tuple<ComponentType, NUM_COMPONENTS> &src)
{
internal::copy_vector<NUM_COMPONENTS>()(this->Components, src.Components);
return *this;
}
VTKM_EXEC_CONT_EXPORT const ComponentType &operator[](int idx) const {
return this->Components[idx];
}
VTKM_EXEC_CONT_EXPORT ComponentType &operator[](int idx) {
return this->Components[idx];
}
VTKM_EXEC_CONT_EXPORT
bool operator==(const Tuple<T,NUM_COMPONENTS> &other) const
{
return internal::equals<NUM_COMPONENTS>()(*this, other);
}
VTKM_EXEC_CONT_EXPORT
bool operator!=(const Tuple<T,NUM_COMPONENTS> &other) const
{
return !(this->operator==(other));
}
VTKM_EXEC_CONT_EXPORT
bool operator<(const Tuple<T,NUM_COMPONENTS> &other) const
{
return((this->Components[0] < other[0]) ||
( !(other[0] < this->Components[0]) &&
(this->Components[1] < other[1])) ||
( !(other[0] < this->Components[0]) &&
!(other[1] < this->Components[1]) &&
(this->Components[