Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_DefaultSerialComm.hpp
Go to the documentation of this file.
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 
17 namespace Teuchos {
18 
25 template<class OrdinalType>
26 class SerialCommStatus : public CommStatus<OrdinalType> {
27 public:
30 
32  OrdinalType getSourceRank () { return 0; }
33 
35  OrdinalType getTag () { return 0; }
36 };
37 
38 
43 template<typename Ordinal>
44 class SerialComm : public Comm<Ordinal> {
45 public:
51  int getTag () const { return 0; }
52 
53  int incrementTag() {
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;
142  virtual RCP<CommRequest<Ordinal> >
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;
152  virtual RCP<CommRequest<Ordinal> >
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;
165  virtual RCP<CommStatus<Ordinal> >
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 
192 template<typename Ordinal>
194 {
195  return Teuchos::rcp(new SerialComm<Ordinal>);
196 }
197 
198 
199 // ////////////////////////
200 // Implementations
201 
202 
203 // Constructors
204 
205 
206 template<typename Ordinal>
208 {}
209 
210 template<typename Ordinal>
212 {}
213 
214 
215 // Overridden from Comm
216 
217 
218 template<typename Ordinal>
220 {
221  return 0;
222 }
223 
224 
225 template<typename Ordinal>
227 {
228  return 1;
229 }
230 
231 
232 template<typename Ordinal>
234 {
235  // Nothing to do
236 }
237 
238 
239 template<typename Ordinal>
241  const int /*rootRank*/, const Ordinal /*bytes*/, char []/*buffer*/
242  ) const
243 {
244  // Nothing to do
245 }
246 
247 
248 template<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 
265 template<typename Ordinal>
266 void
267 SerialComm<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 
285 template<typename Ordinal>
287  const ValueTypeReductionOp<Ordinal,char> &reductOp
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 
296 template<typename Ordinal>
298  const ValueTypeReductionOp<Ordinal,char> &reductOp
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 
307 template<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 
319 template<typename Ordinal>
321 send (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 
333 template<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 
345 template<typename Ordinal>
346 void
347 SerialComm<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 
359 template<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 
372 template<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 
385 template<typename Ordinal>
386 void
387 SerialComm<Ordinal>::readySend (const Ordinal bytes,
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 
404 template<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 
414 template<typename Ordinal>
417 isend (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 
425 template<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 
435 template<typename Ordinal>
438 ireceive (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 
446 template<typename Ordinal>
448 {
449  (void) requests;
450  // There's nothing to wait on!
451 }
452 
453 
454 template<typename Ordinal>
455 void
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 
472 template<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 
487 template< typename Ordinal>
490 {
491  return rcp(new SerialComm<Ordinal>(*this));
492 }
493 
494 template<typename Ordinal>
496 SerialComm<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 
505 template<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 
519 template<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
virtual RCP< CommStatus< Ordinal > > wait(const Ptr< RCP< CommRequest< Ordinal > > > &request) const
virtual void barrier() const
int getTag() const
The current tag.
virtual RCP< CommRequest< Ordinal > > isend(const ArrayView< const char > &sendBuffer, const int destRank) const
virtual RCP< CommRequest< Ordinal > > ireceive(const ArrayView< char > &Buffer, const int sourceRank) const
virtual int receive(const int sourceRank, const Ordinal bytes, char recvBuffer[]) const
bool is_null(const std::shared_ptr< T > &p)
Returns true if p.get()==NULL.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
size_type size() const
The total number of items in the managed array.
Concrete serial communicator subclass.
virtual void broadcast(const int rootRank, const Ordinal bytes, char buffer[]) const
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
virtual void waitAll(const ArrayView< RCP< CommRequest< Ordinal > > > &requests) const
Encapsulation of the result of a receive (blocking or nonblocking).
virtual void scan(const ValueTypeReductionOp< Ordinal, char > &reductOp, const Ordinal bytes, const char sendBuffer[], char scanReducts[]) const
std::string description() const
Implementation of CommStatus for a serial communicator.
OrdinalType getTag()
The tag of the received message.
virtual RCP< Comm< Ordinal > > createSubcommunicator(const ArrayView< const int > &ranks) const
Abstract interface for distributed-memory communication.
virtual void readySend(const ArrayView< const char > &sendBuffer, const int destRank) const
Nonowning array view.
Defines basic traits for the ordinal field type.
virtual RCP< Comm< Ordinal > > split(const int color, const int key) const
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.
OrdinalType getSourceRank()
The source rank that sent the message (must be zero).
static std::string name()
Returns name of this ordinal type.
virtual void reduceAll(const ValueTypeReductionOp< Ordinal, char > &reductOp, const Ordinal bytes, const char sendBuffer[], char globalReducts[]) const
Smart reference counting pointer class for automatic garbage collection.
virtual void gatherAll(const Ordinal sendBytes, const char sendBuffer[], const Ordinal recvBytes, char recvBuffer[]) const
Encapsulation of a pending nonblocking communication operation.
SerialCommStatus()
Default constructor.
virtual RCP< Comm< Ordinal > > duplicate() const
virtual void ssend(const Ordinal bytes, const char sendBuffer[], const int destRank) const
int incrementTag()
Increments the tag and then returns it.
RCP< SerialComm< Ordinal > > createSerialComm()
Nonmember constructor.
#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...
Simple wrapper class for raw pointers to single objects where no persisting relationship exists...
virtual void send(const Ordinal bytes, const char sendBuffer[], const int destRank) const