Amesos2 - Direct Sparse Solver Interfaces  Version of the Day
Amesos2_PardisoMKL_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 
44 
56 #ifndef AMESOS2_PARDISOMKL_TYPEMAP_HPP
57 #define AMESOS2_PARDISOMKL_TYPEMAP_HPP
58 
59 #ifdef HAVE_TEUCHOS_COMPLEX
60 #include <complex>
61 #endif
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 namespace Amesos2{
71  namespace PMKL {
72 # include <mkl_dss.h>
73 # include <mkl_types.h>
74  } // end namespace PMKL
75 } // end namespace Amesos2
76 
77 
78 /* ==================== Conversion ====================
79  *
80  * Define here, in the Teuchos namespace, any conversions between
81  * commonly used date types and the solver-specific data types. Use
82  * template specializations of the Teuchos::ValueTypeConversionTraits
83  * class.
84  */
85 namespace Teuchos {
86 
87  template <typename TypeFrom>
88  class ValueTypeConversionTraits<Amesos2::PMKL::_MKL_Complex8, TypeFrom>
89  {
90  public:
91  static Amesos2::PMKL::_MKL_Complex8 convert( const TypeFrom t )
92  { // adapt conversion as necessary
93  Amesos2::PMKL::_MKL_Complex8 ret;
94  ret.real = Teuchos::as<float>(t.real());
95  ret.imag = Teuchos::as<float>(t.imag());
96  return( ret );
97  }
98 
99  static Amesos2::PMKL::_MKL_Complex8 safeConvert( const TypeFrom t )
100  { // adapt conversion as necessary
101  Amesos2::PMKL::_MKL_Complex8 ret;
102  ret.real = Teuchos::as<float>(t.real());
103  ret.imag = Teuchos::as<float>(t.imag());
104  return( ret );
105  }
106  };
107 
108 
109  template <typename TypeFrom>
110  class ValueTypeConversionTraits<Amesos2::PMKL::_DOUBLE_COMPLEX_t, TypeFrom>
111  {
112  public:
113  static Amesos2::PMKL::_DOUBLE_COMPLEX_t convert( const TypeFrom t )
114  { // adapt conversion as necessary
115  Amesos2::PMKL::_DOUBLE_COMPLEX_t ret;
116  ret.r = Teuchos::as<double>(t.real());
117  ret.i = Teuchos::as<double>(t.imag());
118  return( ret );
119  }
120 
121  static Amesos2::PMKL::_DOUBLE_COMPLEX_t safeConvert( const TypeFrom t )
122  { // adapt conversion as necessary
123  Amesos2::PMKL::_DOUBLE_COMPLEX_t ret;
124  ret.r = Teuchos::as<double>(t.real());
125  ret.i = Teuchos::as<double>(t.imag());
126  return( ret );
127  }
128  };
129 
130 
131  // Also convert *from* New_Solver types
132  template <typename TypeTo>
133  class ValueTypeConversionTraits<TypeTo, Amesos2::PMKL::_MKL_Complex8>
134  {
135  public:
136  static TypeTo convert( const Amesos2::PMKL::_MKL_Complex8 t )
137  { // adapt conversion as necessary
138  typedef typename TypeTo::value_type value_type;
139  value_type ret_r = Teuchos::as<value_type>( t.real );
140  value_type ret_i = Teuchos::as<value_type>( t.imag );
141  return ( TypeTo( ret_r, ret_i ) );
142  }
143 
144  static TypeTo safeConvert( const Amesos2::PMKL::_MKL_Complex8 t )
145  { // adapt conversion as necessary
146  typedef typename TypeTo::value_type value_type;
147  value_type ret_r = Teuchos::as<value_type>( t.real );
148  value_type ret_i = Teuchos::as<value_type>( t.imag );
149  return ( TypeTo( ret_r, ret_i ) );
150  }
151  };
152 
153 
154  template <typename TypeTo>
155  class ValueTypeConversionTraits<TypeTo, Amesos2::PMKL::_DOUBLE_COMPLEX_t>
156  {
157  public:
158  static TypeTo convert( const Amesos2::PMKL::_DOUBLE_COMPLEX_t t )
159  {
160  typedef typename TypeTo::value_type value_type;
161  value_type ret_r = Teuchos::as<value_type>( t.r );
162  value_type ret_i = Teuchos::as<value_type>( t.i );
163  return ( TypeTo( ret_r, ret_i ) );
164  }
165 
166  // No special checks for safe Convert
167  static TypeTo safeConvert( const Amesos2::PMKL::_DOUBLE_COMPLEX_t t )
168  {
169  typedef typename TypeTo::value_type value_type;
170  value_type ret_r = Teuchos::as<value_type>( t.r );
171  value_type ret_i = Teuchos::as<value_type>( t.i );
172  return ( TypeTo( ret_r, ret_i ) );
173  }
174  };
175 
177 
178 } // end namespace Teuchos
179 
180 
181 namespace Amesos2 {
182 
183  // forward declaration due to circular reference
184  template <class, class> class PardisoMKL;
185 
186  /* Specialize the Amesos::TypeMap struct for PardisoMKL types.
187  *
188  * Additional nested types may be added without harm. For an example, look at
189  * Amesos2_Superlu_TypeMap.hpp
190  */
191  template <>
192  struct TypeMap<PardisoMKL,float>
193  {
194  typedef PMKL::_REAL_t type;
195  typedef PMKL::_REAL_t magnitude_type;
196  };
197 
198 
199  template <>
200  struct TypeMap<PardisoMKL,double>
201  {
202  typedef PMKL::_DOUBLE_PRECISION_t type;
203  typedef PMKL::_DOUBLE_PRECISION_t magnitude_type;
204  };
205 
206 #ifdef HAVE_TEUCHOS_COMPLEX
207 
208  /*
209  * We map the std complex types to the appropriate PardisoMKL complex
210  * types.
211  */
212 
213  template <>
214  struct TypeMap<PardisoMKL,std::complex<float> >
215  {
216  typedef PMKL::_MKL_Complex8 type;
217  typedef PMKL::_REAL_t magnitude_type;
218  };
219 
220 
221  template <>
222  struct TypeMap<PardisoMKL,std::complex<double> >
223  {
224  typedef PMKL::_DOUBLE_COMPLEX_t type;
225  typedef PMKL::_DOUBLE_PRECISION_t magnitude_type;
226  };
227 
228 
229  template <>
230  struct TypeMap<PardisoMKL,PMKL::_MKL_Complex8>
231  {
232  typedef PMKL::_MKL_Complex8 type;
233  typedef PMKL::_REAL_t magnitude_type;
234  };
235 
236 
237  template <>
238  struct TypeMap<PardisoMKL,PMKL::_DOUBLE_COMPLEX_t>
239  {
240  typedef PMKL::_DOUBLE_COMPLEX_t type;
241  typedef PMKL::_DOUBLE_PRECISION_t magnitude_type;
242  };
243 #endif // HAVE_TEUCHOS_COMPLEX
244 
245  template <>
246  struct TypeMap<PardisoMKL,int>
247  {
248  typedef PMKL::_INTEGER_t type;
249  };
250 
251  template <>
252  struct TypeMap<PardisoMKL,long long int>
253  {
254  typedef long long int type;
255  };
256 
257  /*
258  * We check whether the size of long int is bigger than an int. If
259  * it is, then long int should be the same size as a long long int,
260  * so we can safely promote. Otherwise, long int will probably be
261  * the same size as int, and we can safely treat it as such.
262  */
263  template <>
264  struct TypeMap<PardisoMKL,long int>
265  {
266  typedef Meta::if_then_else<
267  sizeof(int) < sizeof(long int),
268  TypeMap<PardisoMKL,long long int>::type,
269  TypeMap<PardisoMKL,int>::type >::type type;
270  };
271 
272 } // end namespace Amesos
273 
274 #endif // AMESOS2_PARDISOMKL_TYPEMAP_HPP