Teuchos_MpiReductionOpSetter.cpp

00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 //                    Teuchos: Common Tools Package
00005 //                 Copyright (2004) Sandia Corporation
00006 // 
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 // 
00010 // This library is free software; you can redistribute it and/or modify
00011 // it under the terms of the GNU Lesser General Public License as
00012 // published by the Free Software Foundation; either version 2.1 of the
00013 // License, or (at your option) any later version.
00014 //  
00015 // This library is distributed in the hope that it will be useful, but
00016 // WITHOUT ANY WARRANTY; without even the implied warranty of
00017 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018 // Lesser General Public License for more details.
00019 //  
00020 // You should have received a copy of the GNU Lesser General Public
00021 // License along with this library; if not, write to the Free Software
00022 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
00023 // USA
00024 // Questions? Contact Michael A. Heroux (maherou@sandia.gov) 
00025 // 
00026 // ***********************************************************************
00027 // @HEADER
00028 
00029 #include "Teuchos_MpiReductionOpSetter.hpp"
00030 #include "Teuchos_OpaqueWrapper.hpp"
00031 #include "Teuchos_GlobalMPISession.hpp"
00032 
00033 #ifdef MPIAPI
00034 #define CALL_API MPIAPI
00035 #else
00036 #define CALL_API 
00037 #endif
00038 
00039 //
00040 // This implementation of handling the reduction operator
00041 // will work just fine in a single threaded program.
00042 // For multi-threaded program this has to be reworked!
00043 //
00044 
00045 //
00046 // The callback that an MPI implementation will actually call
00047 //
00048 
00049 extern "C" {
00050 void CALL_API Teuchos_MPI_reduction_op(
00051   void              *invec
00052   ,void             *inoutvec
00053   ,int              *len
00054   ,MPI_Datatype     *datatype
00055   );
00056 
00057 void Teuchos_MPI_Op_free( MPI_Op *op )
00058 {
00059   //if(Teuchos::GlobalMPISession::mpiIsInitialized())
00060   //  MPI_Op_free(op);
00061   //else
00062   //  *op = MPI_OP_NULL;
00063   //
00064   // RAB: I have commented this out because this is getting called after
00065   // MPI_Finalize() is called when the Teuchos::GlobalMPISession class is not
00066   // being used..  On some systems, like MPICH, this was not problem.
00067   // However, there are some systems that complain when you do this.
00068   // Therefore, since I don't really know how to fix this problem, I am just
00069   // going to punt and just not delete this MPI_Op object.  I suspect that
00070   // many people do not clean up their MPI objects correctly so I would guess
00071   // that almost every MPI implementation allows you to not free objects and
00072   // end just fine.
00073 }
00074 
00075 } // extern "C"
00076 
00077 //
00078 // Manage the reduction operation as static data.  I have used access
00079 // functions here to allow more insulation for other implementations other
00080 // other than just single threaded programs.
00081 //
00082 
00083 namespace {
00084 
00085 Teuchos::RCP<const Teuchos::MpiReductionOpBase> the_reduct_op = Teuchos::null;
00086 
00087 Teuchos::RCP<const Teuchos::OpaqueWrapper<MPI_Op> > the_mpi_op = Teuchos::null;
00088 
00089 Teuchos::RCP<const Teuchos::MpiReductionOpBase> get_reduct_op()
00090 {
00091   return the_reduct_op;
00092 }
00093 
00094 void set_reduct_op( const Teuchos::RCP<const Teuchos::MpiReductionOpBase>& reduct_op )
00095 {
00096   using Teuchos::null;
00097 #ifdef TEUCHOS_DEBUG
00098   TEST_FOR_EXCEPT( get_reduct_op() != null && reduct_op != null  );
00099 #endif
00100   if(!the_mpi_op.get()) {
00101     MPI_Op mpi_op = MPI_OP_NULL;
00102     TEST_FOR_EXCEPT(
00103       0!=MPI_Op_create( &Teuchos_MPI_reduction_op ,1 ,&mpi_op ) // Assume op is commutative?
00104       );
00105     the_mpi_op = Teuchos::opaqueWrapper(mpi_op,Teuchos_MPI_Op_free);
00106   }
00107   the_reduct_op = reduct_op;
00108 }
00109 
00110 } // namespace
00111 
00112 extern "C" {
00113 void CALL_API Teuchos_MPI_reduction_op(
00114   void              *invec
00115   ,void             *inoutvec
00116   ,int              *len
00117   ,MPI_Datatype     *datatype
00118   )
00119 {
00120   get_reduct_op()->reduce(invec,inoutvec,len,datatype);
00121 }
00122 
00123 } // extern "C"
00124 
00125 namespace Teuchos {
00126 
00127 MpiReductionOpSetter::MpiReductionOpSetter(
00128   const Teuchos::RCP<const MpiReductionOpBase>& reduct_op
00129   )
00130 {
00131 #ifdef TEUCHOS_DEBUG
00132   TEST_FOR_EXCEPT(!reduct_op.get())
00133 #endif
00134   set_reduct_op(reduct_op);
00135 }
00136 
00137 MpiReductionOpSetter::~MpiReductionOpSetter()
00138 {
00139   set_reduct_op( null );
00140 }
00141 
00142 MPI_Op MpiReductionOpSetter::mpi_op() const
00143 {
00144   return (*the_mpi_op)();
00145 }
00146 
00147 } // namespace Teuchos
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Generated on Wed Apr 13 09:57:43 2011 for Teuchos - Trilinos Tools Package by  doxygen 1.6.3