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