Xpetra  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Xpetra_CrsMatrixUtils.hpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // Xpetra: A linear algebra interface package
6 // Copyright 2012 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
39 // Jonathan Hu (jhu@sandia.gov)
40 // Ray Tuminaro (rstumin@sandia.gov)
41 // Chris Siefert (csiefer@sandia.gov)
42 // Luc Berger-Vergoat (lberge@sandia.gov)
43 //
44 // ***********************************************************************
45 //
46 // @HEADER
47 #ifndef PACKAGES_XPETRA_CRSMATRIX_UTILS_HPP_
48 #define PACKAGES_XPETRA_CRSMATRIX_UTILS_HPP_
49 
50 #include "Xpetra_ConfigDefs.hpp"
51 #include "Xpetra_Exceptions.hpp"
52 #include "Xpetra_Map.hpp" // definition of UnderlyingLib
53 
54 #ifdef HAVE_XPETRA_EPETRA
55 #include "Epetra_Util.h"
56 #endif
57 
58 #ifdef HAVE_XPETRA_TPETRA
59 #include "Tpetra_Import_Util2.hpp"
60 #endif
61 
62 namespace Xpetra {
63 
71 template <class Scalar,
72  class LocalOrdinal,
73  class GlobalOrdinal,
74  class Node = Tpetra::KokkosClassic::DefaultNode::DefaultNodeType>
75 class CrsMatrixUtils {
76 #undef XPETRA_CRSMATRIXUTILS_SHORT
77 
78  public:
81  static void sortCrsEntries(const Teuchos::ArrayView<size_t>& CRS_rowptr,
82  const Teuchos::ArrayView<LocalOrdinal>& CRS_colind,
83  const Teuchos::ArrayView<Scalar>& CRS_vals,
84  const UnderlyingLib lib) {
85  if (lib == Xpetra::UseEpetra) {
86 #if defined(HAVE_XPETRA_EPETRA)
87  throw(Xpetra::Exceptions::RuntimeError("Xpetra::CrsMatrixUtils::sortCrsEntries only available for GO=int or GO=long long with EpetraNode (Serial or OpenMP depending on configuration)"));
88 #endif // HAVE_XPETRA_EPETRA
89  } else if (lib == Xpetra::UseTpetra) {
90 #ifdef HAVE_XPETRA_TPETRA
91  Tpetra::Import_Util::sortCrsEntries(CRS_rowptr, CRS_colind, CRS_vals);
92 #endif // HAVE_XPETRA_TPETRA
93  }
94 
95  return;
96  }
97 
100  static void sortAndMergeCrsEntries(const Teuchos::ArrayView<size_t>& CRS_rowptr,
101  const Teuchos::ArrayView<LocalOrdinal>& CRS_colind,
102  const Teuchos::ArrayView<Scalar>& CRS_vals,
103  const UnderlyingLib lib) {
104  if (lib == Xpetra::UseEpetra) {
105 #if defined(HAVE_XPETRA_EPETRA)
106  throw(Xpetra::Exceptions::RuntimeError("Xpetra::CrsMatrixUtils::sortAndMergeCrsEntries only available for GO=int or GO=long long with EpetraNode (Serial or OpenMP depending on configuration)"));
107 #endif // HAVE_XPETRA_EPETRA
108  } else if (lib == Xpetra::UseTpetra) {
109 #ifdef HAVE_XPETRA_TPETRA
110  Tpetra::Import_Util::sortAndMergeCrsEntries(CRS_rowptr, CRS_colind, CRS_vals);
111 #endif // HAVE_XPETRA_TPETRA
112  }
113 
114  return;
115  }
116 
117 }; // end class CrsMatrixUtils
118 
119 #ifdef HAVE_XPETRA_EPETRA
120 // Specialization for double, int, int, EpetraNode
121 template <>
122 class CrsMatrixUtils<double, int, int, EpetraNode> {
123  typedef double Scalar;
124  typedef int LocalOrdinal;
125  typedef int GlobalOrdinal;
126  typedef EpetraNode Node;
127 #undef XPETRA_CRSMATRIXUTILS_SHORT
128 
129  public:
132  static void sortCrsEntries(const Teuchos::ArrayView<size_t>& CRS_rowptr,
133  const Teuchos::ArrayView<LocalOrdinal>& CRS_colind,
134  const Teuchos::ArrayView<Scalar>& CRS_vals,
135  const UnderlyingLib lib) {
136  if (lib == Xpetra::UseEpetra) {
137 #if defined(HAVE_XPETRA_EPETRA)
138  int rv = Epetra_Util::SortCrsEntries(Teuchos::as<int>(CRS_rowptr.size() - 1),
139  CRS_rowptr.getRawPtr(),
140  CRS_colind.getRawPtr(),
141  CRS_vals.getRawPtr());
142  if (rv != 0) {
143  throw Exceptions::RuntimeError("Epetra_Util::SortCrsEntries() return value of " + Teuchos::toString(rv));
144  }
145 #endif // HAVE_XPETRA_EPETRA
146  } else if (lib == Xpetra::UseTpetra) {
147 #ifdef HAVE_XPETRA_TPETRA
148  Tpetra::Import_Util::sortCrsEntries(CRS_rowptr, CRS_colind, CRS_vals);
149 #endif // HAVE_XPETRA_TPETRA
150  }
151 
152  return;
153  }
154 
157  static void sortAndMergeCrsEntries(const Teuchos::ArrayView<size_t>& CRS_rowptr,
158  const Teuchos::ArrayView<LocalOrdinal>& CRS_colind,
159  const Teuchos::ArrayView<Scalar>& CRS_vals,
160  const UnderlyingLib lib) {
161  if (lib == Xpetra::UseEpetra) {
162 #if defined(HAVE_XPETRA_EPETRA)
163  int rv = Epetra_Util::SortAndMergeCrsEntries(Teuchos::as<int>(CRS_rowptr.size() - 1),
164  CRS_rowptr.getRawPtr(),
165  CRS_colind.getRawPtr(),
166  CRS_vals.getRawPtr());
167  if (rv != 0) {
168  throw Exceptions::RuntimeError("Epetra_Util::SortCrsEntries() return value of " + Teuchos::toString(rv));
169  }
170 #endif // HAVE_XPETRA_EPETRA
171  } else if (lib == Xpetra::UseTpetra) {
172 #ifdef HAVE_XPETRA_TPETRA
173  Tpetra::Import_Util::sortAndMergeCrsEntries(CRS_rowptr, CRS_colind, CRS_vals);
174 #endif // HAVE_XPETRA_TPETRA
175  }
176 
177  return;
178  }
179 
180 }; // end class CrsMatrixUtils
181 
182 // Specialization for double, int, long long, EpetraNode
183 template <>
184 class CrsMatrixUtils<double, int, long long, EpetraNode> {
185  typedef double Scalar;
186  typedef int LocalOrdinal;
187  typedef long long GlobalOrdinal;
188  typedef EpetraNode Node;
189 #undef XPETRA_CRSMATRIXUTILS_SHORT
190 
191  public:
194  static void sortCrsEntries(const Teuchos::ArrayView<size_t>& CRS_rowptr,
195  const Teuchos::ArrayView<LocalOrdinal>& CRS_colind,
196  const Teuchos::ArrayView<Scalar>& CRS_vals,
197  const UnderlyingLib lib) {
198  if (lib == Xpetra::UseEpetra) {
199 #if defined(HAVE_XPETRA_EPETRA)
200  int rv = Epetra_Util::SortCrsEntries(Teuchos::as<int>(CRS_rowptr.size() - 1),
201  CRS_rowptr.getRawPtr(),
202  CRS_colind.getRawPtr(),
203  CRS_vals.getRawPtr());
204  if (rv != 0) {
205  throw Exceptions::RuntimeError("Epetra_Util::SortCrsEntries() return value of " + Teuchos::toString(rv));
206  }
207 #endif // HAVE_XPETRA_EPETRA
208  } else if (lib == Xpetra::UseTpetra) {
209 #ifdef HAVE_XPETRA_TPETRA
210  Tpetra::Import_Util::sortCrsEntries(CRS_rowptr, CRS_colind, CRS_vals);
211 #endif // HAVE_XPETRA_TPETRA
212  }
213 
214  return;
215  }
216 
219  static void sortAndMergeCrsEntries(const Teuchos::ArrayView<size_t>& CRS_rowptr,
220  const Teuchos::ArrayView<LocalOrdinal>& CRS_colind,
221  const Teuchos::ArrayView<Scalar>& CRS_vals,
222  const UnderlyingLib lib) {
223  if (lib == Xpetra::UseEpetra) {
224 #if defined(HAVE_XPETRA_EPETRA)
225  int rv = Epetra_Util::SortAndMergeCrsEntries(Teuchos::as<int>(CRS_rowptr.size() - 1),
226  CRS_rowptr.getRawPtr(),
227  CRS_colind.getRawPtr(),
228  CRS_vals.getRawPtr());
229  if (rv != 0) {
230  throw Exceptions::RuntimeError("Epetra_Util::SortCrsEntries() return value of " + Teuchos::toString(rv));
231  }
232 #endif // HAVE_XPETRA_EPETRA
233  } else if (lib == Xpetra::UseTpetra) {
234 #ifdef HAVE_XPETRA_TPETRA
235  Tpetra::Import_Util::sortAndMergeCrsEntries(CRS_rowptr, CRS_colind, CRS_vals);
236 #endif // HAVE_XPETRA_TPETRA
237  }
238 
239  return;
240  }
241 
242 }; // end class CrsMatrixUtils
243 #endif // HAVE_XPETRA_EPETRA for Epetra scpecialization
244 
245 } // end namespace Xpetra
246 
247 #define XPETRA_CRSMATRIXUTILS_SHORT
248 
249 #endif // PACKAGES_XPETRA_CRSMATRIX_UTILS_HPP_
static void sortAndMergeCrsEntries(const Teuchos::ArrayView< size_t > &CRS_rowptr, const Teuchos::ArrayView< LocalOrdinal > &CRS_colind, const Teuchos::ArrayView< Scalar > &CRS_vals, const UnderlyingLib lib)
Sort and merge the entries of the (raw CSR) matrix by column index within each row.
std::string toString(Xpetra::UnderlyingLib lib)
Convert a Xpetra::UnderlyingLib to a std::string.
Xpetra utility class for CrsMatrix-related routines.
Exception throws to report errors in the internal logical of the program.
static void sortCrsEntries(const Teuchos::ArrayView< size_t > &CRS_rowptr, const Teuchos::ArrayView< LocalOrdinal > &CRS_colind, const Teuchos::ArrayView< Scalar > &CRS_vals, const UnderlyingLib lib)
Sort the entries of the (raw CSR) matrix by column index within each row.
static void sortCrsEntries(const Teuchos::ArrayView< size_t > &CRS_rowptr, const Teuchos::ArrayView< LocalOrdinal > &CRS_colind, const Teuchos::ArrayView< Scalar > &CRS_vals, const UnderlyingLib lib)
Sort the entries of the (raw CSR) matrix by column index within each row.
static void sortAndMergeCrsEntries(const Teuchos::ArrayView< size_t > &CRS_rowptr, const Teuchos::ArrayView< LocalOrdinal > &CRS_colind, const Teuchos::ArrayView< Scalar > &CRS_vals, const UnderlyingLib lib)
Sort and merge the entries of the (raw CSR) matrix by column index within each row.
static void sortCrsEntries(const Teuchos::ArrayView< size_t > &CRS_rowptr, const Teuchos::ArrayView< LocalOrdinal > &CRS_colind, const Teuchos::ArrayView< Scalar > &CRS_vals, const UnderlyingLib lib)
Sort the entries of the (raw CSR) matrix by column index within each row.
Tpetra::KokkosCompat::KokkosSerialWrapperNode EpetraNode
static void sortAndMergeCrsEntries(const Teuchos::ArrayView< size_t > &CRS_rowptr, const Teuchos::ArrayView< LocalOrdinal > &CRS_colind, const Teuchos::ArrayView< Scalar > &CRS_vals, const UnderlyingLib lib)
Sort and merge the entries of the (raw CSR) matrix by column index within each row.