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 // RAB: I have commented this out because this is getting called after 00060 // MPI_Finalize() is called when the Teuchos::GlobalMPISession class is not 00061 // being used.. On some systems, like MPICH, this was not problem. 00062 // However, there are some systems that complain when you do this. 00063 // Therefore, since I don't really know how to fix this problem, I am just 00064 // going to punt and just not delete this MPI_Op object. I suspect that 00065 // many people do not clean up their MPI objects correctly so I would guess 00066 // that almost every MPI implementation allows you to not free objects and 00067 // end just fine. 00068 } 00069 00070 } // extern "C" 00071 00072 // 00073 // Manage the reduction operation as static data. I have used access 00074 // functions here to allow more insulation for other implementations other 00075 // other than just single threaded programs. 00076 // 00077 00078 namespace { 00079 00080 Teuchos::RCP<const Teuchos::MpiReductionOpBase> the_reduct_op = Teuchos::null; 00081 00082 Teuchos::RCP<const Teuchos::OpaqueWrapper<MPI_Op> > the_mpi_op = Teuchos::null; 00083 00084 Teuchos::RCP<const Teuchos::MpiReductionOpBase> get_reduct_op() 00085 { 00086 return the_reduct_op; 00087 } 00088 00089 void set_reduct_op( const Teuchos::RCP<const Teuchos::MpiReductionOpBase>& reduct_op ) 00090 { 00091 using Teuchos::null; 00092 #ifdef TEUCHOS_DEBUG 00093 TEST_FOR_EXCEPT( get_reduct_op() != null && reduct_op != null ); 00094 #endif 00095 if(!the_mpi_op.get()) { 00096 MPI_Op mpi_op = MPI_OP_NULL; 00097 TEST_FOR_EXCEPT( 00098 0!=MPI_Op_create( &Teuchos_MPI_reduction_op ,1 ,&mpi_op ) // Assume op is commutative? 00099 ); 00100 the_mpi_op = Teuchos::opaqueWrapper(mpi_op,Teuchos_MPI_Op_free); 00101 } 00102 the_reduct_op = reduct_op; 00103 } 00104 00105 } // namespace 00106 00107 extern "C" { 00108 00109 void Teuchos_MPI_reduction_op( 00110 void *invec 00111 ,void *inoutvec 00112 ,int *len 00113 ,MPI_Datatype *datatype 00114 ) 00115 { 00116 get_reduct_op()->reduce(invec,inoutvec,len,datatype); 00117 } 00118 00119 } // extern "C" 00120 00121 namespace Teuchos { 00122 00123 MpiReductionOpSetter::MpiReductionOpSetter( 00124 const Teuchos::RCP<const MpiReductionOpBase>& reduct_op 00125 ) 00126 { 00127 #ifdef TEUCHOS_DEBUG 00128 TEST_FOR_EXCEPT(!reduct_op.get()) 00129 #endif 00130 set_reduct_op(reduct_op); 00131 } 00132 00133 MpiReductionOpSetter::~MpiReductionOpSetter() 00134 { 00135 set_reduct_op( null ); 00136 } 00137 00138 MPI_Op MpiReductionOpSetter::mpi_op() const 00139 { 00140 return (*the_mpi_op)(); 00141 } 00142 00143 } // namespace Teuchos
1.4.7