00001 #ifndef VIENNACL_TOOLS_TOOLS_HPP_
00002 #define VIENNACL_TOOLS_TOOLS_HPP_
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00024 #include <string>
00025 #include <fstream>
00026 #include <sstream>
00027 #include "viennacl/forwards.h"
00028 #include "viennacl/tools/adapter.hpp"
00029
00030
00031 #ifdef VIENNACL_HAVE_UBLAS
00032 #include <boost/numeric/ublas/matrix_sparse.hpp>
00033 #include <boost/numeric/ublas/matrix.hpp>
00034 #endif
00035
00036 #ifdef VIENNACL_HAVE_EIGEN
00037 #include <Eigen/Core>
00038 #include <Eigen/Sparse>
00039 #endif
00040
00041 #ifdef VIENNACL_HAVE_MTL4
00042 #include <boost/numeric/mtl/mtl.hpp>
00043 #endif
00044
00045 #include <vector>
00046 #include <map>
00047
00048 namespace viennacl
00049 {
00050 namespace tools
00051 {
00052
00054 template <class SCALARTYPE, typename F, unsigned int ALIGNMENT>
00055 struct MATRIX_ITERATOR_INCREMENTER<viennacl::row_iteration, viennacl::matrix<SCALARTYPE, F, ALIGNMENT> >
00056 {
00057 static void apply(const viennacl::matrix<SCALARTYPE, F, ALIGNMENT> & mat, unsigned int & row, unsigned int & col) { ++row; }
00058 };
00059
00060 template <class SCALARTYPE, typename F, unsigned int ALIGNMENT>
00061 struct MATRIX_ITERATOR_INCREMENTER<viennacl::col_iteration, viennacl::matrix<SCALARTYPE, F, ALIGNMENT> >
00062 {
00063 static void apply(const viennacl::matrix<SCALARTYPE, F, ALIGNMENT> & mat, unsigned int & row, unsigned int & col) { ++col; }
00064 };
00065
00066
00068 template <typename T>
00069 struct CHECK_SCALAR_TEMPLATE_ARGUMENT
00070 {
00071 typedef typename T::ERROR_SCALAR_MUST_HAVE_TEMPLATE_ARGUMENT_FLOAT_OR_DOUBLE ResultType;
00072 };
00073
00074 template <>
00075 struct CHECK_SCALAR_TEMPLATE_ARGUMENT<float>
00076 {
00077 typedef float ResultType;
00078 };
00079
00080 template <>
00081 struct CHECK_SCALAR_TEMPLATE_ARGUMENT<double>
00082 {
00083 typedef double ResultType;
00084 };
00085
00086
00087
00093 inline std::string readTextFromFile(const std::string & filename)
00094 {
00095 std::ifstream f(filename.c_str());
00096 if (!f) return std::string();
00097
00098 std::stringstream result;
00099 std::string tmp;
00100 while (std::getline(f, tmp))
00101 result << tmp << std::endl;
00102
00103 return result.str();
00104 }
00105
00113 inline std::string strReplace(const std::string & text, std::string to_search, std::string to_replace)
00114 {
00115 std::string::size_type pos = 0;
00116 std::string result;
00117 std::string::size_type found;
00118 while( (found = text.find(to_search, pos)) != std::string::npos )
00119 {
00120 result.append(text.substr(pos,found-pos));
00121 result.append(to_replace);
00122 pos = found + to_search.length();
00123 }
00124 if (pos < text.length())
00125 result.append(text.substr(pos));
00126 return result;
00127 }
00128
00136 template <class INT_TYPE>
00137 INT_TYPE roundUpToNextMultiple(INT_TYPE to_reach, INT_TYPE base)
00138 {
00139 if (to_reach % base == 0) return to_reach;
00140 return ((to_reach / base) + 1) * base;
00141 }
00142
00143
00150 inline std::string make_double_kernel(std::string const & source, std::string platform_info)
00151
00152 {
00153 std::stringstream ss;
00154 if (platform_info.compare(0, 8, "Advanced") == 0)
00155 ss << "#pragma OPENCL EXTENSION cl_amd_fp64 : enable\n\n";
00156 else
00157 ss << "#pragma OPENCL EXTENSION cl_khr_fp64 : enable\n\n";
00158
00159 std::string result = ss.str();
00160 result.append(strReplace(source, "float", "double"));
00161 return result;
00162 }
00163
00164
00166 template <typename T>
00167 struct CONST_REMOVER
00168 {
00169 typedef T ResultType;
00170 };
00171
00172 template <typename T>
00173 struct CONST_REMOVER<const T>
00174 {
00175 typedef T ResultType;
00176 };
00177
00178
00184 template <typename LHS, typename RHS>
00185 struct VECTOR_EXTRACTOR_IMPL
00186 {
00187 typedef typename LHS::ERROR_COULD_NOT_EXTRACT_VECTOR_INFORMATION_FROM_VECTOR_EXPRESSION ResultType;
00188 };
00189
00190 template <typename LHS, typename ScalarType, unsigned int A>
00191 struct VECTOR_EXTRACTOR_IMPL<LHS, viennacl::vector<ScalarType, A> >
00192 {
00193 typedef viennacl::vector<ScalarType, A> ResultType;
00194 };
00195
00196 template <typename RHS, typename ScalarType, unsigned int A>
00197 struct VECTOR_EXTRACTOR_IMPL<viennacl::vector<ScalarType, A>, RHS>
00198 {
00199 typedef viennacl::vector<ScalarType, A> ResultType;
00200 };
00201
00202
00203 template <typename ScalarType, unsigned int A>
00204 struct VECTOR_EXTRACTOR_IMPL<viennacl::vector<ScalarType, A>, viennacl::vector<ScalarType, A> >
00205 {
00206 typedef viennacl::vector<ScalarType, A> ResultType;
00207 };
00208
00209 template <typename LHS, typename RHS>
00210 struct VECTOR_EXTRACTOR
00211 {
00212 typedef typename VECTOR_EXTRACTOR_IMPL<typename CONST_REMOVER<LHS>::ResultType,
00213 typename CONST_REMOVER<RHS>::ResultType>::ResultType ResultType;
00214 };
00215
00222 template <typename LHS, typename RHS, typename OP>
00223 struct VECTOR_SIZE_DEDUCER
00224 {
00225
00226 };
00227
00228
00229 template <typename ScalarType, unsigned int A, typename RHS>
00230 struct VECTOR_SIZE_DEDUCER<const viennacl::vector<ScalarType, A>, RHS, viennacl::op_prod>
00231 {
00232 static size_t size(const viennacl::vector<ScalarType, A> & lhs,
00233 const RHS & rhs) { return lhs.size(); }
00234 };
00235
00236 template <typename ScalarType, unsigned int A, typename RHS>
00237 struct VECTOR_SIZE_DEDUCER<const viennacl::vector<ScalarType, A>, RHS, viennacl::op_div>
00238 {
00239 static size_t size(const viennacl::vector<ScalarType, A> & lhs,
00240 const RHS & rhs) { return lhs.size(); }
00241 };
00242
00243
00244 template <typename ScalarType, typename F, unsigned int Amat, unsigned int A>
00245 struct VECTOR_SIZE_DEDUCER<const viennacl::matrix<ScalarType, F, Amat>, const viennacl::vector<ScalarType, A>, viennacl::op_prod>
00246 {
00247 static size_t size(const viennacl::matrix<ScalarType, F, Amat> & lhs,
00248 const viennacl::vector<ScalarType, A> & rhs) { return lhs.size1(); }
00249 };
00250
00251 template <typename ScalarType, unsigned int Amat, unsigned int A>
00252 struct VECTOR_SIZE_DEDUCER<const viennacl::circulant_matrix<ScalarType, Amat>, const viennacl::vector<ScalarType, A>, viennacl::op_prod>
00253 {
00254 static size_t size(const viennacl::circulant_matrix<ScalarType, Amat> & lhs,
00255 const viennacl::vector<ScalarType, A> & rhs) { return lhs.size1(); }
00256 };
00257
00258 template <typename ScalarType, unsigned int Amat, unsigned int A>
00259 struct VECTOR_SIZE_DEDUCER<const viennacl::compressed_matrix<ScalarType, Amat>, const viennacl::vector<ScalarType, A>, viennacl::op_prod>
00260 {
00261 static size_t size(const viennacl::compressed_matrix<ScalarType, Amat> & lhs,
00262 const viennacl::vector<ScalarType, A> & rhs) { return lhs.size1(); }
00263 };
00264
00265 template <typename ScalarType, unsigned int Amat, unsigned int A>
00266 struct VECTOR_SIZE_DEDUCER<const viennacl::coordinate_matrix<ScalarType, Amat>, const viennacl::vector<ScalarType, A>, viennacl::op_prod>
00267 {
00268 static size_t size(const viennacl::coordinate_matrix<ScalarType, Amat> & lhs,
00269 const viennacl::vector<ScalarType, A> & rhs) { return lhs.size1(); }
00270 };
00271
00272
00273 template <typename ScalarType, typename F, unsigned int Amat, unsigned int A>
00274 struct VECTOR_SIZE_DEDUCER<const viennacl::matrix_expression< const viennacl::matrix<ScalarType, F, Amat>,
00275 const viennacl::matrix<ScalarType, F, Amat>,
00276 op_trans>,
00277 const viennacl::vector<ScalarType, A>,
00278 viennacl::op_prod>
00279 {
00280 static size_t size(const viennacl::matrix_expression< const viennacl::matrix<ScalarType, F, Amat>,
00281 const viennacl::matrix<ScalarType, F, Amat>,
00282 op_trans> & lhs,
00283 const viennacl::vector<ScalarType, A> & rhs) { return lhs.lhs().size2(); }
00284 };
00285
00286
00287
00288
00289
00294 template <typename T>
00295 struct CPU_SCALAR_TYPE_DEDUCER
00296 {
00297
00298
00299 };
00300
00301 template <>
00302 struct CPU_SCALAR_TYPE_DEDUCER< float >
00303 {
00304 typedef float ResultType;
00305 };
00306
00307 template <>
00308 struct CPU_SCALAR_TYPE_DEDUCER< double >
00309 {
00310 typedef double ResultType;
00311 };
00312
00313 template <typename T>
00314 struct CPU_SCALAR_TYPE_DEDUCER< viennacl::scalar<T> >
00315 {
00316 typedef T ResultType;
00317 };
00318
00319 template <typename T, unsigned int A>
00320 struct CPU_SCALAR_TYPE_DEDUCER< viennacl::vector<T, A> >
00321 {
00322 typedef T ResultType;
00323 };
00324
00325 template <typename T, typename F, unsigned int A>
00326 struct CPU_SCALAR_TYPE_DEDUCER< viennacl::matrix<T, F, A> >
00327 {
00328 typedef T ResultType;
00329 };
00330
00331
00332 template <typename T, typename F, unsigned int A>
00333 struct CPU_SCALAR_TYPE_DEDUCER< viennacl::matrix_expression<const matrix<T, F, A>, const matrix<T, F, A>, op_trans> >
00334 {
00335 typedef T ResultType;
00336 };
00337
00338
00339 }
00340 }
00341
00342
00343 #endif