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_ = Teuchos::rcp(new Details::DistributorPlan(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 auto revPlan = plan_->getReversePlan();
200 reverseDistributor_->plan_ = revPlan;
201 reverseDistributor_->verbose_ = verbose_;
202
203 // requests_: Allocated on demand.
204 // reverseDistributor_: See note below
205
206 // I am my reverse Distributor's reverse Distributor.
207 // Thus, it would be legit to do the following:
208 //
209 // reverseDistributor_->reverseDistributor_ = Teuchos::rcp (this, false);
210 //
211 // (Note use of a "weak reference" to avoid a circular RCP
212 // dependency.) The only issue is that if users hold on to the
213 // reverse Distributor but let go of the forward one, this
214 // reference won't be valid anymore. However, the reverse
215 // Distributor is really an implementation detail of Distributor
216 // and not meant to be used directly, so we don't need to do this.
217 reverseDistributor_->reverseDistributor_ = Teuchos::null;
218}
219
221 actor_.doWaits(*plan_);
222}
223
225 // call doWaits() on the reverse Distributor, if it exists
226 if (!reverseDistributor_.is_null()) {
227 reverseDistributor_->doWaits();
228 }
229}
230
231std::string Distributor::description() const {
232 std::ostringstream out;
233
234 out << "\"Tpetra::Distributor\": {";
235 const std::string label = this->getObjectLabel();
236 if (label != "") {
237 out << "Label: " << label << ", ";
238 }
239 out << "How initialized: "
240 << Details::DistributorHowInitializedEnumToString(plan_->howInitialized())
241 << ", Parameters: {"
242 << "Send type: "
243 << DistributorSendTypeEnumToString(plan_->getSendType())
244 << ", Debug: " << (verbose_ ? "true" : "false")
245 << "}}";
246 return out.str();
247}
248
249std::string
250Distributor::
251 localDescribeToString(const Teuchos::EVerbosityLevel vl) const {
252 using std::endl;
253 using Teuchos::toString;
254 using Teuchos::VERB_EXTREME;
255 using Teuchos::VERB_HIGH;
256
257 // This preserves current behavior of Distributor.
259 return std::string();
260 }
261
262 auto outStringP = Teuchos::rcp(new std::ostringstream());
263 auto outp = Teuchos::getFancyOStream(outStringP); // returns RCP
264 Teuchos::FancyOStream& out = *outp;
265
266 const int myRank = plan_->getComm()->getRank();
267 const int numProcs = plan_->getComm()->getSize();
268 out << "Process " << myRank << " of " << numProcs << ":" << endl;
269 Teuchos::OSTab tab1(out);
270
271 out << "selfMessage: " << hasSelfMessage() << endl;
272 out << "numSends: " << getNumSends() << endl;
273 if (vl == VERB_HIGH || vl == VERB_EXTREME) {
274 out << "procsTo: " << toString(plan_->getProcsTo()) << endl;
275 out << "lengthsTo: " << toString(plan_->getLengthsTo()) << endl;
276 out << "maxSendLength: " << getMaxSendLength() << endl;
277 }
278 if (vl == VERB_EXTREME) {
279 out << "startsTo: " << toString(plan_->getStartsTo()) << endl;
280 out << "indicesTo: " << toString(plan_->getIndicesTo()) << endl;
281 }
282 if (vl == VERB_HIGH || vl == VERB_EXTREME) {
283 out << "numReceives: " << getNumReceives() << endl;
284 out << "totalReceiveLength: " << getTotalReceiveLength() << endl;
285 out << "lengthsFrom: " << toString(plan_->getLengthsFrom()) << endl;
286 out << "procsFrom: " << toString(plan_->getProcsFrom()) << endl;
287 }
288
289 out.flush(); // make sure the ostringstream got everything
290 return outStringP->str();
291}
292
294 describe(Teuchos::FancyOStream& out,
295 const Teuchos::EVerbosityLevel verbLevel) const {
296 using std::endl;
297 using Teuchos::VERB_DEFAULT;
298 using Teuchos::VERB_EXTREME;
299 using Teuchos::VERB_HIGH;
300 using Teuchos::VERB_LOW;
301 using Teuchos::VERB_MEDIUM;
302 using Teuchos::VERB_NONE;
303 const Teuchos::EVerbosityLevel vl =
305
306 if (vl == VERB_NONE) {
307 return; // don't print anything
308 }
309 // If this Distributor's Comm is null, then the the calling
310 // process does not participate in Distributor-related collective
311 // operations with the other processes. In that case, it is not
312 // even legal to call this method. The reasonable thing to do in
313 // that case is nothing.
314 if (plan_->getComm().is_null()) {
315 return;
316 }
317 const int myRank = plan_->getComm()->getRank();
318 const int numProcs = plan_->getComm()->getSize();
319
320 // Only Process 0 should touch the output stream, but this method
321 // in general may need to do communication. Thus, we may need to
322 // preserve the current tab level across multiple "if (myRank ==
323 // 0) { ... }" inner scopes. This is why we sometimes create
324 // OSTab instances by pointer, instead of by value. We only need
325 // to create them by pointer if the tab level must persist through
326 // multiple inner scopes.
327 Teuchos::RCP<Teuchos::OSTab> tab0, tab1;
328
329 if (myRank == 0) {
330 // At every verbosity level but VERB_NONE, Process 0 prints.
331 // By convention, describe() always begins with a tab before
332 // printing.
333 tab0 = Teuchos::rcp(new Teuchos::OSTab(out));
334 // We quote the class name because it contains colons.
335 // This makes the output valid YAML.
336 out << "\"Tpetra::Distributor\":" << endl;
337 tab1 = Teuchos::rcp(new Teuchos::OSTab(out));
338
339 const std::string label = this->getObjectLabel();
340 if (label != "") {
341 out << "Label: " << label << endl;
342 }
343 out << "Number of processes: " << numProcs << endl
344 << "How initialized: "
345 << Details::DistributorHowInitializedEnumToString(plan_->howInitialized())
346 << endl;
347 {
348 out << "Parameters: " << endl;
349 Teuchos::OSTab tab2(out);
350 out << "\"Send type\": "
351 << DistributorSendTypeEnumToString(plan_->getSendType()) << endl
352 << "\"Debug\": " << (verbose_ ? "true" : "false") << endl;
353 }
354 } // if myRank == 0
355
356 // This is collective over the Map's communicator.
357 if (vl > VERB_LOW) {
358 const std::string lclStr = this->localDescribeToString(vl);
359 Tpetra::Details::gathervPrint(out, lclStr, *plan_->getComm());
360 }
361
362 out << "Reverse Distributor:";
363 if (reverseDistributor_.is_null()) {
364 out << " null" << endl;
365 } else {
366 out << endl;
367 reverseDistributor_->describe(out, vl);
368 }
369}
370
371size_t
373 createFromSends(const Teuchos::ArrayView<const int>& exportProcIDs) {
374 return plan_->createFromSends(exportProcIDs);
375}
376
378 createFromSendsAndRecvs(const Teuchos::ArrayView<const int>& exportProcIDs,
379 const Teuchos::ArrayView<const int>& remoteProcIDs) {
380 plan_->createFromSendsAndRecvs(exportProcIDs, remoteProcIDs);
381}
382
383} // 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.