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 //
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  virtual void scan(
126  const ValueTypeReductionOp<Ordinal,char> &reductOp
127  ,const Ordinal bytes, const char sendBuffer[], char scanReducts[]
128  ) const;
130  virtual void send(
131  const Ordinal bytes, const char sendBuffer[], const int destRank
132  ) const;
134  virtual void
135  send (const Ordinal bytes,
136  const char sendBuffer[],
137  const int destRank,
138  const int tag) const;
140  virtual void ssend(
141  const Ordinal bytes, const char sendBuffer[], const int destRank
142  ) const;
144  virtual void
145  ssend (const Ordinal bytes,
146  const char sendBuffer[],
147  const int destRank,
148  const int tag) const;
150  virtual int receive(
151  const int sourceRank, const Ordinal bytes, char recvBuffer[]
152  ) const;
154  virtual void readySend(
155  const ArrayView<const char> &sendBuffer,
156  const int destRank
157  ) const;
159  virtual void
160  readySend (const Ordinal bytes,
161  const char sendBuffer[],
162  const int destRank,
163  const int tag) const;
166  const ArrayView<const char> &sendBuffer,
167  const int destRank
168  ) const;
170  virtual RCP<CommRequest<Ordinal> >
171  isend (const ArrayView<const char> &sendBuffer,
172  const int destRank,
173  const int tag) const;
176  const ArrayView<char> &Buffer,
177  const int sourceRank
178  ) const;
180  virtual RCP<CommRequest<Ordinal> >
181  ireceive (const ArrayView<char> &Buffer,
182  const int sourceRank,
183  const int tag) const;
185  virtual void waitAll(
186  const ArrayView<RCP<CommRequest<Ordinal> > > &requests
187  ) const;
189  virtual void
190  waitAll (const ArrayView<RCP<CommRequest<Ordinal> > >& requests,
191  const ArrayView<RCP<CommStatus<Ordinal> > >& statuses) const;
193  virtual RCP<CommStatus<Ordinal> >
194  wait (const Ptr<RCP<CommRequest<Ordinal> > >& request) const;
196  virtual RCP< Comm<Ordinal> > duplicate() const;
198  virtual RCP< Comm<Ordinal> > split(const int color, const int key) const;
201  const ArrayView<const int> & ranks) const;
202 
204 
206 
207 
209  std::string description() const;
210 
212 
213 };
214 
215 
220 template<typename Ordinal>
222 {
223  return Teuchos::rcp(new SerialComm<Ordinal>);
224 }
225 
226 
227 // ////////////////////////
228 // Implementations
229 
230 
231 // Constructors
232 
233 
234 template<typename Ordinal>
236 {}
237 
238 template<typename Ordinal>
240 {}
241 
242 
243 // Overridden from Comm
244 
245 
246 template<typename Ordinal>
248 {
249  return 0;
250 }
251 
252 
253 template<typename Ordinal>
255 {
256  return 1;
257 }
258 
259 
260 template<typename Ordinal>
262 {
263  // Nothing to do
264 }
265 
266 
267 template<typename Ordinal>
269  const int /*rootRank*/, const Ordinal /*bytes*/, char []/*buffer*/
270  ) const
271 {
272  // Nothing to do
273 }
274 
275 
276 template<typename Ordinal>
278  const Ordinal sendBytes, const char sendBuffer[]
279  ,const Ordinal recvBytes, char recvBuffer[]
280  ) const
281 {
282  (void)sendBytes; // to remove "unused parameter" warning
283  (void)recvBytes;
284  (void)sendBuffer;
285  (void)recvBuffer;
286 #ifdef TEUCHOS_DEBUG
287  TEUCHOS_TEST_FOR_EXCEPT(!(sendBytes==recvBytes));
288 #endif
289  std::copy(sendBuffer,sendBuffer+sendBytes,recvBuffer);
290 }
291 
292 
293 template<typename Ordinal>
294 void
295 SerialComm<Ordinal>::gather (const Ordinal sendBytes,
296  const char sendBuffer[],
297  const Ordinal recvBytes,
298  char recvBuffer[],
299  const int root) const
300 {
301  (void) sendBytes; // to remove "unused parameter" warning
302  (void) recvBytes;
303  (void) sendBuffer;
304  (void) recvBuffer;
305  (void) root;
306 #ifdef TEUCHOS_DEBUG
307  TEUCHOS_TEST_FOR_EXCEPT(!(sendBytes==recvBytes));
308 #endif
309  std::copy (sendBuffer, sendBuffer + sendBytes, recvBuffer);
310 }
311 
312 
313 template<typename Ordinal>
315  const ValueTypeReductionOp<Ordinal,char> &reductOp
316  ,const Ordinal bytes, const char sendBuffer[], char globalReducts[]
317  ) const
318 {
319  (void)reductOp;
320  std::copy(sendBuffer,sendBuffer+bytes,globalReducts);
321 }
322 
323 
324 template<typename Ordinal>
326  const ValueTypeReductionOp<Ordinal,char> &reductOp
327  ,const Ordinal bytes, const char sendBuffer[], char scanReducts[]
328  ) const
329 {
330  (void)reductOp;
331  std::copy(sendBuffer,sendBuffer+bytes,scanReducts);
332 }
333 
334 
335 template<typename Ordinal>
337  const Ordinal /*bytes*/, const char []/*sendBuffer*/, const int /*destRank*/
338  ) const
339 {
341  true, std::logic_error
342  ,"SerialComm<Ordinal>::send(...): Error, you can not call send(...) when you"
343  " only have one process!"
344  );
345 }
346 
347 template<typename Ordinal>
349 send (const Ordinal /*bytes*/,
350  const char []/*sendBuffer*/,
351  const int /*destRank*/,
352  const int /*tag*/) const
353 {
355  true, std::logic_error
356  ,"SerialComm<Ordinal>::send(...): Error, you can not call send(...) when you"
357  " only have one process!"
358  );
359 }
360 
361 template<typename Ordinal>
363  const Ordinal /*bytes*/, const char []/*sendBuffer*/, const int /*destRank*/
364  ) const
365 {
367  true, std::logic_error
368  ,"SerialComm<Ordinal>::send(...): Error, you can not call ssend(...) when you"
369  " only have one process!"
370  );
371 }
372 
373 template<typename Ordinal>
374 void
375 SerialComm<Ordinal>::ssend (const Ordinal /* bytes */,
376  const char* /* sendBuffer */,
377  const int /* destRank */,
378  const int /* tag */) const
379 {
381  true, std::logic_error
382  ,"SerialComm<Ordinal>::send(...): Error, you can not call ssend(...) when you"
383  " only have one process!"
384  );
385 }
386 
387 template<typename Ordinal>
389  const int /*sourceRank*/, const Ordinal /*bytes*/, char []/*recvBuffer*/
390  ) const
391 {
393  true, std::logic_error
394  ,"SerialComm<Ordinal>::receive(...): Error, you can not call receive(...) when you"
395  " only have one process!"
396  );
397 }
398 
399 
400 template<typename Ordinal>
402  const ArrayView<const char> &/*sendBuffer*/,
403  const int /*destRank*/
404  ) const
405 {
407  true, std::logic_error
408  ,"SerialComm<Ordinal>::readySend(...): Error, you can not call readySend(...) when you"
409  " only have one process!"
410  );
411 }
412 
413 template<typename Ordinal>
414 void
415 SerialComm<Ordinal>::readySend (const Ordinal bytes,
416  const char sendBuffer[],
417  const int destRank,
418  const int tag) const
419 {
420  (void) bytes;
421  (void) sendBuffer;
422  (void) destRank;
423  (void) tag;
424 
426  true, std::logic_error
427  ,"SerialComm<Ordinal>::readySend(...): Error, you can not call readySend(...) when you"
428  " only have one process!"
429  );
430 }
431 
432 template<typename Ordinal>
434  const ArrayView<const char> &/*sendBuffer*/,
435  const int /*destRank*/
436  ) const
437 {
438  TEUCHOS_TEST_FOR_EXCEPTION( true, std::logic_error, "SerialComm<Ordinal>::isend: You cannot call isend when you only have one process." );
439 }
440 
441 
442 template<typename Ordinal>
445 isend (const ArrayView<const char> &/*sendBuffer*/,
446  const int /*destRank*/,
447  const int /*tag*/) const
448 {
449  TEUCHOS_TEST_FOR_EXCEPTION( true, std::logic_error, "SerialComm<Ordinal>::isend: You cannot call isend when you only have one process." );
450 }
451 
452 
453 template<typename Ordinal>
455  const ArrayView<char> &/*Buffer*/,
456  const int /*sourceRank*/
457  ) const
458 {
459  TEUCHOS_TEST_FOR_EXCEPTION( true, std::logic_error, "SerialComm<Ordinal>::ireceive: You cannot call isend when you only have one process." );
460 }
461 
462 
463 template<typename Ordinal>
466 ireceive (const ArrayView<char> &/*Buffer*/,
467  const int /*sourceRank*/,
468  const int /*tag*/) const
469 {
470  TEUCHOS_TEST_FOR_EXCEPTION( true, std::logic_error, "SerialComm<Ordinal>::ireceive: You cannot call isend when you only have one process." );
471 }
472 
473 
474 template<typename Ordinal>
476 {
477  (void) requests;
478  // There's nothing to wait on!
479 }
480 
481 
482 template<typename Ordinal>
483 void
486  const ArrayView<RCP<CommStatus<Ordinal> > >& statuses) const
487 {
488  TEUCHOS_TEST_FOR_EXCEPTION(statuses.size() < requests.size(),
489  std::invalid_argument, "Teuchos::SerialComm::waitAll: There are not enough "
490  "entries in the statuses array to hold all the results of the communication"
491  " requests. requests.size() = " << requests.size() << " > statuses.size() "
492  "= " << statuses.size() << ".");
493 
494  for (typename ArrayView<RCP<CommRequest<Ordinal> > >::iterator it = requests.begin();
495  it != requests.end(); ++it) {
496  *it = null; // A postcondition of the Teuchos::Comm interface.
497  }
498 }
499 
500 template<typename Ordinal>
503 {
504  (void) request;
505  TEUCHOS_TEST_FOR_EXCEPTION(request.getRawPtr() == NULL, std::invalid_argument,
506  "Teuchos::SerialComm::wait: On input, the request pointer is null.");
507 
508  if (is_null (*request)) {
509  return null; // Nothing to wait on...
510  }
511  *request = null;
512  return rcp (new SerialCommStatus<Ordinal>);
513 }
514 
515 template< typename Ordinal>
518 {
519  return rcp(new SerialComm<Ordinal>(*this));
520 }
521 
522 template<typename Ordinal>
524 SerialComm<Ordinal>::split(const int color, const int /*key*/) const
525 {
526  if (color < 0) {
527  return RCP< Comm<Ordinal> >();
528  }
529  // Simply return a copy of this communicator.
530  return rcp(new SerialComm<Ordinal>(*this));
531 }
532 
533 template<typename Ordinal>
536 {
537  if ((ranks.size()) == 1 && (ranks[0] == 0)) {
538  return rcp(new SerialComm<Ordinal>(*this));
539  } else {
540  return RCP< Comm<Ordinal> >();
541  }
542 }
543 
544 // Overridden from Describable
545 
546 
547 template<typename Ordinal>
549 {
550  std::ostringstream oss;
551  oss << "Teuchos::SerialComm<"<<OrdinalTraits<Ordinal>::name()<<">";
552  return oss.str();
553 }
554 
555 
556 } // namespace Teuchos
557 
558 
559 #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