ViennaCL - The Vienna Computing Library  1.5.0
viennacl/forwards.h
Go to the documentation of this file.
00001 #ifndef VIENNACL_FORWARDS_H
00002 #define VIENNACL_FORWARDS_H
00003 
00004 /* =========================================================================
00005    Copyright (c) 2010-2013, Institute for Microelectronics,
00006                             Institute for Analysis and Scientific Computing,
00007                             TU Wien.
00008    Portions of this software are copyright by UChicago Argonne, LLC.
00009 
00010                             -----------------
00011                   ViennaCL - The Vienna Computing Library
00012                             -----------------
00013 
00014    Project Head:    Karl Rupp                   rupp@iue.tuwien.ac.at
00015 
00016    (A list of authors and contributors can be found in the PDF manual)
00017 
00018    License:         MIT (X11), see file LICENSE in the base directory
00019 ============================================================================= */
00020 
00021 
00036 //compatibility defines:
00037 #ifdef VIENNACL_HAVE_UBLAS
00038   #define VIENNACL_WITH_UBLAS
00039 #endif
00040 
00041 #ifdef VIENNACL_HAVE_EIGEN
00042   #define VIENNACL_WITH_EIGEN
00043 #endif
00044 
00045 #ifdef VIENNACL_HAVE_MTL4
00046   #define VIENNACL_WITH_MTL4
00047 #endif
00048 
00049 #include <cstddef>
00050 #include <cassert>
00051 #include <string>
00052 
00053 #include "viennacl/meta/enable_if.hpp"
00054 
00056 namespace viennacl
00057 {
00058   typedef std::size_t                                       vcl_size_t;
00059   typedef std::ptrdiff_t                                    vcl_ptrdiff_t;
00060 
00061 
00063   struct op_assign {};
00065   struct op_inplace_add {};
00067   struct op_inplace_sub {};
00068 
00070   struct op_add {};
00072   struct op_sub {};
00074   struct op_mult {};
00076   struct op_prod {};
00078   struct op_mat_mat_prod {};
00080   struct op_div {};
00082   struct op_pow {};
00083 
00085   template <typename OP>
00086   struct op_element_binary {};
00087 
00089   template <typename OP>
00090   struct op_element_unary {};
00091 
00093   struct op_abs {};
00095   struct op_acos {};
00097   struct op_asin {};
00099   struct op_atan {};
00101   struct op_atan2 {};
00103   struct op_ceil {};
00105   struct op_cos {};
00107   struct op_cosh {};
00109   struct op_exp {};
00111   struct op_fabs {};
00113   struct op_fdim {};
00115   struct op_floor {};
00117   struct op_fmax {};
00119   struct op_fmin {};
00121   struct op_fmod {};
00123   struct op_log {};
00125   struct op_log10 {};
00127   struct op_sin {};
00129   struct op_sinh {};
00131   struct op_sqrt {};
00133   struct op_tan {};
00135   struct op_tanh {};
00136 
00138   struct op_matrix_diag {};
00139 
00141   struct op_vector_diag {};
00142 
00144   struct op_row {};
00145 
00147   struct op_column {};
00148 
00150   struct op_inner_prod {};
00151 
00153   struct op_norm_1 {};
00154 
00156   struct op_norm_2 {};
00157 
00159   struct op_norm_inf {};
00160 
00162   struct op_norm_frobenius {};
00163 
00165   struct op_trans {};
00166 
00168   struct op_flip_sign {};
00169 
00170   //forward declaration of basic types:
00171   template<class TYPE>
00172   class scalar;
00173 
00174   template <typename LHS, typename RHS, typename OP>
00175   class scalar_expression;
00176 
00177   template <typename SCALARTYPE>
00178   class entry_proxy;
00179 
00180   template <typename LHS, typename RHS, typename OP>
00181   class vector_expression;
00182 
00183   template<class SCALARTYPE, unsigned int ALIGNMENT>
00184   class vector_iterator;
00185 
00186   template<class SCALARTYPE, unsigned int ALIGNMENT>
00187   class const_vector_iterator;
00188 
00189   template<typename SCALARTYPE>
00190   class implicit_vector_base;
00191 
00192   template <typename SCALARTYPE>
00193   class zero_vector;
00194 
00195   template <typename SCALARTYPE>
00196   class unit_vector;
00197 
00198   template <typename SCALARTYPE>
00199   class one_vector;
00200 
00201   template <typename SCALARTYPE>
00202   class scalar_vector;
00203 
00204   template<class SCALARTYPE, typename SizeType = vcl_size_t, typename DistanceType = vcl_ptrdiff_t>
00205   class vector_base;
00206 
00207   template<class SCALARTYPE, unsigned int ALIGNMENT = 1>
00208   class vector;
00209 
00210   template <typename ScalarT>
00211   class vector_tuple;
00212 
00213   //the following forwards are needed for GMRES
00214   template <typename SCALARTYPE, unsigned int ALIGNMENT, typename CPU_ITERATOR>
00215   void copy(CPU_ITERATOR const & cpu_begin,
00216             CPU_ITERATOR const & cpu_end,
00217             vector_iterator<SCALARTYPE, ALIGNMENT> gpu_begin);
00218 
00219   template <typename SCALARTYPE, unsigned int ALIGNMENT_SRC, unsigned int ALIGNMENT_DEST>
00220   void copy(const_vector_iterator<SCALARTYPE, ALIGNMENT_SRC> const & gpu_src_begin,
00221             const_vector_iterator<SCALARTYPE, ALIGNMENT_SRC> const & gpu_src_end,
00222             vector_iterator<SCALARTYPE, ALIGNMENT_DEST> gpu_dest_begin);
00223 
00224   template <typename SCALARTYPE, unsigned int ALIGNMENT_SRC, unsigned int ALIGNMENT_DEST>
00225   void copy(const_vector_iterator<SCALARTYPE, ALIGNMENT_SRC> const & gpu_src_begin,
00226             const_vector_iterator<SCALARTYPE, ALIGNMENT_SRC> const & gpu_src_end,
00227             const_vector_iterator<SCALARTYPE, ALIGNMENT_DEST> gpu_dest_begin);
00228 
00229   template <typename SCALARTYPE, unsigned int ALIGNMENT, typename CPU_ITERATOR>
00230   void fast_copy(const const_vector_iterator<SCALARTYPE, ALIGNMENT> & gpu_begin,
00231                  const const_vector_iterator<SCALARTYPE, ALIGNMENT> & gpu_end,
00232                  CPU_ITERATOR cpu_begin );
00233 
00234   template <typename CPU_ITERATOR, typename SCALARTYPE, unsigned int ALIGNMENT>
00235   void fast_copy(CPU_ITERATOR const & cpu_begin,
00236                   CPU_ITERATOR const & cpu_end,
00237                   vector_iterator<SCALARTYPE, ALIGNMENT> gpu_begin);
00238 
00239 
00241   struct row_major_tag {};
00243   struct column_major_tag {};
00244 
00246   struct row_major
00247   {
00248     typedef row_major_tag         orientation_category;
00249 
00256     static vcl_size_t mem_index(vcl_size_t i, vcl_size_t j, vcl_size_t /* num_rows */, vcl_size_t num_cols)
00257     {
00258       return i * num_cols + j;
00259     }
00260   };
00261 
00263   struct column_major
00264   {
00265     typedef column_major_tag         orientation_category;
00266 
00273     static vcl_size_t mem_index(vcl_size_t i, vcl_size_t j, vcl_size_t num_rows, vcl_size_t /* num_cols */)
00274     {
00275       return i + j * num_rows;
00276     }
00277   };
00278 
00279   struct row_iteration;
00280   struct col_iteration;
00281 
00282   template <typename LHS, typename RHS, typename OP>
00283   class matrix_expression;
00284 
00285   //
00286   // Matrix types:
00287   //
00288 
00289   template<class SCALARTYPE, typename F = row_major, typename SizeType = vcl_size_t, typename DistanceType = vcl_ptrdiff_t>
00290   class matrix_base;
00291 
00292   template <class SCALARTYPE, typename F = row_major, unsigned int ALIGNMENT = 1>
00293   class matrix;
00294 
00295   template<typename SCALARTYPE>
00296   class implicit_matrix_base;
00297 
00298   template <class SCALARTYPE>
00299   class identity_matrix;
00300 
00301   template <class SCALARTYPE>
00302   class zero_matrix;
00303 
00304   template <class SCALARTYPE>
00305   class scalar_matrix;
00306 
00307   template<class SCALARTYPE, unsigned int ALIGNMENT = 1>
00308   class compressed_matrix;
00309 
00310   template<class SCALARTYPE>
00311   class compressed_compressed_matrix;
00312 
00313 
00314   template<class SCALARTYPE, unsigned int ALIGNMENT = 128>
00315   class coordinate_matrix;
00316 
00317   template<class SCALARTYPE, unsigned int ALIGNMENT = 1>
00318   class ell_matrix;
00319 
00320   template<class SCALARTYPE, unsigned int ALIGNMENT = 1>
00321   class hyb_matrix;
00322 
00323   template<class SCALARTYPE, unsigned int ALIGNMENT = 1>
00324   class circulant_matrix;
00325 
00326   template<class SCALARTYPE, unsigned int ALIGNMENT = 1>
00327   class hankel_matrix;
00328 
00329   template<class SCALARTYPE, unsigned int ALIGNMENT = 1>
00330   class toeplitz_matrix;
00331 
00332   template<class SCALARTYPE, unsigned int ALIGNMENT = 1>
00333   class vandermonde_matrix;
00334 
00335   //
00336   // Proxies:
00337   //
00338   template <typename SizeType = vcl_size_t, typename DistanceType = std::ptrdiff_t>
00339   class basic_range;
00340 
00341   typedef basic_range<>  range;
00342 
00343   template <typename SizeType = vcl_size_t, typename DistanceType = std::ptrdiff_t>
00344   class basic_slice;
00345 
00346   typedef basic_slice<>  slice;
00347 
00348   template <typename VectorType>
00349   class vector_range;
00350 
00351   template <typename VectorType>
00352   class vector_slice;
00353 
00354   template <typename MatrixType>
00355   class matrix_range;
00356 
00357   template <typename MatrixType>
00358   class matrix_slice;
00359 
00360 
00362   template <typename T>
00363   struct is_cpu_scalar
00364   {
00365     enum { value = false };
00366   };
00367 
00369   template <typename T>
00370   struct is_scalar
00371   {
00372     enum { value = false };
00373   };
00374 
00376   template <typename T>
00377   struct is_flip_sign_scalar
00378   {
00379     enum { value = false };
00380   };
00381 
00383   template <typename T>
00384   struct is_any_scalar
00385   {
00386     enum { value = (is_scalar<T>::value || is_cpu_scalar<T>::value || is_flip_sign_scalar<T>::value )};
00387   };
00388 
00390   template<typename T>
00391   struct is_any_vector { enum { value = 0 }; };
00392 
00394   template<typename T>
00395   struct is_any_dense_matrix { enum { value = 0 }; };
00396 
00398   template <typename T>
00399   struct is_row_major
00400   {
00401     enum { value = false };
00402   };
00403 
00405   template <typename T>
00406   struct is_compressed_matrix
00407   {
00408     enum { value = false };
00409   };
00410 
00412   template <typename T>
00413   struct is_coordinate_matrix
00414   {
00415     enum { value = false };
00416   };
00417 
00419   template <typename T>
00420   struct is_ell_matrix
00421   {
00422     enum { value = false };
00423   };
00424 
00426   template <typename T>
00427   struct is_hyb_matrix
00428   {
00429     enum { value = false };
00430   };
00431 
00433   template <typename T>
00434   struct is_any_sparse_matrix
00435   {
00436     enum { value = false };
00437   };
00438 
00439 
00441   template <typename T>
00442   struct is_circulant_matrix
00443   {
00444     enum { value = false };
00445   };
00446 
00448   template <typename T>
00449   struct is_hankel_matrix
00450   {
00451     enum { value = false };
00452   };
00453 
00455   template <typename T>
00456   struct is_toeplitz_matrix
00457   {
00458     enum { value = false };
00459   };
00460 
00462   template <typename T>
00463   struct is_vandermonde_matrix
00464   {
00465     enum { value = false };
00466   };
00467 
00469   template <typename T>
00470   struct is_any_dense_structured_matrix
00471   {
00472     enum { value = viennacl::is_circulant_matrix<T>::value || viennacl::is_hankel_matrix<T>::value || viennacl::is_toeplitz_matrix<T>::value || viennacl::is_vandermonde_matrix<T>::value };
00473   };
00474 
00475 
00476   enum memory_types
00477   {
00478     MEMORY_NOT_INITIALIZED
00479     , MAIN_MEMORY
00480     , OPENCL_MEMORY
00481     , CUDA_MEMORY
00482   };
00483 
00485   class memory_exception : public std::exception
00486   {
00487   public:
00488     memory_exception() : message_() {}
00489     memory_exception(std::string message) : message_("ViennaCL: Internal memory error: " + message) {}
00490 
00491     virtual const char* what() const throw() { return message_.c_str(); }
00492 
00493     virtual ~memory_exception() throw() {}
00494   private:
00495     std::string message_;
00496   };
00497 
00498   class cuda_not_available_exception : public std::exception
00499   {
00500   public:
00501     cuda_not_available_exception() : message_("ViennaCL was compiled without CUDA support, but CUDA functionality required for this operation.") {}
00502 
00503     virtual const char* what() const throw() { return message_.c_str(); }
00504 
00505     virtual ~cuda_not_available_exception() throw() {}
00506   private:
00507     std::string message_;
00508   };
00509 
00510 
00511   class context;
00512 
00513   namespace tools
00514   {
00515     //helper for matrix row/col iterators
00516     //must be specialized for every viennacl matrix type
00518     template <typename ROWCOL, typename MATRIXTYPE>
00519     struct MATRIX_ITERATOR_INCREMENTER
00520     {
00521       typedef typename MATRIXTYPE::ERROR_SPECIALIZATION_FOR_THIS_MATRIX_TYPE_MISSING          ErrorIndicator;
00522 
00523       static void apply(const MATRIXTYPE & /*mat*/, unsigned int & /*row*/, unsigned int & /*col*/) {}
00524     };
00525   }
00526 
00527   namespace linalg
00528   {
00529 #if !defined(_MSC_VER) || defined(__CUDACC__)
00530 
00531     template<class SCALARTYPE, unsigned int ALIGNMENT>
00532     void convolve_i(viennacl::vector<SCALARTYPE, ALIGNMENT>& input1,
00533                     viennacl::vector<SCALARTYPE, ALIGNMENT>& input2,
00534                     viennacl::vector<SCALARTYPE, ALIGNMENT>& output);
00535 
00536     template <typename T>
00537     viennacl::vector_expression<const vector_base<T>, const vector_base<T>, op_element_binary<op_prod> >
00538     element_prod(vector_base<T> const & v1, vector_base<T> const & v2);
00539 
00540     template <typename T>
00541     viennacl::vector_expression<const vector_base<T>, const vector_base<T>, op_element_binary<op_div> >
00542     element_div(vector_base<T> const & v1, vector_base<T> const & v2);
00543 
00544 
00545 
00546     template <typename T>
00547     void inner_prod_impl(vector_base<T> const & vec1,
00548                          vector_base<T> const & vec2,
00549                          scalar<T> & result);
00550 
00551     template <typename LHS, typename RHS, typename OP, typename T>
00552     void inner_prod_impl(viennacl::vector_expression<LHS, RHS, OP> const & vec1,
00553                          vector_base<T> const & vec2,
00554                          scalar<T> & result);
00555 
00556     template <typename T, typename LHS, typename RHS, typename OP>
00557     void inner_prod_impl(vector_base<T> const & vec1,
00558                          viennacl::vector_expression<LHS, RHS, OP> const & vec2,
00559                          scalar<T> & result);
00560 
00561     template <typename LHS1, typename RHS1, typename OP1,
00562               typename LHS2, typename RHS2, typename OP2, typename T>
00563     void inner_prod_impl(viennacl::vector_expression<LHS1, RHS1, OP1> const & vec1,
00564                          viennacl::vector_expression<LHS2, RHS2, OP2> const & vec2,
00565                          scalar<T> & result);
00566 
00568 
00569     template <typename T>
00570     void inner_prod_cpu(vector_base<T> const & vec1,
00571                         vector_base<T> const & vec2,
00572                         T & result);
00573 
00574     template <typename LHS, typename RHS, typename OP, typename T>
00575     void inner_prod_cpu(viennacl::vector_expression<LHS, RHS, OP> const & vec1,
00576                         vector_base<T> const & vec2,
00577                         T & result);
00578 
00579     template <typename T, typename LHS, typename RHS, typename OP>
00580     void inner_prod_cpu(vector_base<T> const & vec1,
00581                         viennacl::vector_expression<LHS, RHS, OP> const & vec2,
00582                         T & result);
00583 
00584     template <typename LHS1, typename RHS1, typename OP1,
00585               typename LHS2, typename RHS2, typename OP2, typename S3>
00586     void inner_prod_cpu(viennacl::vector_expression<LHS1, RHS1, OP1> const & vec1,
00587                         viennacl::vector_expression<LHS2, RHS2, OP2> const & vec2,
00588                         S3 & result);
00589 
00590 
00591 
00592     //forward definition of norm_1_impl function
00593     template <typename T>
00594     void norm_1_impl(vector_base<T> const & vec, scalar<T> & result);
00595 
00596     //template <typename T, typename F>
00597     //void norm_1_impl(matrix_base<T, F> const & A, scalar<T> & result);
00598 
00599     template <typename LHS, typename RHS, typename OP, typename T>
00600     void norm_1_impl(viennacl::vector_expression<LHS, RHS, OP> const & vec,
00601                      scalar<T> & result);
00602 
00603 
00604     template <typename T>
00605     void norm_1_cpu(vector_base<T> const & vec,
00606                     T & result);
00607 
00608     //template <typename T, typename F>
00609     //void norm_1_cpu(matrix_base<T, F> const & vec,
00610     //                T & result);
00611 
00612     template <typename LHS, typename RHS, typename OP, typename S2>
00613     void norm_1_cpu(viennacl::vector_expression<LHS, RHS, OP> const & vec,
00614                     S2 & result);
00615 
00616     //forward definition of norm_2_impl function
00617     template <typename T>
00618     void norm_2_impl(vector_base<T> const & vec, scalar<T> & result);
00619 
00620     template <typename LHS, typename RHS, typename OP, typename T>
00621     void norm_2_impl(viennacl::vector_expression<LHS, RHS, OP> const & vec,
00622                      scalar<T> & result);
00623 
00624     template <typename T>
00625     void norm_2_cpu(vector_base<T> const & vec, T & result);
00626 
00627     template <typename LHS, typename RHS, typename OP, typename S2>
00628     void norm_2_cpu(viennacl::vector_expression<LHS, RHS, OP> const & vec,
00629                     S2 & result);
00630 
00631 
00632     //forward definition of norm_inf_impl function
00633     template <typename T>
00634     void norm_inf_impl(vector_base<T> const & vec, scalar<T> & result);
00635 
00636     //template <typename T, typename F>
00637     //void norm_inf_impl(matrix_base<T, F> const & vec, scalar<T> & result);
00638 
00639     template <typename LHS, typename RHS, typename OP, typename T>
00640     void norm_inf_impl(viennacl::vector_expression<LHS, RHS, OP> const & vec,
00641                       scalar<T> & result);
00642 
00643 
00644     template <typename T>
00645     void norm_inf_cpu(vector_base<T> const & vec, T & result);
00646 
00647     //template <typename T, typename F>
00648     //void norm_inf_cpu(matrix_base<T, F> const & vec, T & result);
00649 
00650     template <typename LHS, typename RHS, typename OP, typename S2>
00651     void norm_inf_cpu(viennacl::vector_expression<LHS, RHS, OP> const & vec,
00652                       S2 & result);
00653 
00654     template <typename T, typename F>
00655     void norm_frobenius_impl(matrix_base<T, F> const & vec, scalar<T> & result);
00656 
00657     template <typename T, typename F>
00658     void norm_frobenius_cpu(matrix_base<T, F> const & vec, T & result);
00659 
00660 
00661     template <typename T>
00662     vcl_size_t index_norm_inf(vector_base<T> const & vec);
00663 
00664     template <typename LHS, typename RHS, typename OP>
00665     vcl_size_t index_norm_inf(viennacl::vector_expression<LHS, RHS, OP> const & vec);
00666 
00667     //forward definition of prod_impl functions
00668 
00669     template <typename NumericT, typename F>
00670     void prod_impl(const matrix_base<NumericT, F> & mat,
00671                    const vector_base<NumericT> & vec,
00672                          vector_base<NumericT> & result);
00673 
00674     template <typename NumericT, typename F>
00675     void prod_impl(const matrix_expression< const matrix_base<NumericT, F>, const matrix_base<NumericT, F>, op_trans> & mat_trans,
00676                    const vector_base<NumericT> & vec,
00677                          vector_base<NumericT> & result);
00678 
00679     template<typename SparseMatrixType, class SCALARTYPE, unsigned int ALIGNMENT>
00680     typename viennacl::enable_if< viennacl::is_any_sparse_matrix<SparseMatrixType>::value,
00681                                   vector_expression<const SparseMatrixType,
00682                                                     const vector<SCALARTYPE, ALIGNMENT>,
00683                                                     op_prod >
00684                                  >::type
00685     prod_impl(const SparseMatrixType & mat,
00686               const vector<SCALARTYPE, ALIGNMENT> & vec);
00687 #endif
00688 
00689     namespace detail
00690     {
00691       enum row_info_types
00692       {
00693         SPARSE_ROW_NORM_INF = 0,
00694         SPARSE_ROW_NORM_1,
00695         SPARSE_ROW_NORM_2,
00696         SPARSE_ROW_DIAGONAL
00697       };
00698 
00699     }
00700 
00701 
00703     struct lower_tag
00704     {
00705       static const char * name() { return "lower"; }
00706     };      //lower triangular matrix
00708     struct upper_tag
00709     {
00710       static const char * name() { return "upper"; }
00711     };      //upper triangular matrix
00713     struct unit_lower_tag
00714     {
00715       static const char * name() { return "unit_lower"; }
00716     }; //unit lower triangular matrix
00718     struct unit_upper_tag
00719     {
00720       static const char * name() { return "unit_upper"; }
00721     }; //unit upper triangular matrix
00722 
00723     //preconditioner tags
00724     class ilut_tag;
00725 
00727     class no_precond
00728     {
00729       public:
00730         template <typename VectorType>
00731         void apply(VectorType &) const {}
00732     };
00733 
00734 
00735   } //namespace linalg
00736 
00737   //
00738   // More namespace comments to follow:
00739   //
00740 
00742   namespace backend
00743   {
00745     namespace cpu_ram
00746     {
00748       namespace detail {}
00749     }
00750 
00752     namespace cuda
00753     {
00755       namespace detail {}
00756     }
00757 
00759     namespace detail {}
00760 
00762     namespace opencl
00763     {
00765       namespace detail {}
00766     }
00767   }
00768 
00769 
00771   namespace detail
00772   {
00774     namespace fft
00775     {
00777       namespace FFT_DATA_ORDER {}
00778     }
00779   }
00780 
00781 
00783   namespace generator
00784   {
00786     namespace autotune {}
00787 
00789     namespace detail {}
00790 
00792     namespace profiles {}
00793 
00795     namespace utils {}
00796   }
00797 
00799   namespace io
00800   {
00802     namespace detail {}
00803 
00805     namespace tag {}
00806 
00808     namespace val {}
00809   }
00810 
00812   namespace linalg
00813   {
00815     namespace cuda
00816     {
00818       namespace detail {}
00819     }
00820 
00822     namespace detail
00823     {
00825       namespace amg {}
00826 
00828       namespace spai {}
00829     }
00830 
00832     namespace host_based
00833     {
00835       namespace detail {}
00836     }
00837 
00839     namespace kernels {}
00840 
00842     namespace opencl
00843     {
00845       namespace detail {}
00846 
00848       namespace kernels
00849       {
00851         namespace detail {}
00852       }
00853     }
00854   }
00855 
00857   namespace ocl {}
00858 
00860   namespace result_of {}
00861 
00863   namespace tools
00864   {
00866     namespace detail {}
00867   }
00868 
00870   namespace traits {}
00871 
00873   namespace scheduler
00874   {
00876     namespace detail {}
00877 
00879     namespace result_of {}
00880   }
00881 
00882 } //namespace viennacl
00883 
00884 #endif
00885