EpetraExt  Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
EpetraExt_MultiMpiComm.cpp
Go to the documentation of this file.
1 //@HEADER
2 // ***********************************************************************
3 //
4 // EpetraExt: Epetra Extended - Linear Algebra Services Package
5 // Copyright (2011) Sandia Corporation
6 //
7 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
8 // the U.S. Government retains certain rights in this software.
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 "EpetraExt_MultiMpiComm.h"
43 #include "Teuchos_Assert.hpp"
44 #include "Teuchos_VerbosityLevel.hpp"
45 
46 namespace EpetraExt {
47 
48 MultiMpiComm::MultiMpiComm(MPI_Comm globalMpiComm, int subDomainProcs, int numTimeSteps_,
49  const Teuchos::EVerbosityLevel verbLevel) :
50  Epetra_MpiComm(globalMpiComm),
51  Teuchos::VerboseObject<MultiMpiComm>(verbLevel),
52  myComm(Teuchos::rcp(new Epetra_MpiComm(globalMpiComm))),
53  subComm(0)
54 {
55  Teuchos::RCP<Teuchos::FancyOStream> out = this->getOStream();
56 
57  // The default output stream only outputs to proc 0, which is not what
58  // we generally want. Manually override this if necessary so we get output
59  // to all processors
60  int outputRootRank = out->getOutputToRootOnly();
61  if (outputRootRank >= 0) {
62  out->setOutputToRootOnly(-1);
63  }
64 
65  //Need to construct subComm for each sub domain, compute subDomainRank,
66  //and check that all integer arithmatic works out correctly.
67 
68  int ierrmpi, size, rank;
69  ierrmpi = MPI_Comm_size(globalMpiComm, &size);
70  ierrmpi = MPI_Comm_rank(globalMpiComm, &rank);
71 
72  TEUCHOS_TEST_FOR_EXCEPTION(
73  size % subDomainProcs != 0,
74  std::logic_error,
75  "ERROR: num subDomainProcs "<< subDomainProcs <<
76  " does not divide into num total procs " << size << std::endl);
77 
78  numSubDomains = size / subDomainProcs;
79  numTimeDomains = subDomainProcs;
80 
81  // Create split communicators
82  MPI_Comm split_MPI_Comm;
83  MPI_Comm time_split_MPI_Comm;
84  subDomainRank = rank / subDomainProcs;
85  timeDomainRank = rank % subDomainProcs;
86  ierrmpi = MPI_Comm_split(globalMpiComm, subDomainRank, rank,
87  &split_MPI_Comm);
88  ierrmpi = MPI_Comm_split(globalMpiComm, timeDomainRank, rank,
89  &time_split_MPI_Comm);
90 
91  // Construct second epetra communicators
92  subComm = new Epetra_MpiComm(split_MPI_Comm);
93  timeComm = new Epetra_MpiComm(time_split_MPI_Comm);
94 
95  // Compute number of time steps on this sub domain
96  ResetNumTimeSteps(numTimeSteps_);
97 
98  if (verbLevel != Teuchos::VERB_NONE) {
99  if (numTimeSteps_ > 0)
100  *out << "Processor " << rank << " is on subdomain " << subDomainRank
101  << " and owns " << numTimeStepsOnDomain
102  << " time steps, starting with "
103  << firstTimeStepOnDomain << std::endl;
104  else
105  *out << "Processor " << rank << " is on subdomain " << subDomainRank
106  << std::endl;
107  }
108 
109  // Reset output flag if we changed it
110  if (outputRootRank >= 0) {
111  out->setOutputToRootOnly(outputRootRank);
112  }
113 }
114 
115 // This constructor is for just one subdomain, so only adds the info
116 // for multiple time steps on the domain. No two-level parallelism.
117 MultiMpiComm::MultiMpiComm(const Epetra_MpiComm& EpetraMpiComm_, int numTimeSteps_,
118  const Teuchos::EVerbosityLevel verbLevel) :
119  Epetra_MpiComm(EpetraMpiComm_),
120  Teuchos::VerboseObject<MultiMpiComm>(verbLevel),
121  myComm(Teuchos::rcp(new Epetra_MpiComm(EpetraMpiComm_))),
122  subComm(0)
123 {
124 
125  numSubDomains = 1;
126  subDomainRank = 0;
127  numTimeSteps = numTimeSteps_;
128  numTimeStepsOnDomain = numTimeSteps_;
130 
131  subComm = new Epetra_MpiComm(EpetraMpiComm_);
132 
133  // Create split communicators for time domain
134  MPI_Comm time_split_MPI_Comm;
135  int rank = EpetraMpiComm_.MyPID();
136  int ierrmpi = MPI_Comm_split(EpetraMpiComm_.Comm(), rank, rank,
137  &time_split_MPI_Comm);
138  (void) ierrmpi; // Silence "unused variable" compiler warning.
139  timeComm = new Epetra_MpiComm(time_split_MPI_Comm);
140  numTimeDomains = EpetraMpiComm_.NumProc();
141  timeDomainRank = rank;
142 }
143 
144 //Copy Constructor
146  Epetra_MpiComm(MMC),
147  myComm(Teuchos::rcp(new Epetra_MpiComm(dynamic_cast<const Epetra_MpiComm&>(MMC)))),
148  subComm(new Epetra_MpiComm(*MMC.subComm)),
149  timeComm(new Epetra_MpiComm(*MMC.timeComm))
150 {
155 
159 }
160 
162 {
163  delete subComm;
164  delete timeComm;
165 }
166 
167 void MultiMpiComm::ResetNumTimeSteps(int numTimeSteps_)
168 {
169  numTimeSteps = numTimeSteps_;
170 
171  // Compute number of time steps on this sub domain
172  if (numTimeSteps > 0) {
173  // Compute part for number of domains dividing evenly into number of steps
176 
177  // Dole out remainder
178  int remainder = numTimeSteps % numSubDomains;
179  if (subDomainRank < remainder) {
182  }
183  else firstTimeStepOnDomain += remainder;
184  }
185  else {
188  }
189 }
190 
191 } //namespace EpetraExt
void ResetNumTimeSteps(int numTimeSteps)
Reset total number of time steps, allowing time steps per domain to.
MultiMpiComm(MPI_Comm globalComm, int subDomainProcs, int numTimeSteps_=-1, const Teuchos::EVerbosityLevel verbLevel=Teuchos::VERB_DEFAULT)
MultiMpiComm constuctor.
virtual ~MultiMpiComm()
Destructor.