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 #include <EpetraExt_ConfigDefs.h>
00030 #include <EpetraExt_MMHelpers.h>
00031 #include <Epetra_Comm.h>
00032 #include <Epetra_Map.h>
00033 #include <Epetra_CrsMatrix.h>
00034
00035 namespace EpetraExt {
00036
00037 CrsMatrixStruct::CrsMatrixStruct()
00038 : numRows(0), numEntriesPerRow(NULL), indices(NULL), values(NULL),
00039 remote(NULL), numRemote(0), rowMap(NULL), colMap(NULL),
00040 domainMap(NULL), importColMap(NULL), importMatrix(NULL)
00041 {
00042 }
00043
00044 CrsMatrixStruct::~CrsMatrixStruct()
00045 {
00046 deleteContents();
00047 }
00048
00049 void CrsMatrixStruct::deleteContents()
00050 {
00051 numRows = 0;
00052 delete [] numEntriesPerRow; numEntriesPerRow = NULL;
00053 delete [] indices; indices = NULL;
00054 delete [] values; values = NULL;
00055 delete [] remote; remote = NULL;
00056 numRemote = 0;
00057 delete importMatrix;
00058 }
00059
00060 int dumpCrsMatrixStruct(const CrsMatrixStruct& M)
00061 {
00062 cout << "proc " << M.rowMap->Comm().MyPID()<<endl;
00063 cout << "numRows: " << M.numRows<<endl;
00064 for(int i=0; i<M.numRows; ++i) {
00065 for(int j=0; j<M.numEntriesPerRow[i]; ++j) {
00066 if (M.remote[i]) {
00067 cout << " *"<<M.rowMap->GID(i)<<" "
00068 <<M.importColMap->GID(M.indices[i][j])<<" "<<M.values[i][j]<<endl;
00069 }
00070 else {
00071 cout << " "<<M.rowMap->GID(i)<<" "
00072 <<M.colMap->GID(M.indices[i][j])<<" "<<M.values[i][j]<<endl;
00073 }
00074 }
00075 }
00076 return(0);
00077 }
00078
00079 CrsWrapper_Epetra_CrsMatrix::CrsWrapper_Epetra_CrsMatrix(Epetra_CrsMatrix& epetracrsmatrix)
00080 : ecrsmat_(epetracrsmatrix)
00081 {
00082 }
00083
00084 CrsWrapper_Epetra_CrsMatrix::~CrsWrapper_Epetra_CrsMatrix()
00085 {
00086 }
00087
00088 const Epetra_Map&
00089 CrsWrapper_Epetra_CrsMatrix::RowMap() const
00090 {
00091 return ecrsmat_.RowMap();
00092 }
00093
00094 bool CrsWrapper_Epetra_CrsMatrix::Filled()
00095 {
00096 return ecrsmat_.Filled();
00097 }
00098
00099 int
00100 CrsWrapper_Epetra_CrsMatrix::InsertGlobalValues(int GlobalRow, int NumEntries, double* Values, int* Indices)
00101 {
00102 return ecrsmat_.InsertGlobalValues(GlobalRow, NumEntries, Values, Indices);
00103 }
00104
00105 int
00106 CrsWrapper_Epetra_CrsMatrix::SumIntoGlobalValues(int GlobalRow, int NumEntries, double* Values, int* Indices)
00107 {
00108 return ecrsmat_.SumIntoGlobalValues(GlobalRow, NumEntries, Values, Indices);
00109 }
00110
00111
00112
00113
00114 CrsWrapper_GraphBuilder::CrsWrapper_GraphBuilder(const Epetra_Map& emap)
00115 : graph_(),
00116 rowmap_(emap),
00117 max_row_length_(0)
00118 {
00119 int num_rows = emap.NumMyElements();
00120 int* rows = emap.MyGlobalElements();
00121
00122 for(int i=0; i<num_rows; ++i) {
00123 graph_[rows[i]] = new std::set<int>;
00124 }
00125 }
00126
00127 CrsWrapper_GraphBuilder::~CrsWrapper_GraphBuilder()
00128 {
00129 std::map<int,std::set<int>*>::iterator
00130 iter = graph_.begin(), iter_end = graph_.end();
00131 for(; iter!=iter_end; ++iter) {
00132 delete iter->second;
00133 }
00134
00135 graph_.clear();
00136 }
00137
00138 bool CrsWrapper_GraphBuilder::Filled()
00139 {
00140 return false;
00141 }
00142
00143 int
00144 CrsWrapper_GraphBuilder::InsertGlobalValues(int GlobalRow, int NumEntries, double* Values, int* Indices)
00145 {
00146 std::map<int,std::set<int>*>::iterator
00147 iter = graph_.find(GlobalRow);
00148
00149 if (iter == graph_.end()) return(-1);
00150
00151 std::set<int>& cols = *(iter->second);
00152
00153 for(int i=0; i<NumEntries; ++i) {
00154 cols.insert(Indices[i]);
00155 }
00156
00157 int row_length = cols.size();
00158 if (row_length > max_row_length_) max_row_length_ = row_length;
00159
00160 return(0);
00161 }
00162
00163 int
00164 CrsWrapper_GraphBuilder::SumIntoGlobalValues(int GlobalRow, int NumEntries, double* Values, int* Indices)
00165 {
00166 return InsertGlobalValues(GlobalRow, NumEntries, Values, Indices);
00167 }
00168
00169 std::map<int,std::set<int>*>&
00170 CrsWrapper_GraphBuilder::get_graph()
00171 {
00172 return graph_;
00173 }
00174
00175 void insert_matrix_locations(CrsWrapper_GraphBuilder& graphbuilder,
00176 Epetra_CrsMatrix& C)
00177 {
00178 int max_row_length = graphbuilder.get_max_row_length();
00179 std::vector<int> indices(max_row_length);
00180 int* indices_ptr = &indices[0];
00181 std::vector<double> zeros(max_row_length, 0.0);
00182 double* zeros_ptr = &zeros[0];
00183
00184 std::map<int,std::set<int>*>& graph = graphbuilder.get_graph();
00185
00186 std::map<int,std::set<int>*>::iterator
00187 iter = graph.begin(), iter_end = graph.end();
00188
00189 for(; iter!=iter_end; ++iter) {
00190 int row = iter->first;
00191 std::set<int>& cols = *(iter->second);
00192 int num_entries = cols.size();
00193
00194 std::set<int>::iterator
00195 col_iter = cols.begin(), col_end = cols.end();
00196 for(int j=0; col_iter!=col_end; ++col_iter, ++j) {
00197 indices_ptr[j] = *col_iter;
00198 }
00199
00200 C.InsertGlobalValues(row, num_entries, zeros_ptr, indices_ptr);
00201 }
00202 }
00203
00204 }
00205