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