Amesos2 - Direct Sparse Solver Interfaces  Version of the Day
Amesos2_MatrixAdapter_def.hpp
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // Amesos2: Templated Direct Sparse Solver Package
6 // Copyright 2011 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
39 //
40 // ***********************************************************************
41 //
42 // @HEADER
43 
44 
45 #ifndef AMESOS2_MATRIXADAPTER_DEF_HPP
46 #define AMESOS2_MATRIXADAPTER_DEF_HPP
47 #include <Tpetra_Util.hpp>
48 #include "Amesos2_MatrixAdapter_decl.hpp"
49 #include "Amesos2_ConcreteMatrixAdapter_def.hpp"
50 //#include "Amesos2_ConcreteMatrixAdapter.hpp"
51 
52 
53 namespace Amesos2 {
54 
55 
56  template < class Matrix >
57  MatrixAdapter<Matrix>::MatrixAdapter(const Teuchos::RCP<Matrix> m)
58  : mat_(m)
59  {
60  comm_ = static_cast<const adapter_t*>(this)->getComm_impl();
61  col_map_ = static_cast<const adapter_t*>(this)->getColMap_impl();
62  row_map_ = static_cast<const adapter_t*>(this)->getRowMap_impl();
63  }
64 
65  template < class Matrix >
66  void
67  MatrixAdapter<Matrix>::getCrs(const Teuchos::ArrayView<scalar_t> nzval,
68  const Teuchos::ArrayView<global_ordinal_t> colind,
69  const Teuchos::ArrayView<typename MatrixAdapter<Matrix>::global_size_t> rowptr,
70  typename MatrixAdapter<Matrix>::global_size_t& nnz,
71  const Teuchos::Ptr<const Tpetra::Map<local_ordinal_t, global_ordinal_t, node_t> > rowmap,
72  EStorage_Ordering ordering) const
73  {
74  help_getCrs(nzval, colind, rowptr,
75  nnz, rowmap, ordering,
76  typename adapter_t::get_crs_spec());
77  }
78 
79  template < class Matrix >
80  void
81  MatrixAdapter<Matrix>::getCrs(const Teuchos::ArrayView<scalar_t> nzval,
82  const Teuchos::ArrayView<global_ordinal_t> colind,
83  const Teuchos::ArrayView<typename MatrixAdapter<Matrix>::global_size_t> rowptr,
84  typename MatrixAdapter<Matrix>::global_size_t& nnz,
85  EDistribution distribution,
86  EStorage_Ordering ordering) const
87  {
88  const Teuchos::RCP<const Tpetra::Map<local_ordinal_t,global_ordinal_t,node_t> > rowmap
89  = Util::getDistributionMap<local_ordinal_t,global_ordinal_t,global_size_t,node_t>(distribution,
90  this->getGlobalNumRows(),
91  this->getComm());
92  this->getCrs(nzval, colind, rowptr, nnz, Teuchos::ptrInArg(*rowmap), ordering);
93  }
94 
95  template < class Matrix >
96  void
97  MatrixAdapter<Matrix>::getCcs(const Teuchos::ArrayView<scalar_t> nzval,
98  const Teuchos::ArrayView<global_ordinal_t> rowind,
99  const Teuchos::ArrayView<typename MatrixAdapter<Matrix>::global_size_t> colptr,
100  typename MatrixAdapter<Matrix>::global_size_t& nnz,
101  const Teuchos::Ptr<const Tpetra::Map<local_ordinal_t, global_ordinal_t, node_t> > colmap,
102  EStorage_Ordering ordering) const
103  {
104  help_getCcs(nzval, rowind, colptr,
105  nnz, colmap, ordering,
106  typename adapter_t::get_ccs_spec());
107  }
108 
109  template < class Matrix >
110  void
111  MatrixAdapter<Matrix>::getCcs(const Teuchos::ArrayView<scalar_t> nzval,
112  const Teuchos::ArrayView<global_ordinal_t> rowind,
113  const Teuchos::ArrayView<typename MatrixAdapter<Matrix>::global_size_t> colptr,
114  typename MatrixAdapter<Matrix>::global_size_t& nnz,
115  EDistribution distribution,
116  EStorage_Ordering ordering) const
117  {
118  const Teuchos::RCP<const Tpetra::Map<local_ordinal_t,global_ordinal_t,node_t> > colmap
119  = Util::getDistributionMap<local_ordinal_t,global_ordinal_t,global_size_t,node_t>(distribution,
120  this->getGlobalNumCols(),
121  this->getComm());
122  this->getCcs(nzval, rowind, colptr, nnz, Teuchos::ptrInArg(*colmap), ordering);
123  }
124 
125  template < class Matrix >
126  typename MatrixAdapter<Matrix>::global_size_t
128  {
129  return static_cast<const adapter_t*>(this)->getGlobalNumRows_impl();
130  }
131 
132  template < class Matrix >
133  typename MatrixAdapter<Matrix>::global_size_t
135  {
136  return static_cast<const adapter_t*>(this)->getGlobalNumCols_impl();
137  }
138 
139  template < class Matrix >
140  typename MatrixAdapter<Matrix>::global_size_t
142  {
143  return row_map_->getIndexBase();
144  }
145 
146  template < class Matrix >
147  typename MatrixAdapter<Matrix>::global_size_t
149  {
150  return col_map_->getIndexBase();
151  }
152 
153  template < class Matrix >
154  typename MatrixAdapter<Matrix>::global_size_t
156  {
157  return static_cast<const adapter_t*>(this)->getGlobalNNZ_impl();
158  }
159 
160  template < class Matrix >
161  size_t
163  {
164  return row_map_->getNodeNumElements(); // TODO: verify
165  }
166 
167  template < class Matrix >
168  size_t
170  {
171  return col_map_->getNodeNumElements();
172  }
173 
174  template < class Matrix >
175  size_t
177  {
178  return static_cast<const adapter_t*>(this)->getLocalNNZ_impl();
179  }
180 
181  template < class Matrix >
182  std::string
184  {
185  std::ostringstream oss;
186  oss << "Amesos2::MatrixAdapter wrapping: ";
187  oss << mat_->description();
188  return oss.str();
189  }
190 
191  template < class Matrix >
192  void
193  MatrixAdapter<Matrix>::describe(Teuchos::FancyOStream &out,
194  const Teuchos::EVerbosityLevel verbLevel) const
195  {}
196 
197 
198 
199  /******************************
200  * Private method definitions *
201  ******************************/
202 
203  template < class Matrix >
204  void
205  MatrixAdapter<Matrix>::help_getCrs(const Teuchos::ArrayView<scalar_t> nzval,
206  const Teuchos::ArrayView<global_ordinal_t> colind,
207  const Teuchos::ArrayView<typename MatrixAdapter<Matrix>::global_size_t> rowptr,
208  typename MatrixAdapter<Matrix>::global_size_t& nnz,
209  const Teuchos::Ptr<const Tpetra::Map<local_ordinal_t,global_ordinal_t,node_t> > rowmap,
210  EStorage_Ordering ordering,
211  has_special_impl hsi) const
212  {
213  static_cast<const adapter_t*>(this)->getCrs_spec(nzval, colind, rowptr,
214  nnz, rowmap, ordering);
215  }
216 
217  template < class Matrix >
218  void
219  MatrixAdapter<Matrix>::help_getCrs(const Teuchos::ArrayView<scalar_t> nzval,
220  const Teuchos::ArrayView<global_ordinal_t> colind,
221  const Teuchos::ArrayView<typename MatrixAdapter<Matrix>::global_size_t> rowptr,
222  typename MatrixAdapter<Matrix>::global_size_t& nnz,
223  const Teuchos::Ptr<const Tpetra::Map<local_ordinal_t,global_ordinal_t,node_t> > rowmap,
224  EStorage_Ordering ordering,
225  no_special_impl nsi) const
226  {
227  do_getCrs(nzval, colind, rowptr,
228  nnz, rowmap, ordering,
229  typename adapter_t::major_access());
230  }
231 
232  template < class Matrix >
233  void
234  MatrixAdapter<Matrix>::do_getCrs(const Teuchos::ArrayView<scalar_t> nzval,
235  const Teuchos::ArrayView<global_ordinal_t> colind,
236  const Teuchos::ArrayView<typename MatrixAdapter<Matrix>::global_size_t> rowptr,
237  typename MatrixAdapter<Matrix>::global_size_t& nnz,
238  const Teuchos::Ptr<const Tpetra::Map<local_ordinal_t,global_ordinal_t,node_t> > rowmap,
239  EStorage_Ordering ordering,
240  row_access ra) const
241  {
242  using Teuchos::rcp;
243  using Teuchos::RCP;
244  using Teuchos::ArrayView;
245  using Teuchos::OrdinalTraits;
246 
247  RCP<const type> get_mat;
248  if( *rowmap == *this->row_map_ ){
249  // No need to redistribute
250  get_mat = rcp(this,false); // non-owning
251  } else {
252  get_mat = get(rowmap);
253  }
254  // RCP<const type> get_mat = get(rowmap);
255 
256  // rmap may not necessarily check same as rowmap because rmap may
257  // have been constructued with Tpetra's "expert" constructor,
258  // which assumes that the map points are non-contiguous.
259  //
260  // TODO: There may be some more checking between the row map
261  // compatibility, but things are working fine now.
262  RCP<const Tpetra::Map<local_ordinal_t,global_ordinal_t,node_t> > rmap = get_mat->getRowMap();
263 
264  ArrayView<const global_ordinal_t> node_elements = rmap->getNodeElementList();
265  if( node_elements.size() == 0 ) return; // no more contribution
266 
267  typename ArrayView<const global_ordinal_t>::iterator row_it, row_end;
268  row_end = node_elements.end();
269 
270  size_t rowptr_ind = OrdinalTraits<size_t>::zero();
271  global_ordinal_t rowInd = OrdinalTraits<global_ordinal_t>::zero();
272  for( row_it = node_elements.begin(); row_it != row_end; ++row_it ){
273  rowptr[rowptr_ind++] = rowInd;
274  size_t rowNNZ = get_mat->getGlobalRowNNZ(*row_it);
275  size_t nnzRet = OrdinalTraits<size_t>::zero();
276  ArrayView<global_ordinal_t> colind_view = colind.view(rowInd,rowNNZ);
277  ArrayView<scalar_t> nzval_view = nzval.view(rowInd,rowNNZ);
278 
279  get_mat->getGlobalRowCopy(*row_it, colind_view, nzval_view, nnzRet);
280  for (size_t rr = 0; rr < nnzRet ; rr++)
281  {
282  colind_view[rr] = colind_view[rr] - rmap->getIndexBase();
283  }
284 
285  // It was suggested that instead of sorting each row's indices
286  // individually, that we instead do a double-transpose at the
287  // end, which would also lead to the indices being sorted.
288  if( ordering == SORTED_INDICES ){
289  Tpetra::sort2(colind_view.begin(), colind_view.end(), nzval_view.begin());
290  }
291 
292  TEUCHOS_TEST_FOR_EXCEPTION( rowNNZ != nnzRet,
293  std::runtime_error,
294  "Number of values returned different from "
295  "number of values reported");
296  rowInd += rowNNZ;
297  }
298  rowptr[rowptr_ind] = nnz = rowInd;
299  }
300 
301  // TODO: This may not work with distributed matrices.
302  template < class Matrix >
303  void
304  MatrixAdapter<Matrix>::do_getCrs(const Teuchos::ArrayView<scalar_t> nzval,
305  const Teuchos::ArrayView<global_ordinal_t> colind,
306  const Teuchos::ArrayView<typename MatrixAdapter<Matrix>::global_size_t> rowptr,
307  typename MatrixAdapter<Matrix>::global_size_t& nnz,
308  const Teuchos::Ptr<const Tpetra::Map<local_ordinal_t,global_ordinal_t,node_t> > rowmap,
309  EStorage_Ordering ordering,
310  col_access ca) const
311  {
312  using Teuchos::Array;
313  // get the ccs and transpose
314 
315  Array<scalar_t> nzval_tmp(nzval.size(), 0);
316  Array<global_ordinal_t> rowind(colind.size(), 0);
317  Array<global_size_t> colptr(this->getGlobalNumCols() + 1);
318  this->getCcs(nzval_tmp(), rowind(), colptr(), nnz, rowmap, ordering);
319 
320  if( !nzval.is_null() && !colind.is_null() && !rowptr.is_null() )
321  Util::transpose(nzval_tmp(), rowind(), colptr(), nzval, colind, rowptr);
322  }
323 
324  template < class Matrix >
325  void
326  MatrixAdapter<Matrix>::help_getCcs(const Teuchos::ArrayView<scalar_t> nzval,
327  const Teuchos::ArrayView<global_ordinal_t> rowind,
328  const Teuchos::ArrayView<typename MatrixAdapter<Matrix>::global_size_t> colptr,
329  typename MatrixAdapter<Matrix>::global_size_t& nnz,
330  const Teuchos::Ptr<const Tpetra::Map<local_ordinal_t,global_ordinal_t,node_t> > colmap,
331  EStorage_Ordering ordering,
332  has_special_impl hsi) const
333  {
334  static_cast<const adapter_t*>(this)->getCcs_spec(nzval, rowind, colptr,
335  nnz, colmap, ordering);
336  }
337 
338  template < class Matrix >
339  void
340  MatrixAdapter<Matrix>::help_getCcs(const Teuchos::ArrayView<scalar_t> nzval,
341  const Teuchos::ArrayView<global_ordinal_t> rowind,
342  const Teuchos::ArrayView<typename MatrixAdapter<Matrix>::global_size_t> colptr,
343  typename MatrixAdapter<Matrix>::global_size_t& nnz,
344  const Teuchos::Ptr<const Tpetra::Map<local_ordinal_t,global_ordinal_t,node_t> > colmap,
345  EStorage_Ordering ordering,
346  no_special_impl nsi) const
347  {
348  do_getCcs(nzval, rowind, colptr,
349  nnz, colmap, ordering,
350  typename adapter_t::major_access());
351  }
352 
353  template < class Matrix >
354  void
355  MatrixAdapter<Matrix>::do_getCcs(const Teuchos::ArrayView<scalar_t> nzval,
356  const Teuchos::ArrayView<global_ordinal_t> rowind,
357  const Teuchos::ArrayView<typename MatrixAdapter<Matrix>::global_size_t> colptr,
358  typename MatrixAdapter<Matrix>::global_size_t& nnz,
359  const Teuchos::Ptr<const Tpetra::Map<local_ordinal_t,global_ordinal_t,node_t> > colmap,
360  EStorage_Ordering ordering,
361  row_access ra) const
362  {
363  using Teuchos::Array;
364  // get the crs and transpose
365 
366  Array<scalar_t> nzval_tmp(nzval.size(), 0);
367  Array<global_ordinal_t> colind(rowind.size(), 0);
368  Array<global_size_t> rowptr(this->getGlobalNumRows() + 1);
369  this->getCrs(nzval_tmp(), colind(), rowptr(), nnz, colmap, ordering);
370 
371  if( !nzval.is_null() && !rowind.is_null() && !colptr.is_null() )
372  Util::transpose(nzval_tmp(), colind(), rowptr(), nzval, rowind, colptr);
373  }
374 
375  template < class Matrix >
376  void
377  MatrixAdapter<Matrix>::do_getCcs(const Teuchos::ArrayView<scalar_t> nzval,
378  const Teuchos::ArrayView<global_ordinal_t> rowind,
379  const Teuchos::ArrayView<typename MatrixAdapter<Matrix>::global_size_t> colptr,
380  typename MatrixAdapter<Matrix>::global_size_t& nnz,
381  const Teuchos::Ptr<const Tpetra::Map<local_ordinal_t,global_ordinal_t,node_t> > colmap,
382  EStorage_Ordering ordering,
383  col_access ca) const
384  {
385  using Teuchos::RCP;
386  using Teuchos::ArrayView;
387  using Teuchos::OrdinalTraits;
388 
389  RCP<const type> get_mat;
390  if( *colmap == *this->col_map_ ){
391  // No need to redistribute
392  get_mat = rcp(this,false); // non-owning
393  } else {
394  get_mat = get(colmap);
395  }
396 
397  // If all is well and good, then colmap == cmap
398  RCP<const Tpetra::Map<scalar_t,local_ordinal_t,global_ordinal_t> > cmap = get_mat->getColMap();
399  TEUCHOS_ASSERT( *colmap == *cmap );
400 
401  ArrayView<global_ordinal_t> node_elements = cmap->getNodeElementList();
402  if( node_elements.size() == 0 ) return; // no more contribution
403 
404  typename ArrayView<global_ordinal_t>::iterator col_it, col_end;
405  col_end = node_elements.end();
406 
407  size_t colptr_ind = OrdinalTraits<size_t>::zero();
408  global_ordinal_t colInd = OrdinalTraits<global_ordinal_t>::zero();
409  for( col_it = node_elements.begin(); col_it != col_end; ++col_it ){
410  colptr[colptr_ind++] = colInd;
411  size_t colNNZ = getGlobalColNNZ(*col_it);
412  size_t nnzRet = 0;
413  ArrayView<global_ordinal_t> rowind_view = rowind.view(colInd,colNNZ);
414  ArrayView<scalar_t> nzval_view = nzval.view(colInd,colNNZ);
415  getGlobalColCopy(*col_it, rowind_view, nzval_view, nnzRet);
416 
417  // It was suggested that instead of sorting each row's indices
418  // individually, that we instead do a double-transpose at the
419  // end, which would also lead to the indices being sorted.
420  if( ordering == SORTED_INDICES ){
421  Tpetra::sort2(rowind_view.begin(), rowind_view.end(), nzval_view.begin());
422  }
423 
424  TEUCHOS_TEST_FOR_EXCEPTION( colNNZ != nnzRet,
425  std::runtime_error,
426  "Number of values returned different from "
427  "number of values reported");
428  colInd += colNNZ;
429  }
430  colptr[colptr_ind] = nnz = colInd;
431  }
432 
433 
434  // These will link to concrete implementations
435  template < class Matrix >
436  void
438  const Teuchos::ArrayView<global_ordinal_t>& indices,
439  const Teuchos::ArrayView<scalar_t>& vals,
440  size_t& nnz) const
441  {
442  static_cast<const adapter_t*>(this)->getGlobalRowCopy_impl(row, indices, vals, nnz);
443  }
444 
445  template < class Matrix >
446  void
448  const Teuchos::ArrayView<global_ordinal_t>& indices,
449  const Teuchos::ArrayView<scalar_t>& vals,
450  size_t& nnz) const
451  {
452  static_cast<const adapter_t*>(this)->getGlobalColCopy_impl(col, indices, vals, nnz);
453  }
454 
455  template < class Matrix >
456  size_t
458  {
459  return static_cast<const adapter_t*>(this)->getMaxRowNNZ_impl();
460  }
461 
462  template < class Matrix >
463  size_t
464  MatrixAdapter<Matrix>::getMaxColNNZ() const
465  {
466  return static_cast<const adapter_t*>(this)->getMaxColNNZ_impl();
467  }
468 
469  template < class Matrix >
470  size_t
471  MatrixAdapter<Matrix>::getGlobalRowNNZ(global_ordinal_t row) const
472  {
473  return static_cast<const adapter_t*>(this)->getGlobalRowNNZ_impl(row);
474  }
475 
476  template < class Matrix >
477  size_t
478  MatrixAdapter<Matrix>::getLocalRowNNZ(local_ordinal_t row) const
479  {
480  return static_cast<const adapter_t*>(this)->getLocalRowNNZ_impl(row);
481  }
482 
483  template < class Matrix >
484  size_t
485  MatrixAdapter<Matrix>::getGlobalColNNZ(global_ordinal_t col) const
486  {
487  return static_cast<const adapter_t*>(this)->getGlobalColNNZ_impl(col);
488  }
489 
490  template < class Matrix >
491  size_t
492  MatrixAdapter<Matrix>::getLocalColNNZ(local_ordinal_t col) const
493  {
494  return static_cast<const adapter_t*>(this)->getLocalColNNZ_impl(col);
495  }
496 
497  template < class Matrix >
498  bool
499  MatrixAdapter<Matrix>::isLocallyIndexed() const
500  {
501  return static_cast<const adapter_t*>(this)->isLocallyIndexed_impl();
502  }
503 
504  template < class Matrix >
505  bool
506  MatrixAdapter<Matrix>::isGloballyIndexed() const
507  {
508  return static_cast<const adapter_t*>(this)->isGloballyIndexed_impl();
509  }
510 
511  template < class Matrix >
512  Teuchos::RCP<const MatrixAdapter<Matrix> >
513  MatrixAdapter<Matrix>::get(const Teuchos::Ptr<const Tpetra::Map<local_ordinal_t,global_ordinal_t,node_t> > map) const
514  {
515  return static_cast<const adapter_t*>(this)->get_impl(map);
516  }
517 
518 
519  template <class Matrix>
520  Teuchos::RCP<MatrixAdapter<Matrix> >
521  createMatrixAdapter(Teuchos::RCP<Matrix> m){
522  using Teuchos::rcp;
523  using Teuchos::rcp_const_cast;
524 
525  if(m.is_null()) return Teuchos::null;
526  return( rcp(new ConcreteMatrixAdapter<Matrix>(m)) );
527  }
528 
529  template <class Matrix>
530  Teuchos::RCP<const MatrixAdapter<Matrix> >
531  createConstMatrixAdapter(Teuchos::RCP<const Matrix> m){
532  using Teuchos::rcp;
533  using Teuchos::rcp_const_cast;
534 
535  if(m.is_null()) return Teuchos::null;
536  return( rcp(new ConcreteMatrixAdapter<Matrix>(rcp_const_cast<Matrix,const Matrix>(m))).getConst() );
537  }
538 
539 } // end namespace Amesos2
540 
541 #endif // AMESOS2_MATRIXADAPTER_DEF_HPP
EStorage_Ordering
Definition: Amesos2_TypeDecl.hpp:132
Indicates that the concrete class has a special implementation that should be called.
Definition: Amesos2_TypeDecl.hpp:82
void transpose(ArrayView< Scalar > vals, ArrayView< GlobalOrdinal > indices, ArrayView< GlobalSizeT > ptr, ArrayView< Scalar > trans_vals, ArrayView< GlobalOrdinal > trans_indices, ArrayView< GlobalSizeT > trans_ptr)
std::string description() const
Returns a short description of this Solver.
Definition: Amesos2_MatrixAdapter_def.hpp:183
A Matrix adapter interface for Amesos2.
Definition: Amesos2_MatrixAdapter_decl.hpp:76
EDistribution
Definition: Amesos2_TypeDecl.hpp:115