10 #include "Teko_BlockingEpetra.hpp"
12 #include "Epetra_IntVector.h"
13 #include "Epetra_LocalMap.h"
35 const MapPair buildSubMap(
const std::vector<int> &gid,
const Epetra_Comm &comm) {
36 Teuchos::RCP<Epetra_Map> gidMap = rcp(
new Epetra_Map(-1, gid.size(), &gid[0], 0, comm));
37 Teuchos::RCP<Epetra_Map> contigMap = rcp(
new Epetra_Map(-1, gid.size(), 0, comm));
39 return std::make_pair(gidMap, contigMap);
51 const ImExPair buildExportImport(
const Epetra_Map &baseMap,
const MapPair &maps) {
52 return std::make_pair(rcp(
new Epetra_Import(*maps.first, baseMap)),
53 rcp(
new Epetra_Export(*maps.first, baseMap)));
63 void buildSubVectors(
const std::vector<MapPair> &maps,
64 std::vector<RCP<Epetra_MultiVector> > &vectors,
int count) {
65 std::vector<MapPair>::const_iterator mapItr;
68 for (mapItr = maps.begin(); mapItr != maps.end(); mapItr++) {
70 RCP<Epetra_MultiVector> mv = rcp(
new Epetra_MultiVector(*(*mapItr).second, count));
71 vectors.push_back(mv);
82 void one2many(std::vector<RCP<Epetra_MultiVector> > &many,
const Epetra_MultiVector &single,
83 const std::vector<RCP<Epetra_Import> > &subImport) {
85 std::vector<RCP<Epetra_MultiVector> >::const_iterator vecItr;
86 std::vector<RCP<Epetra_Import> >::const_iterator impItr;
89 for (vecItr = many.begin(), impItr = subImport.begin(); vecItr != many.end();
92 RCP<Epetra_MultiVector> destVec = *vecItr;
95 const Epetra_BlockMap &globalMap = (*impItr)->TargetMap();
98 Epetra_MultiVector importVector(View, globalMap, destVec->Values(), destVec->Stride(),
99 destVec->NumVectors());
102 importVector.Import(single, **impItr, Insert);
116 void many2one(Epetra_MultiVector &one,
const std::vector<RCP<const Epetra_MultiVector> > &many,
117 const std::vector<RCP<Epetra_Export> > &subExport) {
119 std::vector<RCP<const Epetra_MultiVector> >::const_iterator vecItr;
120 std::vector<RCP<Epetra_Export> >::const_iterator expItr;
123 for (vecItr = many.begin(), expItr = subExport.begin(); vecItr != many.end();
124 ++vecItr, ++expItr) {
126 RCP<const Epetra_MultiVector> srcVec = *vecItr;
129 const Epetra_BlockMap &globalMap = (*expItr)->SourceMap();
132 Epetra_MultiVector exportVector(View, globalMap, srcVec->Values(), srcVec->Stride(),
133 srcVec->NumVectors());
134 one.Export(exportVector, **expItr, Insert);
143 RCP<Epetra_IntVector> getSubBlockColumnGIDs(
const Epetra_CrsMatrix &A,
const MapPair &mapPair) {
144 RCP<const Epetra_Map> blkGIDMap = mapPair.first;
145 RCP<const Epetra_Map> blkContigMap = mapPair.second;
148 Epetra_IntVector rIndex(A.RowMap(),
true);
149 for (
int i = 0; i < A.NumMyRows(); i++) {
151 int lid = blkGIDMap->LID(A.GRID(i));
153 rIndex[i] = blkContigMap->GID(lid);
159 Epetra_Import
import(A.ColMap(), A.RowMap());
160 RCP<Epetra_IntVector> cIndex = rcp(
new Epetra_IntVector(A.ColMap(),
true));
161 cIndex->Import(rIndex,
import, Insert);
167 RCP<Epetra_CrsMatrix> buildSubBlock(
int i,
int j,
const Epetra_CrsMatrix &A,
168 const std::vector<MapPair> &subMaps) {
170 int numVarFamily = subMaps.size();
172 TEUCHOS_ASSERT(i >= 0 && i < numVarFamily);
173 TEUCHOS_ASSERT(j >= 0 && j < numVarFamily);
175 const Epetra_Map &gRowMap = *subMaps[i].first;
176 const Epetra_Map &rowMap = *subMaps[i].second;
177 const Epetra_Map &colMap = *subMaps[j].second;
179 const RCP<Epetra_IntVector> plocal2ContigGIDs = getSubBlockColumnGIDs(A, subMaps[j]);
180 Epetra_IntVector &local2ContigGIDs = *plocal2ContigGIDs;
182 RCP<Epetra_CrsMatrix> mat = rcp(
new Epetra_CrsMatrix(Copy, rowMap, 0));
185 int numMyRows = rowMap.NumMyElements();
186 int maxNumEntries = A.MaxNumEntries();
189 std::vector<int> indices(maxNumEntries);
190 std::vector<double> values(maxNumEntries);
193 std::vector<int> colIndices(maxNumEntries);
194 std::vector<double> colValues(maxNumEntries);
198 for (
int localRow = 0; localRow < numMyRows; localRow++) {
200 int globalRow = gRowMap.GID(localRow);
201 int lid = A.RowMap().LID(globalRow);
202 int contigRow = rowMap.GID(localRow);
203 TEUCHOS_ASSERT(lid > -1);
205 int err = A.ExtractMyRowCopy(lid, maxNumEntries, numEntries, &values[0], &indices[0]);
206 TEUCHOS_ASSERT(err == 0);
208 int numOwnedCols = 0;
209 for (
int localCol = 0; localCol < numEntries; localCol++) {
210 TEUCHOS_ASSERT(indices[localCol] > -1);
213 int gid = local2ContigGIDs[indices[localCol]];
214 if (gid == -1)
continue;
216 colIndices[numOwnedCols] = gid;
217 colValues[numOwnedCols] = values[localCol];
222 mat->InsertGlobalValues(contigRow, numOwnedCols, &colValues[0], &colIndices[0]);
226 mat->FillComplete(colMap, rowMap);
232 void rebuildSubBlock(
int i,
int j,
const Epetra_CrsMatrix &A,
const std::vector<MapPair> &subMaps,
233 Epetra_CrsMatrix &mat) {
235 int numVarFamily = subMaps.size();
237 TEUCHOS_ASSERT(i >= 0 && i < numVarFamily);
238 TEUCHOS_ASSERT(j >= 0 && j < numVarFamily);
240 const Epetra_Map &gRowMap = *subMaps[i].first;
241 const Epetra_Map &rowMap = *subMaps[i].second;
243 const RCP<Epetra_IntVector> plocal2ContigGIDs = getSubBlockColumnGIDs(A, subMaps[j]);
244 Epetra_IntVector &local2ContigGIDs = *plocal2ContigGIDs;
249 int numMyRows = rowMap.NumMyElements();
250 int maxNumEntries = A.MaxNumEntries();
253 std::vector<int> indices(maxNumEntries);
254 std::vector<double> values(maxNumEntries);
257 std::vector<int> colIndices(maxNumEntries);
258 std::vector<double> colValues(maxNumEntries);
262 for (
int localRow = 0; localRow < numMyRows; localRow++) {
264 int globalRow = gRowMap.GID(localRow);
265 int lid = A.RowMap().LID(globalRow);
266 int contigRow = rowMap.GID(localRow);
267 TEUCHOS_ASSERT(lid > -1);
269 int err = A.ExtractMyRowCopy(lid, maxNumEntries, numEntries, &values[0], &indices[0]);
270 TEUCHOS_ASSERT(err == 0);
272 int numOwnedCols = 0;
273 for (
int localCol = 0; localCol < numEntries; localCol++) {
274 TEUCHOS_ASSERT(indices[localCol] > -1);
277 int gid = local2ContigGIDs[indices[localCol]];
278 if (gid == -1)
continue;
280 colIndices[numOwnedCols] = gid;
281 colValues[numOwnedCols] = values[localCol];
286 mat.SumIntoGlobalValues(contigRow, numOwnedCols, &colValues[0], &colIndices[0]);