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  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>
332  const ValueTypeReductionOp<Ordinal,char> &reductOp
333  ,const Ordinal sendBytes, const char sendBuffer[]
334  ,const Ordinal recvCounts[], char myGlobalReducts[]
335  ) const
336 {
337  // Ignore unused arguments
338  (void)reductOp;
339  (void)sendBytes;
340  (void)sendBuffer;
341  (void)recvCounts;
342  (void)myGlobalReducts;
343 
344 #ifdef TEUCHOS_DEBUG
345  TEUCHOS_TEST_FOR_EXCEPT( recvCounts==NULL || recvCounts[0] != sendBytes );
346 #endif
347  std::copy(sendBuffer,sendBuffer+sendBytes,myGlobalReducts);
348 }
349 
350 
351 template<typename Ordinal>
353  const ValueTypeReductionOp<Ordinal,char> &reductOp
354  ,const Ordinal bytes, const char sendBuffer[], char scanReducts[]
355  ) const
356 {
357  (void)reductOp;
358  std::copy(sendBuffer,sendBuffer+bytes,scanReducts);
359 }
360 
361 
362 template<typename Ordinal>
364  const Ordinal /*bytes*/, const char []/*sendBuffer*/, const int /*destRank*/
365  ) const
366 {
368  true, std::logic_error
369  ,"SerialComm<Ordinal>::send(...): Error, you can not call send(...) when you"
370  " only have one process!"
371  );
372 }
373 
374 template<typename Ordinal>
376 send (const Ordinal /*bytes*/,
377  const char []/*sendBuffer*/,
378  const int /*destRank*/,
379  const int /*tag*/) const
380 {
382  true, std::logic_error
383  ,"SerialComm<Ordinal>::send(...): Error, you can not call send(...) when you"
384  " only have one process!"
385  );
386 }
387 
388 template<typename Ordinal>
390  const Ordinal /*bytes*/, const char []/*sendBuffer*/, const int /*destRank*/
391  ) const
392 {
394  true, std::logic_error
395  ,"SerialComm<Ordinal>::send(...): Error, you can not call ssend(...) when you"
396  " only have one process!"
397  );
398 }
399 
400 template<typename Ordinal>
401 void
402 SerialComm<Ordinal>::ssend (const Ordinal bytes,
403  const char sendBuffer[],
404  const int destRank,
405  const int tag) const
406 {
408  true, std::logic_error
409  ,"SerialComm<Ordinal>::send(...): Error, you can not call ssend(...) when you"
410  " only have one process!"
411  );
412 }
413 
414 template<typename Ordinal>
416  const int /*sourceRank*/, const Ordinal /*bytes*/, char []/*recvBuffer*/
417  ) const
418 {
420  true, std::logic_error
421  ,"SerialComm<Ordinal>::receive(...): Error, you can not call receive(...) when you"
422  " only have one process!"
423  );
424  // The next line will never be reached, but a return is required on some platforms
425  return 0;
426 }
427 
428 
429 template<typename Ordinal>
431  const ArrayView<const char> &/*sendBuffer*/,
432  const int /*destRank*/
433  ) const
434 {
436  true, std::logic_error
437  ,"SerialComm<Ordinal>::readySend(...): Error, you can not call readySend(...) when you"
438  " only have one process!"
439  );
440 }
441 
442 template<typename Ordinal>
443 void
444 SerialComm<Ordinal>::readySend (const Ordinal bytes,
445  const char sendBuffer[],
446  const int destRank,
447  const int tag) const
448 {
449  (void) bytes;
450  (void) sendBuffer;
451  (void) destRank;
452  (void) tag;
453 
455  true, std::logic_error
456  ,"SerialComm<Ordinal>::readySend(...): Error, you can not call readySend(...) when you"
457  " only have one process!"
458  );
459 }
460 
461 template<typename Ordinal>
463  const ArrayView<const char> &/*sendBuffer*/,
464  const int /*destRank*/
465  ) const
466 {
468  return null;
469 }
470 
471 
472 template<typename Ordinal>
475 isend (const ArrayView<const char> &/*sendBuffer*/,
476  const int /*destRank*/,
477  const int /*tag*/) const
478 {
480  return null;
481 }
482 
483 
484 template<typename Ordinal>
486  const ArrayView<char> &/*Buffer*/,
487  const int /*sourceRank*/
488  ) const
489 {
491  return null;
492 }
493 
494 
495 template<typename Ordinal>
498 ireceive (const ArrayView<char> &/*Buffer*/,
499  const int /*sourceRank*/,
500  const int /*tag*/) const
501 {
503  return null;
504 }
505 
506 
507 template<typename Ordinal>
509 {
510  (void) requests;
511  // There's nothing to wait on!
512 }
513 
514 
515 template<typename Ordinal>
516 void
519  const ArrayView<RCP<CommStatus<Ordinal> > >& statuses) const
520 {
521  TEUCHOS_TEST_FOR_EXCEPTION(statuses.size() < requests.size(),
522  std::invalid_argument, "Teuchos::SerialComm::waitAll: There are not enough "
523  "entries in the statuses array to hold all the results of the communication"
524  " requests. requests.size() = " << requests.size() << " > statuses.size() "
525  "= " << statuses.size() << ".");
526 
527  for (typename ArrayView<RCP<CommRequest<Ordinal> > >::iterator it = requests.begin();
528  it != requests.end(); ++it) {
529  *it = null; // A postcondition of the Teuchos::Comm interface.
530  }
531 }
532 
533 template<typename Ordinal>
536 {
537  (void) request;
538  TEUCHOS_TEST_FOR_EXCEPTION(request.getRawPtr() == NULL, std::invalid_argument,
539  "Teuchos::SerialComm::wait: On input, the request pointer is null.");
540 
541  if (is_null (*request)) {
542  return null; // Nothing to wait on...
543  }
544  *request = null;
545  return rcp (new SerialCommStatus<Ordinal>);
546 }
547 
548 template< typename Ordinal>
551 {
552  return rcp(new SerialComm<Ordinal>(*this));
553 }
554 
555 template<typename Ordinal>
557 SerialComm<Ordinal>::split(const int color, const int /*key*/) const
558 {
559  if (color < 0) {
560  return RCP< Comm<Ordinal> >();
561  }
562  // Simply return a copy of this communicator.
563  return rcp(new SerialComm<Ordinal>(*this));
564 }
565 
566 template<typename Ordinal>
569 {
570  if ((ranks.size()) == 1 && (ranks[0] == 0)) {
571  return rcp(new SerialComm<Ordinal>(*this));
572  } else {
573  return RCP< Comm<Ordinal> >();
574  }
575 }
576 
577 // Overridden from Describable
578 
579 
580 template<typename Ordinal>
582 {
583  std::ostringstream oss;
584  oss << "Teuchos::SerialComm<"<<OrdinalTraits<Ordinal>::name()<<">";
585  return oss.str();
586 }
587 
588 
589 } // namespace Teuchos
590 
591 
592 #endif // TEUCHOS_SERIAL_COMM_HPP
bool is_null(const boost::shared_ptr< T > &p)
Returns true if p.get()==NULL.
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
virtual void reduceAllAndScatter(const ValueTypeReductionOp< Ordinal, char > &reductOp, const Ordinal sendBytes, const char sendBuffer[], const Ordinal recvCounts[], char myGlobalReducts[]) const
#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 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