Skylark (Sketching Library)  0.1
/var/lib/jenkins/jobs/Skylark/workspace/algorithms/Krylov/iter_solver_op_CombBLAS.hpp
Go to the documentation of this file.
00001 #ifndef SKYLARK_ITER_SOLVER_OP_COMBBLAS_HPP
00002 #define SKYLARK_ITER_SOLVER_OP_COMBBLAS_HPP
00003 
00004 /*****
00005  *****    NOTE: THIS CLASS IS NOT USED ANYMORE!
00006  *****    It is kept until the useful code can be transfered
00007  *****    as functions in namespace base.
00008  *****/
00009 
00010 #include <functional>
00011 #include <algorithm>
00012 #include <CombBLAS.h>
00013 
00014 namespace skylark { namespace nla {
00015 
00016 template <typename IndexType, 
00017           typename ValueType>
00018 struct iter_solver_op_t <SpParMat<IndexType, 
00019                                   ValueType, 
00020                                   SpDCCols<IndexType, ValueType> >,
00021                          FullyDistMultiVec<IndexType, ValueType> > {
00023   template <int p, bool invert>
00024   struct pow_t {
00025     typedef ValueType argument_type;
00026     typedef ValueType result_type;
00027 
00028     static result_type apply(const argument_type& arg) { 
00029       return ((false==invert) ? pow(arg,p):pow(arg,1./p)); 
00030     }
00031 
00032     result_type operator()(const argument_type& arg) const { 
00033       return apply(arg); 
00034     }
00035   };
00036 
00037   struct scale_t {
00038     typedef ValueType argument_type;
00039     typedef ValueType result_type;
00040     
00041     const argument_type scaling_factor;
00042 
00043     scale_t (argument_type scaling_factor): scaling_factor(scaling_factor) {}
00044 
00045     result_type operator()(const argument_type& arg) const { 
00046       return scaling_factor*arg; 
00047     }
00048   };
00049 
00050   template <typename BinaryOperation>
00051   struct scaled_bin_op_t {
00052     typedef ValueType argument_type;
00053     typedef ValueType result_type;
00054     
00055     BinaryOperation bin_op;
00056     argument_type scale_arg_1;
00057     argument_type scale_arg_2;
00058 
00059     scaled_bin_op_t (argument_type scale_arg_1,
00060                      argument_type scale_arg_2) : scale_arg_1(scale_arg_1),
00061                                                   scale_arg_2(scale_arg_2) {}
00062 
00063     result_type operator()(const argument_type& arg_1,
00064                            const argument_type& arg_2) {
00065       return bin_op(scale_arg_1*arg_1, scale_arg_2*arg_2);
00066     }
00067   };
00068 
00070   typedef IndexType index_t; 
00071   typedef ValueType value_t;
00072   typedef SpDCCols<IndexType, ValueType> seq_matrix_t;
00073   typedef FullyDistMultiVec<IndexType, ValueType> mpi_multi_vector_t;
00074   typedef typename mpi_multi_vector_t::mpi_vector_t mpi_vector_t;
00075   typedef SpParMat<IndexType, 
00076                    ValueType, 
00077                    SpDCCols<IndexType, ValueType> > mpi_matrix_t;
00078   typedef PlusTimesSRing<ValueType,ValueType> semi_ring_t;
00079   typedef pow_t<2, false> square_t;
00080   typedef pow_t<2, true> square_root_t;
00081   typedef scaled_bin_op_t<std::plus<value_t> > ax_plus_by_t;
00082 
00083   /************************************************************************/
00084   /*                     TYPEDEFs -- if needed externally                 */
00085   /************************************************************************/
00086   typedef index_t index_type;
00087   typedef value_t value_type;
00088   typedef mpi_matrix_t matrix_type;
00089   typedef mpi_vector_t vector_type;
00090   typedef mpi_multi_vector_t multi_vector_type;
00091 
00092   /*************************************************************************/
00093   /*          Operations between a matrix and a multi-vector               */
00094   /*                            UNOPTIMIZED                                */
00095   /*************************************************************************/
00096   static mpi_matrix_t transpose (const mpi_matrix_t& A) { 
00097     mpi_matrix_t AT = A;
00098     AT.Transpose();
00099     return AT;
00100   }
00101 
00102   static void mat_vec (const mpi_matrix_t& A, 
00103                        const mpi_multi_vector_t& X,
00104                        mpi_multi_vector_t& AX) {
00106     index_t m = A.getnrow();
00107     index_t n = A.getncol();
00108 
00109     if (n != X.dim) { /* error */ }
00110 
00112     for (index_t i=0; i<X.size; ++i) AX[i] = SpMV<semi_ring_t>(A,X[i]);
00113   }
00114 
00115   template <typename RandomAccessContainer>
00116   static void ax_plus_by (const RandomAccessContainer& a,
00117                           mpi_multi_vector_t& X,
00118                           const RandomAccessContainer& b,
00119                           const mpi_multi_vector_t& Y) {
00121     if (X.dim != Y.dim) { /* error */ }
00122     if (X.size != Y.size) { /* error */ }
00123     if (a.size() != X.size) { /* error */ }
00124     if (b.size() != Y.size) { /* error */ }
00125 
00127     for (index_t i=0; i<X.size; ++i) 
00128       X[i].EWiseApply(Y[i], ax_plus_by_t(a[i], b[i]));
00129   }
00130 
00131   template <typename RandomAccessContainer>
00132   static void scale (const RandomAccessContainer& a,
00133                      mpi_multi_vector_t& X) {
00134     if (a.size() != X.size) { /* error */ }
00135     for (index_t i=0; i<X.size; ++i) X[i].Apply(scale_t(a[i]));
00136   }
00137 
00138   template <typename RandomAccessContainer>
00139   static void norm (const mpi_multi_vector_t& X,
00140                     RandomAccessContainer& norms) {
00141     for (index_t i=0; i<X.size; ++i) norms[i] = square_root_t::apply 
00142       (X[i].Reduce(std::plus<double>(), 0.0, square_t()));
00143   }
00144 
00145   static void get_dim (const mpi_multi_vector_t& X, index_t& M, index_t& N) {
00146     M = X.dim; 
00147     N = X.size;
00148   }
00149 
00150   static void get_dim (const mpi_matrix_t& A, index_t& M, index_t& N) {
00151     M = A.getnrow();
00152     N = A.getncol();
00153   }
00154 
00155   template <typename RandomAccessContainer>
00156   static void residual_norms (const mpi_matrix_t& A,
00157                               const mpi_multi_vector_t& B,
00158                               const mpi_multi_vector_t& X,
00159                               RandomAccessContainer& r_norms) {
00160     mpi_multi_vector_t R(B);
00161     
00163     mat_vec (A, X, R);
00164 
00166     RandomAccessContainer ONES(X.size, 1.0);
00167     RandomAccessContainer MINUS_ONES(X.size, -1.0);
00168     ax_plus_by (MINUS_ONES, R, ONES, B);
00169 
00171     norm (R, r_norms);
00172   }
00173 };
00174 
00175 } } 
00177 #endif  // SKYLARK_ITER_SOLVER_OP_COMBBLAS_HPP