|
EpetraExt Development
|
00001 //@HEADER 00002 // *********************************************************************** 00003 // 00004 // EpetraExt: Epetra Extended - Linear Algebra Services Package 00005 // Copyright (2001) 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 #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 if (max_row_length < 1) return; 00180 00181 std::vector<int> indices(max_row_length); 00182 int* indices_ptr = &indices[0]; 00183 std::vector<double> zeros(max_row_length, 0.0); 00184 double* zeros_ptr = &zeros[0]; 00185 00186 std::map<int,std::set<int>*>& graph = graphbuilder.get_graph(); 00187 00188 std::map<int,std::set<int>*>::iterator 00189 iter = graph.begin(), iter_end = graph.end(); 00190 00191 for(; iter!=iter_end; ++iter) { 00192 int row = iter->first; 00193 std::set<int>& cols = *(iter->second); 00194 int num_entries = cols.size(); 00195 00196 std::set<int>::iterator 00197 col_iter = cols.begin(), col_end = cols.end(); 00198 for(int j=0; col_iter!=col_end; ++col_iter, ++j) { 00199 indices_ptr[j] = *col_iter; 00200 } 00201 00202 C.InsertGlobalValues(row, num_entries, zeros_ptr, indices_ptr); 00203 } 00204 } 00205 00206 }//namespace EpetraExt 00207
1.7.4