Teko  Version of the Day
 All Classes Files Functions Variables Pages
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 #ifndef NDEBUG
126  LO numMyElmts = numLocalVars * numBlocks;
127 #endif
128 
129  // create global arrays describing the as of yet uncreated maps
130  std::vector<GO> subGlobals;
131  std::vector<GO> contigGlobals; // the contiguous globals
132 
133  // loop over each block of variables
134  LO count = 0;
135  for(LO blockNum=0;blockNum<numBlocks;blockNum++) {
136 
137  // loop over each local variable in the block
138  for(LO local=0;local<numLocalVars;++local) {
139  // global block number = minGID+blockNum
140  // block begin global id = numGlobalVars*(minGID+blockNum)
141  // global id block offset = blockOffset+local
142  subGlobals.push_back((minBlockID+blockNum)*numGlobalVars+blockOffset+local);
143 
144  // also build the contiguous IDs
145  contigGlobals.push_back(numLocalVars*minBlockID+count);
146  count++;
147  }
148  }
149 
150  // sanity check
151  assert((size_t) numMyElmts==subGlobals.size());
152 
153  // create the map with contiguous elements and the map with global elements
154  RCP<Tpetra::Map<LO,GO,NT> > subMap = rcp(new Tpetra::Map<LO,GO,NT>(numAllElmts,Teuchos::ArrayView<GO>(subGlobals),0,rcpFromRef(comm)));
155  RCP<Tpetra::Map<LO,GO,NT> > contigMap = rcp(new Tpetra::Map<LO,GO,NT>(numAllElmts,Teuchos::ArrayView<GO>(contigGlobals),0,rcpFromRef(comm)));
156 
157  Teuchos::set_extra_data(contigMap,"contigMap",Teuchos::inOutArg(subMap));
158  subMaps.push_back(std::make_pair(numLocalVars,subMap));
159 
160  // update the block offset
161  blockOffset += numLocalVars;
162  }
163 }
164 
165 void buildExportImport(const Tpetra::Map<LO,GO,NT> & baseMap, const std::vector<std::pair<int,RCP<Tpetra::Map<LO,GO,NT> > > > & subMaps,
166  std::vector<RCP<Tpetra::Export<LO,GO,NT> > > & subExport,
167  std::vector<RCP<Tpetra::Import<LO,GO,NT> > > & subImport)
168 {
169  std::vector<std::pair<int,RCP<Tpetra::Map<LO,GO,NT> > > >::const_iterator mapItr;
170 
171  // build importers and exporters
172  for(mapItr=subMaps.begin();mapItr!=subMaps.end();++mapItr) {
173  // exctract basic map
174  const Tpetra::Map<LO,GO,NT> & map = *(mapItr->second);
175 
176  // add new elements to vectors
177  subImport.push_back(rcp(new Tpetra::Import<LO,GO,NT>(rcpFromRef(baseMap),rcpFromRef(map))));
178  subExport.push_back(rcp(new Tpetra::Export<LO,GO,NT>(rcpFromRef(map),rcpFromRef(baseMap))));
179  }
180 }
181 
182 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)
183 {
184  std::vector<std::pair<int,RCP<Tpetra::Map<LO,GO,NT> > > >::const_iterator mapItr;
185 
186  // build vectors
187  for(mapItr=subMaps.begin();mapItr!=subMaps.end();++mapItr) {
188  // exctract basic map
189  const Tpetra::Map<LO,GO,NT> & map = *(Teuchos::get_extra_data<RCP<Tpetra::Map<LO,GO,NT> > >(mapItr->second,"contigMap"));
190 
191  // add new elements to vectors
192  RCP<Tpetra::MultiVector<ST,LO,GO,NT> > mv = rcp(new Tpetra::MultiVector<ST,LO,GO,NT>(rcpFromRef(map),count));
193  Teuchos::set_extra_data(mapItr->second,"globalMap",Teuchos::inOutArg(mv));
194  subVectors.push_back(mv);
195  }
196 }
197 
198 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)
199 {
200  std::vector<std::pair<int,RCP<Tpetra::Map<LO,GO,NT> > > >::const_iterator mapItr;
201  std::vector<RCP<const Tpetra::MultiVector<ST,LO,GO,NT> > >::iterator vecItr;
202 
203  TEUCHOS_ASSERT(subMaps.size()==subVectors.size());
204 
205  // associate the sub vectors with the subMaps
206  for(mapItr=subMaps.begin(),vecItr=subVectors.begin();mapItr!=subMaps.end();++mapItr,++vecItr)
207  Teuchos::set_extra_data(mapItr->second,"globalMap",Teuchos::inOutArg(*vecItr),Teuchos::POST_DESTROY,false);
208 }
209 
210 // build a single subblock Epetra_CrsMatrix
211 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)
212 {
213  // get the number of variables families
214  int numVarFamily = subMaps.size();
215 
216  TEUCHOS_ASSERT(i>=0 && i<numVarFamily);
217  TEUCHOS_ASSERT(j>=0 && j<numVarFamily);
218 
219  const Tpetra::Map<LO,GO,NT> & gRowMap = *subMaps[i].second;
220  const Tpetra::Map<LO,GO,NT> & rowMap = *Teuchos::get_extra_data<RCP<Tpetra::Map<LO,GO,NT> > >(subMaps[i].second,"contigMap");
221  const Tpetra::Map<LO,GO,NT> & colMap = *Teuchos::get_extra_data<RCP<Tpetra::Map<LO,GO,NT> > >(subMaps[j].second,"contigMap");
222  int colFamilyCnt = subMaps[j].first;
223 
224  // compute the number of global variables
225  // and the row and column block offset
226  GO numGlobalVars = 0;
227  GO rowBlockOffset = 0;
228  GO colBlockOffset = 0;
229  for(int k=0;k<numVarFamily;k++) {
230  numGlobalVars += subMaps[k].first;
231 
232  // compute block offsets
233  if(k<i) rowBlockOffset += subMaps[k].first;
234  if(k<j) colBlockOffset += subMaps[k].first;
235  }
236 
237  // copy all global rows to here
238  Tpetra::Import<LO,GO,NT> import(A->getRowMap(),rcpFromRef(gRowMap));
239  Tpetra::CrsMatrix<ST,LO,GO,NT> localA(rcpFromRef(gRowMap),0);
240  localA.doImport(*A,import,Tpetra::INSERT);
241 
242  RCP<Tpetra::CrsMatrix<ST,LO,GO,NT> > mat = Tpetra::createCrsMatrix<ST,LO,GO,NT>(rcpFromRef(rowMap),0);
243 
244  // get entry information
245  LO numMyRows = rowMap.getNodeNumElements();
246  LO maxNumEntries = A->getGlobalMaxNumRowEntries();
247 
248  // for extraction
249  std::vector<GO> indices(maxNumEntries);
250  std::vector<ST> values(maxNumEntries);
251 
252  // for insertion
253  std::vector<GO> colIndices(maxNumEntries);
254  std::vector<ST> colValues(maxNumEntries);
255 
256  // insert each row into subblock
257  // let FillComplete handle column distribution
258  for(LO localRow=0;localRow<numMyRows;localRow++) {
259  size_t numEntries = -1;
260  GO globalRow = gRowMap.getGlobalElement(localRow);
261  GO contigRow = rowMap.getGlobalElement(localRow);
262 
263  TEUCHOS_ASSERT(globalRow>=0);
264  TEUCHOS_ASSERT(contigRow>=0);
265 
266  // extract a global row copy
267  localA.getGlobalRowCopy(globalRow, Teuchos::ArrayView<GO>(indices), Teuchos::ArrayView<ST>(values), numEntries);
268  LO numOwnedCols = 0;
269  for(size_t localCol=0;localCol<numEntries;localCol++) {
270  GO globalCol = indices[localCol];
271 
272  // determinate which block this column ID is in
273  int block = globalCol / numGlobalVars;
274 
275  bool inFamily = true;
276 
277  // test the beginning of the block
278  inFamily &= (block*numGlobalVars+colBlockOffset <= globalCol);
279  inFamily &= ((block*numGlobalVars+colBlockOffset+colFamilyCnt) > globalCol);
280 
281  // is this column in the variable family
282  if(inFamily) {
283  GO familyOffset = globalCol-(block*numGlobalVars+colBlockOffset);
284 
285  colIndices[numOwnedCols] = block*colFamilyCnt + familyOffset;
286  colValues[numOwnedCols] = values[localCol];
287 
288  numOwnedCols++;
289  }
290  }
291 
292  // insert it into the new matrix
293  colIndices.resize(numOwnedCols);
294  colValues.resize(numOwnedCols);
295  mat->insertGlobalValues(contigRow,Teuchos::ArrayView<GO>(colIndices),Teuchos::ArrayView<ST>(colValues));
296  colIndices.resize(maxNumEntries);
297  colValues.resize(maxNumEntries);
298  }
299 
300  // fill it and automagically optimize the storage
301  mat->fillComplete(rcpFromRef(colMap),rcpFromRef(rowMap));
302 
303  return mat;
304 }
305 
306 // rebuild a single subblock Epetra_CrsMatrix
307 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)
308 {
309  // get the number of variables families
310  int numVarFamily = subMaps.size();
311 
312  TEUCHOS_ASSERT(i>=0 && i<numVarFamily);
313  TEUCHOS_ASSERT(j>=0 && j<numVarFamily);
314  TEUCHOS_ASSERT(mat.isFillComplete());
315 
316  const Tpetra::Map<LO,GO,NT> & gRowMap = *subMaps[i].second;
317  const Tpetra::Map<LO,GO,NT> & rowMap = *Teuchos::get_extra_data<RCP<Tpetra::Map<LO,GO,NT> > >(subMaps[i].second,"contigMap");
318  const Tpetra::Map<LO,GO,NT> & colMap = *Teuchos::get_extra_data<RCP<Tpetra::Map<LO,GO,NT> > >(subMaps[j].second,"contigMap");
319  int colFamilyCnt = subMaps[j].first;
320 
321  // compute the number of global variables
322  // and the row and column block offset
323  GO numGlobalVars = 0;
324  GO rowBlockOffset = 0;
325  GO colBlockOffset = 0;
326  for(int k=0;k<numVarFamily;k++) {
327  numGlobalVars += subMaps[k].first;
328 
329  // compute block offsets
330  if(k<i) rowBlockOffset += subMaps[k].first;
331  if(k<j) colBlockOffset += subMaps[k].first;
332  }
333 
334  // copy all global rows to here
335  Tpetra::Import<LO,GO,NT> import(A->getRowMap(),rcpFromRef(gRowMap));
336  Tpetra::CrsMatrix<ST,LO,GO,NT> localA(rcpFromRef(gRowMap),0);
337  localA.doImport(*A,import,Tpetra::INSERT);
338 
339  // clear out the old matrix
340  mat.resumeFill();
341  mat.setAllToScalar(0.0);
342 
343  // get entry information
344  LO numMyRows = rowMap.getNodeNumElements();
345  GO maxNumEntries = A->getGlobalMaxNumRowEntries();
346 
347  // for extraction
348  std::vector<GO> indices(maxNumEntries);
349  std::vector<ST> values(maxNumEntries);
350 
351  // for insertion
352  std::vector<GO> colIndices(maxNumEntries);
353  std::vector<ST> colValues(maxNumEntries);
354 
355  // insert each row into subblock
356  // let FillComplete handle column distribution
357  for(LO localRow=0;localRow<numMyRows;localRow++) {
358  size_t numEntries = -1;
359  GO globalRow = gRowMap.getGlobalElement(localRow);
360  GO contigRow = rowMap.getGlobalElement(localRow);
361 
362  TEUCHOS_ASSERT(globalRow>=0);
363  TEUCHOS_ASSERT(contigRow>=0);
364 
365  // extract a global row copy
366  localA.getGlobalRowCopy(globalRow, Teuchos::ArrayView<GO>(indices), Teuchos::ArrayView<ST>(values), numEntries);
367 
368  LO numOwnedCols = 0;
369  for(size_t localCol=0;localCol<numEntries;localCol++) {
370  GO globalCol = indices[localCol];
371 
372  // determinate which block this column ID is in
373  int block = globalCol / numGlobalVars;
374 
375  bool inFamily = true;
376 
377  // test the beginning of the block
378  inFamily &= (block*numGlobalVars+colBlockOffset <= globalCol);
379  inFamily &= ((block*numGlobalVars+colBlockOffset+colFamilyCnt) > globalCol);
380 
381  // is this column in the variable family
382  if(inFamily) {
383  GO familyOffset = globalCol-(block*numGlobalVars+colBlockOffset);
384 
385  colIndices[numOwnedCols] = block*colFamilyCnt + familyOffset;
386  colValues[numOwnedCols] = values[localCol];
387 
388  numOwnedCols++;
389  }
390  }
391 
392  // insert it into the new matrix
393  colIndices.resize(numOwnedCols);
394  colValues.resize(numOwnedCols);
395  mat.sumIntoGlobalValues(contigRow,Teuchos::ArrayView<GO>(colIndices),Teuchos::ArrayView<ST>(colValues));
396  colIndices.resize(maxNumEntries);
397  colValues.resize(maxNumEntries);
398  }
399  mat.fillComplete(rcpFromRef(colMap),rcpFromRef(rowMap));
400 }
401 
402 
403 // collect subvectors into a single global vector
404 void many2one(Tpetra::MultiVector<ST,LO,GO,NT> & one, const std::vector<RCP<const Tpetra::MultiVector<ST,LO,GO,NT> > > & many,
405  const std::vector<RCP<Tpetra::Export<LO,GO,NT> > > & subExport)
406 {
407  // std::vector<RCP<const Epetra_Vector> >::const_iterator vecItr;
408  std::vector<RCP<const Tpetra::MultiVector<ST,LO,GO,NT> > >::const_iterator vecItr;
409  std::vector<RCP<Tpetra::Export<LO,GO,NT> > >::const_iterator expItr;
410 
411  // using Exporters fill the empty vector from the sub-vectors
412  for(vecItr=many.begin(),expItr=subExport.begin();
413  vecItr!=many.end();++vecItr,++expItr) {
414 
415  // for ease of access to the source
416  RCP<const Tpetra::MultiVector<ST,LO,GO,NT> > srcVec = *vecItr;
417 
418  // extract the map with global indicies from the current vector
419  const Tpetra::Map<LO,GO,NT> & globalMap = *(Teuchos::get_extra_data<RCP<Tpetra::Map<LO,GO,NT> > >(srcVec,"globalMap"));
420 
421  // build the export vector as a view of the destination
422  GO lda = srcVec->getStride();
423  GO srcSize = srcVec->getGlobalLength()*srcVec->getNumVectors();
424  std::vector<ST> srcArray(srcSize);
425  Teuchos::ArrayView<ST> srcVals(srcArray);
426  srcVec->get1dCopy(srcVals,lda);
427  Tpetra::MultiVector<ST,LO,GO,NT> exportVector(rcpFromRef(globalMap),srcVals,lda,srcVec->getNumVectors());
428 
429  // perform the export
430  one.doExport(exportVector,**expItr,Tpetra::INSERT);
431  }
432 }
433 
434 // distribute one global vector into a many subvectors
435 void one2many(std::vector<RCP<Tpetra::MultiVector<ST,LO,GO,NT> > > & many,const Tpetra::MultiVector<ST,LO,GO,NT> & single,
436  const std::vector<RCP<Tpetra::Import<LO,GO,NT> > > & subImport)
437 {
438  // std::vector<RCP<Epetra_Vector> >::const_iterator vecItr;
439  std::vector<RCP<Tpetra::MultiVector<ST,LO,GO,NT> > >::const_iterator vecItr;
440  std::vector<RCP<Tpetra::Import<LO,GO,NT> > >::const_iterator impItr;
441 
442  // using Importers fill the sub vectors from the mama vector
443  for(vecItr=many.begin(),impItr=subImport.begin();
444  vecItr!=many.end();++vecItr,++impItr) {
445  // for ease of access to the destination
446  RCP<Tpetra::MultiVector<ST,LO,GO,NT> > destVec = *vecItr;
447 
448  // extract the map with global indicies from the current vector
449  const Tpetra::Map<LO,GO,NT> & globalMap = *(Teuchos::get_extra_data<RCP<Tpetra::Map<LO,GO,NT> > >(destVec,"globalMap"));
450 
451  // build the import vector as a view on the destination
452  GO destLDA = destVec->getStride();
453  GO destSize = destVec->getGlobalLength()*destVec->getNumVectors();
454  std::vector<ST> destArray(destSize);
455  Teuchos::ArrayView<ST> destVals(destArray);
456  destVec->get1dCopy(destVals,destLDA);
457  Tpetra::MultiVector<ST,LO,GO,NT> importVector(rcpFromRef(globalMap),destVals,destLDA,destVec->getNumVectors());
458 
459  // perform the import
460  importVector.doImport(single,**impItr,Tpetra::INSERT);
461 
462  Tpetra::Import<LO,GO,NT> importer(destVec->getMap(),destVec->getMap());
463  importVector.replaceMap(destVec->getMap());
464  destVec->doImport(importVector,importer,Tpetra::INSERT);
465 
466  }
467 }
468 
469 }
470 } // end namespace Tpetra
471 } // end namespace Teko