47 #include "Teko_BlockingTpetra.hpp"
50 #include "Tpetra_Vector.hpp"
51 #include "Tpetra_Map.hpp"
57 namespace TpetraHelpers {
73 const MapPair buildSubMap(
const std::vector< GO > & gid,
const Teuchos::Comm<int> &comm)
75 Teuchos::RCP<Tpetra::Map<LO,GO,NT> > gidMap = rcp(
new Tpetra::Map<LO,GO,NT>(-1,Teuchos::ArrayView<const GO>(gid),0,rcpFromRef(comm)));
76 Teuchos::RCP<Tpetra::Map<LO,GO,NT> > contigMap = rcp(
new Tpetra::Map<LO,GO,NT>(-1,gid.size(),0,rcpFromRef(comm)));
78 return std::make_pair(gidMap,contigMap);
90 const ImExPair buildExportImport(
const Tpetra::Map<LO,GO,NT> & baseMap,
const MapPair & maps)
92 return std::make_pair(rcp(
new Tpetra::Import<LO,GO,NT>(rcpFromRef(baseMap),maps.first)),
93 rcp(
new Tpetra::Export<LO,GO,NT>(maps.first,rcpFromRef(baseMap))));
103 void buildSubVectors(
const std::vector<MapPair> & maps,
104 std::vector<RCP<Tpetra::MultiVector<ST,LO,GO,NT> > > & vectors,
int count)
106 std::vector<MapPair>::const_iterator mapItr;
109 for(mapItr=maps.begin();mapItr!=maps.end();mapItr++) {
111 RCP<Tpetra::MultiVector<ST,LO,GO,NT> > mv = rcp(
new Tpetra::MultiVector<ST,LO,GO,NT>((*mapItr).second,count));
112 vectors.push_back(mv);
122 void one2many(std::vector<RCP<Tpetra::MultiVector<ST,LO,GO,NT> > > & many,
const Tpetra::MultiVector<ST,LO,GO,NT> & single,
123 const std::vector<RCP<Tpetra::Import<LO,GO,NT> > > & subImport)
126 std::vector<RCP<Tpetra::MultiVector<ST,LO,GO,NT> > >::const_iterator vecItr;
127 std::vector<RCP<Tpetra::Import<LO,GO,NT> > >::const_iterator impItr;
130 for(vecItr=many.begin(),impItr=subImport.begin();
131 vecItr!=many.end();++vecItr,++impItr) {
133 RCP<Tpetra::MultiVector<ST,LO,GO,NT> > destVec = *vecItr;
136 const Tpetra::Map<LO,GO,NT> & globalMap = *(*impItr)->getTargetMap();
139 GO lda = destVec->getStride();
140 GO destSize = destVec->getGlobalLength()*destVec->getNumVectors();
141 std::vector<ST> destArray(destSize);
142 Teuchos::ArrayView<ST> destVals(destArray);
143 destVec->get1dCopy(destVals,lda);
144 Tpetra::MultiVector<ST,LO,GO,NT> importVector(rcpFromRef(globalMap),destVals,lda,destVec->getNumVectors());
147 importVector.doImport(single,**impItr,Tpetra::INSERT);
149 Tpetra::Import<LO,GO,NT> importer(destVec->getMap(),destVec->getMap());
150 importVector.replaceMap(destVec->getMap());
151 destVec->doExport(importVector,importer,Tpetra::INSERT);
164 void many2one(Tpetra::MultiVector<ST,LO,GO,NT> & one,
const std::vector<RCP<
const Tpetra::MultiVector<ST,LO,GO,NT> > > & many,
165 const std::vector<RCP<Tpetra::Export<LO,GO,NT> > > & subExport)
168 std::vector<RCP<const Tpetra::MultiVector<ST,LO,GO,NT> > >::const_iterator vecItr;
169 std::vector<RCP<Tpetra::Export<LO,GO,NT> > >::const_iterator expItr;
172 for(vecItr=many.begin(),expItr=subExport.begin();
173 vecItr!=many.end();++vecItr,++expItr) {
176 RCP<const Tpetra::MultiVector<ST,LO,GO,NT> > srcVec = *vecItr;
179 const Tpetra::Map<LO,GO,NT> & globalMap = *(*expItr)->getSourceMap();
182 GO lda = srcVec->getStride();
183 GO srcSize = srcVec->getGlobalLength()*srcVec->getNumVectors();
184 std::vector<ST> srcArray(srcSize);
185 Teuchos::ArrayView<ST> srcVals(srcArray);
186 srcVec->get1dCopy(srcVals,lda);
187 Tpetra::MultiVector<ST,LO,GO,NT> exportVector(rcpFromRef(globalMap),srcVals,lda,srcVec->getNumVectors());
190 one.doExport(exportVector,**expItr,Tpetra::INSERT);
199 RCP<Tpetra::Vector<GO,LO,GO,NT> > getSubBlockColumnGIDs(
const Tpetra::CrsMatrix<ST,LO,GO,NT> & A,
const MapPair & mapPair)
201 RCP<const Tpetra::Map<LO,GO,NT> > blkGIDMap = mapPair.first;
202 RCP<const Tpetra::Map<LO,GO,NT> > blkContigMap = mapPair.second;
205 Tpetra::Vector<GO,LO,GO,NT> rIndex(A.getRowMap(),
true);
206 for(
size_t i=0;i<A.getNodeNumRows();i++) {
208 LO lid = blkGIDMap->getLocalElement(A.getRowMap()->getGlobalElement(i));
210 rIndex.replaceLocalValue(i,blkContigMap->getGlobalElement(lid));
212 rIndex.replaceLocalValue(i,-1);
217 Tpetra::Import<LO,GO,NT>
import(A.getRowMap(),A.getColMap());
218 RCP<Tpetra::Vector<GO,LO,GO,NT> > cIndex = rcp(
new Tpetra::Vector<GO,LO,GO,NT>(A.getColMap(),
true));
219 cIndex->doImport(rIndex,
import,Tpetra::INSERT);
225 RCP<Tpetra::CrsMatrix<ST,LO,GO,NT> > buildSubBlock(
int i,
int j,
const RCP<
const Tpetra::CrsMatrix<ST,LO,GO,NT> >& A,
const std::vector<MapPair> & subMaps)
228 int numVarFamily = subMaps.size();
230 TEUCHOS_ASSERT(i>=0 && i<numVarFamily);
231 TEUCHOS_ASSERT(j>=0 && j<numVarFamily);
232 const RCP<const Tpetra::Map<LO,GO,NT> > gRowMap = subMaps[i].first;
233 const RCP<const Tpetra::Map<LO,GO,NT> > rowMap = subMaps[i].second;
234 const RCP<const Tpetra::Map<LO,GO,NT> > colMap = subMaps[j].second;
236 const RCP<Tpetra::Vector<GO,LO,GO,NT> > plocal2ContigGIDs = getSubBlockColumnGIDs(*A,subMaps[j]);
237 Tpetra::Vector<GO,LO,GO,NT> & local2ContigGIDs = *plocal2ContigGIDs;
241 LO numMyRows = rowMap->getNodeNumElements();
242 LO maxNumEntries = A->getNodeMaxNumRowEntries();
245 std::vector<LO> indices(maxNumEntries);
246 std::vector<ST> values(maxNumEntries);
249 std::vector<GO> colIndices(maxNumEntries);
250 std::vector<ST> colValues(maxNumEntries);
253 std::vector<size_t> nEntriesPerRow(numMyRows);
257 for(LO localRow=0;localRow<numMyRows;localRow++) {
258 size_t numEntries = -1;
259 GO globalRow = gRowMap->getGlobalElement(localRow);
260 LO lid = A->getRowMap()->getLocalElement(globalRow);
261 TEUCHOS_ASSERT(lid>-1);
263 A->getLocalRowCopy(lid, Teuchos::ArrayView<LO>(indices), Teuchos::ArrayView<ST>(values),numEntries);
266 for(
size_t localCol=0;localCol<numEntries;localCol++) {
267 TEUCHOS_ASSERT(indices[localCol]>-1);
271 GO gid = local2ContigGIDs.getData()[indices[localCol]];
272 if(gid==-1)
continue;
275 nEntriesPerRow[localRow] = numOwnedCols;
278 RCP<Tpetra::CrsMatrix<ST,LO,GO,NT> > mat = rcp(
new Tpetra::CrsMatrix<ST,LO,GO,NT>(rowMap,Teuchos::ArrayView<size_t>(nEntriesPerRow)));
282 for(LO localRow=0;localRow<numMyRows;localRow++) {
283 size_t numEntries = -1;
284 GO globalRow = gRowMap->getGlobalElement(localRow);
285 LO lid = A->getRowMap()->getLocalElement(globalRow);
286 GO contigRow = rowMap->getGlobalElement(localRow);
287 TEUCHOS_ASSERT(lid>-1);
289 A->getLocalRowCopy(lid, Teuchos::ArrayView<LO>(indices), Teuchos::ArrayView<ST>(values),numEntries);
292 for(
size_t localCol=0;localCol<numEntries;localCol++) {
293 TEUCHOS_ASSERT(indices[localCol]>-1);
297 GO gid = local2ContigGIDs.getData()[indices[localCol]];
298 if(gid==-1)
continue;
300 colIndices[numOwnedCols] = gid;
301 colValues[numOwnedCols] = values[localCol];
306 colIndices.resize(numOwnedCols);
307 colValues.resize(numOwnedCols);
308 mat->insertGlobalValues(contigRow,Teuchos::ArrayView<GO>(colIndices),Teuchos::ArrayView<ST>(colValues));
309 colIndices.resize(maxNumEntries);
310 colValues.resize(maxNumEntries);
314 mat->fillComplete(colMap,rowMap);
320 void rebuildSubBlock(
int i,
int j,
const RCP<
const Tpetra::CrsMatrix<ST,LO,GO,NT> >& A,
const std::vector<MapPair> & subMaps,Tpetra::CrsMatrix<ST,LO,GO,NT> & mat)
323 int numVarFamily = subMaps.size();
325 TEUCHOS_ASSERT(i>=0 && i<numVarFamily);
326 TEUCHOS_ASSERT(j>=0 && j<numVarFamily);
328 const Tpetra::Map<LO,GO,NT> & gRowMap = *subMaps[i].first;
329 const Tpetra::Map<LO,GO,NT> & rowMap = *subMaps[i].second;
330 const Tpetra::Map<LO,GO,NT> & colMap = *subMaps[j].second;
332 const RCP<Tpetra::Vector<GO,LO,GO,NT> > plocal2ContigGIDs = getSubBlockColumnGIDs(*A,subMaps[j]);
333 Tpetra::Vector<GO,LO,GO,NT> & local2ContigGIDs = *plocal2ContigGIDs;
336 mat.setAllToScalar(0.0);
339 LO numMyRows = rowMap.getNodeNumElements();
340 LO maxNumEntries = A->getNodeMaxNumRowEntries();
343 std::vector<LO> indices(maxNumEntries);
344 std::vector<ST> values(maxNumEntries);
347 std::vector<GO> colIndices(maxNumEntries);
348 std::vector<ST> colValues(maxNumEntries);
352 for(LO localRow=0;localRow<numMyRows;localRow++) {
353 size_t numEntries = -1;
354 GO globalRow = gRowMap.getGlobalElement(localRow);
355 LO lid = A->getRowMap()->getLocalElement(globalRow);
356 GO contigRow = rowMap.getGlobalElement(localRow);
357 TEUCHOS_ASSERT(lid>-1);
359 A->getLocalRowCopy(lid, Teuchos::ArrayView<LO>(indices), Teuchos::ArrayView<ST>(values), numEntries);
362 for(
size_t localCol=0;localCol<numEntries;localCol++) {
363 TEUCHOS_ASSERT(indices[localCol]>-1);
366 GO gid = local2ContigGIDs.getData()[indices[localCol]];
367 if(gid==-1)
continue;
369 colIndices[numOwnedCols] = gid;
370 colValues[numOwnedCols] = values[localCol];
375 colIndices.resize(numOwnedCols);
376 colValues.resize(numOwnedCols);
377 mat.sumIntoGlobalValues(contigRow,Teuchos::ArrayView<GO>(colIndices),Teuchos::ArrayView<ST>(colValues));
378 colIndices.resize(maxNumEntries);
379 colValues.resize(maxNumEntries);
381 mat.fillComplete(rcpFromRef(colMap),rcpFromRef(rowMap));