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 
54 
55 
57  SerialComm();
58 
60  SerialComm(const SerialComm<Ordinal>& other);
61 
63 
65 
66 
68  virtual int getRank() const;
70  virtual int getSize() const;
72  virtual void barrier() const;
74  virtual void broadcast(
75  const int rootRank, const Ordinal bytes, char buffer[]
76  ) const;
78  virtual void
79  gather (const Ordinal sendBytes, const char sendBuffer[],
80  const Ordinal recvBytes, char recvBuffer[],
81  const int root) const;
83  virtual void gatherAll(
84  const Ordinal sendBytes, const char sendBuffer[]
85  ,const Ordinal recvBytes, char recvBuffer[]
86  ) const;
88  virtual void reduceAll(
90  ,const Ordinal bytes, const char sendBuffer[], char globalReducts[]
91  ) const;
93  virtual void scan(
95  ,const Ordinal bytes, const char sendBuffer[], char scanReducts[]
96  ) const;
98  virtual void send(
99  const Ordinal bytes, const char sendBuffer[], const int destRank
100  ) const;
102  virtual void
103  send (const Ordinal bytes,
104  const char sendBuffer[],
105  const int destRank,
106  const int tag) const;
108  virtual void ssend(
109  const Ordinal bytes, const char sendBuffer[], const int destRank
110  ) const;
112  virtual void
113  ssend (const Ordinal bytes,
114  const char sendBuffer[],
115  const int destRank,
116  const int tag) const;
118  virtual int receive(
119  const int sourceRank, const Ordinal bytes, char recvBuffer[]
120  ) const;
122  virtual void readySend(
123  const ArrayView<const char> &sendBuffer,
124  const int destRank
125  ) const;
127  virtual void
128  readySend (const Ordinal bytes,
129  const char sendBuffer[],
130  const int destRank,
131  const int tag) const;
134  const ArrayView<const char> &sendBuffer,
135  const int destRank
136  ) const;
138  virtual RCP<CommRequest<Ordinal> >
139  isend (const ArrayView<const char> &sendBuffer,
140  const int destRank,
141  const int tag) const;
144  const ArrayView<char> &Buffer,
145  const int sourceRank
146  ) const;
148  virtual RCP<CommRequest<Ordinal> >
149  ireceive (const ArrayView<char> &Buffer,
150  const int sourceRank,
151  const int tag) const;
153  virtual void waitAll(
154  const ArrayView<RCP<CommRequest<Ordinal> > > &requests
155  ) const;
157  virtual void
158  waitAll (const ArrayView<RCP<CommRequest<Ordinal> > >& requests,
159  const ArrayView<RCP<CommStatus<Ordinal> > >& statuses) const;
161  virtual RCP<CommStatus<Ordinal> >
162  wait (const Ptr<RCP<CommRequest<Ordinal> > >& request) const;
164  virtual RCP< Comm<Ordinal> > duplicate() const;
166  virtual RCP< Comm<Ordinal> > split(const int color, const int key) const;
169  const ArrayView<const int> & ranks) const;
170 
172 
174 
175 
177  std::string description() const;
178 
180 
181 };
182 
183 
188 template<typename Ordinal>
190 {
191  return Teuchos::rcp(new SerialComm<Ordinal>);
192 }
193 
194 
195 // ////////////////////////
196 // Implementations
197 
198 
199 // Constructors
200 
201 
202 template<typename Ordinal>
204 {}
205 
206 template<typename Ordinal>
208 {}
209 
210 
211 // Overridden from Comm
212 
213 
214 template<typename Ordinal>
216 {
217  return 0;
218 }
219 
220 
221 template<typename Ordinal>
223 {
224  return 1;
225 }
226 
227 
228 template<typename Ordinal>
230 {
231  // Nothing to do
232 }
233 
234 
235 template<typename Ordinal>
237  const int /*rootRank*/, const Ordinal /*bytes*/, char []/*buffer*/
238  ) const
239 {
240  // Nothing to do
241 }
242 
243 
244 template<typename Ordinal>
246  const Ordinal sendBytes, const char sendBuffer[]
247  ,const Ordinal recvBytes, char recvBuffer[]
248  ) const
249 {
250  (void)sendBytes; // to remove "unused parameter" warning
251  (void)recvBytes;
252  (void)sendBuffer;
253  (void)recvBuffer;
254 #ifdef TEUCHOS_DEBUG
255  TEUCHOS_TEST_FOR_EXCEPT(!(sendBytes==recvBytes));
256 #endif
257  std::copy(sendBuffer,sendBuffer+sendBytes,recvBuffer);
258 }
259 
260 
261 template<typename Ordinal>
262 void
263 SerialComm<Ordinal>::gather (const Ordinal sendBytes,
264  const char sendBuffer[],
265  const Ordinal recvBytes,
266  char recvBuffer[],
267  const int root) const
268 {
269  (void) sendBytes; // to remove "unused parameter" warning
270  (void) recvBytes;
271  (void) sendBuffer;
272  (void) recvBuffer;
273  (void) root;
274 #ifdef TEUCHOS_DEBUG
275  TEUCHOS_TEST_FOR_EXCEPT(!(sendBytes==recvBytes));
276 #endif
277  std::copy (sendBuffer, sendBuffer + sendBytes, recvBuffer);
278 }
279 
280 
281 template<typename Ordinal>
283  const ValueTypeReductionOp<Ordinal,char> &reductOp
284  ,const Ordinal bytes, const char sendBuffer[], char globalReducts[]
285  ) const
286 {
287  (void)reductOp;
288  std::copy(sendBuffer,sendBuffer+bytes,globalReducts);
289 }
290 
291 
292 template<typename Ordinal>
294  const ValueTypeReductionOp<Ordinal,char> &reductOp
295  ,const Ordinal bytes, const char sendBuffer[], char scanReducts[]
296  ) const
297 {
298  (void)reductOp;
299  std::copy(sendBuffer,sendBuffer+bytes,scanReducts);
300 }
301 
302 
303 template<typename Ordinal>
305  const Ordinal /*bytes*/, const char []/*sendBuffer*/, const int /*destRank*/
306  ) const
307 {
309  true, std::logic_error
310  ,"SerialComm<Ordinal>::send(...): Error, you can not call send(...) when you"
311  " only have one process!"
312  );
313 }
314 
315 template<typename Ordinal>
317 send (const Ordinal /*bytes*/,
318  const char []/*sendBuffer*/,
319  const int /*destRank*/,
320  const int /*tag*/) const
321 {
323  true, std::logic_error
324  ,"SerialComm<Ordinal>::send(...): Error, you can not call send(...) when you"
325  " only have one process!"
326  );
327 }
328 
329 template<typename Ordinal>
331  const Ordinal /*bytes*/, const char []/*sendBuffer*/, const int /*destRank*/
332  ) const
333 {
335  true, std::logic_error
336  ,"SerialComm<Ordinal>::send(...): Error, you can not call ssend(...) when you"
337  " only have one process!"
338  );
339 }
340 
341 template<typename Ordinal>
342 void
343 SerialComm<Ordinal>::ssend (const Ordinal /* bytes */,
344  const char* /* sendBuffer */,
345  const int /* destRank */,
346  const int /* tag */) const
347 {
349  true, std::logic_error
350  ,"SerialComm<Ordinal>::send(...): Error, you can not call ssend(...) when you"
351  " only have one process!"
352  );
353 }
354 
355 template<typename Ordinal>
357  const int /*sourceRank*/, const Ordinal /*bytes*/, char []/*recvBuffer*/
358  ) const
359 {
361  true, std::logic_error
362  ,"SerialComm<Ordinal>::receive(...): Error, you can not call receive(...) when you"
363  " only have one process!"
364  );
365 }
366 
367 
368 template<typename Ordinal>
370  const ArrayView<const char> &/*sendBuffer*/,
371  const int /*destRank*/
372  ) const
373 {
375  true, std::logic_error
376  ,"SerialComm<Ordinal>::readySend(...): Error, you can not call readySend(...) when you"
377  " only have one process!"
378  );
379 }
380 
381 template<typename Ordinal>
382 void
383 SerialComm<Ordinal>::readySend (const Ordinal bytes,
384  const char sendBuffer[],
385  const int destRank,
386  const int tag) const
387 {
388  (void) bytes;
389  (void) sendBuffer;
390  (void) destRank;
391  (void) tag;
392 
394  true, std::logic_error
395  ,"SerialComm<Ordinal>::readySend(...): Error, you can not call readySend(...) when you"
396  " only have one process!"
397  );
398 }
399 
400 template<typename Ordinal>
402  const ArrayView<const char> &/*sendBuffer*/,
403  const int /*destRank*/
404  ) const
405 {
406  TEUCHOS_TEST_FOR_EXCEPTION( true, std::logic_error, "SerialComm<Ordinal>::isend: You cannot call isend when you only have one process." );
407 }
408 
409 
410 template<typename Ordinal>
413 isend (const ArrayView<const char> &/*sendBuffer*/,
414  const int /*destRank*/,
415  const int /*tag*/) const
416 {
417  TEUCHOS_TEST_FOR_EXCEPTION( true, std::logic_error, "SerialComm<Ordinal>::isend: You cannot call isend when you only have one process." );
418 }
419 
420 
421 template<typename Ordinal>
423  const ArrayView<char> &/*Buffer*/,
424  const int /*sourceRank*/
425  ) const
426 {
427  TEUCHOS_TEST_FOR_EXCEPTION( true, std::logic_error, "SerialComm<Ordinal>::ireceive: You cannot call isend when you only have one process." );
428 }
429 
430 
431 template<typename Ordinal>
434 ireceive (const ArrayView<char> &/*Buffer*/,
435  const int /*sourceRank*/,
436  const int /*tag*/) const
437 {
438  TEUCHOS_TEST_FOR_EXCEPTION( true, std::logic_error, "SerialComm<Ordinal>::ireceive: You cannot call isend when you only have one process." );
439 }
440 
441 
442 template<typename Ordinal>
444 {
445  (void) requests;
446  // There's nothing to wait on!
447 }
448 
449 
450 template<typename Ordinal>
451 void
454  const ArrayView<RCP<CommStatus<Ordinal> > >& statuses) const
455 {
456  TEUCHOS_TEST_FOR_EXCEPTION(statuses.size() < requests.size(),
457  std::invalid_argument, "Teuchos::SerialComm::waitAll: There are not enough "
458  "entries in the statuses array to hold all the results of the communication"
459  " requests. requests.size() = " << requests.size() << " > statuses.size() "
460  "= " << statuses.size() << ".");
461 
462  for (typename ArrayView<RCP<CommRequest<Ordinal> > >::iterator it = requests.begin();
463  it != requests.end(); ++it) {
464  *it = null; // A postcondition of the Teuchos::Comm interface.
465  }
466 }
467 
468 template<typename Ordinal>
471 {
472  (void) request;
473  TEUCHOS_TEST_FOR_EXCEPTION(request.getRawPtr() == NULL, std::invalid_argument,
474  "Teuchos::SerialComm::wait: On input, the request pointer is null.");
475 
476  if (is_null (*request)) {
477  return null; // Nothing to wait on...
478  }
479  *request = null;
480  return rcp (new SerialCommStatus<Ordinal>);
481 }
482 
483 template< typename Ordinal>
486 {
487  return rcp(new SerialComm<Ordinal>(*this));
488 }
489 
490 template<typename Ordinal>
492 SerialComm<Ordinal>::split(const int color, const int /*key*/) const
493 {
494  if (color < 0) {
495  return RCP< Comm<Ordinal> >();
496  }
497  // Simply return a copy of this communicator.
498  return rcp(new SerialComm<Ordinal>(*this));
499 }
500 
501 template<typename Ordinal>
504 {
505  if ((ranks.size()) == 1 && (ranks[0] == 0)) {
506  return rcp(new SerialComm<Ordinal>(*this));
507  } else {
508  return RCP< Comm<Ordinal> >();
509  }
510 }
511 
512 // Overridden from Describable
513 
514 
515 template<typename Ordinal>
517 {
518  std::ostringstream oss;
519  oss << "Teuchos::SerialComm<"<<OrdinalTraits<Ordinal>::name()<<">";
520  return oss.str();
521 }
522 
523 
524 } // namespace Teuchos
525 
526 
527 #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
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