Amesos2 - Direct Sparse Solver Interfaces Version of the Day
Amesos2_Superlumt_TypeMap.hpp
Go to the documentation of this file.
00001 // @HEADER
00002 //
00003 // ***********************************************************************
00004 //
00005 //           Amesos2: Templated Direct Sparse Solver Package 
00006 //                  Copyright 2011 Sandia Corporation
00007 //
00008 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00009 // the U.S. Government retains certain rights in this software.
00010 //
00011 // Redistribution and use in source and binary forms, with or without
00012 // modification, are permitted provided that the following conditions are
00013 // met:
00014 //
00015 // 1. Redistributions of source code must retain the above copyright
00016 // notice, this list of conditions and the following disclaimer.
00017 //
00018 // 2. Redistributions in binary form must reproduce the above copyright
00019 // notice, this list of conditions and the following disclaimer in the
00020 // documentation and/or other materials provided with the distribution.
00021 //
00022 // 3. Neither the name of the Corporation nor the names of the
00023 // contributors may be used to endorse or promote products derived from
00024 // this software without specific prior written permission.
00025 //
00026 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00027 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00029 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00030 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00031 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00032 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00033 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00034 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00035 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00036 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037 //
00038 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00039 //
00040 // ***********************************************************************
00041 //
00042 // @HEADER
00043 
00054 #ifndef AMESOS2_SUPERLUMT_TYPEMAP_HPP
00055 #define AMESOS2_SUPERLUMT_TYPEMAP_HPP
00056 
00057 #include <functional>
00058 
00059 #include <Teuchos_as.hpp>
00060 #ifdef HAVE_TEUCHOS_COMPLEX
00061 #include <Teuchos_SerializationTraits.hpp>
00062 #endif
00063 
00064 #include "Amesos2_TypeMap.hpp"
00065 
00066 namespace SLUMT {
00067 
00068 typedef int int_t;
00069 
00070 extern "C" {
00071 
00072 #undef __SUPERLU_SUPERMATRIX
00073 #include "supermatrix.h"  // for Dtype_t declaration
00074 
00075 #ifdef HAVE_TEUCHOS_COMPLEX
00076   
00077 namespace C {
00078 #undef __SUPERLU_SCOMPLEX
00079 #undef SCOMPLEX_INCLUDE
00080 #include "slu_scomplex.h"     // single-precision complex data type definitions
00081 }
00082 
00083 namespace Z {
00084 #undef __SUPERLU_DCOMPLEX
00085 #undef DCOMPLEX_INCLUDE
00086 #include "slu_dcomplex.h"     // double-precision complex data type definitions
00087 }
00088 
00089 #endif  // HAVE_TEUCHOS_COMPLEX
00090 
00091 } // end extern "C"
00092 
00093   // Declare and specialize a std::binary_funtion class for
00094   // multiplication of SLUMT types
00095   template <typename slu_scalar_t, typename slu_mag_t>
00096   struct slu_mt_mult {};
00097 
00098   // This specialization handles the generic case were the scalar and
00099   // magnitude types are double or float.
00100   template <typename T>
00101   struct slu_mt_mult<T,T> : std::multiplies<T> {};
00102 
00103 #ifdef HAVE_TEUCHOS_COMPLEX
00104   
00105   // For namespace/macro reasons, we prefix our variables with amesos_*
00106   template <>
00107   struct slu_mt_mult<C::complex,float>
00108     : std::binary_function<C::complex,float,C::complex> {
00109     C::complex operator()(C::complex amesos_c, float amesos_f) {
00110       C::complex amesos_cr;
00111       cs_mult(&amesos_cr, &amesos_c, amesos_f); // cs_mult is a macro, so no namespacing
00112       return( amesos_cr );
00113     }
00114   };
00115 
00116   template <>
00117   struct slu_mt_mult<C::complex,C::complex>
00118     : std::binary_function<C::complex,C::complex,C::complex> {
00119     C::complex operator()(C::complex amesos_c1, C::complex amesos_c2) {
00120       C::complex amesos_cr;
00121       cc_mult(&amesos_cr, &amesos_c1, &amesos_c2); // cc_mult is a macro, so no namespacing
00122       return( amesos_cr );
00123     }
00124   };
00125     
00126   template <>
00127   struct slu_mt_mult<Z::doublecomplex,double>
00128     : std::binary_function<Z::doublecomplex,double,Z::doublecomplex> {
00129     Z::doublecomplex operator()(Z::doublecomplex amesos_z, double amesos_d) {
00130       Z::doublecomplex amesos_zr;
00131       zd_mult(&amesos_zr, &amesos_z, amesos_d); // zd_mult is a macro, so no namespacing
00132       return( amesos_zr );
00133     }
00134   };
00135 
00136   template <>
00137   struct slu_mt_mult<Z::doublecomplex,Z::doublecomplex>
00138     : std::binary_function<Z::doublecomplex,Z::doublecomplex,Z::doublecomplex> {
00139     Z::doublecomplex operator()(Z::doublecomplex amesos_z1, Z::doublecomplex amesos_z2) {
00140       Z::doublecomplex amesos_zr;
00141       zz_mult(&amesos_zr, &amesos_z1, &amesos_z2);    // zz_mult is a macro, so no namespacing
00142       return( amesos_zr );
00143     }
00144   };
00145 
00146 #endif  // HAVE_TEUCHOS_COMPLEX
00147 } // end namespace SLUMt
00148 #ifdef HAVE_TEUCHOS_COMPLEX
00149 
00150 /* ==================== Conversion ==================== */
00151 namespace Teuchos {
00152 
00163 template <typename TypeFrom>
00164 class ValueTypeConversionTraits<SLUMT::C::complex, TypeFrom>
00165 {
00166 public:
00167   static SLUMT::C::complex convert( const TypeFrom t )
00168     {
00169       SLUMT::C::complex ret;
00170       ret.r = Teuchos::as<float>(t.real());
00171       ret.i = Teuchos::as<float>(t.imag());
00172       return( ret );
00173     }
00174 
00175   static SLUMT::C::complex safeConvert( const TypeFrom t )
00176     {
00177       SLUMT::C::complex ret;
00178       ret.r = Teuchos::as<float>(t.real());
00179       ret.i = Teuchos::as<float>(t.imag());
00180       return( ret );
00181     }
00182 };
00183 
00184 
00185 template <typename TypeFrom>
00186 class ValueTypeConversionTraits<SLUMT::Z::doublecomplex, TypeFrom>
00187 {
00188 public:
00189   static SLUMT::Z::doublecomplex convert( const TypeFrom t )
00190     {
00191       SLUMT::Z::doublecomplex ret;
00192       ret.r = Teuchos::as<double>(t.real());
00193       ret.i = Teuchos::as<double>(t.imag());
00194       return( ret );
00195     }
00196 
00197   static SLUMT::Z::doublecomplex safeConvert( const TypeFrom t )
00198     {
00199       SLUMT::Z::doublecomplex ret;
00200       ret.r = Teuchos::as<double>(t.real());
00201       ret.i = Teuchos::as<double>(t.imag());
00202       return( ret );
00203     }
00204 };
00205 
00206 
00207 // Also convert from SLU types
00208 template <typename TypeTo>
00209 class ValueTypeConversionTraits<TypeTo, SLUMT::C::complex>
00210 {
00211 public:
00212   static TypeTo convert( const SLUMT::C::complex t )
00213     {
00214       typedef typename TypeTo::value_type value_type;
00215       value_type ret_r = Teuchos::as<value_type>( t.r );
00216       value_type ret_i = Teuchos::as<value_type>( t.i );
00217       return ( TypeTo( ret_r, ret_i ) );
00218     }
00219 
00220   // No special checks for safe Convert
00221   static TypeTo safeConvert( const SLUMT::C::complex t )
00222     {
00223       typedef typename TypeTo::value_type value_type;
00224       value_type ret_r = Teuchos::as<value_type>( t.r );
00225       value_type ret_i = Teuchos::as<value_type>( t.i );
00226       return ( TypeTo( ret_r, ret_i ) );
00227     }
00228 };
00229 
00230 
00231 template <typename TypeTo>
00232 class ValueTypeConversionTraits<TypeTo, SLUMT::Z::doublecomplex>
00233 {
00234 public:
00235   static TypeTo convert( const SLUMT::Z::doublecomplex t )
00236     {
00237       typedef typename TypeTo::value_type value_type;
00238       value_type ret_r = Teuchos::as<value_type>( t.r );
00239       value_type ret_i = Teuchos::as<value_type>( t.i );
00240       return ( TypeTo( ret_r, ret_i ) );
00241     }
00242 
00243   // No special checks for safe Convert
00244   static TypeTo safeConvert( const SLUMT::Z::doublecomplex t )
00245     {
00246       typedef typename TypeTo::value_type value_type;
00247       value_type ret_r = Teuchos::as<value_type>( t.r );
00248       value_type ret_i = Teuchos::as<value_type>( t.i );
00249       return ( TypeTo( ret_r, ret_i ) );
00250     }
00251 };
00252 
00253 template <typename Ordinal>
00254 class SerializationTraits<Ordinal,SLUMT::C::complex>
00255   : public DirectSerializationTraits<Ordinal,SLUMT::C::complex>
00256 {};
00257 
00258 template <typename Ordinal>
00259 class SerializationTraits<Ordinal,SLUMT::Z::doublecomplex>
00260   : public DirectSerializationTraits<Ordinal,SLUMT::Z::doublecomplex>
00261 {};
00262 
00264 
00265 } // end namespace Teuchos
00266 
00267 
00268 
00274 namespace std {
00275   // C++-style output functions for Superlumt complex types
00276   ostream& operator<<(ostream& out, const SLUMT::C::complex c);
00277 
00278   ostream& operator<<(ostream& out, const SLUMT::Z::doublecomplex z);
00279   
00281 }
00282 
00283 #endif  // HAVE_TEUCHOS_COMPLEX
00284 
00285 
00286 namespace Amesos2 {
00287 
00288 template <class, class> class Superlumt;
00289 
00290 /* Specialize the Amesos2::TypeMap struct for Superlumt types
00291  *
00292  * \cond Superlumt_type_specializations 
00293  */
00294 template <>
00295 struct TypeMap<Superlumt,float>
00296 {
00297   static SLUMT::Dtype_t dtype;
00298   typedef float type;
00299   typedef float magnitude_type;
00300 };
00301 
00302 
00303 template <>
00304 struct TypeMap<Superlumt,double>
00305 {
00306   static SLUMT::Dtype_t dtype;
00307   typedef double type;
00308   typedef double magnitude_type;
00309 };
00310 
00311 
00312 #ifdef HAVE_TEUCHOS_COMPLEX
00313 template <>
00314 struct TypeMap<Superlumt,std::complex<float> >
00315 {
00316   static SLUMT::Dtype_t dtype;
00317   typedef SLUMT::C::complex type;
00318   typedef float magnitude_type;
00319 };
00320 
00321 
00322 template <>
00323 struct TypeMap<Superlumt,std::complex<double> >
00324 {
00325   static SLUMT::Dtype_t dtype;
00326   typedef SLUMT::Z::doublecomplex type;
00327   typedef double magnitude_type;
00328 };
00329 
00330 
00331 template <>
00332 struct TypeMap<Superlumt,SLUMT::C::complex>
00333 {
00334   static SLUMT::Dtype_t dtype;
00335   typedef SLUMT::C::complex type;
00336   typedef float magnitude_type;
00337 };
00338 
00339 
00340 template <>
00341 struct TypeMap<Superlumt,SLUMT::Z::doublecomplex>
00342 {
00343   static SLUMT::Dtype_t dtype;
00344   typedef SLUMT::Z::doublecomplex type;
00345   typedef double magnitude_type;
00346 };
00347 
00348 #endif  // HAVE_TEUCHOS_COMPLEX
00349 
00350 /* \endcond Superlumt_type_specializations */
00351 
00352 
00353 } // end namespace Amesos2
00354 
00355 #endif  // AMESOS2_SUPERLUMT_TYPEMAP_HPP