Teuchos - Trilinos Tools Package  Version of the Day
Teuchos_DefaultSerialComm.hpp
1 // @HEADER
2 // ***********************************************************************
3 //
4 // Teuchos: Common Tools Package
5 // Copyright (2004) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
38 //
39 // ***********************************************************************
40 // @HEADER
41 
42 #ifndef TEUCHOS_SERIAL_COMM_HPP
43 #define TEUCHOS_SERIAL_COMM_HPP
44 
45 #include "Teuchos_Comm.hpp"
47 
48 
49 namespace Teuchos {
50 
57 template<class OrdinalType>
58 class SerialCommStatus : public CommStatus<OrdinalType> {
59 public:
62 
64  OrdinalType getSourceRank () { return 0; }
65 
67  OrdinalType getTag () { return 0; }
68 };
69 
70 
75 template<typename Ordinal>
76 class SerialComm : public Comm<Ordinal> {
77 public:
83  int getTag () const { return 0; }
84 
86 
87 
89  SerialComm();
90 
92  SerialComm(const SerialComm<Ordinal>& other);
93 
95 
97 
98 
100  virtual int getRank() const;
102  virtual int getSize() const;
104  virtual void barrier() const;
106  virtual void broadcast(
107  const int rootRank, const Ordinal bytes, char buffer[]
108  ) const;
110  virtual void
111  gather (const Ordinal sendBytes, const char sendBuffer[],
112  const Ordinal recvBytes, char recvBuffer[],
113  const int root) const;
115  virtual void gatherAll(
116  const Ordinal sendBytes, const char sendBuffer[]
117  ,const Ordinal recvBytes, char recvBuffer[]
118  ) const;
120  virtual void reduceAll(
121  const ValueTypeReductionOp<Ordinal,char> &reductOp
122  ,const Ordinal bytes, const char sendBuffer[], char globalReducts[]
123  ) const;
125  TEUCHOS_DEPRECATED virtual void reduceAllAndScatter(
126  const ValueTypeReductionOp<Ordinal,char> &reductOp
127  ,const Ordinal sendBytes, const char sendBuffer[]
128  ,const Ordinal recvCounts[], char myGlobalReducts[]
129  ) const;
131  virtual void scan(
132  const ValueTypeReductionOp<Ordinal,char> &reductOp
133  ,const Ordinal bytes, const char sendBuffer[], char scanReducts[]
134  ) const;
136  virtual void send(
137  const Ordinal bytes, const char sendBuffer[], const int destRank
138  ) const;
140  virtual void
141  send (const Ordinal bytes,
142  const char sendBuffer[],
143  const int destRank,
144  const int tag) const;
146  virtual void ssend(
147  const Ordinal bytes, const char sendBuffer[], const int destRank
148  ) const;
150  virtual void
151  ssend (const Ordinal bytes,
152  const char sendBuffer[],
153  const int destRank,
154  const int tag) const;
156  virtual int receive(
157  const int sourceRank, const Ordinal bytes, char recvBuffer[]
158  ) const;
160  virtual void readySend(
161  const ArrayView<const char> &sendBuffer,
162  const int destRank
163  ) const;
165  virtual void
166  readySend (const Ordinal bytes,
167  const char sendBuffer[],
168  const int destRank,
169  const int tag) const;
171  virtual RCP<CommRequest<Ordinal> > isend(
172  const ArrayView<const char> &sendBuffer,
173  const int destRank
174  ) const;
176  virtual RCP<CommRequest<Ordinal> >
177  isend (const ArrayView<const char> &sendBuffer,
178  const int destRank,
179  const int tag) const;
182  const ArrayView<char> &Buffer,
183  const int sourceRank
184  ) const;
186  virtual RCP<CommRequest<Ordinal> >
187  ireceive (const ArrayView<char> &Buffer,
188  const int sourceRank,
189  const int tag) const;
191  virtual void waitAll(
192  const ArrayView<RCP<CommRequest<Ordinal> > > &requests
193  ) const;
195  virtual void
196  waitAll (const ArrayView<RCP<CommRequest<Ordinal> > >& requests,
197  const ArrayView<RCP<CommStatus<Ordinal> > >& statuses) const;
199  virtual RCP<CommStatus<Ordinal> >
200  wait (const Ptr<RCP<CommRequest<Ordinal> > >& request) const;
202  virtual RCP< Comm<Ordinal> > duplicate() const;
204  virtual RCP< Comm<Ordinal> > split(const int color, const int key) const;
206  virtual RCP< Comm<Ordinal> > createSubcommunicator(
207  const ArrayView<const int> & ranks) const;
208 
210 
212 
213 
215  std::string description() const;
216 
218 
219 };
220 
221 
226 template<typename Ordinal>
228 {
229  return Teuchos::rcp(new SerialComm<Ordinal>);
230 }
231 
232 
233 // ////////////////////////
234 // Implementations
235 
236 
237 // Constructors
238 
239 
240 template<typename Ordinal>
242 {}
243 
244 template<typename Ordinal>
246 {}
247 
248 
249 // Overridden from Comm
250 
251 
252 template<typename Ordinal>
254 {
255  return 0;
256 }
257 
258 
259 template<typename Ordinal>
261 {
262  return 1;
263 }
264 
265 
266 template<typename Ordinal>
268 {
269  // Nothing to do
270 }
271 
272 
273 template<typename Ordinal>
275  const int /*rootRank*/, const Ordinal /*bytes*/, char []/*buffer*/
276  ) const
277 {
278  // Nothing to do
279 }
280 
281 
282 template<typename Ordinal>
284  const Ordinal sendBytes, const char sendBuffer[]
285  ,const Ordinal recvBytes, char recvBuffer[]
286  ) const
287 {
288  (void)sendBytes; // to remove "unused parameter" warning
289  (void)recvBytes;
290  (void)sendBuffer;
291  (void)recvBuffer;
292 #ifdef TEUCHOS_DEBUG
293  TEUCHOS_TEST_FOR_EXCEPT(!(sendBytes==recvBytes));
294 #endif
295  std::copy(sendBuffer,sendBuffer+sendBytes,recvBuffer);
296 }
297 
298 
299 template<typename Ordinal>
300 void
301 SerialComm<Ordinal>::gather (const Ordinal sendBytes,
302  const char sendBuffer[],
303  const Ordinal recvBytes,
304  char recvBuffer[],
305  const int root) const
306 {
307  (void) sendBytes; // to remove "unused parameter" warning
308  (void) recvBytes;
309  (void) sendBuffer;
310  (void) recvBuffer;
311  (void) root;
312 #ifdef TEUCHOS_DEBUG
313  TEUCHOS_TEST_FOR_EXCEPT(!(sendBytes==recvBytes));
314 #endif
315  std::copy (sendBuffer, sendBuffer + sendBytes, recvBuffer);
316 }
317 
318 
319 template<typename Ordinal>
321  const ValueTypeReductionOp<Ordinal,char> &reductOp
322  ,const Ordinal bytes, const char sendBuffer[], char globalReducts[]
323  ) const
324 {
325  (void)reductOp;
326  std::copy(sendBuffer,sendBuffer+bytes,globalReducts);
327 }
328 
329 
330 template<typename Ordinal>
331 TEUCHOS_DEPRECATED
333  const ValueTypeReductionOp<Ordinal,char> &reductOp
334  ,const Ordinal sendBytes, const char sendBuffer[]
335  ,const Ordinal recvCounts[], char myGlobalReducts[]
336  ) const
337 {
338  // Ignore unused arguments
339  (void)reductOp;
340  (void)sendBytes;
341  (void)sendBuffer;
342  (void)recvCounts;
343  (void)myGlobalReducts;
344 
345 #ifdef TEUCHOS_DEBUG
346  TEUCHOS_TEST_FOR_EXCEPT( recvCounts==NULL || recvCounts[0] != sendBytes );
347 #endif
348  std::copy(sendBuffer,sendBuffer+sendBytes,myGlobalReducts);
349 }
350 
351 
352 template<typename Ordinal>
354  const ValueTypeReductionOp<Ordinal,char> &reductOp
355  ,const Ordinal bytes, const char sendBuffer[], char scanReducts[]
356  ) const
357 {
358  (void)reductOp;
359  std::copy(sendBuffer,sendBuffer+bytes,scanReducts);
360 }
361 
362 
363 template<typename Ordinal>
365  const Ordinal /*bytes*/, const char []/*sendBuffer*/, const int /*destRank*/
366  ) const
367 {
369  true, std::logic_error
370  ,"SerialComm<Ordinal>::send(...): Error, you can not call send(...) when you"
371  " only have one process!"
372  );
373 }
374 
375 template<typename Ordinal>
377 send (const Ordinal /*bytes*/,
378  const char []/*sendBuffer*/,
379  const int /*destRank*/,
380  const int /*tag*/) const
381 {
383  true, std::logic_error
384  ,"SerialComm<Ordinal>::send(...): Error, you can not call send(...) when you"
385  " only have one process!"
386  );
387 }
388 
389 template<typename Ordinal>
391  const Ordinal /*bytes*/, const char []/*sendBuffer*/, const int /*destRank*/
392  ) const
393 {
395  true, std::logic_error
396  ,"SerialComm<Ordinal>::send(...): Error, you can not call ssend(...) when you"
397  " only have one process!"
398  );
399 }
400 
401 template<typename Ordinal>
402 void
403 SerialComm<Ordinal>::ssend (const Ordinal bytes,
404  const char sendBuffer[],
405  const int destRank,
406  const int tag) const
407 {
409  true, std::logic_error
410  ,"SerialComm<Ordinal>::send(...): Error, you can not call ssend(...) when you"
411  " only have one process!"
412  );
413 }
414 
415 template<typename Ordinal>
417  const int /*sourceRank*/, const Ordinal /*bytes*/, char []/*recvBuffer*/
418  ) const
419 {
421  true, std::logic_error
422  ,"SerialComm<Ordinal>::receive(...): Error, you can not call receive(...) when you"
423  " only have one process!"
424  );
425 }
426 
427 
428 template<typename Ordinal>
430  const ArrayView<const char> &/*sendBuffer*/,
431  const int /*destRank*/
432  ) const
433 {
435  true, std::logic_error
436  ,"SerialComm<Ordinal>::readySend(...): Error, you can not call readySend(...) when you"
437  " only have one process!"
438  );
439 }
440 
441 template<typename Ordinal>
442 void
443 SerialComm<Ordinal>::readySend (const Ordinal bytes,
444  const char sendBuffer[],
445  const int destRank,
446  const int tag) const
447 {
448  (void) bytes;
449  (void) sendBuffer;
450  (void) destRank;
451  (void) tag;
452 
454  true, std::logic_error
455  ,"SerialComm<Ordinal>::readySend(...): Error, you can not call readySend(...) when you"
456  " only have one process!"
457  );
458 }
459 
460 template<typename Ordinal>
462  const ArrayView<const char> &/*sendBuffer*/,
463  const int /*destRank*/
464  ) const
465 {
466  TEUCHOS_TEST_FOR_EXCEPTION( true, std::logic_error, "SerialComm<Ordinal>::isend: You cannot call isend when you only have one process." );
467 }
468 
469 
470 template<typename Ordinal>
473 isend (const ArrayView<const char> &/*sendBuffer*/,
474  const int /*destRank*/,
475  const int /*tag*/) const
476 {
477  TEUCHOS_TEST_FOR_EXCEPTION( true, std::logic_error, "SerialComm<Ordinal>::isend: You cannot call isend when you only have one process." );
478 }
479 
480 
481 template<typename Ordinal>
483  const ArrayView<char> &/*Buffer*/,
484  const int /*sourceRank*/
485  ) const
486 {
487  TEUCHOS_TEST_FOR_EXCEPTION( true, std::logic_error, "SerialComm<Ordinal>::ireceive: You cannot call isend when you only have one process." );
488 }
489 
490 
491 template<typename Ordinal>
494 ireceive (const ArrayView<char> &/*Buffer*/,
495  const int /*sourceRank*/,
496  const int /*tag*/) const
497 {
498  TEUCHOS_TEST_FOR_EXCEPTION( true, std::logic_error, "SerialComm<Ordinal>::ireceive: You cannot call isend when you only have one process." );
499 }
500 
501 
502 template<typename Ordinal>
504 {
505  (void) requests;
506  // There's nothing to wait on!
507 }
508 
509 
510 template<typename Ordinal>
511 void
514  const ArrayView<RCP<CommStatus<Ordinal> > >& statuses) const
515 {
516  TEUCHOS_TEST_FOR_EXCEPTION(statuses.size() < requests.size(),
517  std::invalid_argument, "Teuchos::SerialComm::waitAll: There are not enough "
518  "entries in the statuses array to hold all the results of the communication"
519  " requests. requests.size() = " << requests.size() << " > statuses.size() "
520  "= " << statuses.size() << ".");
521 
522  for (typename ArrayView<RCP<CommRequest<Ordinal> > >::iterator it = requests.begin();
523  it != requests.end(); ++it) {
524  *it = null; // A postcondition of the Teuchos::Comm interface.
525  }
526 }
527 
528 template<typename Ordinal>
531 {
532  (void) request;
533  TEUCHOS_TEST_FOR_EXCEPTION(request.getRawPtr() == NULL, std::invalid_argument,
534  "Teuchos::SerialComm::wait: On input, the request pointer is null.");
535 
536  if (is_null (*request)) {
537  return null; // Nothing to wait on...
538  }
539  *request = null;
540  return rcp (new SerialCommStatus<Ordinal>);
541 }
542 
543 template< typename Ordinal>
546 {
547  return rcp(new SerialComm<Ordinal>(*this));
548 }
549 
550 template<typename Ordinal>
552 SerialComm<Ordinal>::split(const int color, const int /*key*/) const
553 {
554  if (color < 0) {
555  return RCP< Comm<Ordinal> >();
556  }
557  // Simply return a copy of this communicator.
558  return rcp(new SerialComm<Ordinal>(*this));
559 }
560 
561 template<typename Ordinal>
564 {
565  if ((ranks.size()) == 1 && (ranks[0] == 0)) {
566  return rcp(new SerialComm<Ordinal>(*this));
567  } else {
568  return RCP< Comm<Ordinal> >();
569  }
570 }
571 
572 // Overridden from Describable
573 
574 
575 template<typename Ordinal>
577 {
578  std::ostringstream oss;
579  oss << "Teuchos::SerialComm<"<<OrdinalTraits<Ordinal>::name()<<">";
580  return oss.str();
581 }
582 
583 
584 } // namespace Teuchos
585 
586 
587 #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
TEUCHOS_DEPRECATED void scan(const Comm< Ordinal > &comm, const EReductionType reductType, const Packet &send, Packet *scanReduct)
Deprecated.
TEUCHOS_DEPRECATED void reduceAll(const Comm< Ordinal > &comm, const EReductionType reductType, const Packet &send, Packet *globalReduct)
Deprecated .
Encapsulation of the result of a receive (blocking or nonblocking).
void send(const Packet sendBuffer[], const Ordinal count, const int destRank, const int tag, const Comm< Ordinal > &comm)
Variant of send() that takes a tag (and restores the correct order of arguments). ...
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.
RCP< CommRequest< Ordinal > > ireceive(const ArrayRCP< Packet > &recvBuffer, const int sourceRank, const int tag, const Comm< Ordinal > &comm)
Variant of ireceive that takes a tag argument (and restores the correct order of arguments).
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.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos, as well as a number of utility routines.
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).
void ssend(const Packet sendBuffer[], const Ordinal count, const int destRank, const int tag, const Comm< Ordinal > &comm)
Variant of ssend() that takes a tag (and restores the correct order of arguments).
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 TEUCHOS_DEPRECATED void reduceAllAndScatter(const ValueTypeReductionOp< Ordinal, char > &reductOp, const Ordinal sendBytes, const char sendBuffer[], const Ordinal recvCounts[], char myGlobalReducts[]) const
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
void readySend(const Packet sendBuffer[], const Ordinal count, const int destRank, const int tag, const Comm< Ordinal > &comm)
Variant of readySend() that accepts a message tag.
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