Amesos2 - Direct Sparse Solver Interfaces  Version of the Day
Amesos2_Cholmod_TypeMap.hpp
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_CHOLMOD_TYPEMAP_HPP
57 #define AMESOS2_CHOLMOD_TYPEMAP_HPP
58 
59 #include <functional>
60 #ifdef HAVE_TEUCHOS_COMPLEX
61 #include <complex>
62 #endif
63 
64 #include <Teuchos_as.hpp>
65 #ifdef HAVE_TEUCHOS_COMPLEX
66 #include <Teuchos_SerializationTraits.hpp>
67 #endif
68 
69 #include "Amesos2_TypeMap.hpp"
70 
71 namespace Amesos2{
72  namespace CHOL {
73 
74  struct complex
75  {
76  double complexpair[2];
77  };
78 
79  # include "cholmod.h"
80  //# include <mkl_types.h>
81  } // end namespace PMKL
82 } // end namespace Amesos2
83 
84 
85 /* ==================== Conversion ====================
86  *
87  * Define here, in the Teuchos namespace, any conversions between
88  * commonly used date types and the solver-specific data types. Use
89  * template specializations of the Teuchos::ValueTypeConversionTraits
90  * class.
91  */
92 namespace Teuchos {
93 
94  template <typename TypeFrom>
95  class ValueTypeConversionTraits<Amesos2::CHOL::complex, TypeFrom>
96  {
97  public:
98  static Amesos2::CHOL::complex convert( const TypeFrom t )
99  { // adapt conversion as necessary
100  Amesos2::CHOL::complex ret;
101  //ret.r = Teuchos::as<float>(t.real());
102  //ret.i = Teuchos::as<float>(t.imag());
103  ret.complexpair[0] = Teuchos::as<float>(t.real());
104  ret.complexpair[1] = Teuchos::as<float>(t.imag());
105  return( ret );
106  }
107 
108  static Amesos2::CHOL::complex safeConvert( const TypeFrom t )
109  { // adapt conversion as necessary
110  Amesos2::CHOL::complex ret;
111  //ret.r = Teuchos::as<float>(t.real());
112  //ret.i = Teuchos::as<float>(t.imag());
113  ret.complexpair[0] = Teuchos::as<float>(t.real());
114  ret.complexpair[1] = Teuchos::as<float>(t.imag());
115  return( ret );
116  }
117  };
118 
119  // Also convert *from* New_Solver types
120  template <typename TypeTo>
121  class ValueTypeConversionTraits<TypeTo, Amesos2::CHOL::complex>
122  {
123  public:
124  static TypeTo convert( const Amesos2::CHOL::complex t )
125  { // adapt conversion as necessary
126  typedef typename TypeTo::value_type value_type;
127  //value_type ret_r = Teuchos::as<value_type>( t.real );
128  //value_type ret_i = Teuchos::as<value_type>( t.imag );
129  value_type ret_r = Teuchos::as<value_type>(t.complexpair[0]);
130  value_type ret_i = Teuchos::as<value_type>(t.complexpair[1]);
131  return ( TypeTo( ret_r, ret_i ) );
132  }
133 
134  static TypeTo safeConvert( const Amesos2::CHOL::complex t )
135  { // adapt conversion as necessary
136  typedef typename TypeTo::value_type value_type;
137  //value_type ret_r = Teuchos::as<value_type>( t.real );
138  //value_type ret_i = Teuchos::as<value_type>( t.imag );
139  value_type ret_r = Teuchos::as<value_type>(t.complexpair[0]);
140  value_type ret_i = Teuchos::as<value_type>(t.complexpair[1]);
141  return ( TypeTo( ret_r, ret_i ) );
142  }
143  };
144 
146 
147 } // end namespace Teuchos
148 
149 
150 namespace Amesos2 {
151 
152  // forward declaration due to circular reference
153  template <class, class> class Cholmod;
154 
155  template <>
156  struct TypeMap<Cholmod,float>
157  {
158  typedef float type;
159  typedef float magnitude_type;
160  };
161 
162  template <>
163  struct TypeMap<Cholmod,double>
164  {
165  typedef double type;
166  typedef double magnitude_type;
167  };
168 
169 #ifdef HAVE_TEUCHOS_COMPLEX
170 
171  template <>
172  struct TypeMap<Cholmod,std::complex<double> >
173  {
174  typedef CHOL::complex type;
175  typedef double magnitude_type;
176  };
177 
178 #endif // HAVE_TEUCHOS_COMPLEX
179 
180  /* \endcond Choldmod_type_specializations */
181 
182 } // end namespace Amesos
183 
184 #endif // AMESOS2_CHOLMOD_TYPEMAP_HPP