10 #include "Teko_InterlacedTpetra.hpp"
11 #include "Tpetra_Import.hpp"
19 namespace TpetraHelpers {
25 void buildSubMaps(GO numGlobals,
int numVars,
const Teuchos::Comm<int>& comm,
26 std::vector<std::pair<
int, RCP<Tpetra::Map<LO, GO, NT> > > >& subMaps) {
27 std::vector<int> vars;
30 for (
int i = 0; i < numVars; i++) vars.push_back(1);
33 buildSubMaps(numGlobals, vars, comm, subMaps);
37 void buildSubMaps(
const Tpetra::Map<LO, GO, NT>& globalMap,
const std::vector<int>& vars,
38 const Teuchos::Comm<int>& comm,
39 std::vector<std::pair<
int, Teuchos::RCP<Tpetra::Map<LO, GO, NT> > > >& subMaps) {
40 buildSubMaps(globalMap.getGlobalNumElements(), globalMap.getLocalNumElements(),
41 globalMap.getMinGlobalIndex(), vars, comm, subMaps);
45 void buildSubMaps(GO numGlobals,
const std::vector<int>& vars,
const Teuchos::Comm<int>& comm,
46 std::vector<std::pair<
int, Teuchos::RCP<Tpetra::Map<LO, GO, NT> > > >& subMaps) {
47 std::vector<int>::const_iterator varItr;
50 int numGlobalVars = 0;
51 for (varItr = vars.begin(); varItr != vars.end(); ++varItr) numGlobalVars += *varItr;
54 TEUCHOS_ASSERT((numGlobals % numGlobalVars) == 0);
56 Tpetra::Map<LO, GO, NT> sampleMap(numGlobals / numGlobalVars, 0, rcpFromRef(comm));
58 buildSubMaps(numGlobals, numGlobalVars * sampleMap.getLocalNumElements(),
59 numGlobalVars * sampleMap.getMinGlobalIndex(), vars, comm, subMaps);
63 void buildSubMaps(GO numGlobals, LO numMyElements, GO minMyGID,
const std::vector<int>& vars,
64 const Teuchos::Comm<int>& comm,
65 std::vector<std::pair<
int, Teuchos::RCP<Tpetra::Map<LO, GO, NT> > > >& subMaps) {
66 std::vector<int>::const_iterator varItr;
69 int numGlobalVars = 0;
70 for (varItr = vars.begin(); varItr != vars.end(); ++varItr) numGlobalVars += *varItr;
73 TEUCHOS_ASSERT((numGlobals % numGlobalVars) == 0);
74 TEUCHOS_ASSERT((numMyElements % numGlobalVars) == 0);
75 TEUCHOS_ASSERT((minMyGID % numGlobalVars) == 0);
77 LO numBlocks = numMyElements / numGlobalVars;
78 GO minBlockID = minMyGID / numGlobalVars;
84 for (varItr = vars.begin(); varItr != vars.end(); ++varItr) {
85 LO numLocalVars = *varItr;
86 GO numAllElmts = numLocalVars * numGlobals / numGlobalVars;
88 LO numMyElmts = numLocalVars * numBlocks;
92 std::vector<GO> subGlobals;
93 std::vector<GO> contigGlobals;
97 for (LO blockNum = 0; blockNum < numBlocks; blockNum++) {
99 for (LO local = 0; local < numLocalVars; ++local) {
103 subGlobals.push_back((minBlockID + blockNum) * numGlobalVars + blockOffset + local);
106 contigGlobals.push_back(numLocalVars * minBlockID + count);
112 assert((
size_t)numMyElmts == subGlobals.size());
115 RCP<Tpetra::Map<LO, GO, NT> > subMap = rcp(
new Tpetra::Map<LO, GO, NT>(
116 numAllElmts, Teuchos::ArrayView<GO>(subGlobals), 0, rcpFromRef(comm)));
117 RCP<Tpetra::Map<LO, GO, NT> > contigMap = rcp(
new Tpetra::Map<LO, GO, NT>(
118 numAllElmts, Teuchos::ArrayView<GO>(contigGlobals), 0, rcpFromRef(comm)));
120 Teuchos::set_extra_data(contigMap,
"contigMap", Teuchos::inOutArg(subMap));
121 subMaps.push_back(std::make_pair(numLocalVars, subMap));
124 blockOffset += numLocalVars;
128 void buildExportImport(
const Tpetra::Map<LO, GO, NT>& baseMap,
129 const std::vector<std::pair<
int, RCP<Tpetra::Map<LO, GO, NT> > > >& subMaps,
130 std::vector<RCP<Tpetra::Export<LO, GO, NT> > >& subExport,
131 std::vector<RCP<Tpetra::Import<LO, GO, NT> > >& subImport) {
132 std::vector<std::pair<int, RCP<Tpetra::Map<LO, GO, NT> > > >::const_iterator mapItr;
135 for (mapItr = subMaps.begin(); mapItr != subMaps.end(); ++mapItr) {
137 const Tpetra::Map<LO, GO, NT>& map = *(mapItr->second);
140 subImport.push_back(rcp(
new Tpetra::Import<LO, GO, NT>(rcpFromRef(baseMap), rcpFromRef(map))));
141 subExport.push_back(rcp(
new Tpetra::Export<LO, GO, NT>(rcpFromRef(map), rcpFromRef(baseMap))));
145 void buildSubVectors(
const std::vector<std::pair<
int, RCP<Tpetra::Map<LO, GO, NT> > > >& subMaps,
146 std::vector<RCP<Tpetra::MultiVector<ST, LO, GO, NT> > >& subVectors,
148 std::vector<std::pair<int, RCP<Tpetra::Map<LO, GO, NT> > > >::const_iterator mapItr;
151 for (mapItr = subMaps.begin(); mapItr != subMaps.end(); ++mapItr) {
153 const Tpetra::Map<LO, GO, NT>& map =
154 *(Teuchos::get_extra_data<RCP<Tpetra::Map<LO, GO, NT> > >(mapItr->second,
"contigMap"));
157 RCP<Tpetra::MultiVector<ST, LO, GO, NT> > mv =
158 rcp(
new Tpetra::MultiVector<ST, LO, GO, NT>(rcpFromRef(map), count));
159 Teuchos::set_extra_data(mapItr->second,
"globalMap", Teuchos::inOutArg(mv));
160 subVectors.push_back(mv);
164 void associateSubVectors(
165 const std::vector<std::pair<
int, RCP<Tpetra::Map<LO, GO, NT> > > >& subMaps,
166 std::vector<RCP<
const Tpetra::MultiVector<ST, LO, GO, NT> > >& subVectors) {
167 std::vector<std::pair<int, RCP<Tpetra::Map<LO, GO, NT> > > >::const_iterator mapItr;
168 std::vector<RCP<const Tpetra::MultiVector<ST, LO, GO, NT> > >::iterator vecItr;
170 TEUCHOS_ASSERT(subMaps.size() == subVectors.size());
173 for (mapItr = subMaps.begin(), vecItr = subVectors.begin(); mapItr != subMaps.end();
175 Teuchos::set_extra_data(mapItr->second,
"globalMap", Teuchos::inOutArg(*vecItr),
176 Teuchos::POST_DESTROY,
false);
180 RCP<Tpetra::CrsMatrix<ST, LO, GO, NT> > buildSubBlock(
181 int i,
int j,
const RCP<
const Tpetra::CrsMatrix<ST, LO, GO, NT> >& A,
182 const std::vector<std::pair<
int, RCP<Tpetra::Map<LO, GO, NT> > > >& subMaps) {
184 int numVarFamily = subMaps.size();
186 TEUCHOS_ASSERT(i >= 0 && i < numVarFamily);
187 TEUCHOS_ASSERT(j >= 0 && j < numVarFamily);
189 const Tpetra::Map<LO, GO, NT>& gRowMap = *subMaps[i].second;
190 const Tpetra::Map<LO, GO, NT>& rowMap =
191 *Teuchos::get_extra_data<RCP<Tpetra::Map<LO, GO, NT> > >(subMaps[i].second,
"contigMap");
192 const Tpetra::Map<LO, GO, NT>& colMap =
193 *Teuchos::get_extra_data<RCP<Tpetra::Map<LO, GO, NT> > >(subMaps[j].second,
"contigMap");
194 int colFamilyCnt = subMaps[j].first;
198 GO numGlobalVars = 0;
199 GO rowBlockOffset = 0;
200 GO colBlockOffset = 0;
201 for (
int k = 0; k < numVarFamily; k++) {
202 numGlobalVars += subMaps[k].first;
205 if (k < i) rowBlockOffset += subMaps[k].first;
206 if (k < j) colBlockOffset += subMaps[k].first;
210 Tpetra::Import<LO, GO, NT>
import(A->getRowMap(), rcpFromRef(gRowMap));
211 Tpetra::CrsMatrix<ST, LO, GO, NT> localA(rcpFromRef(gRowMap), 0);
212 localA.doImport(*A,
import, Tpetra::INSERT);
215 LO numMyRows = rowMap.getLocalNumElements();
216 LO maxNumEntries = A->getGlobalMaxNumRowEntries();
219 auto indices =
typename Tpetra::CrsMatrix<ST, LO, GO, NT>::nonconst_global_inds_host_view_type(
220 Kokkos::ViewAllocateWithoutInitializing(
"rowIndices"), maxNumEntries);
221 auto values =
typename Tpetra::CrsMatrix<ST, LO, GO, NT>::nonconst_values_host_view_type(
222 Kokkos::ViewAllocateWithoutInitializing(
"rowIndices"), maxNumEntries);
225 std::vector<size_t> numEntriesPerRow(numMyRows, 0);
227 const size_t invalid = Teuchos::OrdinalTraits<size_t>::invalid();
230 for (LO localRow = 0; localRow < numMyRows; localRow++) {
231 size_t numEntries = invalid;
232 GO globalRow = gRowMap.getGlobalElement(localRow);
233 GO contigRow = rowMap.getGlobalElement(localRow);
235 TEUCHOS_ASSERT(globalRow >= 0);
236 TEUCHOS_ASSERT(contigRow >= 0);
239 localA.getGlobalRowCopy(globalRow, indices, values, numEntries);
241 for (
size_t localCol = 0; localCol < numEntries; localCol++) {
242 GO globalCol = indices(localCol);
245 int block = globalCol / numGlobalVars;
247 bool inFamily =
true;
250 inFamily &= (block * numGlobalVars + colBlockOffset <= globalCol);
251 inFamily &= ((block * numGlobalVars + colBlockOffset + colFamilyCnt) > globalCol);
258 numEntriesPerRow[localRow] += numOwnedCols;
261 RCP<Tpetra::CrsMatrix<ST, LO, GO, NT> > mat = rcp(
new Tpetra::CrsMatrix<ST, LO, GO, NT>(
262 rcpFromRef(rowMap), Teuchos::ArrayView<const size_t>(numEntriesPerRow)));
265 std::vector<GO> colIndices(maxNumEntries);
266 std::vector<ST> colValues(maxNumEntries);
270 for (LO localRow = 0; localRow < numMyRows; localRow++) {
271 size_t numEntries = invalid;
272 GO globalRow = gRowMap.getGlobalElement(localRow);
273 GO contigRow = rowMap.getGlobalElement(localRow);
275 TEUCHOS_ASSERT(globalRow >= 0);
276 TEUCHOS_ASSERT(contigRow >= 0);
279 localA.getGlobalRowCopy(globalRow, indices, values, numEntries);
281 for (
size_t localCol = 0; localCol < numEntries; localCol++) {
282 GO globalCol = indices(localCol);
285 int block = globalCol / numGlobalVars;
287 bool inFamily =
true;
290 inFamily &= (block * numGlobalVars + colBlockOffset <= globalCol);
291 inFamily &= ((block * numGlobalVars + colBlockOffset + colFamilyCnt) > globalCol);
295 GO familyOffset = globalCol - (block * numGlobalVars + colBlockOffset);
297 colIndices[numOwnedCols] = block * colFamilyCnt + familyOffset;
298 colValues[numOwnedCols] = values(localCol);
305 colIndices.resize(numOwnedCols);
306 colValues.resize(numOwnedCols);
307 mat->insertGlobalValues(contigRow, Teuchos::ArrayView<GO>(colIndices),
308 Teuchos::ArrayView<ST>(colValues));
309 colIndices.resize(maxNumEntries);
310 colValues.resize(maxNumEntries);
314 mat->fillComplete(rcpFromRef(colMap), rcpFromRef(rowMap));
320 void rebuildSubBlock(
int i,
int j,
const RCP<
const Tpetra::CrsMatrix<ST, LO, GO, NT> >& A,
321 const std::vector<std::pair<
int, RCP<Tpetra::Map<LO, GO, NT> > > >& subMaps,
322 Tpetra::CrsMatrix<ST, LO, GO, NT>& mat) {
324 int numVarFamily = subMaps.size();
326 TEUCHOS_ASSERT(i >= 0 && i < numVarFamily);
327 TEUCHOS_ASSERT(j >= 0 && j < numVarFamily);
328 TEUCHOS_ASSERT(mat.isFillComplete());
330 const Tpetra::Map<LO, GO, NT>& gRowMap = *subMaps[i].second;
331 const Tpetra::Map<LO, GO, NT>& rowMap =
332 *Teuchos::get_extra_data<RCP<Tpetra::Map<LO, GO, NT> > >(subMaps[i].second,
"contigMap");
333 const Tpetra::Map<LO, GO, NT>& colMap =
334 *Teuchos::get_extra_data<RCP<Tpetra::Map<LO, GO, NT> > >(subMaps[j].second,
"contigMap");
335 int colFamilyCnt = subMaps[j].first;
339 GO numGlobalVars = 0;
340 GO rowBlockOffset = 0;
341 GO colBlockOffset = 0;
342 for (
int k = 0; k < numVarFamily; k++) {
343 numGlobalVars += subMaps[k].first;
346 if (k < i) rowBlockOffset += subMaps[k].first;
347 if (k < j) colBlockOffset += subMaps[k].first;
351 Tpetra::Import<LO, GO, NT>
import(A->getRowMap(), rcpFromRef(gRowMap));
352 Tpetra::CrsMatrix<ST, LO, GO, NT> localA(rcpFromRef(gRowMap), 0);
353 localA.doImport(*A,
import, Tpetra::INSERT);
357 mat.setAllToScalar(0.0);
360 LO numMyRows = rowMap.getLocalNumElements();
361 GO maxNumEntries = A->getGlobalMaxNumRowEntries();
364 auto indices =
typename Tpetra::CrsMatrix<ST, LO, GO, NT>::nonconst_global_inds_host_view_type(
365 Kokkos::ViewAllocateWithoutInitializing(
"rowIndices"), maxNumEntries);
366 auto values =
typename Tpetra::CrsMatrix<ST, LO, GO, NT>::nonconst_values_host_view_type(
367 Kokkos::ViewAllocateWithoutInitializing(
"rowIndices"), maxNumEntries);
370 std::vector<GO> colIndices(maxNumEntries);
371 std::vector<ST> colValues(maxNumEntries);
373 const size_t invalid = Teuchos::OrdinalTraits<size_t>::invalid();
377 for (LO localRow = 0; localRow < numMyRows; localRow++) {
378 size_t numEntries = invalid;
379 GO globalRow = gRowMap.getGlobalElement(localRow);
380 GO contigRow = rowMap.getGlobalElement(localRow);
382 TEUCHOS_ASSERT(globalRow >= 0);
383 TEUCHOS_ASSERT(contigRow >= 0);
386 localA.getGlobalRowCopy(globalRow, indices, values, numEntries);
389 for (
size_t localCol = 0; localCol < numEntries; localCol++) {
390 GO globalCol = indices(localCol);
393 int block = globalCol / numGlobalVars;
395 bool inFamily =
true;
398 inFamily &= (block * numGlobalVars + colBlockOffset <= globalCol);
399 inFamily &= ((block * numGlobalVars + colBlockOffset + colFamilyCnt) > globalCol);
403 GO familyOffset = globalCol - (block * numGlobalVars + colBlockOffset);
405 colIndices[numOwnedCols] = block * colFamilyCnt + familyOffset;
406 colValues[numOwnedCols] = values(localCol);
413 colIndices.resize(numOwnedCols);
414 colValues.resize(numOwnedCols);
415 mat.sumIntoGlobalValues(contigRow, Teuchos::ArrayView<GO>(colIndices),
416 Teuchos::ArrayView<ST>(colValues));
417 colIndices.resize(maxNumEntries);
418 colValues.resize(maxNumEntries);
420 mat.fillComplete(rcpFromRef(colMap), rcpFromRef(rowMap));
424 void many2one(Tpetra::MultiVector<ST, LO, GO, NT>& one,
425 const std::vector<RCP<
const Tpetra::MultiVector<ST, LO, GO, NT> > >& many,
426 const std::vector<RCP<Tpetra::Export<LO, GO, NT> > >& subExport) {
428 std::vector<RCP<const Tpetra::MultiVector<ST, LO, GO, NT> > >::const_iterator vecItr;
429 std::vector<RCP<Tpetra::Export<LO, GO, NT> > >::const_iterator expItr;
432 for (vecItr = many.begin(), expItr = subExport.begin(); vecItr != many.end();
433 ++vecItr, ++expItr) {
435 RCP<const Tpetra::MultiVector<ST, LO, GO, NT> > srcVec = *vecItr;
438 const Tpetra::Map<LO, GO, NT>& globalMap =
439 *(Teuchos::get_extra_data<RCP<Tpetra::Map<LO, GO, NT> > >(srcVec,
"globalMap"));
442 GO lda = srcVec->getStride();
443 GO srcSize = srcVec->getGlobalLength() * srcVec->getNumVectors();
444 std::vector<ST> srcArray(srcSize);
445 Teuchos::ArrayView<ST> srcVals(srcArray);
446 srcVec->get1dCopy(srcVals, lda);
447 Tpetra::MultiVector<ST, LO, GO, NT> exportVector(rcpFromRef(globalMap), srcVals, lda,
448 srcVec->getNumVectors());
451 one.doExport(exportVector, **expItr, Tpetra::INSERT);
456 void one2many(std::vector<RCP<Tpetra::MultiVector<ST, LO, GO, NT> > >& many,
457 const Tpetra::MultiVector<ST, LO, GO, NT>& single,
458 const std::vector<RCP<Tpetra::Import<LO, GO, NT> > >& subImport) {
460 std::vector<RCP<Tpetra::MultiVector<ST, LO, GO, NT> > >::const_iterator vecItr;
461 std::vector<RCP<Tpetra::Import<LO, GO, NT> > >::const_iterator impItr;
464 for (vecItr = many.begin(), impItr = subImport.begin(); vecItr != many.end();
465 ++vecItr, ++impItr) {
467 RCP<Tpetra::MultiVector<ST, LO, GO, NT> > destVec = *vecItr;
470 const Tpetra::Map<LO, GO, NT>& globalMap =
471 *(Teuchos::get_extra_data<RCP<Tpetra::Map<LO, GO, NT> > >(destVec,
"globalMap"));
474 GO destLDA = destVec->getStride();
475 GO destSize = destVec->getGlobalLength() * destVec->getNumVectors();
476 std::vector<ST> destArray(destSize);
477 Teuchos::ArrayView<ST> destVals(destArray);
478 destVec->get1dCopy(destVals, destLDA);
479 Tpetra::MultiVector<ST, LO, GO, NT> importVector(rcpFromRef(globalMap), destVals, destLDA,
480 destVec->getNumVectors());
483 importVector.doImport(single, **impItr, Tpetra::INSERT);
485 Tpetra::Import<LO, GO, NT> importer(destVec->getMap(), destVec->getMap());
486 importVector.replaceMap(destVec->getMap());
487 destVec->doImport(importVector, importer, Tpetra::INSERT);