PlayaMPIDataType.cpp
Go to the documentation of this file.
00001 /* @HEADER@ */
00002 // ************************************************************************
00003 // 
00004 //                 Playa: Programmable Linear Algebra
00005 //                 Copyright 2012 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 Kevin Long (kevin.long@ttu.edu)
00038 // 
00039 
00040 /* @HEADER@ */
00041 
00042 #include "PlayaMPIDataType.hpp"
00043 
00044 #ifdef HAVE_MPI
00045 // Provide an explicit template specialization for the opaque type MPI_Datatype
00046 // so that the instantiation of Teuchos::RCP<MPI_Datatype> objects compiles correctly in debug mode
00047 // without relying on the implementation details of the MPI library.
00048 #include "Teuchos_TypeNameTraits.hpp"
00049 namespace Teuchos
00050 {
00051 // MPI_Datatype is typedef int in MPICH, so avoid duplicate instantiation
00052 #if !defined(MPICH) && !defined(MPICH2)
00053   TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(MPI_Datatype);
00054 #endif
00055 } // namespace Teuchos
00056 #endif
00057 
00058 namespace Playa
00059 {
00060 using Teuchos::RCP;
00061 using Teuchos::rcp;
00062 
00063 MPIDataType::MPIDataType(const std::string& name)
00064   : name_(name)
00065 #ifdef HAVE_MPI
00066   , mpiType_()
00067 #endif
00068 {}
00069 
00070 #ifdef HAVE_MPI
00071 MPIDataType::MPIDataType(const std::string& name, 
00072   const RCP<MPI_Datatype>& mpiType)
00073   : name_(name), mpiType_(mpiType){}
00074 
00075 
00076 MPI_Datatype* MPIDataType::ptr() 
00077 {
00078   TEUCHOS_TEST_FOR_EXCEPT(mpiType_.get()==0);
00079   return mpiType_.get();
00080 }
00081 
00082 const MPI_Datatype& MPIDataType::handle() const 
00083 {
00084   TEUCHOS_TEST_FOR_EXCEPT(mpiType_.get()==0);
00085   return *(mpiType_.get());
00086 }
00087 #endif
00088 
00089 
00090 
00091 std::stack<MPIDataType>& MPIDataType::typeRegistry()
00092 {
00093   static std::stack<MPIDataType> rtn;
00094 
00095   return rtn;
00096 }
00097 
00098 void MPIDataType::clearTypeRegistry()
00099 {
00100   while(!typeRegistry().empty())
00101   {
00102 #ifdef HAVE_MPI
00103 //    MPIDataType t = typeRegistry().top();
00104 //    std::cerr << "clearing type " << typeRegistry().top().name() << std::endl;
00105 
00106     int ierr = MPI_Type_free(typeRegistry().top().ptr());
00107 
00108     TEUCHOS_TEST_FOR_EXCEPTION(ierr != 0, std::runtime_error,
00109       "Error code=" << ierr << " detected in MPI_Type_free()");
00110 #endif
00111     typeRegistry().pop();
00112   }
00113 }
00114 
00115 void MPIDataType::registerType(const MPIDataType& dataType)
00116 {
00117   typeRegistry().push(dataType);
00118 }
00119 
00120 MPIDataType MPIDataType::intType()
00121 {
00122 #ifdef HAVE_MPI
00123   static MPIDataType rtn("MPI int", rcp(new MPI_Datatype(MPI_INT)));
00124 #else
00125   static MPIDataType rtn("MPI int");
00126 #endif
00127   return rtn;
00128 }
00129 
00130 
00131 
00132 MPIDataType MPIDataType::floatType()
00133 {
00134 #ifdef HAVE_MPI
00135   static MPIDataType rtn("MPI float", rcp(new MPI_Datatype(MPI_FLOAT)));
00136 #else
00137   static MPIDataType rtn("MPI float");
00138 #endif
00139   return rtn;
00140 }
00141 
00142 
00143 
00144 MPIDataType MPIDataType::doubleType()
00145 {
00146 #ifdef HAVE_MPI
00147   static MPIDataType rtn("MPI double", rcp(new MPI_Datatype(MPI_DOUBLE)));
00148 #else
00149   static MPIDataType rtn("MPI double");
00150 #endif
00151   return rtn;
00152 }
00153 
00154 
00155 
00156 MPIDataType MPIDataType::doubleIntPairType()
00157 {
00158 #ifdef HAVE_MPI
00159   static MPIDataType rtn("MPI double/int pair", rcp(new MPI_Datatype(MPI_DOUBLE_INT)));
00160 #else
00161   static MPIDataType rtn("MPI double/int pair");
00162 #endif
00163   return rtn;
00164 }
00165 
00166 
00167 
00168 
00169 MPIDataType MPIDataType::charType()
00170 {
00171 #ifdef HAVE_MPI
00172   static MPIDataType rtn("MPI char", rcp(new MPI_Datatype(MPI_CHAR)));
00173 #else
00174   static MPIDataType rtn("MPI char");
00175 #endif
00176   return rtn;
00177 }
00178 
00179 
00180 
00181   
00182 } // namespace Playa
00183 

Site Contact