Teuchos - Trilinos Tools Package Version of the Day
Loading...
Searching...
No Matches
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
49namespace Teuchos {
50
57template<class OrdinalType>
58class SerialCommStatus : public CommStatus<OrdinalType> {
59public:
62
64 OrdinalType getSourceRank () { return 0; }
65
67 OrdinalType getTag () { return 0; }
68};
69
70
75template<typename Ordinal>
76class SerialComm : public Comm<Ordinal> {
77public:
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(
122 ,const Ordinal bytes, const char sendBuffer[], char globalReducts[]
123 ) const;
125 virtual void scan(
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(
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;
167 const int destRank
168 ) const;
172 const int destRank,
173 const int tag) const;
176 const ArrayView<char> &Buffer,
177 const int sourceRank
178 ) const;
182 const int sourceRank,
183 const int tag) const;
185 virtual void waitAll(
187 ) const;
189 virtual void
191 const ArrayView<RCP<CommStatus<Ordinal> > >& statuses) const;
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
220template<typename Ordinal>
225
226
227// ////////////////////////
228// Implementations
229
230
231// Constructors
232
233
234template<typename Ordinal>
237
238template<typename Ordinal>
241
242
243// Overridden from Comm
244
245
246template<typename Ordinal>
248{
249 return 0;
250}
251
252
253template<typename Ordinal>
255{
256 return 1;
257}
258
259
260template<typename Ordinal>
262{
263 // Nothing to do
264}
265
266
267template<typename Ordinal>
269 const int /*rootRank*/, const Ordinal /*bytes*/, char []/*buffer*/
270 ) const
271{
272 // Nothing to do
273}
274
275
276template<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
286#ifdef TEUCHOS_DEBUG
288#endif
290}
291
292
293template<typename Ordinal>
294void
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;
305 (void) root;
306#ifdef TEUCHOS_DEBUG
308#endif
310}
311
312
313template<typename Ordinal>
316 ,const Ordinal bytes, const char sendBuffer[], char globalReducts[]
317 ) const
318{
319 (void)reductOp;
321}
322
323
324template<typename Ordinal>
327 ,const Ordinal bytes, const char sendBuffer[], char scanReducts[]
328 ) const
329{
330 (void)reductOp;
332}
333
334
335template<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
347template<typename Ordinal>
349send (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
361template<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
373template<typename Ordinal>
374void
375SerialComm<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
387template<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
400template<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
413template<typename Ordinal>
414void
416 const char sendBuffer[],
417 const int destRank,
418 const int tag) const
419{
420 (void) bytes;
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
432template<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
442template<typename Ordinal>
445isend (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
453template<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
463template<typename Ordinal>
466ireceive (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
474template<typename Ordinal>
476{
477 (void) requests;
478 // There's nothing to wait on!
479}
480
481
482template<typename Ordinal>
483void
486 const ArrayView<RCP<CommStatus<Ordinal> > >& statuses) const
487{
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
500template<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
515template< typename Ordinal>
518{
519 return rcp(new SerialComm<Ordinal>(*this));
520}
521
522template<typename Ordinal>
524SerialComm<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
533template<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
547template<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
Defines basic traits for the ordinal field type.
Nonowning array view.
Encapsulation of the result of a receive (blocking or nonblocking).
Abstract interface for distributed-memory communication.
Simple wrapper class for raw pointers to single objects where no persisting relationship exists.
Smart reference counting pointer class for automatic garbage collection.
RCP(ENull null_arg=null)
Initialize RCP<T> to NULL.
RCP< T > rcp(const boost::shared_ptr< T > &sptr)
Conversion function that takes in a boost::shared_ptr object and spits out a Teuchos::RCP object.
bool is_null() const
Returns true if the underlying pointer is null.
T * getRawPtr() const
Get the raw C++ pointer to the underlying object.
Implementation of CommStatus for a serial communicator.
OrdinalType getSourceRank()
The source rank that sent the message (must be zero).
OrdinalType getTag()
The tag of the received message.
Concrete serial communicator subclass.
virtual RCP< CommRequest< Ordinal > > ireceive(const ArrayView< char > &Buffer, const int sourceRank) const
virtual RCP< CommRequest< Ordinal > > isend(const ArrayView< const char > &sendBuffer, const int destRank) const
virtual void broadcast(const int rootRank, const Ordinal bytes, char buffer[]) const
virtual RCP< Comm< Ordinal > > createSubcommunicator(const ArrayView< const int > &ranks) const
virtual void reduceAll(const ValueTypeReductionOp< Ordinal, char > &reductOp, const Ordinal bytes, const char sendBuffer[], char globalReducts[]) 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.
virtual int receive(const int sourceRank, const Ordinal bytes, char recvBuffer[]) const
int getTag() const
The current tag.
virtual void readySend(const ArrayView< const char > &sendBuffer, const int destRank) const
RCP< SerialComm< Ordinal > > createSerialComm()
Nonmember constructor.
virtual RCP< Comm< Ordinal > > duplicate() const
virtual void ssend(const Ordinal bytes, const char sendBuffer[], const int destRank, const int tag) const
virtual void scan(const ValueTypeReductionOp< Ordinal, char > &reductOp, const Ordinal bytes, const char sendBuffer[], char scanReducts[]) const
virtual void send(const Ordinal bytes, const char sendBuffer[], const int destRank) const
virtual RCP< CommStatus< Ordinal > > wait(const Ptr< RCP< CommRequest< Ordinal > > > &request) const
virtual void waitAll(const ArrayView< RCP< CommRequest< Ordinal > > > &requests) const
virtual RCP< Comm< Ordinal > > split(const int color, const int key) const
virtual void gatherAll(const Ordinal sendBytes, const char sendBuffer[], const Ordinal recvBytes, char recvBuffer[]) const
virtual void ssend(const Ordinal bytes, const char sendBuffer[], const int destRank) const
#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.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
static std::string name()
Returns name of this ordinal type.