Teuchos - Trilinos Tools Package  Version of the Day
Teuchos_MpiReductionOpSetter.cpp
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #include "Teuchos_MpiReductionOpSetter.hpp"
43 #include "Teuchos_OpaqueWrapper.hpp"
45 
46 #ifdef MPIAPI
47 #define CALL_API MPIAPI
48 #else
49 #define CALL_API
50 #endif
51 
52 //
53 // This implementation of handling the reduction operator
54 // will work just fine in a single threaded program.
55 // For multi-threaded program this has to be reworked!
56 //
57 
58 //
59 // The callback that an MPI implementation will actually call
60 //
61 
62 extern "C" {
63 void CALL_API Teuchos_MPI_reduction_op(
64  void *invec
65  ,void *inoutvec
66  ,int *len
67  ,MPI_Datatype *datatype
68  );
69 
70 void Teuchos_MPI_Op_free( MPI_Op *op )
71 {
72  //if(Teuchos::GlobalMPISession::mpiIsInitialized())
73  // MPI_Op_free(op);
74  //else
75  // *op = MPI_OP_NULL;
76  //
77  // RAB: I have commented this out because this is getting called after
78  // MPI_Finalize() is called when the Teuchos::GlobalMPISession class is not
79  // being used.. On some systems, like MPICH, this was not problem.
80  // However, there are some systems that complain when you do this.
81  // Therefore, since I don't really know how to fix this problem, I am just
82  // going to punt and just not delete this MPI_Op object. I suspect that
83  // many people do not clean up their MPI objects correctly so I would guess
84  // that almost every MPI implementation allows you to not free objects and
85  // end just fine.
86 }
87 
88 } // extern "C"
89 
90 //
91 // Manage the reduction operation as static data. I have used access
92 // functions here to allow more insulation for other implementations other
93 // other than just single threaded programs.
94 //
95 
96 namespace {
97 
98 Teuchos::RCP<const Teuchos::MpiReductionOpBase> the_reduct_op = Teuchos::null;
99 
100 Teuchos::RCP<const Teuchos::OpaqueWrapper<MPI_Op> > the_mpi_op = Teuchos::null;
101 
103 {
104  return the_reduct_op;
105 }
106 
107 void set_reduct_op( const Teuchos::RCP<const Teuchos::MpiReductionOpBase>& reduct_op )
108 {
109  using Teuchos::null;
110 #ifdef TEUCHOS_DEBUG
111  TEUCHOS_TEST_FOR_EXCEPT( get_reduct_op() != null && reduct_op != null );
112 #endif
113  if(!the_mpi_op.get()) {
114  MPI_Op mpi_op = MPI_OP_NULL;
116  0!=MPI_Op_create( &Teuchos_MPI_reduction_op ,1 ,&mpi_op ) // Assume op is commutative?
117  );
118  the_mpi_op = Teuchos::opaqueWrapper(mpi_op,Teuchos_MPI_Op_free);
119  }
120  the_reduct_op = reduct_op;
121 }
122 
123 } // namespace
124 
125 extern "C" {
126 void CALL_API Teuchos_MPI_reduction_op(
127  void *invec
128  ,void *inoutvec
129  ,int *len
130  ,MPI_Datatype *datatype
131  )
132 {
133  get_reduct_op()->reduce(invec,inoutvec,len,datatype);
134 }
135 
136 } // extern "C"
137 
138 namespace Teuchos {
139 
140 MpiReductionOpSetter::MpiReductionOpSetter(
142  )
143 {
144 #ifdef TEUCHOS_DEBUG
145  TEUCHOS_TEST_FOR_EXCEPT(!reduct_op.get())
146 #endif
147  set_reduct_op(reduct_op);
148 }
149 
151 {
152  set_reduct_op( null );
153 }
154 
156 {
157  return (*the_mpi_op)();
158 }
159 
160 } // namespace Teuchos
T * get() const
Get the raw C++ pointer to the underlying object.
MPI_Op mpi_op() const
Return the created MPI_Op reduction object that can be used by MPI.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos, as well as a number of utility routines.
A MPI utilities class, providing methods for initializing, finalizing, and querying the global MPI se...
Smart reference counting pointer class for automatic garbage collection.
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
This macro is designed to be a short version of TEUCHOS_TEST_FOR_EXCEPTION() that is easier to call...