Kokkos Node API and Local Linear Algebra Kernels Version of the Day
Tsqr_MpiDatatype.cpp
00001 //@HEADER
00002 // ************************************************************************
00003 // 
00004 //          Kokkos: Node API and Parallel Node Kernels
00005 //              Copyright (2008) Sandia Corporation
00006 // 
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00008 // the U.S. Government retains certain rights in this software.
00009 // 
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00038 // 
00039 // ************************************************************************
00040 //@HEADER
00041 
00042 #include <Tsqr_ConfigDefs.hpp>
00043 #include <Tsqr_MpiDatatype.hpp>
00044 
00045 #ifdef HAVE_MPI // #defined (or not) via Teuchos_ConfigDefs.hpp
00046 #  ifdef HAVE_TSQR_COMPLEX
00047 #    include <complex>
00048 #  endif // HAVE_TSQR_COMPLEX
00049 #  include <utility> // std::pair
00050 
00051 
00052 namespace TSQR {
00053   namespace MPI {
00054 
00055     MPI_Datatype 
00056     cloneRawDatatype (MPI_Datatype in, const bool needsFree)
00057     {
00058       MPI_Datatype out;
00059       if (needsFree)
00060   {
00061     // Non-simple MPI_Datatype objects (i.e., those on which
00062     // we need to call MPI_Type_free() after we're done using
00063     // them) cannot be copied directly.  Instead, we clone
00064     // them using MPI_Type_contiguous() with a count argument
00065     // of 1.
00066     const int err = MPI_Type_contiguous (1, in, &out);
00067     if (err != MPI_SUCCESS)
00068       throw std::runtime_error("Failed to clone MPI_Datatype object");
00069   }
00070       else
00071   {
00072     // Simple MPI_Datatype objects can be copied directly.
00073     // Here, "simple" means that we don't have to call
00074     // MPI_Type_free() on them when we are done using them.
00075     out = in;
00076   }
00077       return out;
00078     }
00079 
00094     static MPI_Datatype
00095     mpi_pair_of (MPI_Datatype in)
00096     {
00097       // This is just a handle, remember?  It's ok to return it; it
00098       // won't fall out of scope.  The MPI implementation is responsible
00099       // for keeping track of these things.
00100       MPI_Datatype new_type;
00101       int err = MPI_Type_contiguous (2, in, &new_type);
00102       if (err != MPI_SUCCESS)
00103   throw std::logic_error ("Failed to create MPI_Datatype");
00104       return new_type;
00105     }
00106 
00107     template<>
00108     MpiDatatype< double >::MpiDatatype () : 
00109       type_ (MPI_DOUBLE),
00110       needsFree_ (false)
00111     {}
00112 
00113     template<>
00114     MpiDatatype< float >::MpiDatatype () :
00115       type_ (MPI_FLOAT),
00116       needsFree_ (false)
00117     {}
00118 
00119     template<>
00120     MpiDatatype< std::pair< double, double > >::MpiDatatype () : 
00121       type_ (mpi_pair_of (MPI_DOUBLE)),
00122       needsFree_ (true)
00123     {}
00124 
00125     template<>
00126     MpiDatatype< std::pair< float, float > >::MpiDatatype () : 
00127       type_ (mpi_pair_of (MPI_FLOAT)),
00128       needsFree_ (true)
00129     {}
00130 
00131 #ifdef HAVE_TSQR_COMPLEX
00132     template<>
00133     MpiDatatype< std::complex<double> >::MpiDatatype () :
00134       type_ (mpi_pair_of (MPI_DOUBLE)),
00135       needsFree_ (true)
00136     {}
00137 
00138     template<>
00139     MpiDatatype< std::complex<float> >::MpiDatatype () :
00140       type_ (mpi_pair_of (MPI_FLOAT)),
00141       needsFree_ (true)
00142     {}
00143 #endif // HAVE_TSQR_COMPLEX
00144 
00145     template<>
00146     MpiDatatype< int >::MpiDatatype () : 
00147       type_ (MPI_INT),
00148       needsFree_ (false)
00149     {}
00150 
00151     template<>
00152     MpiDatatype< std::pair<int, int> >::MpiDatatype () : 
00153       type_ (MPI_2INT),
00154       needsFree_ (false)
00155     {}
00156 
00157     template<>
00158     MpiDatatype< unsigned long >::MpiDatatype () : 
00159       type_ (MPI_UNSIGNED_LONG),
00160       needsFree_ (false)
00161     {}
00162 
00163     template<>
00164     MpiDatatype< std::pair< unsigned long, unsigned long > >::MpiDatatype () : 
00165       type_ (mpi_pair_of (MPI_UNSIGNED_LONG)),
00166       needsFree_ (true)
00167     {}
00168 
00169   } // namespace MPI
00170 } // namespace TSQR
00171 
00172 
00173 #endif // HAVE_MPI
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends