ViennaCL - The Vienna Computing Library  1.5.2
viennacl/linalg/detail/spai/spai-static.hpp
Go to the documentation of this file.
00001 #ifndef VIENNACL_LINALG_DETAIL_SPAI_SPAI_STATIC_HPP
00002 #define VIENNACL_LINALG_DETAIL_SPAI_SPAI_STATIC_HPP
00003 
00004 /* =========================================================================
00005    Copyright (c) 2010-2014, 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 
00027 #include <utility>
00028 #include <iostream>
00029 #include <fstream>
00030 #include <string>
00031 #include <algorithm>
00032 #include <vector>
00033 #include <math.h>
00034 #include <map>
00035 //#include "spai-dynamic.hpp"
00036 #include "boost/numeric/ublas/vector.hpp"
00037 #include "boost/numeric/ublas/matrix.hpp"
00038 #include "boost/numeric/ublas/matrix_proxy.hpp"
00039 #include "boost/numeric/ublas/vector_proxy.hpp"
00040 #include "boost/numeric/ublas/storage.hpp"
00041 #include "boost/numeric/ublas/io.hpp"
00042 #include "boost/numeric/ublas/lu.hpp"
00043 #include "boost/numeric/ublas/triangular.hpp"
00044 #include "boost/numeric/ublas/matrix_expression.hpp"
00045 // ViennaCL includes
00046 #include "viennacl/linalg/prod.hpp"
00047 #include "viennacl/matrix.hpp"
00048 #include "viennacl/compressed_matrix.hpp"
00049 #include "viennacl/linalg/sparse_matrix_operations.hpp"
00050 #include "viennacl/linalg/matrix_operations.hpp"
00051 #include "viennacl/scalar.hpp"
00052 #include "viennacl/linalg/cg.hpp"
00053 #include "viennacl/linalg/inner_prod.hpp"
00054 
00055 //#include "boost/numeric/ublas/detail/matrix_assign.hpp"
00056 
00057 namespace viennacl
00058 {
00059   namespace linalg
00060   {
00061     namespace detail
00062     {
00063       namespace spai
00064       {
00065 
00070         template <typename SizeType>
00071         bool isInIndexSet(const std::vector<SizeType>& J, SizeType ind)
00072         {
00073           return (std::find(J.begin(), J.end(), ind) != J.end());
00074         }
00075 
00076 
00077 
00078         /********************************* STATIC SPAI FUNCTIONS******************************************/
00079 
00085         template <typename VectorType, typename SparseVectorType>
00086         void fanOutVector(const VectorType& m_in, const std::vector<unsigned int>& J, SparseVectorType& m)
00087         {
00088           unsigned int  cnt = 0;
00089           for (vcl_size_t i = 0; i < J.size(); ++i)
00090             m[J[i]] = m_in(cnt++);
00091         }
00097         template <typename MatrixType, typename VectorType>
00098         void backwardSolve(const MatrixType& R, const VectorType& y, VectorType& x)
00099         {
00100           for (long i = static_cast<long>(R.size2())-1; i >= 0 ; i--)
00101           {
00102             x(i) = y(i);
00103             for (vcl_size_t j = i+1; j < R.size2(); ++j)
00104                 x(i) -= R(i,j)*x(j);
00105 
00106             x(i) /= R(i,i);
00107           }
00108         }
00114         template <typename VectorType, typename ScalarType>
00115         void projectI(const std::vector<unsigned int>& I, VectorType& y, unsigned int ind)
00116         {
00117           for(vcl_size_t i = 0; i < I.size(); ++i)
00118           {
00119             //y.resize(y.size()+1);
00120             if(I[i] == ind)
00121               y(i) = static_cast<ScalarType>(1.0);
00122             else
00123               y(i) = static_cast<ScalarType>(0.0);
00124           }
00125         }
00126 
00131         template <typename SparseVectorType>
00132         void buildColumnIndexSet(const SparseVectorType& v, std::vector<unsigned int>& J)
00133         {
00134             //typedef typename VectorType::value_type ScalarType;
00135             //unsigned int tmp_v;
00136             for(typename SparseVectorType::const_iterator vec_it = v.begin(); vec_it != v.end(); ++vec_it)
00137             {
00138                 //tmp_v = vec_it->first;
00139                 J.push_back(vec_it->first);
00140             }
00141             std::sort(J.begin(), J.end());
00142         }
00143 
00148         template <typename SparseMatrixType>
00149         void initPreconditioner(const SparseMatrixType& A, SparseMatrixType& M)
00150         {
00151           typedef typename SparseMatrixType::value_type ScalarType;
00152           M.resize(A.size1(), A.size2(), false);
00153           for(typename SparseMatrixType::const_iterator1 row_it = A.begin1(); row_it!= A.end1(); ++row_it)
00154           {
00155             //
00156             for(typename SparseMatrixType::const_iterator2 col_it = row_it.begin(); col_it != row_it.end(); ++col_it)
00157             {
00158               M(col_it.index1(),col_it.index2()) = static_cast<ScalarType>(1);
00159             }
00160           }
00161         }
00162 
00168         template <typename SparseVectorType>
00169         void projectRows(const std::vector<SparseVectorType>& A_v_c, const std::vector<unsigned int>& J, std::vector<unsigned int>& I)
00170         {
00171           for(vcl_size_t i = 0; i < J.size(); ++i)
00172           {
00173             for(typename SparseVectorType::const_iterator col_it = A_v_c[J[i]].begin(); col_it!=A_v_c[J[i]].end(); ++col_it)
00174             {
00175               if(!isInIndexSet(I, col_it->first))
00176                 I.push_back(col_it->first);
00177             }
00178           }
00179           std::sort(I.begin(), I.end());
00180         }
00181 
00182 
00183       } //namespace spai
00184     } //namespace detail
00185   } //namespace linalg
00186 } //namespace viennacl
00187 
00188 #endif