Tpetra parallel linear algebra Version of the Day
Loading...
Searching...
No Matches
Tpetra_Distributor.cpp
1// @HEADER
2// *****************************************************************************
3// Tpetra: Templated Linear Algebra Services Package
4//
5// Copyright 2008 NTESS and the Tpetra contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
10#include "Tpetra_Distributor.hpp"
13#include "Tpetra_Util.hpp"
14#include "Tpetra_Details_makeValidVerboseStream.hpp"
15#include "Teuchos_StandardParameterEntryValidators.hpp"
16#include "Teuchos_VerboseObjectParameterListHelpers.hpp"
17#include <numeric>
18
19namespace Tpetra {
20// We set default values of Distributor's Boolean parameters here,
21// in this one place. That way, if we want to change the default
22// value of a parameter, we don't have to search the whole file to
23// ensure a consistent setting.
24namespace {
25// Default value of the "Debug" parameter.
26const bool tpetraDistributorDebugDefault = false;
27} // namespace
28
30 Distributor(const Teuchos::RCP<const Teuchos::Comm<int> >& comm,
31 const Teuchos::RCP<Teuchos::FancyOStream>& /* out */,
32 const Teuchos::RCP<Teuchos::ParameterList>& plist)
33 : plan_(comm) {
34 this->setParameterList(plist);
35}
36
38 Distributor(const Teuchos::RCP<const Teuchos::Comm<int> >& comm)
39 : Distributor(comm, Teuchos::null, Teuchos::null) {}
40
42 Distributor(const Teuchos::RCP<const Teuchos::Comm<int> >& comm,
43 const Teuchos::RCP<Teuchos::FancyOStream>& out)
44 : Distributor(comm, out, Teuchos::null) {}
45
47 Distributor(const Teuchos::RCP<const Teuchos::Comm<int> >& comm,
48 const Teuchos::RCP<Teuchos::ParameterList>& plist)
49 : Distributor(comm, Teuchos::null, plist) {}
50
53 : plan_(distributor.plan_)
54 , actor_(distributor.actor_)
55 , verbose_(distributor.verbose_)
56 , reverseDistributor_(distributor.reverseDistributor_) {
57 using Teuchos::ParameterList;
58 using Teuchos::RCP;
59 using Teuchos::rcp;
60
61 RCP<const ParameterList> rhsList = distributor.getParameterList();
62 RCP<ParameterList> newList = rhsList.is_null() ? Teuchos::null : Teuchos::parameterList(*rhsList);
63 this->setParameterList(newList);
64}
65
67 using Teuchos::ParameterList;
68 using Teuchos::parameterList;
69 using Teuchos::RCP;
70
71 std::swap(plan_, rhs.plan_);
72 std::swap(actor_, rhs.actor_);
73 std::swap(verbose_, rhs.verbose_);
74 std::swap(reverseDistributor_, rhs.reverseDistributor_);
75
76 // Swap parameter lists. If they are the same object, make a deep
77 // copy first, so that modifying one won't modify the other one.
79 RCP<ParameterList> rhsList = rhs.getNonconstParameterList();
80 if (lhsList.getRawPtr() == rhsList.getRawPtr() && !rhsList.is_null()) {
82 }
83 if (!rhsList.is_null()) {
85 }
86 if (!lhsList.is_null()) {
87 rhs.setMyParamList(lhsList);
88 }
89
90 // We don't need to swap timers, because all instances of
91 // Distributor use the same timers.
92}
93
94bool Distributor::getVerbose() {
95 return Details::Behavior::verbose("Distributor") ||
96 Details::Behavior::verbose("Tpetra::Distributor");
97}
98
99std::unique_ptr<std::string>
100Distributor::
101 createPrefix(const char methodName[]) const {
103 plan_.getComm().getRawPtr(), "Distributor", methodName);
104}
105
107 setParameterList(const Teuchos::RCP<Teuchos::ParameterList>& plist) {
108 using std::endl;
109 using Teuchos::FancyOStream;
110 using Teuchos::getIntegralValue;
111 using Teuchos::includesVerbLevel;
112 using Teuchos::ParameterList;
113 using Teuchos::parameterList;
114 using Teuchos::RCP;
115 using ::Tpetra::Details::Behavior;
116
117 if (!plist.is_null()) {
119 plist->validateParametersAndSetDefaults(*validParams);
120
121 // ParameterListAcceptor semantics require pointer identity of the
122 // sublist passed to setParameterList(), so we save the pointer.
123 this->setMyParamList(plist);
124
126 planParams->remove("Debug", false);
127 planParams->remove("VerboseObject", false);
128 plan_.setParameterList(planParams);
129 }
130}
131
132Teuchos::RCP<const Teuchos::ParameterList>
134 using Teuchos::Array;
135 using Teuchos::ParameterList;
136 using Teuchos::parameterList;
137 using Teuchos::RCP;
138 using Teuchos::setStringToIntegralParameter;
139
140 const bool debug = tpetraDistributorDebugDefault;
141
145
146 RCP<ParameterList> plist = parameterList("Tpetra::Distributor");
149 "When using MPI, the variant of send to use in "
150 "do[Reverse]Posts()",
151 sendTypes(), sendTypeEnums(), plist.getRawPtr());
152 plist->set("Debug", debug,
153 "Whether to print copious debugging output on "
154 "all processes.");
155 plist->set("Timer Label", "", "Label for Time Monitor output");
156
157 // mfh 24 Dec 2015: Tpetra no longer inherits from
158 // Teuchos::VerboseObject, so it doesn't need the "VerboseObject"
159 // sublist. However, we retain the "VerboseObject" sublist
160 // anyway, for backwards compatibility (otherwise the above
161 // validation would fail with an invalid parameter name, should
162 // the user still want to provide this list).
163 Teuchos::setupVerboseObjectSublist(&*plist);
164 return Teuchos::rcp_const_cast<const ParameterList>(plist);
165}
166
167size_t Distributor::getTotalReceiveLength() const { return plan_.getTotalReceiveLength(); }
168
169size_t Distributor::getNumReceives() const { return plan_.getNumReceives(); }
170
171bool Distributor::hasSelfMessage() const { return plan_.hasSelfMessage(); }
172
173size_t Distributor::getNumSends() const { return plan_.getNumSends(); }
174
175size_t Distributor::getMaxSendLength() const { return plan_.getMaxSendLength(); }
176
177Teuchos::ArrayView<const int> Distributor::getProcsFrom() const { return plan_.getProcsFrom(); }
178
179Teuchos::ArrayView<const size_t> Distributor::getLengthsFrom() const { return plan_.getLengthsFrom(); }
180
181Teuchos::ArrayView<const int> Distributor::getProcsTo() const { return plan_.getProcsTo(); }
182
183Teuchos::ArrayView<const size_t> Distributor::getLengthsTo() const { return plan_.getLengthsTo(); }
184
185Teuchos::RCP<Distributor>
187 if (reverseDistributor_.is_null() && create) {
188 createReverseDistributor();
189 }
190 TEUCHOS_TEST_FOR_EXCEPTION(reverseDistributor_.is_null() && create, std::logic_error,
191 "The reverse "
192 "Distributor is null after createReverseDistributor returned. "
193 "Please report this bug to the Tpetra developers.");
194 return reverseDistributor_;
195}
196
197void Distributor::createReverseDistributor() const {
198 reverseDistributor_ = Teuchos::rcp(new Distributor(plan_.getComm()));
199 reverseDistributor_->plan_ = *plan_.getReversePlan();
200 reverseDistributor_->verbose_ = verbose_;
201
202 // requests_: Allocated on demand.
203 // reverseDistributor_: See note below
204
205 // I am my reverse Distributor's reverse Distributor.
206 // Thus, it would be legit to do the following:
207 //
208 // reverseDistributor_->reverseDistributor_ = Teuchos::rcp (this, false);
209 //
210 // (Note use of a "weak reference" to avoid a circular RCP
211 // dependency.) The only issue is that if users hold on to the
212 // reverse Distributor but let go of the forward one, this
213 // reference won't be valid anymore. However, the reverse
214 // Distributor is really an implementation detail of Distributor
215 // and not meant to be used directly, so we don't need to do this.
216 reverseDistributor_->reverseDistributor_ = Teuchos::null;
217}
218
220 actor_.doWaits(plan_);
221}
222
224 // call doWaits() on the reverse Distributor, if it exists
225 if (!reverseDistributor_.is_null()) {
226 reverseDistributor_->doWaits();
227 }
228}
229
230std::string Distributor::description() const {
231 std::ostringstream out;
232
233 out << "\"Tpetra::Distributor\": {";
234 const std::string label = this->getObjectLabel();
235 if (label != "") {
236 out << "Label: " << label << ", ";
237 }
238 out << "How initialized: "
239 << Details::DistributorHowInitializedEnumToString(plan_.howInitialized())
240 << ", Parameters: {"
241 << "Send type: "
242 << DistributorSendTypeEnumToString(plan_.getSendType())
243 << ", Debug: " << (verbose_ ? "true" : "false")
244 << "}}";
245 return out.str();
246}
247
248std::string
249Distributor::
250 localDescribeToString(const Teuchos::EVerbosityLevel vl) const {
251 using std::endl;
252 using Teuchos::toString;
253 using Teuchos::VERB_EXTREME;
254 using Teuchos::VERB_HIGH;
255
256 // This preserves current behavior of Distributor.
257 if (vl <= Teuchos::VERB_LOW || plan_.getComm().is_null()) {
258 return std::string();
259 }
260
261 auto outStringP = Teuchos::rcp(new std::ostringstream());
262 auto outp = Teuchos::getFancyOStream(outStringP); // returns RCP
263 Teuchos::FancyOStream& out = *outp;
264
265 const int myRank = plan_.getComm()->getRank();
266 const int numProcs = plan_.getComm()->getSize();
267 out << "Process " << myRank << " of " << numProcs << ":" << endl;
268 Teuchos::OSTab tab1(out);
269
270 out << "selfMessage: " << hasSelfMessage() << endl;
271 out << "numSends: " << getNumSends() << endl;
272 if (vl == VERB_HIGH || vl == VERB_EXTREME) {
273 out << "procsTo: " << toString(plan_.getProcsTo()) << endl;
274 out << "lengthsTo: " << toString(plan_.getLengthsTo()) << endl;
275 out << "maxSendLength: " << getMaxSendLength() << endl;
276 }
277 if (vl == VERB_EXTREME) {
278 out << "startsTo: " << toString(plan_.getStartsTo()) << endl;
279 out << "indicesTo: " << toString(plan_.getIndicesTo()) << endl;
280 }
281 if (vl == VERB_HIGH || vl == VERB_EXTREME) {
282 out << "numReceives: " << getNumReceives() << endl;
283 out << "totalReceiveLength: " << getTotalReceiveLength() << endl;
284 out << "lengthsFrom: " << toString(plan_.getLengthsFrom()) << endl;
285 out << "procsFrom: " << toString(plan_.getProcsFrom()) << endl;
286 }
287
288 out.flush(); // make sure the ostringstream got everything
289 return outStringP->str();
290}
291
293 describe(Teuchos::FancyOStream& out,
294 const Teuchos::EVerbosityLevel verbLevel) const {
295 using std::endl;
296 using Teuchos::VERB_DEFAULT;
297 using Teuchos::VERB_EXTREME;
298 using Teuchos::VERB_HIGH;
299 using Teuchos::VERB_LOW;
300 using Teuchos::VERB_MEDIUM;
301 using Teuchos::VERB_NONE;
302 const Teuchos::EVerbosityLevel vl =
304
305 if (vl == VERB_NONE) {
306 return; // don't print anything
307 }
308 // If this Distributor's Comm is null, then the the calling
309 // process does not participate in Distributor-related collective
310 // operations with the other processes. In that case, it is not
311 // even legal to call this method. The reasonable thing to do in
312 // that case is nothing.
313 if (plan_.getComm().is_null()) {
314 return;
315 }
316 const int myRank = plan_.getComm()->getRank();
317 const int numProcs = plan_.getComm()->getSize();
318
319 // Only Process 0 should touch the output stream, but this method
320 // in general may need to do communication. Thus, we may need to
321 // preserve the current tab level across multiple "if (myRank ==
322 // 0) { ... }" inner scopes. This is why we sometimes create
323 // OSTab instances by pointer, instead of by value. We only need
324 // to create them by pointer if the tab level must persist through
325 // multiple inner scopes.
326 Teuchos::RCP<Teuchos::OSTab> tab0, tab1;
327
328 if (myRank == 0) {
329 // At every verbosity level but VERB_NONE, Process 0 prints.
330 // By convention, describe() always begins with a tab before
331 // printing.
332 tab0 = Teuchos::rcp(new Teuchos::OSTab(out));
333 // We quote the class name because it contains colons.
334 // This makes the output valid YAML.
335 out << "\"Tpetra::Distributor\":" << endl;
336 tab1 = Teuchos::rcp(new Teuchos::OSTab(out));
337
338 const std::string label = this->getObjectLabel();
339 if (label != "") {
340 out << "Label: " << label << endl;
341 }
342 out << "Number of processes: " << numProcs << endl
343 << "How initialized: "
344 << Details::DistributorHowInitializedEnumToString(plan_.howInitialized())
345 << endl;
346 {
347 out << "Parameters: " << endl;
348 Teuchos::OSTab tab2(out);
349 out << "\"Send type\": "
350 << DistributorSendTypeEnumToString(plan_.getSendType()) << endl
351 << "\"Debug\": " << (verbose_ ? "true" : "false") << endl;
352 }
353 } // if myRank == 0
354
355 // This is collective over the Map's communicator.
356 if (vl > VERB_LOW) {
357 const std::string lclStr = this->localDescribeToString(vl);
358 Tpetra::Details::gathervPrint(out, lclStr, *plan_.getComm());
359 }
360
361 out << "Reverse Distributor:";
362 if (reverseDistributor_.is_null()) {
363 out << " null" << endl;
364 } else {
365 out << endl;
366 reverseDistributor_->describe(out, vl);
367 }
368}
369
370size_t
372 createFromSends(const Teuchos::ArrayView<const int>& exportProcIDs) {
373 return plan_.createFromSends(exportProcIDs);
374}
375
377 createFromSendsAndRecvs(const Teuchos::ArrayView<const int>& exportProcIDs,
378 const Teuchos::ArrayView<const int>& remoteProcIDs) {
379 plan_.createFromSendsAndRecvs(exportProcIDs, remoteProcIDs);
380}
381
382} // namespace Tpetra
Declaration of Tpetra::Details::Behavior, a class that describes Tpetra's behavior.
Declaration of a function that prints strings from each process.
Stand-alone utility functions and macros.
Struct that holds views of the contents of a CrsMatrix.
static bool verbose()
Whether Tpetra is in verbose mode.
static std::string defaultSendType()
Default send type.
Sets up and executes a communication plan for a Tpetra DistObject.
size_t getMaxSendLength() const
Maximum number of values this process will send to another single process.
Teuchos::RCP< Distributor > getReverse(bool create=true) const
A reverse communication plan Distributor.
Teuchos::ArrayView< const int > getProcsTo() const
Ranks of the processes to which this process will send values.
size_t getNumReceives() const
The number of processes from which we will receive data.
void setParameterList(const Teuchos::RCP< Teuchos::ParameterList > &plist)
Set Distributor parameters.
size_t getTotalReceiveLength() const
Total number of values this process will receive from other processes.
bool hasSelfMessage() const
Whether the calling process will send or receive messages to itself.
void swap(Distributor &rhs)
Swap the contents of rhs with those of *this.
Teuchos::ArrayView< const size_t > getLengthsTo() const
Number of values this process will send to each process.
Teuchos::ArrayView< const int > getProcsFrom() const
Ranks of the processes sending values to this process.
Distributor(const Teuchos::RCP< const Teuchos::Comm< int > > &comm)
Construct using the specified communicator and default parameters.
std::string description() const
Return a one-line description of this object.
size_t createFromSends(const Teuchos::ArrayView< const int > &exportProcIDs)
Set up Distributor using list of process ranks to which this process will send.
void createFromSendsAndRecvs(const Teuchos::ArrayView< const int > &exportProcIDs, const Teuchos::ArrayView< const int > &remoteProcIDs)
Set up Distributor using list of process ranks to which to send, and list of process ranks from which...
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
List of valid Distributor parameters.
Teuchos::ArrayView< const size_t > getLengthsFrom() const
Number of values this process will receive from each process.
size_t getNumSends() const
The number of processes to which we will send data.
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel=Teuchos::Describable::verbLevel_default) const
Describe this object in a human-readable way to the given output stream.
Teuchos::Array< EDistributorSendType > distributorSendTypeEnums()
Valid enum values of distributor send types.
Teuchos::Array< std::string > distributorSendTypes()
Valid string values for Distributor's "Send type" parameter.
std::unique_ptr< std::string > createPrefix(const int myRank, const char prefix[])
Create string prefix for each line of verbose output.
const std::string & validSendTypeOrThrow(const std::string &s)
Valid enum values of distributor send types.
std::string DistributorHowInitializedEnumToString(EDistributorHowInitialized how)
Convert an EDistributorHowInitialized enum value to a string.
void gathervPrint(std::ostream &out, const std::string &s, const Teuchos::Comm< int > &comm)
On Process 0 in the given communicator, print strings from each process in that communicator,...
Namespace Tpetra contains the class and methods constituting the Tpetra library.