TSFRandomSparseMatrixBuilderImpl.hpp
Go to the documentation of this file.
00001 /* @HEADER@ */
00002 /* ***********************************************************************
00003 // 
00004 //           TSFExtended: Trilinos Solver Framework Extended
00005 //                 Copyright (2004) 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 Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // **********************************************************************/
00027  /* @HEADER@ */
00028 
00029 
00030 #ifndef RANDOMSPARSEMATRIX_BUILDER_IMPL_HPP
00031 #define RANDOMSPARSEMATRIX_BUILDER_IMPL_HPP
00032 
00033 #include "TSFRandomSparseMatrixBuilderDecl.hpp"
00034 #include "TSFIncrementallyConfigurableMatrixFactory.hpp"
00035 #include "TSFLoadableMatrix.hpp"
00036 
00037 
00038 using namespace TSFExtended;
00039 using namespace Teuchos;
00040 
00041 
00042 namespace TSFExtended
00043 {
00044 
00045 template <class Scalar> 
00046 inline RandomSparseMatrixBuilder<Scalar>
00047 ::RandomSparseMatrixBuilder(int nLocalRows, int nLocalCols,
00048   double onProcDensity,
00049   double offProcDensity,
00050   const VectorType<double>& type)
00051   : OperatorBuilder<double>(nLocalRows, nLocalCols, type), op_()
00052 {
00053   initOp(onProcDensity, offProcDensity);
00054 }
00055 
00056 
00057 template <class Scalar> 
00058 inline RandomSparseMatrixBuilder<Scalar>
00059 ::RandomSparseMatrixBuilder(const VectorSpace<Scalar>& d,
00060   const VectorSpace<Scalar>& r,
00061   double onProcDensity,
00062   double offProcDensity,
00063   const VectorType<double>& type)
00064   : OperatorBuilder<double>(d, r, type), op_()
00065 {
00066   initOp(onProcDensity, offProcDensity);
00067 }
00068 
00069 
00070 template <class Scalar> 
00071 inline void RandomSparseMatrixBuilder<Scalar>
00072 ::initOp(double onProcDensity,
00073   double offProcDensity)
00074 {
00075   int rank = MPIComm::world().getRank();
00076   int nProc = MPIComm::world().getNProc();
00077 
00078   RCP<MatrixFactory<double> > mFact 
00079     = this->vecType().createMatrixFactory(this->domain(), this->range());
00080 
00081   int colDimension = this->domain().dim();
00082   int rowDimension = this->range().dim();
00083   int numLocalCols = colDimension / nProc;
00084   int numLocalRows = rowDimension / nProc;
00085   int lowestLocalRow = numLocalRows * rank;
00086 
00087   int lowestLocalCol = numLocalCols * rank;
00088   int highestLocalCol = numLocalCols * (rank+1) - 1;
00089 
00090 
00091   IncrementallyConfigurableMatrixFactory* icmf 
00092     = dynamic_cast<IncrementallyConfigurableMatrixFactory*>(mFact.get());
00093   Array<Array<int> > colIndices(numLocalRows);
00094   for (int i=0; i<numLocalRows; i++)
00095   {
00096     int row = lowestLocalRow + i;
00097 
00098     Array<int>& cols = colIndices[i];
00099 
00100     while (cols.size() == 0)
00101     {
00102       for (int j=0; j<colDimension; j++)
00103       {
00104         double acceptProb;
00105         if (j >= lowestLocalCol && j <= highestLocalCol)
00106         {
00107           acceptProb = onProcDensity;
00108         }
00109         else
00110         {
00111           acceptProb = offProcDensity;
00112         }
00113         double p = 0.5*(ScalarTraits<double>::random() + 1.0);
00114 
00115         if (p < acceptProb)
00116         {
00117           cols.append(j);
00118         }
00119       }
00120       if (cols.size()>0)
00121       {
00122         icmf->initializeNonzerosInRow(row, colIndices[i].size(),
00123           &(colIndices[i][0]));
00124       }
00125     }
00126         
00127   }
00128   icmf->finalize();
00129       
00130   op_ = mFact->createMatrix();
00131       
00132   RCP<LoadableMatrix<double> > mat = op_.matrix();
00133 
00134   /* fill in with the Laplacian operator */
00135   for (int i=0; i<numLocalRows; i++)
00136   {
00137     int row = lowestLocalRow + i;
00138     const Array<int>& cols = colIndices[i];
00139     Array<Scalar> colVals(cols.size());
00140     for (int j=0; j<cols.size(); j++)
00141     {
00142       colVals[j] = ScalarTraits<Scalar>::random();
00143     }
00144     if (cols.size() > 0)
00145     {
00146       mat->addToRow(row, colIndices[i].size(), 
00147         &(colIndices[i][0]), &(colVals[0]));
00148     }
00149   }
00150 }
00151 }
00152 
00153 #endif

Site Contact