PlayaRandomSparseMatrixBuilderImpl.hpp
Go to the documentation of this file.
00001 /* @HEADER@ */
00002 // ************************************************************************
00003 // 
00004 //                 Playa: Programmable Linear Algebra
00005 //                 Copyright 2012 Sandia Corporation
00006 // 
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00008 // the U.S. Government retains certain rights in this software.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Kevin Long (kevin.long@ttu.edu)
00038 // 
00039 
00040 /* @HEADER@ */
00041 
00042 
00043 #ifndef RANDOMSPARSEMATRIX_BUILDER_IMPL_HPP
00044 #define RANDOMSPARSEMATRIX_BUILDER_IMPL_HPP
00045 
00046 #include "PlayaRandomSparseMatrixBuilderDecl.hpp"
00047 #include "PlayaIncrementallyConfigurableMatrixFactory.hpp"
00048 #include "PlayaLoadableMatrix.hpp"
00049 
00050 
00051 using namespace Playa;
00052 using namespace Teuchos;
00053 
00054 
00055 namespace Playa
00056 {
00057 
00058 template <class Scalar> 
00059 inline RandomSparseMatrixBuilder<Scalar>
00060 ::RandomSparseMatrixBuilder(int nLocalRows, int nLocalCols,
00061   double onProcDensity,
00062   double offProcDensity,
00063   const VectorType<double>& type)
00064   : OperatorBuilder<double>(nLocalRows, nLocalCols, type), op_()
00065 {
00066   initOp(onProcDensity, offProcDensity);
00067 }
00068 
00069 
00070 template <class Scalar> 
00071 inline RandomSparseMatrixBuilder<Scalar>
00072 ::RandomSparseMatrixBuilder(const VectorSpace<Scalar>& d,
00073   const VectorSpace<Scalar>& r,
00074   double onProcDensity,
00075   double offProcDensity,
00076   const VectorType<double>& type)
00077   : OperatorBuilder<double>(d, r, type), op_()
00078 {
00079   initOp(onProcDensity, offProcDensity);
00080 }
00081 
00082 
00083 template <class Scalar> 
00084 inline void RandomSparseMatrixBuilder<Scalar>
00085 ::initOp(double onProcDensity,
00086   double offProcDensity)
00087 {
00088   int rank = MPIComm::world().getRank();
00089   int nProc = MPIComm::world().getNProc();
00090 
00091   RCP<MatrixFactory<double> > mFact 
00092     = this->vecType().createMatrixFactory(this->domain(), this->range());
00093 
00094   int colDimension = this->domain().dim();
00095   int rowDimension = this->range().dim();
00096   int numLocalCols = colDimension / nProc;
00097   int numLocalRows = rowDimension / nProc;
00098   int lowestLocalRow = numLocalRows * rank;
00099 
00100   int lowestLocalCol = numLocalCols * rank;
00101   int highestLocalCol = numLocalCols * (rank+1) - 1;
00102 
00103 
00104   IncrementallyConfigurableMatrixFactory* icmf 
00105     = dynamic_cast<IncrementallyConfigurableMatrixFactory*>(mFact.get());
00106   Array<Array<int> > colIndices(numLocalRows);
00107   for (int i=0; i<numLocalRows; i++)
00108   {
00109     int row = lowestLocalRow + i;
00110 
00111     Array<int>& cols = colIndices[i];
00112 
00113     while (cols.size() == 0)
00114     {
00115       for (int j=0; j<colDimension; j++)
00116       {
00117         double acceptProb;
00118         if (j >= lowestLocalCol && j <= highestLocalCol)
00119         {
00120           acceptProb = onProcDensity;
00121         }
00122         else
00123         {
00124           acceptProb = offProcDensity;
00125         }
00126         double p = 0.5*(ScalarTraits<double>::random() + 1.0);
00127 
00128         if (p < acceptProb)
00129         {
00130           cols.append(j);
00131         }
00132       }
00133       if (cols.size()>0)
00134       {
00135         icmf->initializeNonzerosInRow(row, colIndices[i].size(),
00136           &(colIndices[i][0]));
00137       }
00138     }
00139         
00140   }
00141   icmf->finalize();
00142       
00143   op_ = mFact->createMatrix();
00144       
00145   RCP<LoadableMatrix<double> > mat = op_.matrix();
00146 
00147   /* fill in with the Laplacian operator */
00148   for (int i=0; i<numLocalRows; i++)
00149   {
00150     int row = lowestLocalRow + i;
00151     const Array<int>& cols = colIndices[i];
00152     Array<Scalar> colVals(cols.size());
00153     for (int j=0; j<cols.size(); j++)
00154     {
00155       colVals[j] = ScalarTraits<Scalar>::random();
00156     }
00157     if (cols.size() > 0)
00158     {
00159       mat->addToRow(row, colIndices[i].size(), 
00160         &(colIndices[i][0]), &(colVals[0]));
00161     }
00162   }
00163 }
00164 }
00165 
00166 #endif

Site Contact