Teuchos - Trilinos Tools Package Version of the Day
Loading...
Searching...
No Matches
Teuchos_DefaultSerialComm.hpp
1// @HEADER
2// *****************************************************************************
3// Teuchos: Common Tools Package
4//
5// Copyright 2004 NTESS and the Teuchos contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
10#ifndef TEUCHOS_SERIAL_COMM_HPP
11#define TEUCHOS_SERIAL_COMM_HPP
12
13#include "Teuchos_Comm.hpp"
15
16
17namespace Teuchos {
18
25template<class OrdinalType>
26class SerialCommStatus : public CommStatus<OrdinalType> {
27public:
30
32 OrdinalType getSourceRank () { return 0; }
33
35 OrdinalType getTag () { return 0; }
36};
37
38
43template<typename Ordinal>
44class SerialComm : public Comm<Ordinal> {
45public:
51 int getTag () const { return 0; }
52
54 return 0;
55 }
56
58
59
61 SerialComm();
62
64 SerialComm(const SerialComm<Ordinal>& other);
65
67
69
70
72 virtual int getRank() const;
74 virtual int getSize() const;
76 virtual void barrier() const;
78 virtual void broadcast(
79 const int rootRank, const Ordinal bytes, char buffer[]
80 ) const;
82 virtual void
83 gather (const Ordinal sendBytes, const char sendBuffer[],
84 const Ordinal recvBytes, char recvBuffer[],
85 const int root) const;
87 virtual void gatherAll(
88 const Ordinal sendBytes, const char sendBuffer[]
89 ,const Ordinal recvBytes, char recvBuffer[]
90 ) const;
92 virtual void reduceAll(
94 ,const Ordinal bytes, const char sendBuffer[], char globalReducts[]
95 ) const;
97 virtual void scan(
99 ,const Ordinal bytes, const char sendBuffer[], char scanReducts[]
100 ) const;
102 virtual void send(
103 const Ordinal bytes, const char sendBuffer[], const int destRank
104 ) const;
106 virtual void
107 send (const Ordinal bytes,
108 const char sendBuffer[],
109 const int destRank,
110 const int tag) const;
112 virtual void ssend(
113 const Ordinal bytes, const char sendBuffer[], const int destRank
114 ) const;
116 virtual void
117 ssend (const Ordinal bytes,
118 const char sendBuffer[],
119 const int destRank,
120 const int tag) const;
122 virtual int receive(
123 const int sourceRank, const Ordinal bytes, char recvBuffer[]
124 ) const;
126 virtual void readySend(
127 const ArrayView<const char> &sendBuffer,
128 const int destRank
129 ) const;
131 virtual void
132 readySend (const Ordinal bytes,
133 const char sendBuffer[],
134 const int destRank,
135 const int tag) const;
138 const ArrayView<const char> &sendBuffer,
139 const int destRank
140 ) const;
143 isend (const ArrayView<const char> &sendBuffer,
144 const int destRank,
145 const int tag) const;
148 const ArrayView<char> &Buffer,
149 const int sourceRank
150 ) const;
153 ireceive (const ArrayView<char> &Buffer,
154 const int sourceRank,
155 const int tag) const;
157 virtual void waitAll(
158 const ArrayView<RCP<CommRequest<Ordinal> > > &requests
159 ) const;
161 virtual void
162 waitAll (const ArrayView<RCP<CommRequest<Ordinal> > >& requests,
163 const ArrayView<RCP<CommStatus<Ordinal> > >& statuses) const;
166 wait (const Ptr<RCP<CommRequest<Ordinal> > >& request) const;
168 virtual RCP< Comm<Ordinal> > duplicate() const;
170 virtual RCP< Comm<Ordinal> > split(const int color, const int key) const;
173 const ArrayView<const int> & ranks) const;
174
176
178
179
181 std::string description() const;
182
184
185};
186
187
192template<typename Ordinal>
197
198
199// ////////////////////////
200// Implementations
201
202
203// Constructors
204
205
206template<typename Ordinal>
209
210template<typename Ordinal>
213
214
215// Overridden from Comm
216
217
218template<typename Ordinal>
220{
221 return 0;
222}
223
224
225template<typename Ordinal>
227{
228 return 1;
229}
230
231
232template<typename Ordinal>
234{
235 // Nothing to do
236}
237
238
239template<typename Ordinal>
241 const int /*rootRank*/, const Ordinal /*bytes*/, char []/*buffer*/
242 ) const
243{
244 // Nothing to do
245}
246
247
248template<typename Ordinal>
250 const Ordinal sendBytes, const char sendBuffer[]
251 ,const Ordinal recvBytes, char recvBuffer[]
252 ) const
253{
254 (void)sendBytes; // to remove "unused parameter" warning
255 (void)recvBytes;
256 (void)sendBuffer;
257 (void)recvBuffer;
258#ifdef TEUCHOS_DEBUG
259 TEUCHOS_TEST_FOR_EXCEPT(!(sendBytes==recvBytes));
260#endif
261 std::copy(sendBuffer,sendBuffer+sendBytes,recvBuffer);
262}
263
264
265template<typename Ordinal>
266void
267SerialComm<Ordinal>::gather (const Ordinal sendBytes,
268 const char sendBuffer[],
269 const Ordinal recvBytes,
270 char recvBuffer[],
271 const int root) const
272{
273 (void) sendBytes; // to remove "unused parameter" warning
274 (void) recvBytes;
275 (void) sendBuffer;
276 (void) recvBuffer;
277 (void) root;
278#ifdef TEUCHOS_DEBUG
279 TEUCHOS_TEST_FOR_EXCEPT(!(sendBytes==recvBytes));
280#endif
281 std::copy (sendBuffer, sendBuffer + sendBytes, recvBuffer);
282}
283
284
285template<typename Ordinal>
288 ,const Ordinal bytes, const char sendBuffer[], char globalReducts[]
289 ) const
290{
291 (void)reductOp;
292 std::copy(sendBuffer,sendBuffer+bytes,globalReducts);
293}
294
295
296template<typename Ordinal>
299 ,const Ordinal bytes, const char sendBuffer[], char scanReducts[]
300 ) const
301{
302 (void)reductOp;
303 std::copy(sendBuffer,sendBuffer+bytes,scanReducts);
304}
305
306
307template<typename Ordinal>
309 const Ordinal /*bytes*/, const char []/*sendBuffer*/, const int /*destRank*/
310 ) const
311{
313 true, std::logic_error
314 ,"SerialComm<Ordinal>::send(...): Error, you can not call send(...) when you"
315 " only have one process!"
316 );
317}
318
319template<typename Ordinal>
321send (const Ordinal /*bytes*/,
322 const char []/*sendBuffer*/,
323 const int /*destRank*/,
324 const int /*tag*/) const
325{
327 true, std::logic_error
328 ,"SerialComm<Ordinal>::send(...): Error, you can not call send(...) when you"
329 " only have one process!"
330 );
331}
332
333template<typename Ordinal>
335 const Ordinal /*bytes*/, const char []/*sendBuffer*/, const int /*destRank*/
336 ) const
337{
339 true, std::logic_error
340 ,"SerialComm<Ordinal>::send(...): Error, you can not call ssend(...) when you"
341 " only have one process!"
342 );
343}
344
345template<typename Ordinal>
346void
347SerialComm<Ordinal>::ssend (const Ordinal /* bytes */,
348 const char* /* sendBuffer */,
349 const int /* destRank */,
350 const int /* tag */) const
351{
353 true, std::logic_error
354 ,"SerialComm<Ordinal>::send(...): Error, you can not call ssend(...) when you"
355 " only have one process!"
356 );
357}
358
359template<typename Ordinal>
361 const int /*sourceRank*/, const Ordinal /*bytes*/, char []/*recvBuffer*/
362 ) const
363{
365 true, std::logic_error
366 ,"SerialComm<Ordinal>::receive(...): Error, you can not call receive(...) when you"
367 " only have one process!"
368 );
369}
370
371
372template<typename Ordinal>
374 const ArrayView<const char> &/*sendBuffer*/,
375 const int /*destRank*/
376 ) const
377{
379 true, std::logic_error
380 ,"SerialComm<Ordinal>::readySend(...): Error, you can not call readySend(...) when you"
381 " only have one process!"
382 );
383}
384
385template<typename Ordinal>
386void
388 const char sendBuffer[],
389 const int destRank,
390 const int tag) const
391{
392 (void) bytes;
393 (void) sendBuffer;
394 (void) destRank;
395 (void) tag;
396
398 true, std::logic_error
399 ,"SerialComm<Ordinal>::readySend(...): Error, you can not call readySend(...) when you"
400 " only have one process!"
401 );
402}
403
404template<typename Ordinal>
406 const ArrayView<const char> &/*sendBuffer*/,
407 const int /*destRank*/
408 ) const
409{
410 TEUCHOS_TEST_FOR_EXCEPTION( true, std::logic_error, "SerialComm<Ordinal>::isend: You cannot call isend when you only have one process." );
411}
412
413
414template<typename Ordinal>
417isend (const ArrayView<const char> &/*sendBuffer*/,
418 const int /*destRank*/,
419 const int /*tag*/) const
420{
421 TEUCHOS_TEST_FOR_EXCEPTION( true, std::logic_error, "SerialComm<Ordinal>::isend: You cannot call isend when you only have one process." );
422}
423
424
425template<typename Ordinal>
427 const ArrayView<char> &/*Buffer*/,
428 const int /*sourceRank*/
429 ) const
430{
431 TEUCHOS_TEST_FOR_EXCEPTION( true, std::logic_error, "SerialComm<Ordinal>::ireceive: You cannot call isend when you only have one process." );
432}
433
434
435template<typename Ordinal>
438ireceive (const ArrayView<char> &/*Buffer*/,
439 const int /*sourceRank*/,
440 const int /*tag*/) const
441{
442 TEUCHOS_TEST_FOR_EXCEPTION( true, std::logic_error, "SerialComm<Ordinal>::ireceive: You cannot call isend when you only have one process." );
443}
444
445
446template<typename Ordinal>
448{
449 (void) requests;
450 // There's nothing to wait on!
451}
452
453
454template<typename Ordinal>
455void
457waitAll (const ArrayView<RCP<CommRequest<Ordinal> > >& requests,
458 const ArrayView<RCP<CommStatus<Ordinal> > >& statuses) const
459{
460 TEUCHOS_TEST_FOR_EXCEPTION(statuses.size() < requests.size(),
461 std::invalid_argument, "Teuchos::SerialComm::waitAll: There are not enough "
462 "entries in the statuses array to hold all the results of the communication"
463 " requests. requests.size() = " << requests.size() << " > statuses.size() "
464 "= " << statuses.size() << ".");
465
466 for (typename ArrayView<RCP<CommRequest<Ordinal> > >::iterator it = requests.begin();
467 it != requests.end(); ++it) {
468 *it = null; // A postcondition of the Teuchos::Comm interface.
469 }
470}
471
472template<typename Ordinal>
475{
476 (void) request;
477 TEUCHOS_TEST_FOR_EXCEPTION(request.getRawPtr() == NULL, std::invalid_argument,
478 "Teuchos::SerialComm::wait: On input, the request pointer is null.");
479
480 if (is_null (*request)) {
481 return null; // Nothing to wait on...
482 }
483 *request = null;
484 return rcp (new SerialCommStatus<Ordinal>);
485}
486
487template< typename Ordinal>
490{
491 return rcp(new SerialComm<Ordinal>(*this));
492}
493
494template<typename Ordinal>
496SerialComm<Ordinal>::split(const int color, const int /*key*/) const
497{
498 if (color < 0) {
499 return RCP< Comm<Ordinal> >();
500 }
501 // Simply return a copy of this communicator.
502 return rcp(new SerialComm<Ordinal>(*this));
503}
504
505template<typename Ordinal>
508{
509 if ((ranks.size()) == 1 && (ranks[0] == 0)) {
510 return rcp(new SerialComm<Ordinal>(*this));
511 } else {
512 return RCP< Comm<Ordinal> >();
513 }
514}
515
516// Overridden from Describable
517
518
519template<typename Ordinal>
521{
522 std::ostringstream oss;
523 oss << "Teuchos::SerialComm<"<<OrdinalTraits<Ordinal>::name()<<">";
524 return oss.str();
525}
526
527
528} // namespace Teuchos
529
530
531#endif // TEUCHOS_SERIAL_COMM_HPP
Defines basic traits for the ordinal field type.
Nonowning array view.
size_type size() const
The total number of items in the managed array.
Abstract interface for distributed-memory communication.
Encapsulation of a pending nonblocking communication operation.
Encapsulation of the result of a receive (blocking or nonblocking).
Simple wrapper class for raw pointers to single objects where no persisting relationship exists.
Smart reference counting pointer class for automatic garbage collection.
Concrete serial communicator subclass.
virtual RCP< CommRequest< Ordinal > > ireceive(const ArrayView< char > &Buffer, const int sourceRank) const
virtual RCP< CommRequest< Ordinal > > isend(const ArrayView< const char > &sendBuffer, const int destRank) const
virtual void broadcast(const int rootRank, const Ordinal bytes, char buffer[]) const
virtual RCP< Comm< Ordinal > > createSubcommunicator(const ArrayView< const int > &ranks) const
virtual void reduceAll(const ValueTypeReductionOp< Ordinal, char > &reductOp, const Ordinal bytes, const char sendBuffer[], char globalReducts[]) const
int incrementTag()
Increments the tag and then returns it.
virtual void gather(const Ordinal sendBytes, const char sendBuffer[], const Ordinal recvBytes, char recvBuffer[], const int root) const
Gather values from all processes to the root process.
virtual int receive(const int sourceRank, const Ordinal bytes, char recvBuffer[]) const
int getTag() const
The current tag.
virtual void readySend(const ArrayView< const char > &sendBuffer, const int destRank) const
RCP< SerialComm< Ordinal > > createSerialComm()
Nonmember constructor.
virtual RCP< Comm< Ordinal > > duplicate() const
virtual void ssend(const Ordinal bytes, const char sendBuffer[], const int destRank, const int tag) const
virtual void scan(const ValueTypeReductionOp< Ordinal, char > &reductOp, const Ordinal bytes, const char sendBuffer[], char scanReducts[]) const
virtual void send(const Ordinal bytes, const char sendBuffer[], const int destRank) const
virtual RCP< CommStatus< Ordinal > > wait(const Ptr< RCP< CommRequest< Ordinal > > > &request) const
virtual void waitAll(const ArrayView< RCP< CommRequest< Ordinal > > > &requests) const
virtual RCP< Comm< Ordinal > > split(const int color, const int key) const
virtual void gatherAll(const Ordinal sendBytes, const char sendBuffer[], const Ordinal recvBytes, char recvBuffer[]) const
virtual void ssend(const Ordinal bytes, const char sendBuffer[], const int destRank) const
Implementation of CommStatus for a serial communicator.
OrdinalType getSourceRank()
The source rank that sent the message (must be zero).
OrdinalType getTag()
The tag of the received message.
Base interface class for user-defined reduction operations for objects that use value semantics.
#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.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
bool is_null(const std::shared_ptr< T > &p)
Returns true if p.get()==NULL.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
static std::string name()
Returns name of this ordinal type.