AbstractLinAlgPack_ConvertToCSC.cpp

00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization
00005 //                  Copyright (2003) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 #include <assert.h>
00030 
00031 #include <algorithm>
00032 
00033 #include "AbstractLinAlgPack_ConvertToCSC.hpp"
00034 #include "AbstractLinAlgPack_MatrixOp.hpp"
00035 #include "DenseLinAlgPack_DMatrixClass.hpp"
00036 #include "DenseLinAlgPack_BLAS_Cpp.hpp"
00037 #include "DenseLinAlgPack_DVectorOp.hpp"
00038 #include "AbstractLinAlgPack_LinAlgOpPackHack.hpp"
00039 
00040 namespace LinAlgOpPack {
00041   using AbstractLinAlgPack::Mp_StM;
00042 }
00043 
00044 namespace AbstractLinAlgPack {
00045 
00046 void ConvertToSparseCompressedColumnPack::vector_insert_nonzeros(
00047     const DVectorSlice&       vs
00048   , value_type            alpha
00049   , size_type             row_offset
00050   , size_type             col_j
00051   , const IVector::value_type*    row_perm
00052   , size_type*            next_nz_in_col
00053   , FortranTypes::f_dbl_prec*     D_val
00054   , FortranTypes::f_int*        D_row_i     )
00055 {
00056   size_type ele = next_nz_in_col[ col_j - 1 ];
00057   next_nz_in_col[ col_j - 1 ] += vs.size();
00058   // Input the nonzero elements in D
00059   BLAS_Cpp::copy( vs.size(), vs.start_ptr(), vs.stride(), D_val + (ele - 1), 1 );
00060   BLAS_Cpp::scal( vs.size(), alpha, D_val + (ele - 1), 1 );
00061   // Set the row indices
00062   if(D_row_i) {
00063     D_row_i += (ele - 1);
00064     for(size_type i = 1; i <= vs.size(); ++i )
00065       *D_row_i++ = row_perm[ row_offset + i - 1 ];    
00066   }
00067 }
00068 
00069 size_type ConvertToSparseCompressedColumnPack::dense_num_in_column(
00070     size_type             rows
00071   , size_type             cols
00072   , BLAS_Cpp::Transp          trans
00073   , size_type             col_offset
00074   , const IVector::value_type*    col_perm
00075   , size_type*            num_in_col  )
00076 {
00077   if(trans == BLAS_Cpp::trans)
00078     std::swap(rows,cols);
00079   for( size_type j = 1; j <= cols; ++j )
00080     num_in_col[ col_perm[ col_offset + j - 1 ] - 1 ] += rows;
00081   return rows * cols;
00082 }
00083 
00084 void ConvertToSparseCompressedColumnPack::dense_insert_nonzeros(
00085     const DMatrixSlice&       gms
00086   , BLAS_Cpp::Transp          trans
00087   , value_type            alpha
00088   , size_type             row_offset
00089   , size_type             col_offset
00090   , const IVector::value_type*    row_perm
00091   , const IVector::value_type*    col_perm
00092   , size_type*            next_nz_in_col
00093   , FortranTypes::f_dbl_prec*     D_val
00094   , FortranTypes::f_int*        D_row_i     )
00095 {
00096   using DenseLinAlgPack::col;
00097   size_type cols = ( trans == BLAS_Cpp::no_trans ? gms.cols() : gms.rows() );
00098   for( size_type j = 1; j <= cols; ++j) {
00099     vector_insert_nonzeros( col( gms, trans, j ), alpha, row_offset
00100       , col_perm[ col_offset + j - 1 ], row_perm, next_nz_in_col, D_val, D_row_i );
00101   }
00102 }
00103 
00104 value_type ConvertToSparseCompressedColumnPack::dense_insert_scaled_nonzeros(
00105     const DMatrixSlice&       gms
00106   , BLAS_Cpp::Transp          trans
00107   , value_type            scaled_max_ele
00108   , size_type             row_offset
00109   , size_type             col_offset
00110   , const IVector::value_type*    row_perm
00111   , const IVector::value_type*    col_perm
00112   , size_type*            next_nz_in_col
00113   , FortranTypes::f_dbl_prec*     D_val
00114   , FortranTypes::f_int*        D_row_i     )
00115 {
00116   using DenseLinAlgPack::norm_inf;
00117   value_type alpha = 0;
00118   for( size_type j = 1; j <= gms.cols(); ++j )
00119     alpha = std::_MAX( alpha, norm_inf( gms.col(j) ) );
00120   // scaled_max_ele = max|alpha*A| = alpha * max|A| 
00121   alpha = scaled_max_ele / alpha;
00122   dense_insert_nonzeros( gms, trans, alpha, row_offset, col_offset, row_perm
00123     , col_perm, next_nz_in_col, D_val, D_row_i );
00124   return alpha;
00125 }
00126 
00127 size_type ConvertToSparseCompressedColumnPack::num_in_column(
00128     const MatrixOp&       m
00129   , BLAS_Cpp::Transp          trans
00130   , size_type             col_offset
00131   , const IVector::value_type*    col_perm
00132   , size_type*            num_in_col  )
00133 {
00134   const ConvertToCSC*
00135     conv_m = dynamic_cast<const ConvertToCSC*>( &m );
00136   if(conv_m)
00137     return conv_m->num_in_column( trans, col_offset, col_perm, num_in_col );
00138   else
00139     return dense_num_in_column( m.rows(), m.cols(), trans, col_offset, col_perm, num_in_col );     
00140 }
00141 
00142 void ConvertToSparseCompressedColumnPack::insert_nonzeros(
00143     const MatrixOp&       m
00144   , BLAS_Cpp::Transp          trans
00145   , value_type            alpha
00146   , size_type             row_offset
00147   , size_type             col_offset
00148   , const IVector::value_type*    row_perm
00149   , const IVector::value_type*    col_perm
00150   , size_type*            next_nz_in_col
00151   , FortranTypes::f_dbl_prec*     D_val
00152   , FortranTypes::f_int*        D_row_i     )
00153 {
00154   using LinAlgOpPack::assign;
00155   const ConvertToCSC*
00156     conv_m = dynamic_cast<const ConvertToCSC*>( &m );
00157   if(conv_m) {
00158     conv_m->insert_nonzeros( trans, alpha, row_offset, col_offset, row_perm
00159       , col_perm, next_nz_in_col, D_val, D_row_i );
00160   }
00161   else {
00162     DMatrix _m;
00163     assign( &_m, m, BLAS_Cpp::no_trans );
00164     dense_insert_nonzeros( _m, trans, alpha, row_offset, col_offset, row_perm
00165       , col_perm, next_nz_in_col, D_val, D_row_i );    
00166   }
00167 }
00168 
00169 value_type ConvertToSparseCompressedColumnPack::insert_scaled_nonzeros(
00170     const MatrixOp&       m
00171   , BLAS_Cpp::Transp          trans
00172   , value_type            scaled_max_ele
00173   , size_type             row_offset
00174   , size_type             col_offset
00175   , const IVector::value_type*    row_perm
00176   , const IVector::value_type*    col_perm
00177   , size_type*            next_nz_in_col
00178   , FortranTypes::f_dbl_prec*     D_val
00179   , FortranTypes::f_int*        D_row_i     )
00180 {
00181   using LinAlgOpPack::assign;
00182   const ConvertToCSC*
00183     conv_m = dynamic_cast<const ConvertToCSC*>( &m );
00184   if(conv_m) {
00185     return conv_m->insert_scaled_nonzeros( trans, scaled_max_ele, row_offset
00186       , col_offset, row_perm, col_perm, next_nz_in_col, D_val, D_row_i );
00187   }
00188   else {
00189     DMatrix _m;
00190     assign( &_m, m, BLAS_Cpp::no_trans );
00191     return dense_insert_scaled_nonzeros( _m, trans, scaled_max_ele, row_offset
00192       , col_offset, row_perm, col_perm, next_nz_in_col, D_val, D_row_i );    
00193   }
00194 }
00195 
00196 } // end namespace AbstractLinAlgPack

Generated on Thu Sep 18 12:33:50 2008 for AbstractLinAlgPack: C++ Interfaces For Vectors, Matrices And Related Linear Algebra Objects by doxygen 1.3.9.1