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 #include <functional>
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 namespace SLUD {
70 
71 extern "C" {
72 
73  // undefine compiler guard in case we also have the sequential
74  // SuperLU enabled
75 #undef __SUPERLU_SUPERMATRIX
76 #include "superlu_defs.h"
77 
78 #if SUPERLU_DIST_MAJOR_VERSION > 4
79  typedef superlu_dist_options_t amesos2_superlu_dist_options_t;
80  typedef superlu_dist_mem_usage_t amesos2_superlu_dist_mem_usage_t;
81 #else
82  typedef superlu_options_t amesos2_superlu_dist_options_t;
83  typedef mem_usage_t amesos2_superlu_dist_mem_usage_t;
84 #endif
85 
86  namespace D {
87 #include "superlu_ddefs.h" // double-precision real definitions
88  }
89 
90 #if defined(HAVE_TEUCHOS_COMPLEX) && !defined(__clang__)
91  namespace Z {
92 #include "superlu_zdefs.h" // double-precision complex definitions
93  }
94 #endif // HAVE_TEUCHOS_COMPLEX
95 
96 } // end extern "C"
97 #if defined(HAVE_TEUCHOS_COMPLEX) && !defined(__clang__)
98 
99  // Declare and specialize a std::binary_funtion class for
100  // multiplication of SLUD types
101  template <typename slu_scalar_t, typename slu_mag_t>
102  struct slu_mt_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_mt_mult<T,T> : std::multiplies<T> {};
108 
109  // For namespace/macro reasons, we prefix our variables with amesos_*
110  template <>
111  struct slu_mt_mult<Z::doublecomplex,double>
112  : std::binary_function<Z::doublecomplex,double,Z::doublecomplex> {
113  Z::doublecomplex operator()(Z::doublecomplex amesos_z, double amesos_d) {
114  Z::doublecomplex amesos_zr;
115  zd_mult(&amesos_zr, &amesos_z, amesos_d); // zd_mult is a macro, so no namespacing
116  return( amesos_zr );
117  }
118  };
119 
120  template <>
121  struct slu_mt_mult<Z::doublecomplex,Z::doublecomplex>
122  : std::binary_function<Z::doublecomplex,Z::doublecomplex,Z::doublecomplex> {
123  Z::doublecomplex operator()(Z::doublecomplex amesos_z1, Z::doublecomplex amesos_z2) {
124  Z::doublecomplex amesos_zr;
125  zz_mult(&amesos_zr, &amesos_z1, &amesos_z2); // zz_mult is a macro, so no namespacing
126  return( amesos_zr );
127  }
128  };
129 #endif // HAVE_TEUCHOS_COMPLEX
130 } // end namespace SLUD
131 #if defined(HAVE_TEUCHOS_COMPLEX) && !defined(__clang__)
132 
133 
134 /* ==================== Conversion ==================== */
135 namespace Teuchos {
136 
147 template <typename TypeFrom>
148 class ValueTypeConversionTraits<SLUD::Z::doublecomplex, TypeFrom>
149 {
150 public:
151  static SLUD::Z::doublecomplex convert( const TypeFrom t )
152  {
153  SLUD::Z::doublecomplex ret;
154  ret.r = Teuchos::as<double>(t.real());
155  ret.i = Teuchos::as<double>(t.imag());
156  return( ret );
157  }
158 
159  static SLUD::Z::doublecomplex safeConvert( const TypeFrom t )
160  {
161  SLUD::Z::doublecomplex ret;
162  ret.r = Teuchos::as<double>(t.real());
163  ret.i = Teuchos::as<double>(t.imag());
164  return( ret );
165  }
166 };
167 
168 
169 // Also convert from SLU types
170 template <typename TypeTo>
171 class ValueTypeConversionTraits<TypeTo, SLUD::Z::doublecomplex>
172 {
173 public:
174  static TypeTo convert( const SLUD::Z::doublecomplex t )
175  {
176  typedef typename TypeTo::value_type value_type;
177  value_type ret_r = Teuchos::as<value_type>( t.r );
178  value_type ret_i = Teuchos::as<value_type>( t.i );
179  return ( TypeTo( ret_r, ret_i ) );
180  }
181 
182  // No special checks for safe Convert
183  static TypeTo safeConvert( const SLUD::Z::doublecomplex t )
184  {
185  typedef typename TypeTo::value_type value_type;
186  value_type ret_r = Teuchos::as<value_type>( t.r );
187  value_type ret_i = Teuchos::as<value_type>( t.i );
188  return ( TypeTo( ret_r, ret_i ) );
189  }
190 };
191 
192 template <typename Ordinal>
193 class SerializationTraits<Ordinal,SLUD::Z::doublecomplex>
194  : public DirectSerializationTraits<Ordinal,SLUD::Z::doublecomplex>
195 {};
196 
198 
199 } // end namespace Teuchos
200 
201 
202 
208 namespace std {
209  // C++-style output functions for Superludist complex types
210  ostream& operator<<(ostream& out, const SLUD::Z::doublecomplex z);
211 
213 }
214 #endif // HAVE_TEUCHOS_COMPLEX
215 
216 
217 
218 namespace Amesos2 {
219 
220 template <class, class> class Superludist;
221 
222 /* Specialize the Amesos2::TypeMap struct for SuperLU_DIST types
223  *
224  * \cond Superludist_type_specializations
225  */
226 template <>
227 struct TypeMap<Superludist,double>
228 {
229  static const SLUD::Dtype_t dtype = SLUD::SLU_D;
230  typedef double type;
231  typedef double magnitude_type;
232  typedef SLUD::D::LUstruct_t LUstruct_t;
233  typedef SLUD::D::SOLVEstruct_t SOLVEstruct_t;
234 };
235 
236 #if defined(HAVE_TEUCHOS_COMPLEX) && !defined(__clang__)
237 template <>
238 struct TypeMap<Superludist,std::complex<double> >
239 {
240  static const SLUD::Dtype_t dtype = SLUD::SLU_Z;
241  typedef SLUD::Z::doublecomplex type;
242  typedef double magnitude_type;
243  typedef SLUD::Z::LUstruct_t LUstruct_t;
244  typedef SLUD::Z::SOLVEstruct_t SOLVEstruct_t;
245 };
246 
247  // It probably won't happen, but what if someone does create a
248  // matrix or multivector with the SuperLU_DIST doublecomplex type
249  // directly?
250 template <>
251 struct TypeMap<Superludist,SLUD::Z::doublecomplex>
252 {
253  static const SLUD::Dtype_t dtype = SLUD::SLU_Z;
254  typedef SLUD::Z::doublecomplex type;
255  typedef double magnitude_type;
256  typedef SLUD::Z::LUstruct_t LUstruct_t;
257  typedef SLUD::Z::SOLVEstruct_t SOLVEstruct_t;
258 };
259 
260 #endif // HAVE_TEUCHOS_COMPLEX
261 
262 /* \endcond Superludist_type_specializations */
263 
264 
265 } // end namespace Amesos2
266 
267 #endif // AMESOS2_SUPERLUDIST_TYPEMAP_HPP