Amesos2 - Direct Sparse Solver Interfaces  Version of the Day
Amesos2_Superlu_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 
54 #ifndef AMESOS2_SUPERLU_TYPEMAP_HPP
55 #define AMESOS2_SUPERLU_TYPEMAP_HPP
56 
57 #include <functional>
58 #ifdef HAVE_TEUCHOS_COMPLEX
59 #include <complex>
60 #endif
61 
62 #include <Teuchos_as.hpp>
63 #ifdef HAVE_TEUCHOS_COMPLEX
64 #include <Teuchos_SerializationTraits.hpp>
65 #endif
66 
67 #include "Amesos2_TypeMap.hpp"
68 
69 
70 /* The SuperLU comples headers file only need to be included if
71  complex has been enabled in Teuchos. In addition we only need to
72  define the conversion and printing functions if complex has been
73  enabled. */
74 namespace SLU {
75 
76 typedef int int_t;
77 
78 extern "C" {
79 
80 #undef __SUPERLU_SUPERMATRIX
81 #include "supermatrix.h" // for Dtype_t declaration
82 
83 #ifdef HAVE_TEUCHOS_COMPLEX
84 namespace C {
85 #undef __SUPERLU_SCOMPLEX
86 #undef SCOMPLEX_INCLUDE
87 #include "slu_scomplex.h" // single-precision complex data type definitions
88 }
89 
90 namespace Z {
91 #undef __SUPERLU_DCOMPLEX
92 #undef DCOMPLEX_INCLUDE
93 #include "slu_dcomplex.h" // double-precision complex data type definitions
94 }
95 #endif // HAVE_TEUCHOS_COMPLEX
96 
97 } // end extern "C"
98 
99  // Declare and specialize a std::binary_funtion class for
100  // multiplication of SuperLU types
101  template <typename slu_scalar_t, typename slu_mag_t>
102  struct slu_mult {};
103 
104  // This specialization handles the generic case were the scalar and
105  // magnitude types are double or float.
106  template <typename T>
107  struct slu_mult<T,T> : std::multiplies<T> {};
108 
109 #ifdef HAVE_TEUCHOS_COMPLEX
110 
111  // For namespace/macro reasons, we prefix our variables with amesos_*
112  template <>
113  struct slu_mult<C::complex,float>
114  : std::binary_function<C::complex,float,C::complex> {
115  C::complex operator()(C::complex amesos_c, float amesos_f) {
116  C::complex amesos_cr;
117  cs_mult(&amesos_cr, &amesos_c, amesos_f); // cs_mult is a macro, so no namespacing
118  return( amesos_cr );
119  }
120  };
121 
122  template <>
123  struct slu_mult<C::complex,C::complex>
124  : std::binary_function<C::complex,C::complex,C::complex> {
125  C::complex operator()(C::complex amesos_c1, C::complex amesos_c2) {
126  C::complex amesos_cr;
127  cc_mult(&amesos_cr, &amesos_c1, &amesos_c2); // cc_mult is a macro, so no namespacing
128  return( amesos_cr );
129  }
130  };
131 
132  template <>
133  struct slu_mult<Z::doublecomplex,double>
134  : std::binary_function<Z::doublecomplex,double,Z::doublecomplex> {
135  Z::doublecomplex operator()(Z::doublecomplex amesos_z, double amesos_d) {
136  Z::doublecomplex amesos_zr;
137  zd_mult(&amesos_zr, &amesos_z, amesos_d); // zd_mult is a macro, so no namespacing
138  return( amesos_zr );
139  }
140  };
141 
142  template <>
143  struct slu_mult<Z::doublecomplex,Z::doublecomplex>
144  : std::binary_function<Z::doublecomplex,Z::doublecomplex,Z::doublecomplex> {
145  Z::doublecomplex operator()(Z::doublecomplex amesos_z1, Z::doublecomplex amesos_z2) {
146  Z::doublecomplex amesos_zr;
147  zz_mult(&amesos_zr, &amesos_z1, &amesos_z2); // zz_mult is a macro, so no namespacing
148  return( amesos_zr );
149  }
150  };
151 
152 #endif // HAVE_TEUCHOS_COMPLEX
153 } // end namespace SLU
154 #ifdef HAVE_TEUCHOS_COMPLEX
155 
156 /* ==================== Conversion ==================== */
157 namespace Teuchos {
158 
169 template <typename TypeFrom>
170 class ValueTypeConversionTraits<SLU::C::complex, TypeFrom>
171 {
172 public:
173  static SLU::C::complex convert( const TypeFrom t )
174  {
175  SLU::C::complex ret;
176  ret.r = Teuchos::as<float>(t.real());
177  ret.i = Teuchos::as<float>(t.imag());
178  return( ret );
179  }
180 
181  static SLU::C::complex safeConvert( const TypeFrom t )
182  {
183  SLU::C::complex ret;
184  ret.r = Teuchos::as<float>(t.real());
185  ret.i = Teuchos::as<float>(t.imag());
186  return( ret );
187  }
188 };
189 
190 
191 template <typename TypeFrom>
192 class ValueTypeConversionTraits<SLU::Z::doublecomplex, TypeFrom>
193 {
194 public:
195  static SLU::Z::doublecomplex convert( const TypeFrom t )
196  {
197  SLU::Z::doublecomplex ret;
198  ret.r = Teuchos::as<double>(t.real());
199  ret.i = Teuchos::as<double>(t.imag());
200  return( ret );
201  }
202 
203  static SLU::Z::doublecomplex safeConvert( const TypeFrom t )
204  {
205  SLU::Z::doublecomplex ret;
206  ret.r = Teuchos::as<double>(t.real());
207  ret.i = Teuchos::as<double>(t.imag());
208  return( ret );
209  }
210 };
211 
212 
213 // Also convert from SLU types
214 template <typename TypeTo>
215 class ValueTypeConversionTraits<TypeTo, SLU::C::complex>
216 {
217 public:
218  static TypeTo convert( const SLU::C::complex t )
219  {
220  typedef typename TypeTo::value_type value_type;
221  value_type ret_r = Teuchos::as<value_type>( t.r );
222  value_type ret_i = Teuchos::as<value_type>( t.i );
223  return ( TypeTo( ret_r, ret_i ) );
224  }
225 
226  // No special checks for safe Convert
227  static TypeTo safeConvert( const SLU::C::complex t )
228  {
229  typedef typename TypeTo::value_type value_type;
230  value_type ret_r = Teuchos::as<value_type>( t.r );
231  value_type ret_i = Teuchos::as<value_type>( t.i );
232  return ( TypeTo( ret_r, ret_i ) );
233  }
234 };
235 
236 
237 template <typename TypeTo>
238 class ValueTypeConversionTraits<TypeTo, SLU::Z::doublecomplex>
239 {
240 public:
241  static TypeTo convert( const SLU::Z::doublecomplex t )
242  {
243  typedef typename TypeTo::value_type value_type;
244  value_type ret_r = Teuchos::as<value_type>( t.r );
245  value_type ret_i = Teuchos::as<value_type>( t.i );
246  return ( TypeTo( ret_r, ret_i ) );
247  }
248 
249  // No special checks for safe Convert
250  static TypeTo safeConvert( const SLU::Z::doublecomplex t )
251  {
252  typedef typename TypeTo::value_type value_type;
253  value_type ret_r = Teuchos::as<value_type>( t.r );
254  value_type ret_i = Teuchos::as<value_type>( t.i );
255  return ( TypeTo( ret_r, ret_i ) );
256  }
257 };
258 
259 template <typename Ordinal>
260 class SerializationTraits<Ordinal,SLU::C::complex>
261  : public DirectSerializationTraits<Ordinal,SLU::C::complex>
262 {};
263 
264 template <typename Ordinal>
265 class SerializationTraits<Ordinal,SLU::Z::doublecomplex>
266  : public DirectSerializationTraits<Ordinal,SLU::Z::doublecomplex>
267 {};
268 
270 
271 } // end namespace Teuchos
272 
273 // C++-style output functions for Superlu complex types
274 namespace std {
275  ostream& operator<<(ostream& out, const SLU::Z::doublecomplex z);
276 
277  ostream& operator<<(ostream& out, const SLU::C::complex c);
278 }
279 
280 #endif // HAVE_TEUCHOS_COMPLEX
281 
282 
283 namespace Amesos2 {
284 
285 template <class, class> class Superlu;
286 
287 /* Specialize the Amesos2::TypeMap struct for Superlu types
288  *
289  * \cond Superlu_type_specializations
290  */
291 template <>
292 struct TypeMap<Superlu,float>
293 {
294  static SLU::Dtype_t dtype;
295  typedef float type;
296  typedef float magnitude_type;
297 };
298 
299 
300 template <>
301 struct TypeMap<Superlu,double>
302 {
303  static SLU::Dtype_t dtype;
304  typedef double type;
305  typedef double magnitude_type;
306 };
307 
308 
309 #ifdef HAVE_TEUCHOS_COMPLEX
310 
311 template <>
312 struct TypeMap<Superlu,std::complex<float> >
313 {
314  static SLU::Dtype_t dtype;
315  typedef SLU::C::complex type;
316  typedef float magnitude_type;
317 };
318 
319 
320 template <>
321 struct TypeMap<Superlu,std::complex<double> >
322 {
323  static SLU::Dtype_t dtype;
324  typedef SLU::Z::doublecomplex type;
325  typedef double magnitude_type;
326 };
327 
328 
329 template <>
330 struct TypeMap<Superlu,SLU::C::complex>
331 {
332  static SLU::Dtype_t dtype;
333  typedef SLU::C::complex type;
334  typedef float magnitude_type;
335 };
336 
337 
338 template <>
339 struct TypeMap<Superlu,SLU::Z::doublecomplex>
340 {
341  static SLU::Dtype_t dtype;
342  typedef SLU::Z::doublecomplex type;
343  typedef double magnitude_type;
344 };
345 
346 
347 #endif // HAVE_TEUCHOS_COMPLEX
348 
349 /* \endcond Superlu_type_specializations */
350 
351 
352 } // end namespace Amesos2
353 
354 #endif // AMESOS2_SUPERLU_TYPEMAP_HPP
Definition: Amesos2_AbstractConcreteMatrixAdapter.hpp:48
Definition: Amesos2_Superlu_FunctionMap.hpp:74
Definition: Amesos2_Cholmod_TypeMap.hpp:92