All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
tools.hpp
Go to the documentation of this file.
#ifndef VIENNACL_TOOLS_TOOLS_HPP_
#define VIENNACL_TOOLS_TOOLS_HPP_
/* =========================================================================
Copyright (c) 2010-2016, Institute for Microelectronics,
Institute for Analysis and Scientific Computing,
TU Wien.
Portions of this software are copyright by UChicago Argonne, LLC.
-----------------
ViennaCL - The Vienna Computing Library
-----------------
Project Head: Karl Rupp rupp@iue.tuwien.ac.at
(A list of authors and contributors can be found in the manual)
License: MIT (X11), see file LICENSE in the base directory
============================================================================= */
#include <string>
#include <fstream>
#include <sstream>
#include "viennacl/forwards.h"
#include "viennacl/tools/adapter.hpp"
#include <vector>
#include <map>
namespace viennacl
{
namespace tools
{
template<class NumericT, typename F, unsigned int AlignmentV>
struct MATRIX_ITERATOR_INCREMENTER<viennacl::row_iteration, viennacl::matrix<NumericT, F, AlignmentV> >
{
static void apply(const viennacl::matrix<NumericT, F, AlignmentV> & /*mat*/, unsigned int & row, unsigned int & /*col*/)
{
++row;
}
};
template<class NumericT, typename F, unsigned int AlignmentV>
struct MATRIX_ITERATOR_INCREMENTER<viennacl::col_iteration, viennacl::matrix<NumericT, F, AlignmentV> >
{
static void apply(const viennacl::matrix<NumericT, F, AlignmentV> & /*mat*/, unsigned int & /*row*/, unsigned int & col)
{
++col;
}
};
template<typename T>
struct CHECK_SCALAR_TEMPLATE_ARGUMENT
{
typedef typename T::ERROR_SCALAR_MUST_HAVE_TEMPLATE_ARGUMENT_FLOAT_OR_DOUBLE ResultType;
};
template<>
struct CHECK_SCALAR_TEMPLATE_ARGUMENT<float>
{
typedef float ResultType;
};
template<>
struct CHECK_SCALAR_TEMPLATE_ARGUMENT<double>
{
typedef double ResultType;
};
inline std::string read_text_from_file(const std::string & filename)
{
std::ifstream f(filename.c_str());
if (!f) return std::string();
std::stringstream result;
std::string tmp;
while (std::getline(f, tmp))
result << tmp << std::endl;
return result.str();
}
inline std::string str_replace(const std::string & text, std::string to_search, std::string to_replace)
{
std::string::size_type pos = 0;
std::string result;
std::string::size_type found;
while ( (found = text.find(to_search, pos)) != std::string::npos )
{
result.append(text.substr(pos,found-pos));
result.append(to_replace);
pos = found + to_search.length();
}
if (pos < text.length())
result.append(text.substr(pos));
return result;
}
template<class INT_TYPE>
INT_TYPE align_to_multiple(INT_TYPE to_reach, INT_TYPE base)
{
if (to_reach % base == 0) return to_reach;
return ((to_reach / base) + 1) * base;
}
template<class INT_TYPE>
INT_TYPE round_down_to_prevous_multiple(INT_TYPE to_reach, INT_TYPE base)
{
if (to_reach % base == 0) return to_reach;
return (to_reach / base) * base;
}
int inline find_and_replace(std::string & source, std::string const & find, std::string const & replace)
{
int num=0;
vcl_size_t fLen = find.size();
vcl_size_t rLen = replace.size();
for (vcl_size_t pos=0; (pos=source.find(find, pos))!=std::string::npos; pos+=rLen)
{
num++;
source.replace(pos, fLen, replace);
}
return num;
}
template<class InputIterator, class UnaryPredicate>
bool any_of (InputIterator first, InputIterator last, UnaryPredicate pred)
{
while (first!=last)
{
if (pred(*first)) return true;
++first;
}
return false;
}
inline std::string make_double_kernel(std::string const & source, std::string const & fp_extension)
{
std::stringstream ss;
ss << "#pragma OPENCL EXTENSION " << fp_extension << " : enable\n\n";
std::string result = ss.str();
result.append(str_replace(source, "float", "double"));
return result;
}
template<typename T>
struct CONST_REMOVER
{
typedef T ResultType;
};
template<typename T>
struct CONST_REMOVER<const T>
{
typedef T ResultType;
};
template<typename T>
struct CPU_SCALAR_TYPE_DEDUCER
{
//force compiler error if type cannot be deduced
//typedef T ResultType;
};
template<>
struct CPU_SCALAR_TYPE_DEDUCER< float >
{
typedef float ResultType;
};
template<>
struct CPU_SCALAR_TYPE_DEDUCER< double >
{
typedef double ResultType;
};
template<typename T>
struct CPU_SCALAR_TYPE_DEDUCER< viennacl::scalar<T> >
{
typedef T ResultType;
};
template<typename T, unsigned int A>
struct CPU_SCALAR_TYPE_DEDUCER< viennacl::vector<T, A> >
{
typedef T ResultType;
};
template<typename T, typename F, unsigned int A>
struct CPU_SCALAR_TYPE_DEDUCER< viennacl::matrix<T, F, A> >
{
typedef T ResultType;
};
template<typename T, typename F, unsigned int A>
struct CPU_SCALAR_TYPE_DEDUCER< viennacl::matrix_expression<const matrix<T, F, A>, const matrix<T, F, A>, op_trans> >
{
typedef T ResultType;
};
//
// Converts a scalar type when necessary unless it is a viennacl::scalar<> (typical use-case: convert user-provided floats to double (and vice versa) for OpenCL kernels)
//
template<typename HostScalarType>
viennacl::scalar<HostScalarType> const & promote_if_host_scalar(viennacl::scalar<HostScalarType> const & s) { return s; }
template<typename HostScalarType>
viennacl::scalar_expression<const viennacl::scalar<HostScalarType>,
const viennacl::scalar<HostScalarType>,
viennacl::op_flip_sign> const &
promote_if_host_scalar(viennacl::scalar_expression<const viennacl::scalar<HostScalarType>,
const viennacl::scalar<HostScalarType>,
viennacl::op_flip_sign> const & s) { return s; }
template<typename HostScalarType>
HostScalarType promote_if_host_scalar(float s) { return s; }
template<typename HostScalarType>
HostScalarType promote_if_host_scalar(double s) { return s; }
template<typename HostScalarType>
HostScalarType promote_if_host_scalar(long s) { return s; }
template<typename HostScalarType>
HostScalarType promote_if_host_scalar(unsigned long s) { return s; }
template<typename HostScalarType>
HostScalarType promote_if_host_scalar(int s) { return s; }
template<typename HostScalarType>
HostScalarType promote_if_host_scalar(unsigned int s) { return s; }
template<class T>
inline std::string to_string ( T const t )
{
std::stringstream ss;
ss << t;
return ss.str();
}
} //namespace tools
} //namespace viennacl
#endif
viennacl::tools::promote_if_host_scalar
viennacl::scalar< HostScalarType > const & promote_if_host_scalar(viennacl::scalar< HostScalarType > const &s)
Definition: tools.hpp:275
viennacl::scheduler::INT_TYPE
Definition: forwards.h:294
viennacl::tools::CONST_REMOVER
Removes the const qualifier from a type.
Definition: tools.hpp:204
viennacl::scalar
This class represents a single scalar value on the GPU and behaves mostly like a built-in scalar type...
Definition: forwards.h:227
adapter.hpp
Adapter classes for sparse matrices made of the STL type std::vector<std::map<SizeT, NumericT> >
forwards.h
This file provides the forward declarations for the main types used within ViennaCL.
viennacl::matrix
A dense matrix class.
Definition: forwards.h:375
viennacl::scalar_expression
A proxy for scalar expressions (e.g. from inner vector products)
Definition: forwards.h:230
viennacl::tools::any_of
bool any_of(InputIterator first, InputIterator last, UnaryPredicate pred)
Returns true if pred returns true for any of the elements in the range [first,last), and false otherwise.
Definition: tools.hpp:175
viennacl::tools::CHECK_SCALAR_TEMPLATE_ARGUMENT::ResultType
T::ERROR_SCALAR_MUST_HAVE_TEMPLATE_ARGUMENT_FLOAT_OR_DOUBLE ResultType
Definition: tools.hpp:65
viennacl::tools::CPU_SCALAR_TYPE_DEDUCER
Obtain the cpu scalar type from a type, including a GPU type like viennacl::scalar<T> ...
Definition: tools.hpp:225
viennacl::tools::find_and_replace
int find_and_replace(std::string &source, std::string const &find, std::string const &replace)
Replace in a source string a pattern by another.
Definition: tools.hpp:160
viennacl::tools::read_text_from_file
std::string read_text_from_file(const std::string &filename)
Reads a text from a file into a std::string.
Definition: tools.hpp:89
viennacl::vcl_size_t
std::size_t vcl_size_t
Definition: forwards.h:75
viennacl::row
vector_expression< const matrix_base< NumericT, F >, const unsigned int, op_row > row(const matrix_base< NumericT, F > &A, unsigned int i)
Definition: matrix.hpp:910
viennacl::tools::align_to_multiple
INT_TYPE align_to_multiple(INT_TYPE to_reach, INT_TYPE base)
Rounds an integer to the next multiple of another integer.
Definition: tools.hpp:133
viennacl::tools::make_double_kernel
std::string make_double_kernel(std::string const &source, std::string const &fp_extension)
Create a double precision kernel out of a single precision kernel.
Definition: tools.hpp:191
viennacl::tools::round_down_to_prevous_multiple
INT_TYPE round_down_to_prevous_multiple(INT_TYPE to_reach, INT_TYPE base)
Rounds an integer to the previous multiple of another integer.
Definition: tools.hpp:148
viennacl::op_flip_sign
A tag class representing sign flips (for scalars only. Vectors and matrices use the standard multipli...
Definition: forwards.h:223
viennacl::tools::CHECK_SCALAR_TEMPLATE_ARGUMENT
A guard that checks whether the floating point type of GPU types is either float or double...
Definition: tools.hpp:63
viennacl::tools::to_string
std::string to_string(T const t)
Definition: tools.hpp:304
viennacl::tools::MATRIX_ITERATOR_INCREMENTER::apply
static void apply(const MATRIXTYPE &, unsigned int &, unsigned int &)
Definition: forwards.h:621
viennacl::tools::str_replace
std::string str_replace(const std::string &text, std::string to_search, std::string to_replace)
Replaces all occurances of a substring by another stringstream.
Definition: tools.hpp:109
viennacl::tools::CONST_REMOVER::ResultType
T ResultType
Definition: tools.hpp:206