Teko  Version of the Day
Teko_InterlacedTpetra.cpp
1 /*
2 // @HEADER
3 //
4 // ***********************************************************************
5 //
6 // Teko: A package for block and physics based preconditioning
7 // Copyright 2010 Sandia Corporation
8 //
9 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
10 // the U.S. Government retains certain rights in this software.
11 //
12 // Redistribution and use in source and binary forms, with or without
13 // modification, are permitted provided that the following conditions are
14 // met:
15 //
16 // 1. Redistributions of source code must retain the above copyright
17 // notice, this list of conditions and the following disclaimer.
18 //
19 // 2. Redistributions in binary form must reproduce the above copyright
20 // notice, this list of conditions and the following disclaimer in the
21 // documentation and/or other materials provided with the distribution.
22 //
23 // 3. Neither the name of the Corporation nor the names of the
24 // contributors may be used to endorse or promote products derived from
25 // this software without specific prior written permission.
26 //
27 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
28 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
31 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
34 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 //
39 // Questions? Contact Eric C. Cyr (eccyr@sandia.gov)
40 //
41 // ***********************************************************************
42 //
43 // @HEADER
44 
45 */
46 
47 #include "Teko_InterlacedTpetra.hpp"
48 #include "Tpetra_Import.hpp"
49 
50 #include <vector>
51 
52 using Teuchos::RCP;
53 using Teuchos::rcp;
54 
55 namespace Teko {
56 namespace TpetraHelpers {
57 namespace Strided {
58 
59 // this assumes that there are numGlobals with numVars each interlaced
60 // i.e. for numVars = 2 (u,v) then the vector is
61 // [u_0,v_0,u_1,v_1,u_2,v_2, ..., u_(numGlobals-1),v_(numGlobals-1)]
62 void buildSubMaps(GO numGlobals,int numVars,const Teuchos::Comm<int> & comm,std::vector<std::pair<int,RCP<Tpetra::Map<LO,GO,NT> > > > & subMaps)
63 {
64  std::vector<int> vars;
65 
66  // build vector describing the sub maps
67  for(int i=0;i<numVars;i++) vars.push_back(1);
68 
69  // build all the submaps
70  buildSubMaps(numGlobals,vars,comm,subMaps);
71 }
72 
73 // build maps to make other conversions
74 void buildSubMaps(const Tpetra::Map<LO,GO,NT> & globalMap,const std::vector<int> & vars,const Teuchos::Comm<int> & comm,
75  std::vector<std::pair<int,Teuchos::RCP<Tpetra::Map<LO,GO,NT> > > > & subMaps)
76 {
77  buildSubMaps(globalMap.getGlobalNumElements(),globalMap.getNodeNumElements(),globalMap.getMinGlobalIndex(),
78  vars,comm,subMaps);
79 }
80 
81 // build maps to make other conversions
82 void buildSubMaps(GO numGlobals,const std::vector<int> & vars,const Teuchos::Comm<int> & comm,std::vector<std::pair<int,Teuchos::RCP<Tpetra::Map<LO,GO,NT> > > > & subMaps)
83 {
84  std::vector<int>::const_iterator varItr;
85 
86  // compute total number of variables
87  int numGlobalVars = 0;
88  for(varItr=vars.begin();varItr!=vars.end();++varItr)
89  numGlobalVars += *varItr;
90 
91  // must be an even number of globals
92  TEUCHOS_ASSERT((numGlobals%numGlobalVars)==0);
93 
94  Tpetra::Map<LO,GO,NT> sampleMap(numGlobals/numGlobalVars,0,rcpFromRef(comm));
95 
96  buildSubMaps(numGlobals,numGlobalVars*sampleMap.getNodeNumElements(),numGlobalVars*sampleMap.getMinGlobalIndex(),vars,comm,subMaps);
97 }
98 
99 // build maps to make other conversions
100 void buildSubMaps(GO numGlobals,LO numMyElements,GO minMyGID,const std::vector<int> & vars,const Teuchos::Comm<int> & comm,
101  std::vector<std::pair<int,Teuchos::RCP<Tpetra::Map<LO,GO,NT> > > > & subMaps)
102 {
103  std::vector<int>::const_iterator varItr;
104 
105  // compute total number of variables
106  int numGlobalVars = 0;
107  for(varItr=vars.begin();varItr!=vars.end();++varItr)
108  numGlobalVars += *varItr;
109 
110  // must be an even number of globals
111  TEUCHOS_ASSERT((numGlobals%numGlobalVars)==0);
112  TEUCHOS_ASSERT((numMyElements%numGlobalVars)==0);
113  TEUCHOS_ASSERT((minMyGID%numGlobalVars)==0);
114 
115  LO numBlocks = numMyElements/numGlobalVars;
116  GO minBlockID = minMyGID/numGlobalVars;
117 
118  subMaps.clear();
119 
120  // index into local block in strided map
121  GO blockOffset = 0;
122  for(varItr=vars.begin();varItr!=vars.end();++varItr) {
123  LO numLocalVars = *varItr;
124  GO numAllElmts = numLocalVars*numGlobals/numGlobalVars;
125  LO numMyElmts = numLocalVars * numBlocks;
126 
127  // create global arrays describing the as of yet uncreated maps
128  std::vector<GO> subGlobals;
129  std::vector<GO> contigGlobals; // the contiguous globals
130 
131  // loop over each block of variables
132  LO count = 0;
133  for(LO blockNum=0;blockNum<numBlocks;blockNum++) {
134 
135  // loop over each local variable in the block
136  for(LO local=0;local<numLocalVars;++local) {
137  // global block number = minGID+blockNum
138  // block begin global id = numGlobalVars*(minGID+blockNum)
139  // global id block offset = blockOffset+local
140  subGlobals.push_back((minBlockID+blockNum)*numGlobalVars+blockOffset+local);
141 
142  // also build the contiguous IDs
143  contigGlobals.push_back(numLocalVars*minBlockID+count);
144  count++;
145  }
146  }
147 
148  // sanity check
149  assert((size_t) numMyElmts==subGlobals.size());
150 
151  // create the map with contiguous elements and the map with global elements
152  RCP<Tpetra::Map<LO,GO,NT> > subMap = rcp(new Tpetra::Map<LO,GO,NT>(numAllElmts,Teuchos::ArrayView<GO>(subGlobals),0,rcpFromRef(comm)));
153  RCP<Tpetra::Map<LO,GO,NT> > contigMap = rcp(new Tpetra::Map<LO,GO,NT>(numAllElmts,Teuchos::ArrayView<GO>(contigGlobals),0,rcpFromRef(comm)));
154 
155  Teuchos::set_extra_data(contigMap,"contigMap",Teuchos::inOutArg(subMap));
156  subMaps.push_back(std::make_pair(numLocalVars,subMap));
157 
158  // update the block offset
159  blockOffset += numLocalVars;
160  }
161 }
162 
163 void buildExportImport(const Tpetra::Map<LO,GO,NT> & baseMap, const std::vector<std::pair<int,RCP<Tpetra::Map<LO,GO,NT> > > > & subMaps,
164  std::vector<RCP<Tpetra::Export<LO,GO,NT> > > & subExport,
165  std::vector<RCP<Tpetra::Import<LO,GO,NT> > > & subImport)
166 {
167  std::vector<std::pair<int,RCP<Tpetra::Map<LO,GO,NT> > > >::const_iterator mapItr;
168 
169  // build importers and exporters
170  for(mapItr=subMaps.begin();mapItr!=subMaps.end();++mapItr) {
171  // exctract basic map
172  const Tpetra::Map<LO,GO,NT> & map = *(mapItr->second);
173 
174  // add new elements to vectors
175  subImport.push_back(rcp(new Tpetra::Import<LO,GO,NT>(rcpFromRef(baseMap),rcpFromRef(map))));
176  subExport.push_back(rcp(new Tpetra::Export<LO,GO,NT>(rcpFromRef(map),rcpFromRef(baseMap))));
177  }
178 }
179 
180 void buildSubVectors(const std::vector<std::pair<int,RCP<Tpetra::Map<LO,GO,NT> > > > & subMaps,std::vector<RCP<Tpetra::MultiVector<ST,LO,GO,NT> > > & subVectors,int count)
181 {
182  std::vector<std::pair<int,RCP<Tpetra::Map<LO,GO,NT> > > >::const_iterator mapItr;
183 
184  // build vectors
185  for(mapItr=subMaps.begin();mapItr!=subMaps.end();++mapItr) {
186  // exctract basic map
187  const Tpetra::Map<LO,GO,NT> & map = *(Teuchos::get_extra_data<RCP<Tpetra::Map<LO,GO,NT> > >(mapItr->second,"contigMap"));
188 
189  // add new elements to vectors
190  RCP<Tpetra::MultiVector<ST,LO,GO,NT> > mv = rcp(new Tpetra::MultiVector<ST,LO,GO,NT>(rcpFromRef(map),count));
191  Teuchos::set_extra_data(mapItr->second,"globalMap",Teuchos::inOutArg(mv));
192  subVectors.push_back(mv);
193  }
194 }
195 
196 void associateSubVectors(const std::vector<std::pair<int,RCP<Tpetra::Map<LO,GO,NT> > > > & subMaps,std::vector<RCP<const Tpetra::MultiVector<ST,LO,GO,NT> > > & subVectors)
197 {
198  std::vector<std::pair<int,RCP<Tpetra::Map<LO,GO,NT> > > >::const_iterator mapItr;
199  std::vector<RCP<const Tpetra::MultiVector<ST,LO,GO,NT> > >::iterator vecItr;
200 
201  TEUCHOS_ASSERT(subMaps.size()==subVectors.size());
202 
203  // associate the sub vectors with the subMaps
204  for(mapItr=subMaps.begin(),vecItr=subVectors.begin();mapItr!=subMaps.end();++mapItr,++vecItr)
205  Teuchos::set_extra_data(mapItr->second,"globalMap",Teuchos::inOutArg(*vecItr),Teuchos::POST_DESTROY,false);
206 }
207 
208 // build a single subblock Epetra_CrsMatrix
209 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<std::pair<int,RCP<Tpetra::Map<LO,GO,NT> > > > & subMaps)
210 {
211  // get the number of variables families
212  int numVarFamily = subMaps.size();
213 
214  TEUCHOS_ASSERT(i>=0 && i<numVarFamily);
215  TEUCHOS_ASSERT(j>=0 && j<numVarFamily);
216 
217  const Tpetra::Map<LO,GO,NT> & gRowMap = *subMaps[i].second;
218  const Tpetra::Map<LO,GO,NT> & rowMap = *Teuchos::get_extra_data<RCP<Tpetra::Map<LO,GO,NT> > >(subMaps[i].second,"contigMap");
219  const Tpetra::Map<LO,GO,NT> & colMap = *Teuchos::get_extra_data<RCP<Tpetra::Map<LO,GO,NT> > >(subMaps[j].second,"contigMap");
220  int colFamilyCnt = subMaps[j].first;
221 
222  // compute the number of global variables
223  // and the row and column block offset
224  GO numGlobalVars = 0;
225  GO rowBlockOffset = 0;
226  GO colBlockOffset = 0;
227  for(int k=0;k<numVarFamily;k++) {
228  numGlobalVars += subMaps[k].first;
229 
230  // compute block offsets
231  if(k<i) rowBlockOffset += subMaps[k].first;
232  if(k<j) colBlockOffset += subMaps[k].first;
233  }
234 
235  // copy all global rows to here
236  Tpetra::Import<LO,GO,NT> import(A->getRowMap(),rcpFromRef(gRowMap));
237  Tpetra::CrsMatrix<ST,LO,GO,NT> localA(rcpFromRef(gRowMap),0);
238  localA.doImport(*A,import,Tpetra::INSERT);
239 
240  RCP<Tpetra::CrsMatrix<ST,LO,GO,NT> > mat = Tpetra::createCrsMatrix<ST,LO,GO,NT>(rcpFromRef(rowMap),0);
241 
242  // get entry information
243  LO numMyRows = rowMap.getNodeNumElements();
244  LO maxNumEntries = A->getGlobalMaxNumRowEntries();
245 
246  // for extraction
247  std::vector<GO> indices(maxNumEntries);
248  std::vector<ST> values(maxNumEntries);
249 
250  // for insertion
251  std::vector<GO> colIndices(maxNumEntries);
252  std::vector<ST> colValues(maxNumEntries);
253 
254  // insert each row into subblock
255  // let FillComplete handle column distribution
256  for(LO localRow=0;localRow<numMyRows;localRow++) {
257  size_t numEntries = -1;
258  GO globalRow = gRowMap.getGlobalElement(localRow);
259  GO contigRow = rowMap.getGlobalElement(localRow);
260 
261  TEUCHOS_ASSERT(globalRow>=0);
262  TEUCHOS_ASSERT(contigRow>=0);
263 
264  // extract a global row copy
265  localA.getGlobalRowCopy(globalRow, Teuchos::ArrayView<GO>(indices), Teuchos::ArrayView<ST>(values), numEntries);
266  LO numOwnedCols = 0;
267  for(size_t localCol=0;localCol<numEntries;localCol++) {
268  GO globalCol = indices[localCol];
269 
270  // determinate which block this column ID is in
271  int block = globalCol / numGlobalVars;
272 
273  bool inFamily = true;
274 
275  // test the beginning of the block
276  inFamily &= (block*numGlobalVars+colBlockOffset <= globalCol);
277  inFamily &= ((block*numGlobalVars+colBlockOffset+colFamilyCnt) > globalCol);
278 
279  // is this column in the variable family
280  if(inFamily) {
281  GO familyOffset = globalCol-(block*numGlobalVars+colBlockOffset);
282 
283  colIndices[numOwnedCols] = block*colFamilyCnt + familyOffset;
284  colValues[numOwnedCols] = values[localCol];
285 
286  numOwnedCols++;
287  }
288  }
289 
290  // insert it into the new matrix
291  colIndices.resize(numOwnedCols);
292  colValues.resize(numOwnedCols);
293  mat->insertGlobalValues(contigRow,Teuchos::ArrayView<GO>(colIndices),Teuchos::ArrayView<ST>(colValues));
294  colIndices.resize(maxNumEntries);
295  colValues.resize(maxNumEntries);
296  }
297 
298  // fill it and automagically optimize the storage
299  mat->fillComplete(rcpFromRef(colMap),rcpFromRef(rowMap));
300 
301  return mat;
302 }
303 
304 // rebuild a single subblock Epetra_CrsMatrix
305 void rebuildSubBlock(int i,int j,const RCP<const Tpetra::CrsMatrix<ST,LO,GO,NT> > & A,const std::vector<std::pair<int,RCP<Tpetra::Map<LO,GO,NT> > > > & subMaps,Tpetra::CrsMatrix<ST,LO,GO,NT> & mat)
306 {
307  // get the number of variables families
308  int numVarFamily = subMaps.size();
309 
310  TEUCHOS_ASSERT(i>=0 && i<numVarFamily);
311  TEUCHOS_ASSERT(j>=0 && j<numVarFamily);
312  TEUCHOS_ASSERT(mat.isFillComplete());
313 
314  const Tpetra::Map<LO,GO,NT> & gRowMap = *subMaps[i].second;
315  const Tpetra::Map<LO,GO,NT> & rowMap = *Teuchos::get_extra_data<RCP<Tpetra::Map<LO,GO,NT> > >(subMaps[i].second,"contigMap");
316  const Tpetra::Map<LO,GO,NT> & colMap = *Teuchos::get_extra_data<RCP<Tpetra::Map<LO,GO,NT> > >(subMaps[j].second,"contigMap");
317  int colFamilyCnt = subMaps[j].first;
318 
319  // compute the number of global variables
320  // and the row and column block offset
321  GO numGlobalVars = 0;
322  GO rowBlockOffset = 0;
323  GO colBlockOffset = 0;
324  for(int k=0;k<numVarFamily;k++) {
325  numGlobalVars += subMaps[k].first;
326 
327  // compute block offsets
328  if(k<i) rowBlockOffset += subMaps[k].first;
329  if(k<j) colBlockOffset += subMaps[k].first;
330  }
331 
332  // copy all global rows to here
333  Tpetra::Import<LO,GO,NT> import(A->getRowMap(),rcpFromRef(gRowMap));
334  Tpetra::CrsMatrix<ST,LO,GO,NT> localA(rcpFromRef(gRowMap),0);
335  localA.doImport(*A,import,Tpetra::INSERT);
336 
337  // clear out the old matrix
338  mat.resumeFill();
339  mat.setAllToScalar(0.0);
340 
341  // get entry information
342  LO numMyRows = rowMap.getNodeNumElements();
343  GO maxNumEntries = A->getGlobalMaxNumRowEntries();
344 
345  // for extraction
346  std::vector<GO> indices(maxNumEntries);
347  std::vector<ST> values(maxNumEntries);
348 
349  // for insertion
350  std::vector<GO> colIndices(maxNumEntries);
351  std::vector<ST> colValues(maxNumEntries);
352 
353  // insert each row into subblock
354  // let FillComplete handle column distribution
355  for(LO localRow=0;localRow<numMyRows;localRow++) {
356  size_t numEntries = -1;
357  GO globalRow = gRowMap.getGlobalElement(localRow);
358  GO contigRow = rowMap.getGlobalElement(localRow);
359 
360  TEUCHOS_ASSERT(globalRow>=0);
361  TEUCHOS_ASSERT(contigRow>=0);
362 
363  // extract a global row copy
364  localA.getGlobalRowCopy(globalRow, Teuchos::ArrayView<GO>(indices), Teuchos::ArrayView<ST>(values), numEntries);
365 
366  LO numOwnedCols = 0;
367  for(size_t localCol=0;localCol<numEntries;localCol++) {
368  GO globalCol = indices[localCol];
369 
370  // determinate which block this column ID is in
371  int block = globalCol / numGlobalVars;
372 
373  bool inFamily = true;
374 
375  // test the beginning of the block
376  inFamily &= (block*numGlobalVars+colBlockOffset <= globalCol);
377  inFamily &= ((block*numGlobalVars+colBlockOffset+colFamilyCnt) > globalCol);
378 
379  // is this column in the variable family
380  if(inFamily) {
381  GO familyOffset = globalCol-(block*numGlobalVars+colBlockOffset);
382 
383  colIndices[numOwnedCols] = block*colFamilyCnt + familyOffset;
384  colValues[numOwnedCols] = values[localCol];
385 
386  numOwnedCols++;
387  }
388  }
389 
390  // insert it into the new matrix
391  colIndices.resize(numOwnedCols);
392  colValues.resize(numOwnedCols);
393  mat.sumIntoGlobalValues(contigRow,Teuchos::ArrayView<GO>(colIndices),Teuchos::ArrayView<ST>(colValues));
394  colIndices.resize(maxNumEntries);
395  colValues.resize(maxNumEntries);
396  }
397  mat.fillComplete(rcpFromRef(colMap),rcpFromRef(rowMap));
398 }
399 
400 
401 // collect subvectors into a single global vector
402 void many2one(Tpetra::MultiVector<ST,LO,GO,NT> & one, const std::vector<RCP<const Tpetra::MultiVector<ST,LO,GO,NT> > > & many,
403  const std::vector<RCP<Tpetra::Export<LO,GO,NT> > > & subExport)
404 {
405  // std::vector<RCP<const Epetra_Vector> >::const_iterator vecItr;
406  std::vector<RCP<const Tpetra::MultiVector<ST,LO,GO,NT> > >::const_iterator vecItr;
407  std::vector<RCP<Tpetra::Export<LO,GO,NT> > >::const_iterator expItr;
408 
409  // using Exporters fill the empty vector from the sub-vectors
410  for(vecItr=many.begin(),expItr=subExport.begin();
411  vecItr!=many.end();++vecItr,++expItr) {
412 
413  // for ease of access to the source
414  RCP<const Tpetra::MultiVector<ST,LO,GO,NT> > srcVec = *vecItr;
415 
416  // extract the map with global indicies from the current vector
417  const Tpetra::Map<LO,GO,NT> & globalMap = *(Teuchos::get_extra_data<RCP<Tpetra::Map<LO,GO,NT> > >(srcVec,"globalMap"));
418 
419  // build the export vector as a view of the destination
420  GO lda = srcVec->getStride();
421  GO srcSize = srcVec->getGlobalLength()*srcVec->getNumVectors();
422  std::vector<ST> srcArray(srcSize);
423  Teuchos::ArrayView<ST> srcVals(srcArray);
424  srcVec->get1dCopy(srcVals,lda);
425  Tpetra::MultiVector<ST,LO,GO,NT> exportVector(rcpFromRef(globalMap),srcVals,lda,srcVec->getNumVectors());
426 
427  // perform the export
428  one.doExport(exportVector,**expItr,Tpetra::INSERT);
429  }
430 }
431 
432 // distribute one global vector into a many subvectors
433 void one2many(std::vector<RCP<Tpetra::MultiVector<ST,LO,GO,NT> > > & many,const Tpetra::MultiVector<ST,LO,GO,NT> & single,
434  const std::vector<RCP<Tpetra::Import<LO,GO,NT> > > & subImport)
435 {
436  // std::vector<RCP<Epetra_Vector> >::const_iterator vecItr;
437  std::vector<RCP<Tpetra::MultiVector<ST,LO,GO,NT> > >::const_iterator vecItr;
438  std::vector<RCP<Tpetra::Import<LO,GO,NT> > >::const_iterator impItr;
439 
440  // using Importers fill the sub vectors from the mama vector
441  for(vecItr=many.begin(),impItr=subImport.begin();
442  vecItr!=many.end();++vecItr,++impItr) {
443  // for ease of access to the destination
444  RCP<Tpetra::MultiVector<ST,LO,GO,NT> > destVec = *vecItr;
445 
446  // extract the map with global indicies from the current vector
447  const Tpetra::Map<LO,GO,NT> & globalMap = *(Teuchos::get_extra_data<RCP<Tpetra::Map<LO,GO,NT> > >(destVec,"globalMap"));
448 
449  // build the import vector as a view on the destination
450  GO destLDA = destVec->getStride();
451  GO destSize = destVec->getGlobalLength()*destVec->getNumVectors();
452  std::vector<ST> destArray(destSize);
453  Teuchos::ArrayView<ST> destVals(destArray);
454  destVec->get1dCopy(destVals,destLDA);
455  Tpetra::MultiVector<ST,LO,GO,NT> importVector(rcpFromRef(globalMap),destVals,destLDA,destVec->getNumVectors());
456 
457  // perform the import
458  importVector.doImport(single,**impItr,Tpetra::INSERT);
459 
460  Tpetra::Import<LO,GO,NT> importer(destVec->getMap(),destVec->getMap());
461  importVector.replaceMap(destVec->getMap());
462  destVec->doImport(importVector,importer,Tpetra::INSERT);
463 
464  }
465 }
466 
467 }
468 } // end namespace Tpetra
469 } // end namespace Teko