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 //
00034 // This implementation of handling the reduction operator
00035 // will work just fine in a single threaded program.
00036 // For multi-threaded program this has to be reworked!
00037 //
00038 
00039 //
00040 // The callback that an MPI implementation will actually call
00041 //
00042 
00043 extern "C" {
00044 
00045 void Teuchos_MPI_reduction_op(
00046   void              *invec
00047   ,void             *inoutvec
00048   ,int              *len
00049   ,MPI_Datatype     *datatype
00050   );
00051 
00052 void Teuchos_MPI_Op_free( MPI_Op *op )
00053 {
00054   if(Teuchos::GlobalMPISession::mpiIsInitialized())
00055     MPI_Op_free(op);
00056   else
00057     *op = MPI_OP_NULL;
00058 }
00059 
00060 } // extern "C"
00061 
00062 //
00063 // Manage the reduction operation as static data.  I have used access
00064 // functions here to allow more insulation for other implementations other
00065 // other than just single threaded programs.
00066 //
00067 
00068 namespace {
00069 
00070 Teuchos::RefCountPtr<const Teuchos::MpiReductionOpBase> the_reduct_op = Teuchos::null;
00071 
00072 Teuchos::RefCountPtr<const Teuchos::OpaqueWrapper<MPI_Op> > the_mpi_op = Teuchos::null;
00073 
00074 Teuchos::RefCountPtr<const Teuchos::MpiReductionOpBase> get_reduct_op()
00075 {
00076   return the_reduct_op;
00077 }
00078 
00079 void set_reduct_op( const Teuchos::RefCountPtr<const Teuchos::MpiReductionOpBase>& reduct_op )
00080 {
00081   using Teuchos::null;
00082 #ifdef TEUCHOS_DEBUG
00083   TEST_FOR_EXCEPT( get_reduct_op() != null && reduct_op != null  );
00084 #endif
00085   if(!the_mpi_op.get()) {
00086     MPI_Op mpi_op = MPI_OP_NULL;
00087     TEST_FOR_EXCEPT(
00088       0!=MPI_Op_create( &Teuchos_MPI_reduction_op ,1 ,&mpi_op ) // Assume op is commutative?
00089       );
00090     the_mpi_op = Teuchos::opaqueWrapper(mpi_op,Teuchos_MPI_Op_free);
00091   }
00092   the_reduct_op = reduct_op;
00093 }
00094 
00095 } // namespace
00096 
00097 extern "C" {
00098 
00099 void Teuchos_MPI_reduction_op(
00100   void              *invec
00101   ,void             *inoutvec
00102   ,int              *len
00103   ,MPI_Datatype     *datatype
00104   )
00105 {
00106   get_reduct_op()->reduce(invec,inoutvec,len,datatype);
00107 }
00108 
00109 } // extern "C"
00110 
00111 namespace Teuchos {
00112 
00113 MpiReductionOpSetter::MpiReductionOpSetter(
00114   const Teuchos::RefCountPtr<const MpiReductionOpBase>& reduct_op
00115   )
00116 {
00117 #ifdef TEUCHOS_DEBUG
00118   TEST_FOR_EXCEPT(!reduct_op.get())
00119 #endif
00120   set_reduct_op(reduct_op);
00121 }
00122 
00123 MpiReductionOpSetter::~MpiReductionOpSetter()
00124 {
00125   set_reduct_op( null );
00126 }
00127 
00128 MPI_Op MpiReductionOpSetter::mpi_op() const
00129 {
00130   return (*the_mpi_op)();
00131 }
00132 
00133 } // namespace Teuchos

Generated on Thu Sep 18 12:30:30 2008 for Teuchos - Trilinos Tools Package by doxygen 1.3.9.1