Amesos2 - Direct Sparse Solver Interfaces  Version of the Day
Amesos2_Superludist_TypeMap.hpp
Go to the documentation of this file.
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 
57 #ifndef AMESOS2_SUPERLUDIST_TYPEMAP_HPP
58 #define AMESOS2_SUPERLUDIST_TYPEMAP_HPP
59 
60 
61 #include <functional>
62 
63 #include <Teuchos_as.hpp>
64 #ifdef HAVE_TEUCHOS_COMPLEX
65 #include <Teuchos_SerializationTraits.hpp>
66 #endif
67 
68 #include "Amesos2_TypeMap.hpp"
69 
70 
71 namespace SLUD {
72 
73 extern "C" {
74 
75 #include "superlu_dist_config.h" // provides define for size 32 or 64 int_t
76 
78 #define USER_FREE(addr) SLUD::superlu_free_dist(addr)
79 
80  // undefine compiler guard in case we also have the sequential
81  // SuperLU enabled
82 #undef __SUPERLU_SUPERMATRIX
83 #include "superlu_defs.h"
84 //
85 
86 #if SUPERLU_DIST_MAJOR_VERSION > 4
87  typedef superlu_dist_options_t amesos2_superlu_dist_options_t;
88  typedef superlu_dist_mem_usage_t amesos2_superlu_dist_mem_usage_t;
89 #define AMESOS2_ENABLES_SUPERLUDIST_VERSION5_AND_HIGHER 1
90 #else
91  typedef superlu_options_t amesos2_superlu_dist_options_t;
92  typedef mem_usage_t amesos2_superlu_dist_mem_usage_t;
93 #endif
94 
95 
96  namespace D {
97 #include "superlu_ddefs.h" // double-precision real definitions
98  }
99 
100 #if defined(HAVE_TEUCHOS_COMPLEX) && !defined(__clang__)
101  namespace Z {
102 #include "superlu_zdefs.h" // double-precision complex definitions
103  }
104 #endif // HAVE_TEUCHOS_COMPLEX
105 
106 
107 } // end extern "C"
108 
109 
110 #if defined(HAVE_TEUCHOS_COMPLEX) && !defined(__clang__)
111 
112  // Declare and specialize a std::binary_funtion class for
113  // multiplication of SLUD types
114  template <typename slu_scalar_t, typename slu_mag_t>
115  struct slu_mt_mult {};
116 
117  // This specialization handles the generic case were the scalar and
118  // magnitude types are double or float.
119  template <typename T>
120  struct slu_mt_mult<T,T> : std::multiplies<T> {};
121 
122  // For namespace/macro reasons, we prefix our variables with amesos_*
123  template <>
124  struct slu_mt_mult<Z::doublecomplex,double>
125  : std::binary_function<Z::doublecomplex,double,Z::doublecomplex> {
126  Z::doublecomplex operator()(Z::doublecomplex amesos_z, double amesos_d) {
127  Z::doublecomplex amesos_zr;
128  zd_mult(&amesos_zr, &amesos_z, amesos_d); // zd_mult is a macro, so no namespacing
129  return( amesos_zr );
130  }
131  };
132 
133  template <>
134  struct slu_mt_mult<Z::doublecomplex,Z::doublecomplex>
135  : std::binary_function<Z::doublecomplex,Z::doublecomplex,Z::doublecomplex> {
136  Z::doublecomplex operator()(Z::doublecomplex amesos_z1, Z::doublecomplex amesos_z2) {
137  Z::doublecomplex amesos_zr;
138  zz_mult(&amesos_zr, &amesos_z1, &amesos_z2); // zz_mult is a macro, so no namespacing
139  return( amesos_zr );
140  }
141  };
142 #endif // HAVE_TEUCHOS_COMPLEX
143 } // end namespace SLUD
144 #if defined(HAVE_TEUCHOS_COMPLEX) && !defined(__clang__)
145 
146 
147 /* ==================== Conversion ==================== */
148 namespace Teuchos {
149 
160 template <typename TypeFrom>
161 class ValueTypeConversionTraits<SLUD::Z::doublecomplex, TypeFrom>
162 {
163 public:
164  static SLUD::Z::doublecomplex convert( const TypeFrom t )
165  {
166  SLUD::Z::doublecomplex ret;
167  ret.r = Teuchos::as<double>(t.real());
168  ret.i = Teuchos::as<double>(t.imag());
169  return( ret );
170  }
171 
172  static SLUD::Z::doublecomplex safeConvert( const TypeFrom t )
173  {
174  SLUD::Z::doublecomplex ret;
175  ret.r = Teuchos::as<double>(t.real());
176  ret.i = Teuchos::as<double>(t.imag());
177  return( ret );
178  }
179 };
180 
181 
182 // Also convert from SLU types
183 template <typename TypeTo>
184 class ValueTypeConversionTraits<TypeTo, SLUD::Z::doublecomplex>
185 {
186 public:
187  static TypeTo convert( const SLUD::Z::doublecomplex t )
188  {
189  typedef typename TypeTo::value_type value_type;
190  value_type ret_r = Teuchos::as<value_type>( t.r );
191  value_type ret_i = Teuchos::as<value_type>( t.i );
192  return ( TypeTo( ret_r, ret_i ) );
193  }
194 
195  // No special checks for safe Convert
196  static TypeTo safeConvert( const SLUD::Z::doublecomplex t )
197  {
198  typedef typename TypeTo::value_type value_type;
199  value_type ret_r = Teuchos::as<value_type>( t.r );
200  value_type ret_i = Teuchos::as<value_type>( t.i );
201  return ( TypeTo( ret_r, ret_i ) );
202  }
203 };
204 
205 template <typename Ordinal>
206 class SerializationTraits<Ordinal,SLUD::Z::doublecomplex>
207  : public DirectSerializationTraits<Ordinal,SLUD::Z::doublecomplex>
208 {};
209 
211 
212 } // end namespace Teuchos
213 
214 
215 
221 namespace std {
222  // C++-style output functions for Superludist complex types
223  ostream& operator<<(ostream& out, const SLUD::Z::doublecomplex z);
224 
226 }
227 #endif // HAVE_TEUCHOS_COMPLEX
228 
229 
230 
231 namespace Amesos2 {
232 
233 template <class, class> class Superludist;
234 
235 /* Specialize the Amesos2::TypeMap struct for SuperLU_DIST types
236  *
237  * \cond Superludist_type_specializations
238  */
239 template <>
240 struct TypeMap<Superludist,double>
241 {
242  static const SLUD::Dtype_t dtype = SLUD::SLU_D;
243  typedef double type;
244  typedef double magnitude_type;
245 #if SUPERLU_DIST_MAJOR_VERSION > 6 || (SUPERLU_DIST_MAJOR_VERSION == 6 && SUPERLU_DIST_MINOR_VERSION > 2)
246  typedef SLUD::D::dLUstruct_t LUstruct_t;
247  typedef SLUD::D::dSOLVEstruct_t SOLVEstruct_t;
248  typedef SLUD::D::dScalePermstruct_t ScalePermstruct_t;
249 #else
250  typedef SLUD::D::LUstruct_t LUstruct_t;
251  typedef SLUD::D::SOLVEstruct_t SOLVEstruct_t;
252  typedef SLUD::ScalePermstruct_t ScalePermstruct_t;
253 #endif
254 };
255 
256 #if defined(HAVE_TEUCHOS_COMPLEX) && !defined(__clang__)
257 template <>
258 struct TypeMap<Superludist,std::complex<double> >
259 {
260  static const SLUD::Dtype_t dtype = SLUD::SLU_Z;
261  typedef SLUD::Z::doublecomplex type;
262  typedef double magnitude_type;
263 #if SUPERLU_DIST_MAJOR_VERSION > 6 || (SUPERLU_DIST_MAJOR_VERSION == 6 && SUPERLU_DIST_MINOR_VERSION > 2)
264  typedef SLUD::Z::zLUstruct_t LUstruct_t;
265  typedef SLUD::Z::zSOLVEstruct_t SOLVEstruct_t;
266  typedef SLUD::Z::zScalePermstruct_t ScalePermstruct_t;
267 #else
268  typedef SLUD::Z::LUstruct_t LUstruct_t;
269  typedef SLUD::Z::SOLVEstruct_t SOLVEstruct_t;
270  typedef SLUD::ScalePermstruct_t ScalePermstruct_t;
271 #endif
272 };
273 
274  // It probably won't happen, but what if someone does create a
275  // matrix or multivector with the SuperLU_DIST doublecomplex type
276  // directly?
277 template <>
278 struct TypeMap<Superludist,SLUD::Z::doublecomplex>
279 {
280  static const SLUD::Dtype_t dtype = SLUD::SLU_Z;
281  typedef SLUD::Z::doublecomplex type;
282  typedef double magnitude_type;
283 #if SUPERLU_DIST_MAJOR_VERSION > 6 || (SUPERLU_DIST_MAJOR_VERSION == 6 && SUPERLU_DIST_MINOR_VERSION > 2)
284  typedef SLUD::Z::zLUstruct_t LUstruct_t;
285  typedef SLUD::Z::zSOLVEstruct_t SOLVEstruct_t;
286  typedef SLUD::Z::zScalePermstruct_t ScalePermstruct_t;
287 #else
288  typedef SLUD::Z::LUstruct_t LUstruct_t;
289  typedef SLUD::Z::SOLVEstruct_t SOLVEstruct_t;
290  typedef SLUD::ScalePermstruct_t ScalePermstruct_t;
291 #endif
292 };
293 
294 #endif // HAVE_TEUCHOS_COMPLEX
295 
296 /* \endcond Superludist_type_specializations */
297 
298 
299 } // end namespace Amesos2
300 
301 #endif // AMESOS2_SUPERLUDIST_TYPEMAP_HPP