Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
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
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