PlayaMPIOp.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 "PlayaMPIOp.hpp"
00043 
00044 #ifdef HAVE_MPI
00045 // Provide an explicit template specialization for the opaque type MPI_Op
00046 // so that the instantiation of Teuchos::RCP<MPI_Op> 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_Op is typedef int in MPICH, so avoid duplicate instantiation
00052 #if !defined(MPICH) && !defined(MPICH2)
00053   TEUCHOS_TYPE_NAME_TRAITS_BUILTIN_TYPE_SPECIALIZATION(MPI_Op);
00054 #endif
00055 } // namespace Teuchos
00056 #endif
00057 
00058 namespace Playa
00059 {
00060 using Teuchos::RCP;
00061 using Teuchos::rcp;
00062 
00063 MPIOp::MPIOp(const std::string& name)
00064   : name_(name)
00065 #ifdef HAVE_MPI
00066   , mpiOp_()
00067 #endif
00068 {}
00069 
00070 #ifdef HAVE_MPI
00071 MPIOp::MPIOp(const std::string& name, 
00072   const RCP<MPI_Op>& mpiOp)
00073   : name_(name), mpiOp_(mpiOp){}
00074 
00075 
00076 MPI_Op* MPIOp::ptr() 
00077 {
00078   TEUCHOS_TEST_FOR_EXCEPT(mpiOp_.get()==0);
00079   return mpiOp_.get();
00080 }
00081 
00082 const MPI_Op& MPIOp::handle() const 
00083 {
00084   TEUCHOS_TEST_FOR_EXCEPT(mpiOp_.get()==0);
00085   return *(mpiOp_.get());
00086 }
00087 #endif
00088 
00089 
00090 
00091 std::stack<MPIOp>& MPIOp::opRegistry()
00092 {
00093   static std::stack<MPIOp> rtn;
00094 
00095   return rtn;
00096 }
00097 
00098 void MPIOp::clearOpRegistry()
00099 {
00100   while(!opRegistry().empty())
00101   {
00102 #ifdef HAVE_MPI
00103     MPIOp t = opRegistry().top();
00104 
00105     int ierr = MPI_Op_free(t.ptr());
00106 
00107     TEUCHOS_TEST_FOR_EXCEPTION(ierr != 0, std::runtime_error,
00108       "Error code=" << ierr << " detected in MPI_Type_free()");
00109 #endif
00110     opRegistry().pop();
00111   }
00112 }
00113 
00114 void MPIOp::registerOp(const MPIOp& opType)
00115 {
00116   opRegistry().push(opType);
00117 }
00118 
00119 
00120 MPIOp MPIOp::sumOp()
00121 {
00122 #ifdef HAVE_MPI
00123   static MPIOp rtn("MPI sum", rcp(new MPI_Op(MPI_SUM)));
00124 #else
00125   static MPIOp rtn("MPI sum");
00126 #endif
00127   return rtn;
00128 }
00129 
00130 
00131 MPIOp MPIOp::minOp()
00132 {
00133 #ifdef HAVE_MPI
00134   static MPIOp rtn("MPI min", rcp(new MPI_Op(MPI_MIN)));
00135 #else
00136   static MPIOp rtn("MPI min");
00137 #endif
00138   return rtn;
00139 }
00140 
00141 MPIOp MPIOp::maxOp()
00142 {
00143 #ifdef HAVE_MPI
00144   static MPIOp rtn("MPI max", rcp(new MPI_Op(MPI_MAX)));
00145 #else
00146   static MPIOp rtn("MPI max");
00147 #endif
00148   return rtn;
00149 }
00150 
00151 MPIOp MPIOp::minlocOp()
00152 {
00153 #ifdef HAVE_MPI
00154   static MPIOp rtn("MPI minloc", rcp(new MPI_Op(MPI_MINLOC)));
00155 #else
00156   static MPIOp rtn("MPI minloc");
00157 #endif
00158   return rtn;
00159 }
00160 
00161 MPIOp MPIOp::maxlocOp()
00162 {
00163 #ifdef HAVE_MPI
00164   static MPIOp rtn("MPI maxloc", rcp(new MPI_Op(MPI_MAXLOC)));
00165 #else
00166   static MPIOp rtn("MPI maxloc");
00167 #endif
00168   return rtn;
00169 }
00170 
00171 MPIOp MPIOp::productOp()
00172 {
00173 #ifdef HAVE_MPI
00174   static MPIOp rtn("MPI prod", rcp(new MPI_Op(MPI_PROD)));
00175 #else
00176   static MPIOp rtn("MPI prod");
00177 #endif
00178   return rtn;
00179 }
00180 
00181 
00182   
00183 } // namespace Playa
00184 

Site Contact