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 #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
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