Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
Teuchos_GlobalMPISession.cpp
Go to the documentation of this file.
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 
43 #include "Teuchos_Assert.hpp"
44 
45 // The header file does not at all depend on MPI routines or types,
46 // so we can defer inclusion of mpi.h to here. This also fixes Bug
47 // 5631: https://software.sandia.gov/bugzilla/show_bug.cgi?id=5631
48 #ifdef HAVE_MPI
49 #include "mpi.h"
50 #endif
51 
52 
53 namespace Teuchos {
54 
59 
60 GlobalMPISession::GlobalMPISession( int* argc, char*** argv, std::ostream *out )
61 {
62  std::ostringstream oss;
63 
64  // Above is used to create all output before sending to *out to avoid
65  // jumbled parallel output between processors
66 
67 #ifdef HAVE_MPI
68  // initialize MPI
69  int mpiHasBeenStarted = 0, mpierr = 0;
70  MPI_Initialized(&mpiHasBeenStarted);
72  mpiHasBeenStarted, std::runtime_error
73  ,"Error, you can only call this constructor once!"
74  ,out
75  );
76 
77  mpierr = ::MPI_Init (argc, (char ***) argv);
79  mpierr != 0, std::runtime_error
80  ,"Error code=" << mpierr << " detected in GlobalMPISession::GlobalMPISession(argc,argv)"
81  ,out
82  );
83 
84  initialize(out); // Get NProc_ and rank_
85 
86  int nameLen;
87  char procName[MPI_MAX_PROCESSOR_NAME];
88  mpierr = ::MPI_Get_processor_name(procName,&nameLen);
90  mpierr != 0, std::runtime_error
91  ,"Error code=" << mpierr << " detected in MPI_Get_processor_name()"
92  ,out
93  );
94 
95  oss << "Teuchos::GlobalMPISession::GlobalMPISession(): started processor with name "
96  << procName << " and rank " << rank_ << "!" << std::endl;
97 
98 #else
99  oss << "Teuchos::GlobalMPISession::GlobalMPISession(): started serial run" << std::endl;
100 #endif
101 #ifndef TEUCHOS_SUPPRESS_PROC_STARTUP_BANNER
102  // See if we should suppress the startup banner
103  bool printStartupBanner = true;
104  const std::string suppress_option("--teuchos-suppress-startup-banner");
105  for( int opt_i = 0; opt_i < *argc; ++opt_i ) {
106  if( suppress_option == (*argv)[opt_i] ) {
107  // We are suppressing the output!
108  printStartupBanner = false;
109  // Remove this option!
110  // Note that (*argv)[*argc]==0 but convention so we copy it too!
111  for( int i = opt_i; i < *argc; ++i )
112  (*argv)[i] = (*argv)[i+1];
113  --*argc;
114  }
115  }
116  if( out && printStartupBanner )
117  *out << oss.str() << std::flush;
118 #endif
119 }
120 
122 {
123  haveMPIState_ = false;
124  mpiIsFinalized_ = true;
125 #ifdef HAVE_MPI
126  int mpierr = ::MPI_Finalize();
128  mpierr != 0, std::runtime_error
129  ,"Error code=" << mpierr << " detected in MPI_Finalize()"
130  ,&std::cerr
131  );
132 #endif
133 }
134 
135 
138  return haveMPIState_;
139 }
140 
141 
143 {
144  return mpiIsFinalized_;
145 }
146 
147 
149 {
151  return rank_;
152 }
153 
154 
157  return nProc_;
158 }
159 
160 
162 {
164 #ifdef HAVE_MPI
165  MPI_Barrier(MPI_COMM_WORLD);
166 #endif
167 }
168 
169 
170 int GlobalMPISession::sum(int localVal)
171 {
173 #ifdef HAVE_MPI
174  int globalSum = -1;
175  MPI_Allreduce(&localVal, &globalSum, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
176  return globalSum;
177 #else
178  return localVal;
179 #endif
180 }
181 
182 
183 void GlobalMPISession::allGather(int localVal, const ArrayView<int> &allVals)
184 {
186  TEUCHOS_ASSERT_EQUALITY(allVals.size(), getNProc());
187 #ifdef HAVE_MPI
188  MPI_Allgather( &localVal, 1, MPI_INT, allVals.getRawPtr(), 1, MPI_INT,
189  MPI_COMM_WORLD);
190 #else
191  allVals[0] = localVal;
192 #endif
193 }
194 
195 
196 // private
197 
198 
199 void GlobalMPISession::initialize( std::ostream *out )
200 {
201 #ifdef HAVE_MPI
202 
203  if(mpiIsFinalized_) {
204  // MPI has aleady been finalized so we have a serial machine again!
205  rank_ = 0;
206  nProc_ = 1;
207  return;
208  }
209 
210  if(haveMPIState_)
211  return; // We already have what we need!
212 
213  // We don't have the state of MPI so the constructor for this class must not
214  // have been called. However, if MPI has been called in another way we
215  // can still get the state of MPI_COMM_WORLD here.
216 
217  int mpiHasBeenStarted = 0, mpierr = 0;
218  MPI_Initialized(&mpiHasBeenStarted);
219 
220  if(!mpiHasBeenStarted)
221  return; // We have to give up and just leave NProc_ and rank_ at the default values.
222 
223  // Get the state of MPI
224 
225  mpierr = ::MPI_Comm_rank( MPI_COMM_WORLD, &rank_ );
227  mpierr != 0, std::runtime_error
228  ,"Error code=" << mpierr << " detected in MPI_Comm_rank()"
229  ,out
230  );
231 
232  mpierr = ::MPI_Comm_size( MPI_COMM_WORLD, &nProc_ );
234  mpierr != 0, std::runtime_error
235  ,"Error code=" << mpierr << " detected in MPI_Comm_size()"
236  ,out
237  );
238 
239  haveMPIState_ = true;
240  mpiIsFinalized_ = false;
241 
242 #endif // HAVE_MPI
243 
244 }
245 
246 
248 {
249  if(!haveMPIState_)
250  initialize(&std::cerr);
251 }
252 
253 
254 } // namespace Teuchos
static int getRank()
Returns the process rank relative to MPI_COMM_WORLD
static int getNProc()
Returns the number of processors relative to MPI_COMM_WORLD
size_type size() const
The total number of items in the managed array.
static bool mpiIsFinalized()
Return whether MPI was already finalized.
GlobalMPISession(int *argc, char ***argv, std::ostream *out=&std::cout)
Calls MPI_Init() if MPI is enabled.
T * getRawPtr() const
Return a raw pointer to beginning of array or NULL if unsized.
static int sum(int localVal)
Sum a set of integers across processes.
A MPI utilities class, providing methods for initializing, finalizing, and querying the global MPI se...
static void barrier()
Perform a barrier so that all processes line up.
static bool mpiIsInitialized()
Return whether MPI was initialized.
#define TEUCHOS_TEST_FOR_EXCEPTION_PRINT(throw_exception_test, Exception, msg, out_ptr)
This macro is the same as TEUCHOS_TEST_FOR_EXCEPTION() except that the exception will be caught...
static void allGather(int localVal, const ArrayView< int > &allVals)
Global all-to-all of a set of integers across processes.
#define TEUCHOS_ASSERT_EQUALITY(val1, val2)
This macro is checks that to numbers are equal and if not then throws an exception with a good error ...
static void initialize(std::ostream *out)
~GlobalMPISession()
Call MPI_Finalize() if MPI is enabled.