Tpetra Matrix/Vector Services  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Tpetra_BlockCrsGraph_def.hpp
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Tpetra: Templated Linear Algebra Services Package
5 // Copyright (2008) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ************************************************************************
40 // @HEADER
41 
42 #ifndef TPETRA_BLOCKCRSGRAPH_DEF_HPP
43 #define TPETRA_BLOCKCRSGRAPH_DEF_HPP
44 
45 #include "Tpetra_ConfigDefs.hpp"
46 #include "Tpetra_Vector.hpp"
47 
48 #ifdef DOXYGEN_USE_ONLY
50 #endif
51 
52 namespace Tpetra {
53 
54 //-----------------------------------------------------------------
55 template<class LocalOrdinal,class GlobalOrdinal,class Node>
56 Teuchos::RCP<const Tpetra::BlockMap<LocalOrdinal,GlobalOrdinal,Node> >
57 makeBlockColumnMap(
58  const Teuchos::RCP<const Tpetra::BlockMap<LocalOrdinal,GlobalOrdinal,Node> >& blockMap,
59  const Teuchos::RCP<const Tpetra::Map<LocalOrdinal,GlobalOrdinal,Node> >& ptColMap)
60 {
61  global_size_t numGlobalBlocks = Teuchos::OrdinalTraits<global_size_t>::invalid();
62  Teuchos::ArrayView<const GlobalOrdinal> blockIDs = ptColMap->getNodeElementList();
63  Teuchos::ArrayRCP<const LocalOrdinal> firstPoints = blockMap->getNodeFirstPointInBlocks();
64  Teuchos::Array<GlobalOrdinal> points(firstPoints.size()-1);
65  Teuchos::Array<LocalOrdinal> blockSizes(firstPoints.size()-1);
66 
67  typedef typename Teuchos::ArrayView<const LocalOrdinal>::size_type Tsize_t;
68  for(Tsize_t i=0; i<blockSizes.size(); ++i) {
69  points[i] = blockMap->getFirstGlobalPointInLocalBlock(i);
70  blockSizes[i] = firstPoints[i+1]-firstPoints[i];
71  }
72 
73  //We will create a block-map where each block corresponds to a point in
74  //the input-map 'ptColMap', and where each block's size is obtained from
75  //the input-block-map 'blockMap'.
76 
77  //if we are running on a single processor, then it's easy because
78  //we know that blockMap is distributed the same as ptColMap:
79  int numProcessors = ptColMap->getComm()->getSize();
80  if (numProcessors == 1) {
82  numGlobalBlocks, blockIDs, points(), blockSizes(),
83  ptColMap->getIndexBase(), ptColMap->getComm(), ptColMap->getNode()
84  ));
85  }
86 
87  //If we get to here, we're running on multiple processors, and blockMap
88  //is probably not distributed the same as ptColMap, so we have to do
89  //some communication to get the block-sizes from blockMap corresponding
90  //to the blockIDs we got from ptColMap.
91  //We also have to do communication to get the global first-points-in-block
92  //for the blocks in the new block-column-map.
93  //
94  //I think the simplest way to do this is to create vectors where the values
95  //are block-sizes (or first-points-in-block), and import one to the other.
98 
99  Teuchos::RCP<const Tpetra::Map<LocalOrdinal,GlobalOrdinal,Node> > blkPtMap =
100  convertBlockMapToPointMap(*blockMap);
101 
102  LOVec source_sizes(blkPtMap, blockSizes());
103  GOVec source_points(blkPtMap, points());
104  LOVec target_sizes(ptColMap);
105  GOVec target_points(ptColMap);
106 
107  Tpetra::Import<LocalOrdinal,GlobalOrdinal,Node> importer(blkPtMap, ptColMap);
108  target_sizes.doImport(source_sizes, importer, REPLACE);
109  target_points.doImport(source_points, importer, REPLACE);
110 
111  //now we can create our block-column-map:
113  numGlobalBlocks, blockIDs, target_points.get1dView()(), target_sizes.get1dView()(),
114  ptColMap->getIndexBase(), ptColMap->getComm(), ptColMap->getNode()
115  ));
116 }
117 
118 template<class LocalOrdinal,class GlobalOrdinal,class Node>
120  const Teuchos::RCP<const BlockMap<LocalOrdinal,GlobalOrdinal,Node> >& blkRowMap,
121  size_t maxNumEntriesPerRow, ProfileType pftype
122  )
123  : ptGraph_(),
124  blkRowMap_(blkRowMap),
125  blkColMap_(),
126  blkDomainMap_(),
127  blkRangeMap_()
128 {
129  ptGraph_ = Teuchos::rcp(new CrsGraph<LocalOrdinal,GlobalOrdinal,Node>(
130  convertBlockMapToPointMap(*blkRowMap), maxNumEntriesPerRow, pftype));
131 }
132 
133 //-------------------------------------------------------------------
134 template<class LocalOrdinal, class GlobalOrdinal, class Node>
135 void
136 BlockCrsGraph<LocalOrdinal,GlobalOrdinal,Node>::insertGlobalIndices(GlobalOrdinal row, const Teuchos::ArrayView<const GlobalOrdinal> &indices)
137 {
138  ptGraph_->insertGlobalIndices(row, indices);
139 }
140 
141 //-------------------------------------------------------------------
142 template<class LocalOrdinal, class GlobalOrdinal, class Node>
143 Teuchos::ArrayRCP<const size_t>
145 {
146  return ptGraph_->getNodeRowPtrs();
147 }
148 
149 //-------------------------------------------------------------------
150 template<class LocalOrdinal, class GlobalOrdinal, class Node>
151 Teuchos::ArrayRCP<const LocalOrdinal>
153 {
154  return ptGraph_->getNodePackedIndices();
155 }
156 
157 //-------------------------------------------------------------------
158 template<class LocalOrdinal, class GlobalOrdinal, class Node>
159 void
161 {
162  ptGraph_->globalAssemble();
163 }
164 
165 //-------------------------------------------------------------------
166 template<class LocalOrdinal, class GlobalOrdinal, class Node>
167 void
169 {
170  blkDomainMap_ = blkDomainMap;
171  blkRangeMap_ = blkRangeMap;
172 
173  RCP<ParameterList> params = parameterList();
174  if (os == DoOptimizeStorage) params->set("Optimize Storage",true);
175  else params->set("Optimize Storage",false);
176  ptGraph_->fillComplete(convertBlockMapToPointMap(*blkDomainMap),
177  convertBlockMapToPointMap(*blkRangeMap), params);
178 
179  //Now we need to take the point-column-map from ptGraph_ and create a
180  //corresponding block-column-map.
181  //Our block-column-map will have the same distribution as
182  //blkGraph_->getColMap, and we'll get block-size info from the
183  //blkDomainMap_. This will require some communication in cases
184  //where blkDomainMap is distributed differently.
185  blkColMap_ = makeBlockColumnMap(blkDomainMap_, ptGraph_->getColMap());
186 }
187 
188 //-------------------------------------------------------------------
189 template<class LocalOrdinal, class GlobalOrdinal, class Node>
190 void
192 {
193  fillComplete(getBlockRowMap(), getBlockRowMap(), os);
194 }
195 
196 //-------------------------------------------------------------------
197 template<class LocalOrdinal, class GlobalOrdinal, class Node>
198 void
200 {
201  if (ptGraph_->isFillComplete()) {
202  ptGraph_->resumeFill();
203  }
204  RCP<ParameterList> params = parameterList();
205  params->set("Optimize Storage",true);
206  ptGraph_->fillComplete(params);
207 }
208 
209 //-------------------------------------------------------------------
210 template<class LocalOrdinal, class GlobalOrdinal, class Node>
211 bool
213 {
214  return ptGraph_->isFillComplete();
215 }
216 
217 //-------------------------------------------------------------------
218 template<class LocalOrdinal, class GlobalOrdinal, class Node>
219 bool
221 {
222  return ptGraph_->isUpperTriangular();
223 }
224 
225 //-------------------------------------------------------------------
226 template<class LocalOrdinal, class GlobalOrdinal, class Node>
227 bool
229 {
230  return ptGraph_->isLowerTriangular();
231 }
232 
233 //-------------------------------------------------------------------
234 template<class LocalOrdinal, class GlobalOrdinal, class Node>
235 bool
237 {
238  return ptGraph_->isLocallyIndexed();
239 }
240 
241 //-------------------------------------------------------------------
242 template<class LocalOrdinal, class GlobalOrdinal, class Node>
243 size_t
245 {
246  return ptGraph_->getNodeNumEntries();
247 }
248 
249 //-------------------------------------------------------------------
250 template<class LocalOrdinal, class GlobalOrdinal, class Node>
251 size_t
253 {
254  return ptGraph_->getNumEntriesInGlobalRow(row);
255 }
256 
257 //-------------------------------------------------------------------
258 template<class LocalOrdinal, class GlobalOrdinal, class Node>
259 void
261  Teuchos::ArrayView<const GlobalOrdinal>& blockCols) const
262 {
263  ptGraph_->getGlobalRowView(row, blockCols);
264 }
265 
266 //-------------------------------------------------------------------
267 template<class LocalOrdinal, class GlobalOrdinal, class Node>
268 void
270  Teuchos::ArrayView<const LocalOrdinal>& blockCols) const
271 {
272  ptGraph_->getLocalRowView(row, blockCols);
273 }
274 
275 //-------------------------------------------------------------------
276 template<class LocalOrdinal, class GlobalOrdinal, class Node>
277 size_t
279 {
280  return ptGraph_->getNodeNumRows();
281 }
282 
283 //-------------------------------------------------------------------
284 template<class LocalOrdinal, class GlobalOrdinal, class Node>
285 size_t
287 {
288  return ptGraph_->getGlobalNumRows();
289 }
290 
291 //-------------------------------------------------------------------
292 template<class LocalOrdinal, class GlobalOrdinal, class Node>
293 size_t
295 {
296  return ptGraph_->getNodeNumDiags();
297 }
298 
299 //-------------------------------------------------------------------
300 template<class LocalOrdinal, class GlobalOrdinal, class Node>
301 const Teuchos::RCP<const BlockMap<LocalOrdinal,GlobalOrdinal,Node> > &
303 {
304  return blkRowMap_;
305 }
306 
307 //-------------------------------------------------------------------
308 template<class LocalOrdinal, class GlobalOrdinal, class Node>
309 const Teuchos::RCP<const BlockMap<LocalOrdinal,GlobalOrdinal,Node> > &
311 {
312  return blkColMap_;
313 }
314 
315 //-------------------------------------------------------------------
316 template<class LocalOrdinal, class GlobalOrdinal, class Node>
317 const Teuchos::RCP<const BlockMap<LocalOrdinal,GlobalOrdinal,Node> > &
319 {
320  return blkDomainMap_;
321 }
322 
323 //-------------------------------------------------------------------
324 template<class LocalOrdinal, class GlobalOrdinal, class Node>
325 const Teuchos::RCP<const BlockMap<LocalOrdinal,GlobalOrdinal,Node> > &
327 {
328  return blkRangeMap_;
329 }
330 
331 //
332 // Explicit instantiation macro
333 //
334 // Must be expanded from within the Tpetra namespace!
335 //
336 
337 #define TPETRA_BLOCKCRSGRAPH_INSTANT(LO,GO,NODE) \
338  \
339  template class BlockCrsGraph< LO , GO , NODE >;
340 
341 
342 }//namespace Tpetra
343 
344 #endif
345 
Block-entry counterpart to Tpetra::Map.
size_t getNodeNumBlockRows() const
Returns the number of block rows owned on the calling node.
Communication plan for data redistribution from a uniquely-owned to a (possibly) multiply-owned distr...
size_t getGlobalBlockRowLength(GlobalOrdinal row) const
Returns the number of block-columns in the specified global block row.
size_t getNodeNumBlockDiags() const
Returns the number of diagonal entries on the calling node.
size_t getNodeNumBlockEntries() const
Returns the local number of entries in the graph.
void getGlobalBlockRowView(GlobalOrdinal row, Teuchos::ArrayView< const GlobalOrdinal > &blockCols) const
Returns a read-only view of the block-column-indices for the specified global block row...
void fillComplete(const Teuchos::RCP< const BlockMap< LocalOrdinal, GlobalOrdinal, Node > > &blkDomainMap, const Teuchos::RCP< const BlockMap< LocalOrdinal, GlobalOrdinal, Node > > &blkRangeMap, OptimizeOption os=DoOptimizeStorage)
Signal that data entry is complete, specifying domain and range maps. Off-node entries are distribute...
bool isUpperTriangular() const
true if graph is upper-triangular.
const Teuchos::RCP< const BlockMap< LocalOrdinal, GlobalOrdinal, Node > > & getBlockColMap() const
Returns the block-column map.
size_t global_size_t
Global size_t object.
size_t getGlobalNumBlockRows() const
Returns the global number of block rows.
void insertGlobalIndices(GlobalOrdinal row, const Teuchos::ArrayView< const GlobalOrdinal > &indices)
Submit graph indices, using global IDs.
Teuchos::ArrayRCP< const size_t > getNodeRowOffsets() const
Get row-offsets. (This is the bptr array in VBR terminology.)
const Teuchos::RCP< const BlockMap< LocalOrdinal, GlobalOrdinal, Node > > & getBlockRowMap() const
Returns the block-row map.
const Teuchos::RCP< const BlockMap< LocalOrdinal, GlobalOrdinal, Node > > & getBlockDomainMap() const
Returns the block-domain map.
bool isLocallyIndexed() const
If graph indices are in the local range, this function returns true. Otherwise, this function returns...
Teuchos::ArrayRCP< const LocalOrdinal > getNodePackedIndices() const
Get packed-col-indices. (This is the bindx array in VBR terminology.)
BlockCrsGraph(const Teuchos::RCP< const BlockMap< LocalOrdinal, GlobalOrdinal, Node > > &blkRowMap, size_t maxNumEntriesPerRow, ProfileType pftype=DynamicProfile)
BlockCrsGraph constructor specifying a block-row-map and max-num-block-entries-per-row.
void optimizeStorage()
Re-allocate the data into contiguous storage.
A distributed graph accessed by rows (adjacency lists) and stored sparsely.
Describes a parallel distribution of objects over processes.
A distributed dense vector.
bool isFillComplete() const
Returns true if fillComplete() has been called.
bool isLowerTriangular() const
true if graph is lower-triangular.
void globalAssemble()
Communicate non-local contributions to other nodes.
const Teuchos::RCP< const BlockMap< LocalOrdinal, GlobalOrdinal, Node > > & getBlockRangeMap() const
Returns the block-range map.
void getLocalBlockRowView(LocalOrdinal row, Teuchos::ArrayView< const LocalOrdinal > &blockCols) const
Returns a read-only view of the block-column-indices for the specified local block row...