Teuchos - Trilinos Tools Package  Version of the Day
Teuchos_CommHelpers.cpp
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 #include "Teuchos_CommHelpers.hpp"
43 #ifdef HAVE_MPI
44 # include "Teuchos_Details_MpiCommRequest.hpp"
45 #endif // HAVE_MPI
46 
47 namespace Teuchos {
48 namespace { // (anonymous)
49 
50 #ifdef HAVE_MPI
51 MPI_Op getMpiOpForEReductionType (const enum EReductionType reductionType) {
53  switch (reductionType) {
54  case REDUCE_SUM: return MPI_SUM;
55  case REDUCE_MIN: return MPI_MIN;
56  case REDUCE_MAX: return MPI_MAX;
57  case REDUCE_AND: return MPI_LAND; // logical AND, not bitwise AND
58  default:
59  TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument,
60  "The given EReductionType value is invalid.");
61  }
62 }
63 
67 std::string getMpiErrorString (const int errCode) {
68  // Space for storing the error string returned by MPI.
69  // Leave room for null termination, since I don't know if MPI does this.
70  char errString [MPI_MAX_ERROR_STRING+1];
71  int errStringLen = MPI_MAX_ERROR_STRING; // output argument
72  (void) MPI_Error_string (errCode, errString, &errStringLen);
73  // errStringLen on output is the number of characters written.
74  // I'm not sure (the MPI 3.0 Standard doesn't say) if this
75  // includes the '\0', so I'll make sure. We reserved space for
76  // the extra '\0' if needed.
77  if (errString[errStringLen-1] != '\0') {
78  errString[errStringLen] = '\0';
79  }
80  return std::string (errString); // This copies the original string.
81 }
82 
90 template<class T>
91 class MpiTypeTraits {
92 public:
98  static MPI_Datatype getType (const T&);
99 };
100 
101 #ifdef TEUCHOS_HAVE_COMPLEX
102 template<>
103 class MpiTypeTraits<std::complex<double> > {
104 public:
105  static MPI_Datatype getType (const T&) {
106  return MPI_C_DOUBLE_COMPLEX;
107  }
108 };
109 
110 template<>
111 class MpiTypeTraits<std::complex<float> > {
112 public:
113  static MPI_Datatype getType (const T&) {
114  return MPI_C_FLOAT_COMPLEX;
115  }
116 };
117 #endif // TEUCHOS_HAVE_COMPLEX
118 
119 template<>
120 class MpiTypeTraits<double> {
121 public:
122  static MPI_Datatype getType (const double&) {
123  return MPI_DOUBLE;
124  }
125 };
126 
127 template<>
128 class MpiTypeTraits<float> {
129 public:
130  static MPI_Datatype getType (const float&) {
131  return MPI_FLOAT;
132  }
133 };
134 
135 #ifdef TEUCHOS_HAVE_LONG_LONG_INT
136 template<>
137 class MpiTypeTraits<long long> {
138 public:
139  static MPI_Datatype getType (const long long&) {
140  return MPI_LONG_LONG;
141  }
142 };
143 #endif // TEUCHOS_HAVE_LONG_LONG_INT
144 
145 template<>
146 class MpiTypeTraits<long> {
147 public:
148  static MPI_Datatype getType (const long&) {
149  return MPI_LONG;
150  }
151 };
152 
153 template<>
154 class MpiTypeTraits<int> {
155 public:
156  static MPI_Datatype getType (const int&) {
157  return MPI_INT;
158  }
159 };
160 
161 template<>
162 class MpiTypeTraits<short> {
163 public:
164  static MPI_Datatype getType (const short&) {
165  return MPI_SHORT;
166  }
167 };
168 #endif // HAVE_MPI
169 
170 
178 template<class T>
179 void
180 reduceAllImpl (const Comm<int>& comm,
181  const EReductionType reductType,
182  const int count,
183  const T sendBuffer[],
184  T globalReducts[])
185 {
186 #ifdef HAVE_MPI
187  // mfh 17 Oct 2012: Even in an MPI build, Comm might be either a
188  // SerialComm or an MpiComm. If it's something else, we fall back
189  // to the most general implementation.
190  const MpiComm<int>* mpiComm = dynamic_cast<const MpiComm<int>* > (&comm);
191  if (mpiComm == NULL) {
192  // Is it a SerialComm?
193  const SerialComm<int>* serialComm = dynamic_cast<const SerialComm<int>* > (&comm);
194  if (serialComm == NULL) {
195  // We don't know what kind of Comm we have, so fall back to the
196  // most general implementation.
197  std::auto_ptr<ValueTypeReductionOp<int, T> > reductOp (createOp<int, T> (reductType));
198  reduceAll (comm, *reductOp, count, sendBuffer, globalReducts);
199  }
200  else { // It's a SerialComm; there is only 1 process, so just copy.
201  std::copy (sendBuffer, sendBuffer + count, globalReducts);
202  }
203  } else { // It's an MpiComm. Invoke MPI directly.
204  MPI_Op rawMpiOp = getMpiOpForEReductionType (reductType);
205  MPI_Comm rawMpiComm = * (mpiComm->getRawMpiComm ());
206  T t;
207  MPI_Datatype rawMpiType = MpiTypeTraits<T>::getType (t);
208  const int err = MPI_Allreduce (const_cast<T*> (sendBuffer),
209  globalReducts, count, rawMpiType, rawMpiOp, rawMpiComm);
211  err != MPI_SUCCESS,
212  std::runtime_error,
213  "MPI_Allreduce failed with the following error: "
214  << getMpiErrorString (err));
215  }
216 #else
217  // We've built without MPI, so just assume it's a SerialComm and copy the data.
218  std::copy (sendBuffer, sendBuffer + count, globalReducts);
219 #endif // HAVE_MPI
220 }
221 
222 
230 template<class T>
231 void
232 gatherImpl (const T sendBuf[],
233  const int sendCount,
234  T recvBuf[],
235  const int recvCount,
236  const int root,
237  const Comm<int>& comm)
238 {
239 #ifdef HAVE_MPI
240  // mfh 17 Oct 2012: Even in an MPI build, Comm might be either a
241  // SerialComm or an MpiComm. If it's something else, we fall back
242  // to the most general implementation.
243  const MpiComm<int>* mpiComm = dynamic_cast<const MpiComm<int>* > (&comm);
244  if (mpiComm == NULL) {
245  // Is it a SerialComm?
246  const SerialComm<int>* serialComm = dynamic_cast<const SerialComm<int>* > (&comm);
247  if (serialComm == NULL) {
248  // We don't know what kind of Comm we have, so fall back to the
249  // most general implementation.
250  gather<int, T> (sendBuf, sendCount, recvBuf, recvCount, root, comm);
251  }
252  else { // It's a SerialComm; there is only 1 process, so just copy.
253  std::copy (sendBuf, sendBuf + sendCount, recvBuf);
254  }
255  } else { // It's an MpiComm. Invoke MPI directly.
256  MPI_Comm rawMpiComm = * (mpiComm->getRawMpiComm ());
257  T t;
258  MPI_Datatype rawMpiType = MpiTypeTraits<T>::getType (t);
259  const int err = MPI_Gather (const_cast<T*> (sendBuf), sendCount, rawMpiType,
260  recvBuf, recvCount, rawMpiType,
261  root, rawMpiComm);
263  err != MPI_SUCCESS,
264  std::runtime_error,
265  "MPI_Gather failed with the following error: "
266  << getMpiErrorString (err));
267  }
268 #else
269  // We've built without MPI, so just assume it's a SerialComm and copy the data.
270  std::copy (sendBuf, sendBuf + sendCount, recvBuf);
271 #endif // HAVE_MPI
272 }
273 
274 
282 template<class T>
283 void
284 gathervImpl (const T sendBuf[],
285  const int sendCount,
286  T recvBuf[],
287  const int recvCounts[],
288  const int displs[],
289  const int root,
290  const Comm<int>& comm)
291 {
292 #ifdef HAVE_MPI
293  // mfh 17 Oct 2012: Even in an MPI build, Comm might be either a
294  // SerialComm or an MpiComm. If it's something else, we fall back
295  // to the most general implementation.
296  const MpiComm<int>* mpiComm = dynamic_cast<const MpiComm<int>* > (&comm);
297  if (mpiComm == NULL) {
298  // Is it a SerialComm?
299  const SerialComm<int>* serialComm = dynamic_cast<const SerialComm<int>* > (&comm);
300  if (serialComm == NULL) {
301  // We don't know what kind of Comm we have, so fall back to the
302  // most general implementation.
303  gatherv<int, T> (sendBuf, sendCount, recvBuf, recvCounts, displs, root, comm);
304  }
305  else { // It's a SerialComm; there is only 1 process, so just copy.
307  recvCounts[0] > sendCount, std::invalid_argument,
308  "Teuchos::gatherv: If the input communicator contains only one "
309  "process, then you cannot receive more entries than you send. "
310  "You aim to receive " << recvCounts[0] << " entries, but to send "
311  << sendCount << " entries.");
312  // Serial communicator case: just copy. recvCounts[0] is the
313  // amount to receive, so it's the amount to copy. Start writing
314  // to recvbuf at the offset displs[0].
315  std::copy (sendBuf, sendBuf + recvCounts[0], recvBuf + displs[0]);
316  }
317  } else { // It's an MpiComm. Invoke MPI directly.
318  MPI_Comm rawMpiComm = * (mpiComm->getRawMpiComm ());
319  T t;
320  MPI_Datatype rawMpiType = MpiTypeTraits<T>::getType (t);
321  const int err = MPI_Gatherv (const_cast<T*> (sendBuf),
322  sendCount,
323  rawMpiType,
324  recvBuf,
325  const_cast<int*> (recvCounts),
326  const_cast<int*> (displs),
327  rawMpiType,
328  root,
329  rawMpiComm);
331  err != MPI_SUCCESS,
332  std::runtime_error,
333  "MPI_Gatherv failed with the following error: "
334  << getMpiErrorString (err));
335  }
336 #else
337  // We've built without MPI, so just assume it's a SerialComm and copy the data.
339  recvCounts[0] > sendCount, std::invalid_argument,
340  "Teuchos::gatherv: If the input communicator contains only one "
341  "process, then you cannot receive more entries than you send. "
342  "You aim to receive " << recvCounts[0] << " entries, but to send "
343  << sendCount << " entries.");
344  // Serial communicator case: just copy. recvCounts[0] is the
345  // amount to receive, so it's the amount to copy. Start writing
346  // to recvbuf at the offset displs[0].
347  std::copy (sendBuf, sendBuf + recvCounts[0], recvBuf + displs[0]);
348 #endif // HAVE_MPI
349 }
350 
351 
359 template<class T>
360 void
361 reduceAllAndScatterImpl (const Comm<int>& comm,
362  const EReductionType reductType,
363  const int sendCount,
364  const T sendBuffer[],
365  const int recvCounts[],
366  T myGlobalReducts[])
367 {
368 #ifdef HAVE_MPI
369  // mfh 17 Oct 2012: Even in an MPI build, Comm might be either a
370  // SerialComm or an MpiComm. If it's something else, we fall back
371  // to the most general implementation.
372  const MpiComm<int>* mpiComm = dynamic_cast<const MpiComm<int>* > (&comm);
373  if (mpiComm == NULL) {
374  // Is it a SerialComm?
375  const SerialComm<int>* serialComm = dynamic_cast<const SerialComm<int>* > (&comm);
376  if (serialComm == NULL) {
377  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error, "reduceAllAndScatter: "
378  "Not implemented for Comm not MpiComm or SerialComm.");
379  }
380  else { // It's a SerialComm; there is only 1 process, so just copy.
381  //
382  // FIXME (mfh 09 Apr 2013) Is this right? This is what
383  // SerialComm does.
384  //
385  std::copy (sendBuffer, sendBuffer + sendCount, myGlobalReducts);
386  }
387  } else { // It's an MpiComm. Invoke MPI directly.
388  MPI_Op rawMpiOp = getMpiOpForEReductionType (reductType);
389  MPI_Comm rawMpiComm = * (mpiComm->getRawMpiComm ());
390  T t;
391  MPI_Datatype rawMpiType = MpiTypeTraits<T>::getType (t);
392  const int err = MPI_Reduce_scatter (const_cast<T*> (sendBuffer),
393  myGlobalReducts,
394  const_cast<int*> (recvCounts),
395  rawMpiType,
396  rawMpiOp,
397  rawMpiComm);
399  err != MPI_SUCCESS,
400  std::runtime_error,
401  "MPI_Reduce_scatter failed with the following error: "
402  << getMpiErrorString (err));
403  }
404 #else
405  // We've built without MPI, so just assume it's a SerialComm and copy the data.
406  //
407  // FIXME (mfh 09 Apr 2013) Is this right? This is what SerialComm does.
408  //
409  std::copy (sendBuffer, sendBuffer + sendCount, myGlobalReducts);
410 #endif // HAVE_MPI
411 }
412 
413 
419 template<typename Packet>
420 RCP<Teuchos::CommRequest<int> >
421 ireceiveGeneral(const Comm<int>& comm,
422  const ArrayRCP<Packet> &recvBuffer,
423  const int sourceRank)
424 {
425  TEUCHOS_COMM_TIME_MONITOR(
426  "Teuchos::ireceive<int, " << "," << TypeNameTraits<Packet>::name ()
427  << "> ( value type )"
428  );
429  ValueTypeSerializationBuffer<int, Packet>
430  charRecvBuffer (recvBuffer.size (), recvBuffer.getRawPtr ());
431  RCP<CommRequest<int> > commRequest =
432  comm.ireceive (charRecvBuffer.getCharBufferView (), sourceRank);
433  set_extra_data (recvBuffer, "buffer", inOutArg (commRequest));
434  return commRequest;
435 }
436 
439 template<typename Packet>
440 RCP<Teuchos::CommRequest<int> >
441 ireceiveGeneral (const ArrayRCP<Packet> &recvBuffer,
442  const int sourceRank,
443  const int tag,
444  const Comm<int>& comm)
445 {
446  TEUCHOS_COMM_TIME_MONITOR(
447  "Teuchos::ireceive<int, " << "," << TypeNameTraits<Packet>::name ()
448  << "> ( value type )"
449  );
450  ValueTypeSerializationBuffer<int, Packet>
451  charRecvBuffer (recvBuffer.size (), recvBuffer.getRawPtr ());
452  RCP<CommRequest<int> > commRequest =
453  comm.ireceive (charRecvBuffer.getCharBufferView (), sourceRank, tag);
454  set_extra_data (recvBuffer, "buffer", inOutArg (commRequest));
455  return commRequest;
456 }
457 
470 template<class T>
471 RCP<CommRequest<int> >
472 ireceiveImpl (const Comm<int>& comm,
473  const ArrayRCP<T>& recvBuffer,
474  const int sourceRank)
475 {
476 #ifdef HAVE_MPI
477  // Even in an MPI build, Comm might be either a SerialComm or an
478  // MpiComm. If it's something else, we fall back to the most
479  // general implementation.
480  const MpiComm<int>* mpiComm = dynamic_cast<const MpiComm<int>* > (&comm);
481  if (mpiComm == NULL) {
482  // Is it a SerialComm?
483  const SerialComm<int>* serialComm = dynamic_cast<const SerialComm<int>* > (&comm);
484  if (serialComm == NULL) {
485  // We don't know what kind of Comm we have, so fall back to the
486  // most general implementation.
487  return ireceiveGeneral<T> (comm, recvBuffer, sourceRank);
488  }
489  else { // SerialComm doesn't implement ireceive anyway.
491  true,
492  std::logic_error,
493  "ireceiveImpl: Not implemented for a serial communicator.");
494  }
495  }
496  else { // It's an MpiComm. Invoke MPI directly.
497  MPI_Comm rawComm = * (mpiComm->getRawMpiComm ());
498  T t;
499  MPI_Datatype rawType = MpiTypeTraits<T>::getType (t);
500  T* rawRecvBuf = recvBuffer.getRawPtr ();
501  const int count = as<int> (recvBuffer.size ());
502  const int tag = mpiComm->getTag ();
503  MPI_Request rawRequest = MPI_REQUEST_NULL;
504  const int err = MPI_Irecv (rawRecvBuf, count, rawType, sourceRank, tag,
505  rawComm, &rawRequest);
507  err != MPI_SUCCESS, std::runtime_error,
508  "MPI_Irecv failed with the following error: "
509  << getMpiErrorString (err));
510 
511  ArrayRCP<const char> buf =
512  arcp_const_cast<const char> (arcp_reinterpret_cast<char> (recvBuffer));
513  RCP<Details::MpiCommRequest> req (new Details::MpiCommRequest (rawRequest, buf));
514  return rcp_implicit_cast<CommRequest<int> > (req);
515  }
516 #else
518  true,
519  std::logic_error,
520  "ireceiveImpl: Not implemented for a serial communicator.");
521 
522  return null; // Guard to avoid compiler warning about not returning a value.
523 #endif // HAVE_MPI
524 }
525 
528 template<class T>
529 RCP<CommRequest<int> >
530 ireceiveImpl (const ArrayRCP<T>& recvBuffer,
531  const int sourceRank,
532  const int tag,
533  const Comm<int>& comm)
534 {
535 #ifdef HAVE_MPI
536  // Even in an MPI build, Comm might be either a SerialComm or an
537  // MpiComm. If it's something else, we fall back to the most
538  // general implementation.
539  const MpiComm<int>* mpiComm = dynamic_cast<const MpiComm<int>* > (&comm);
540  if (mpiComm == NULL) {
541  // Is it a SerialComm?
542  const SerialComm<int>* serialComm = dynamic_cast<const SerialComm<int>* > (&comm);
543  if (serialComm == NULL) {
544  // We don't know what kind of Comm we have, so fall back to the
545  // most general implementation.
546  return ireceiveGeneral<T> (recvBuffer, sourceRank, tag, comm);
547  }
548  else { // SerialComm doesn't implement ireceive anyway.
550  true,
551  std::logic_error,
552  "ireceiveImpl: Not implemented for a serial communicator.");
553  }
554  }
555  else { // It's an MpiComm. Invoke MPI directly.
556  MPI_Comm rawComm = * (mpiComm->getRawMpiComm ());
557  T t;
558  MPI_Datatype rawType = MpiTypeTraits<T>::getType (t);
559  T* rawRecvBuf = recvBuffer.getRawPtr ();
560  const int count = as<int> (recvBuffer.size ());
561  MPI_Request rawRequest = MPI_REQUEST_NULL;
562  const int err = MPI_Irecv (rawRecvBuf, count, rawType, sourceRank, tag,
563  rawComm, &rawRequest);
565  err != MPI_SUCCESS, std::runtime_error,
566  "MPI_Irecv failed with the following error: "
567  << getMpiErrorString (err));
568 
569  ArrayRCP<const char> buf =
570  arcp_const_cast<const char> (arcp_reinterpret_cast<char> (recvBuffer));
571  RCP<Details::MpiCommRequest> req (new Details::MpiCommRequest (rawRequest, buf));
572  return rcp_implicit_cast<CommRequest<int> > (req);
573  }
574 #else
576  true,
577  std::logic_error,
578  "ireceiveImpl: Not implemented for a serial communicator.");
579 
580  return null; // Guard to avoid compiler warning about not returning a value.
581 #endif // HAVE_MPI
582 }
583 
589 template<class T>
590 void
591 sendGeneral (const Comm<int>& comm,
592  const int count,
593  const T sendBuffer[],
594  const int destRank)
595 {
596  TEUCHOS_COMM_TIME_MONITOR(
597  "Teuchos::send<int, " << TypeNameTraits<T>::name () << ">");
598  ConstValueTypeSerializationBuffer<int,T> charSendBuffer (count, sendBuffer);
599  comm.send (charSendBuffer.getBytes (),
600  charSendBuffer.getCharBuffer (),
601  destRank);
602 }
603 
606 template<class T>
607 void
608 sendGeneral (const T sendBuffer[],
609  const int count,
610  const int destRank,
611  const int tag,
612  const Comm<int>& comm)
613 {
614  TEUCHOS_COMM_TIME_MONITOR(
615  "Teuchos::send<int, " << TypeNameTraits<T>::name () << ">");
616  ConstValueTypeSerializationBuffer<int,T> charSendBuffer (count, sendBuffer);
617  comm.send (charSendBuffer.getBytes (),
618  charSendBuffer.getCharBuffer (),
619  destRank, tag);
620 }
621 
634 template<class T>
635 void
636 sendImpl (const Comm<int>& comm,
637  const int count,
638  const T sendBuffer[],
639  const int destRank)
640 {
641 #ifdef HAVE_MPI
642  // Even in an MPI build, Comm might be either a SerialComm or an
643  // MpiComm. If it's something else, we fall back to the most
644  // general implementation.
645  const MpiComm<int>* mpiComm = dynamic_cast<const MpiComm<int>* > (&comm);
646  if (mpiComm == NULL) {
647  // Is it a SerialComm?
648  const SerialComm<int>* serialComm = dynamic_cast<const SerialComm<int>* > (&comm);
649  if (serialComm == NULL) {
650  // We don't know what kind of Comm we have, so fall back to the
651  // most general implementation.
652  sendGeneral<T> (comm, count, sendBuffer, destRank);
653  }
654  else { // SerialComm doesn't implement send correctly anyway.
656  true,
657  std::logic_error,
658  "sendImpl: Not implemented for a serial communicator.");
659  }
660  }
661  else { // It's an MpiComm. Invoke MPI directly.
662  MPI_Comm rawComm = * (mpiComm->getRawMpiComm ());
663  T t;
664  MPI_Datatype rawType = MpiTypeTraits<T>::getType (t);
665  T* rawBuf = const_cast<T*> (sendBuffer);
666  const int tag = mpiComm->getTag ();
667  const int err = MPI_Send (rawBuf, count, rawType, destRank, tag, rawComm);
669  err != MPI_SUCCESS,
670  std::runtime_error,
671  "MPI_Send failed with the following error: "
672  << getMpiErrorString (err));
673  }
674 #else
676  true,
677  std::logic_error,
678  "sendImpl: Not implemented for a serial communicator.");
679 #endif // HAVE_MPI
680 }
681 
684 template<class T>
685 void
686 sendImpl (const T sendBuffer[],
687  const int count,
688  const int destRank,
689  const int tag,
690  const Comm<int>& comm)
691 {
692 #ifdef HAVE_MPI
693  // Even in an MPI build, Comm might be either a SerialComm or an
694  // MpiComm. If it's something else, we fall back to the most
695  // general implementation.
696  const MpiComm<int>* mpiComm = dynamic_cast<const MpiComm<int>* > (&comm);
697  if (mpiComm == NULL) {
698  // Is it a SerialComm?
699  const SerialComm<int>* serialComm = dynamic_cast<const SerialComm<int>* > (&comm);
700  if (serialComm == NULL) {
701  // We don't know what kind of Comm we have, so fall back to the
702  // most general implementation.
703  sendGeneral<T> (sendBuffer, count, destRank, tag, comm);
704  }
705  else { // SerialComm doesn't implement send correctly anyway.
707  true,
708  std::logic_error,
709  "sendImpl: Not implemented for a serial communicator.");
710  }
711  }
712  else { // It's an MpiComm. Invoke MPI directly.
713  MPI_Comm rawComm = * (mpiComm->getRawMpiComm ());
714  T t;
715  MPI_Datatype rawType = MpiTypeTraits<T>::getType (t);
716  T* rawBuf = const_cast<T*> (sendBuffer);
717  const int err = MPI_Send (rawBuf, count, rawType, destRank, tag, rawComm);
719  err != MPI_SUCCESS,
720  std::runtime_error,
721  "MPI_Send failed with the following error: "
722  << getMpiErrorString (err));
723  }
724 #else
726  true,
727  std::logic_error,
728  "sendImpl: Not implemented for a serial communicator.");
729 #endif // HAVE_MPI
730 }
731 
737 template<class T>
738 RCP<CommRequest<int> >
739 isendGeneral (const Comm<int>& comm,
740  const ArrayRCP<const T>& sendBuffer,
741  const int destRank)
742 {
743  TEUCHOS_COMM_TIME_MONITOR(
744  "Teuchos::isend<int," << TypeNameTraits<T>::name () << ">");
745  ConstValueTypeSerializationBuffer<int, T>
746  charSendBuffer (sendBuffer.size (), sendBuffer.getRawPtr ());
747  RCP<CommRequest<int> > commRequest =
748  comm.isend (charSendBuffer.getCharBufferView (), destRank);
749  set_extra_data (sendBuffer, "buffer", inOutArg (commRequest));
750  return commRequest;
751 }
752 
759 template<class T>
760 RCP<CommRequest<int> >
761 isendGeneral (const ArrayRCP<const T>& sendBuffer,
762  const int destRank,
763  const int tag,
764  const Comm<int>& comm)
765 {
766  TEUCHOS_COMM_TIME_MONITOR(
767  "Teuchos::isend<int," << TypeNameTraits<T>::name () << ">");
768  ConstValueTypeSerializationBuffer<int, T>
769  charSendBuffer (sendBuffer.size (), sendBuffer.getRawPtr ());
770  RCP<CommRequest<int> > commRequest =
771  comm.isend (charSendBuffer.getCharBufferView (), destRank, tag);
772  set_extra_data (sendBuffer, "buffer", inOutArg (commRequest));
773  return commRequest;
774 }
775 
778 template<class T>
779 RCP<Teuchos::CommRequest<int> >
780 isendImpl (const ArrayRCP<const T>& sendBuffer,
781  const int destRank,
782  const int tag,
783  const Comm<int>& comm)
784 {
785 #ifdef HAVE_MPI
786  // Even in an MPI build, Comm might be either a SerialComm or an
787  // MpiComm. If it's something else, we fall back to the most
788  // general implementation.
789  const MpiComm<int>* mpiComm = dynamic_cast<const MpiComm<int>* > (&comm);
790  if (mpiComm == NULL) {
791  // Is it a SerialComm?
792  const SerialComm<int>* serialComm = dynamic_cast<const SerialComm<int>* > (&comm);
793  if (serialComm == NULL) {
794  // We don't know what kind of Comm we have, so fall back to the
795  // most general implementation.
796  return isendGeneral<T> (sendBuffer, destRank, tag, comm);
797  }
798  else { // SerialComm doesn't implement send correctly anyway.
800  true, std::logic_error,
801  "isendImpl: Not implemented for a serial communicator.");
802  }
803  }
804  else { // It's an MpiComm. Invoke MPI directly.
805  MPI_Comm rawComm = * (mpiComm->getRawMpiComm ());
806  T t;
807  MPI_Datatype rawType = MpiTypeTraits<T>::getType (t);
808  // MPI promises not to modify the send buffer; the const_cast
809  // merely ensures compatibilty with C89, which does not have a
810  // "const" keyword.
811  T* rawSendBuf = const_cast<T*> (sendBuffer.getRawPtr ());
812  const int count = as<int> (sendBuffer.size ());
813  MPI_Request rawRequest = MPI_REQUEST_NULL;
814  const int err = MPI_Isend (rawSendBuf, count, rawType, destRank, tag,
815  rawComm, &rawRequest);
817  err != MPI_SUCCESS,
818  std::runtime_error,
819  "MPI_Isend failed with the following error: "
820  << getMpiErrorString (err));
821 
822  ArrayRCP<const char> buf = arcp_reinterpret_cast<const char> (sendBuffer);
823  RCP<Details::MpiCommRequest> req (new Details::MpiCommRequest (rawRequest, buf));
824  return rcp_implicit_cast<CommRequest<int> > (req);
825  }
826 #else
828  true,
829  std::logic_error,
830  "isendImpl: Not implemented for a serial communicator.");
831 #endif // HAVE_MPI
832 }
833 
834 } // namespace (anonymous)
835 
836 // mfh 18 Oct 2012: Note on full template specializations
837 //
838 // To make Windows builds happy, declarations of full template
839 // specializations (as found in Teuchos_CommHelpers.hpp) must use the
840 // TEUCHOSCOMM_LIB_DLL_EXPORT macro. However, _definitions_ of the
841 // specializations (as found in this file) must _not_ use the macro.
842 // That's why we don't use that macro here.
843 
844 #ifdef TEUCHOS_HAVE_COMPLEX
845 // Specialization for Ordinal=int and Packet=std::complex<double>.
846 template<>
847 void
848 reduceAll<int, std::complex<double> > (const Comm<int>& comm,
849  const EReductionType reductType,
850  const int count,
851  const std::complex<double> sendBuffer[],
852  std::complex<double> globalReducts[])
853 {
854  TEUCHOS_COMM_TIME_MONITOR(
855  "Teuchos::reduceAll<int, std::complex<double> > (" << count << ", "
856  << toString (reductType) << ")"
857  );
858  reduceAllImpl<std::complex<double> > (comm, reductType, count, sendBuffer, globalReducts);
859 }
860 
861 template<>
862 RCP<Teuchos::CommRequest<int> >
863 ireceive<int, std::complex<double> > (const Comm<int>& comm,
864  const ArrayRCP<std::complex<double> >& recvBuffer,
865  const int sourceRank)
866 {
867  TEUCHOS_COMM_TIME_MONITOR("ireceive<int, std::complex<double> >");
868  return ireceiveImpl<std::complex<double> > (comm, recvBuffer, sourceRank);
869 }
870 
871 template<>
872 RCP<Teuchos::CommRequest<int> >
873 ireceive<int, std::complex<double> > (const ArrayRCP<std::complex<double> >& recvBuffer,
874  const int sourceRank,
875  const int tag,
876  const Comm<int>& comm)
877 {
878  TEUCHOS_COMM_TIME_MONITOR("ireceive<int, std::complex<double> >");
879  return ireceiveImpl<std::complex<double> > (recvBuffer, sourceRank, tag, comm);
880 }
881 
882 template<>
883 TEUCHOSCOMM_LIB_DLL_EXPORT void
884 send<int, std::complex<double> > (const Comm<int>& comm,
885  const int count,
886  const std::complex<double> sendBuffer[],
887  const int destRank)
888 {
889  sendImpl<std::complex<double> > (comm, count, sendBuffer, destRank);
890 }
891 
892 template<>
893 TEUCHOSCOMM_LIB_DLL_EXPORT void
894 send<int, std::complex<double> > (const std::complex<double> sendBuffer[],
895  const int count,
896  const int destRank,
897  const int tag,
898  const Comm<int>& comm)
899 {
900  sendImpl<std::complex<double> > (sendBuffer, count, destRank, tag, comm);
901 }
902 
903 template<>
904 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<Teuchos::CommRequest<int> >
905 isend (const ArrayRCP<const std::complex<double> >& sendBuffer,
906  const int destRank,
907  const int tag,
908  const Comm<int>& comm)
909 {
910  return isendImpl<std::complex<double> > (sendBuffer, destRank, tag, comm);
911 }
912 
913 // Specialization for Ordinal=int and Packet=std::complex<float>.
914 template<>
915 void
916 reduceAll<int, std::complex<float> > (const Comm<int>& comm,
917  const EReductionType reductType,
918  const int count,
919  const std::complex<float> sendBuffer[],
920  std::complex<float> globalReducts[])
921 {
922  TEUCHOS_COMM_TIME_MONITOR(
923  "Teuchos::reduceAll<int, std::complex<float> > (" << count << ", "
924  << toString (reductType) << ")"
925  );
926  reduceAllImpl<std::complex<float> > (comm, reductType, count, sendBuffer, globalReducts);
927 }
928 
929 template<>
930 RCP<Teuchos::CommRequest<int> >
931 ireceive<int, std::complex<float> > (const Comm<int>& comm,
932  const ArrayRCP<std::complex<float> >& recvBuffer,
933  const int sourceRank)
934 {
935  TEUCHOS_COMM_TIME_MONITOR("ireceive<int, std::complex<float> >");
936  return ireceiveImpl<std::complex<float> > (comm, recvBuffer, sourceRank);
937 }
938 
939 template<>
940 RCP<Teuchos::CommRequest<int> >
941 ireceive<int, std::complex<float> > (const ArrayRCP<std::complex<float> >& recvBuffer,
942  const int sourceRank,
943  const int tag,
944  const Comm<int>& comm)
945 {
946  TEUCHOS_COMM_TIME_MONITOR("ireceive<int, std::complex<float> >");
947  return ireceiveImpl<std::complex<float> > (recvBuffer, sourceRank, tag, comm);
948 }
949 
950 template<>
951 TEUCHOSCOMM_LIB_DLL_EXPORT void
952 send<int, std::complex<float> > (const Comm<int>& comm,
953  const int count,
954  const std::complex<float> sendBuffer[],
955  const int destRank)
956 {
957  return sendImpl<std::complex<float> > (comm, count, sendBuffer, destRank);
958 }
959 
960 template<>
961 TEUCHOSCOMM_LIB_DLL_EXPORT void
962 send<int, std::complex<float> > (const std::complex<float> sendBuffer[],
963  const int count,
964  const int destRank,
965  const int tag,
966  const Comm<int>& comm)
967 {
968  return sendImpl<std::complex<float> > (sendBuffer, count, destRank, tag, comm);
969 }
970 
971 template<>
972 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<Teuchos::CommRequest<int> >
973 isend (const ArrayRCP<const std::complex<float> >& sendBuffer,
974  const int destRank,
975  const int tag,
976  const Comm<int>& comm)
977 {
978  return isendImpl<std::complex<float> > (sendBuffer, destRank, tag, comm);
979 }
980 #endif // TEUCHOS_HAVE_COMPLEX
981 
982 
983 // Specialization for Ordinal=int and Packet=double.
984 template<>
985 void
986 reduceAll<int, double> (const Comm<int>& comm,
987  const EReductionType reductType,
988  const int count,
989  const double sendBuffer[],
990  double globalReducts[])
991 {
992  TEUCHOS_COMM_TIME_MONITOR(
993  "Teuchos::reduceAll<int, double> (" << count << ", "
994  << toString (reductType) << ")"
995  );
996  reduceAllImpl<double> (comm, reductType, count, sendBuffer, globalReducts);
997 }
998 
999 template<>
1000 RCP<Teuchos::CommRequest<int> >
1001 ireceive<int, double> (const Comm<int>& comm,
1002  const ArrayRCP<double>& recvBuffer,
1003  const int sourceRank)
1004 {
1005  TEUCHOS_COMM_TIME_MONITOR("ireceive<int, double>");
1006  return ireceiveImpl<double> (comm, recvBuffer, sourceRank);
1007 }
1008 
1009 template<>
1010 RCP<Teuchos::CommRequest<int> >
1011 ireceive<int, double> (const ArrayRCP<double>& recvBuffer,
1012  const int sourceRank,
1013  const int tag,
1014  const Comm<int>& comm)
1015 {
1016  TEUCHOS_COMM_TIME_MONITOR("ireceive<int, double>");
1017  return ireceiveImpl<double> (recvBuffer, sourceRank, tag, comm);
1018 }
1019 
1020 template<>
1021 TEUCHOSCOMM_LIB_DLL_EXPORT void
1022 send<int, double> (const Comm<int>& comm,
1023  const int count,
1024  const double sendBuffer[],
1025  const int destRank)
1026 {
1027  return sendImpl<double> (comm, count, sendBuffer, destRank);
1028 }
1029 
1030 template<>
1031 TEUCHOSCOMM_LIB_DLL_EXPORT void
1032 send<int, double> (const double sendBuffer[],
1033  const int count,
1034  const int destRank,
1035  const int tag,
1036  const Comm<int>& comm)
1037 {
1038  return sendImpl<double> (sendBuffer, count, destRank, tag, comm);
1039 }
1040 
1041 template<>
1042 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<Teuchos::CommRequest<int> >
1043 isend (const ArrayRCP<const double>& sendBuffer,
1044  const int destRank,
1045  const int tag,
1046  const Comm<int>& comm)
1047 {
1048  return isendImpl<double> (sendBuffer, destRank, tag, comm);
1049 }
1050 
1051 // Specialization for Ordinal=int and Packet=float.
1052 template<>
1053 void
1054 reduceAll<int, float> (const Comm<int>& comm,
1055  const EReductionType reductType,
1056  const int count,
1057  const float sendBuffer[],
1058  float globalReducts[])
1059 {
1060  TEUCHOS_COMM_TIME_MONITOR(
1061  "Teuchos::reduceAll<int, float> (" << count << ", "
1062  << toString (reductType) << ")"
1063  );
1064  reduceAllImpl<float> (comm, reductType, count, sendBuffer, globalReducts);
1065 }
1066 
1067 template<>
1068 RCP<Teuchos::CommRequest<int> >
1069 ireceive<int, float> (const Comm<int>& comm,
1070  const ArrayRCP<float>& recvBuffer,
1071  const int sourceRank)
1072 {
1073  TEUCHOS_COMM_TIME_MONITOR("ireceive<int, float>");
1074  return ireceiveImpl<float> (comm, recvBuffer, sourceRank);
1075 }
1076 
1077 template<>
1078 RCP<Teuchos::CommRequest<int> >
1079 ireceive<int, float> (const ArrayRCP<float>& recvBuffer,
1080  const int sourceRank,
1081  const int tag,
1082  const Comm<int>& comm)
1083 {
1084  TEUCHOS_COMM_TIME_MONITOR("ireceive<int, float>");
1085  return ireceiveImpl<float> (recvBuffer, sourceRank, tag, comm);
1086 }
1087 
1088 template<>
1089 TEUCHOSCOMM_LIB_DLL_EXPORT void
1090 send<int, float> (const Comm<int>& comm,
1091  const int count,
1092  const float sendBuffer[],
1093  const int destRank)
1094 {
1095  return sendImpl<float> (comm, count, sendBuffer, destRank);
1096 }
1097 
1098 template<>
1099 TEUCHOSCOMM_LIB_DLL_EXPORT void
1100 send<int, float> (const float sendBuffer[],
1101  const int count,
1102  const int destRank,
1103  const int tag,
1104  const Comm<int>& comm)
1105 {
1106  return sendImpl<float> (sendBuffer, count, destRank, tag, comm);
1107 }
1108 
1109 template<>
1110 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<Teuchos::CommRequest<int> >
1111 isend (const ArrayRCP<const float>& sendBuffer,
1112  const int destRank,
1113  const int tag,
1114  const Comm<int>& comm)
1115 {
1116  return isendImpl<float> (sendBuffer, destRank, tag, comm);
1117 }
1118 
1119 
1120 #ifdef TEUCHOS_HAVE_LONG_LONG_INT
1121 // Specialization for Ordinal=int and Packet=long long.
1122 template<>
1123 void
1124 gather<int, long long> (const long long sendBuf[],
1125  const int sendCount,
1126  long long recvBuf[],
1127  const int recvCount,
1128  const int root,
1129  const Comm<int>& comm)
1130 {
1131  gatherImpl<long long> (sendBuf, sendCount, recvBuf, recvCount, root, comm);
1132 }
1133 
1134 template<>
1135 void
1136 gatherv<int, long long> (const long long sendBuf[],
1137  const int sendCount,
1138  long long recvBuf[],
1139  const int recvCounts[],
1140  const int displs[],
1141  const int root,
1142  const Comm<int>& comm)
1143 {
1144  gathervImpl<long long> (sendBuf, sendCount, recvBuf, recvCounts, displs, root, comm);
1145 }
1146 
1147 template<>
1148 void
1149 reduceAll<int, long long> (const Comm<int>& comm,
1150  const EReductionType reductType,
1151  const int count,
1152  const long long sendBuffer[],
1153  long long globalReducts[])
1154 {
1155  TEUCHOS_COMM_TIME_MONITOR(
1156  "Teuchos::reduceAll<int, long long> (" << count << ", "
1157  << toString (reductType) << ")"
1158  );
1159  reduceAllImpl<long long> (comm, reductType, count, sendBuffer, globalReducts);
1160 }
1161 
1162 template<>
1163 RCP<Teuchos::CommRequest<int> >
1164 ireceive<int, long long> (const Comm<int>& comm,
1165  const ArrayRCP<long long>& recvBuffer,
1166  const int sourceRank)
1167 {
1168  TEUCHOS_COMM_TIME_MONITOR("ireceive<int, long long>");
1169  return ireceiveImpl<long long> (comm, recvBuffer, sourceRank);
1170 }
1171 
1172 template<>
1173 RCP<Teuchos::CommRequest<int> >
1174 ireceive<int, long long> (const ArrayRCP<long long>& recvBuffer,
1175  const int sourceRank,
1176  const int tag,
1177  const Comm<int>& comm)
1178 {
1179  TEUCHOS_COMM_TIME_MONITOR("ireceive<int, long long>");
1180  return ireceiveImpl<long long> (recvBuffer, sourceRank, tag, comm);
1181 }
1182 
1183 template<>
1184 TEUCHOSCOMM_LIB_DLL_EXPORT void
1185 send<int, long long> (const Comm<int>& comm,
1186  const int count,
1187  const long long sendBuffer[],
1188  const int destRank)
1189 {
1190  return sendImpl<long long> (comm, count, sendBuffer, destRank);
1191 }
1192 
1193 template<>
1194 TEUCHOSCOMM_LIB_DLL_EXPORT void
1195 send<int, long long> (const long long sendBuffer[],
1196  const int count,
1197  const int destRank,
1198  const int tag,
1199  const Comm<int>& comm)
1200 {
1201  return sendImpl<long long> (sendBuffer, count, destRank, tag, comm);
1202 }
1203 
1204 template<>
1205 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<Teuchos::CommRequest<int> >
1206 isend (const ArrayRCP<const long long>& sendBuffer,
1207  const int destRank,
1208  const int tag,
1209  const Comm<int>& comm)
1210 {
1211  return isendImpl<long long> (sendBuffer, destRank, tag, comm);
1212 }
1213 #endif // TEUCHOS_HAVE_LONG_LONG_INT
1214 
1215 
1216 // Specialization for Ordinal=int and Packet=long.
1217 template<>
1218 void
1219 gather<int, long> (const long sendBuf[],
1220  const int sendCount,
1221  long recvBuf[],
1222  const int recvCount,
1223  const int root,
1224  const Comm<int>& comm)
1225 {
1226  gatherImpl<long> (sendBuf, sendCount, recvBuf, recvCount, root, comm);
1227 }
1228 
1229 template<>
1230 void
1231 gatherv<int, long> (const long sendBuf[],
1232  const int sendCount,
1233  long recvBuf[],
1234  const int recvCounts[],
1235  const int displs[],
1236  const int root,
1237  const Comm<int>& comm)
1238 {
1239  gathervImpl<long> (sendBuf, sendCount, recvBuf, recvCounts, displs, root, comm);
1240 }
1241 
1242 template<>
1243 void
1244 reduceAll<int, long> (const Comm<int>& comm,
1245  const EReductionType reductType,
1246  const int count,
1247  const long sendBuffer[],
1248  long globalReducts[])
1249 {
1250  TEUCHOS_COMM_TIME_MONITOR(
1251  "Teuchos::reduceAll<int, long> (" << count << ", "
1252  << toString (reductType) << ")"
1253  );
1254  reduceAllImpl<long> (comm, reductType, count, sendBuffer, globalReducts);
1255 }
1256 
1257 template<>
1258 RCP<Teuchos::CommRequest<int> >
1259 ireceive<int, long> (const Comm<int>& comm,
1260  const ArrayRCP<long>& recvBuffer,
1261  const int sourceRank)
1262 {
1263  TEUCHOS_COMM_TIME_MONITOR("ireceive<int, long>");
1264  return ireceiveImpl<long> (comm, recvBuffer, sourceRank);
1265 }
1266 
1267 template<>
1268 RCP<Teuchos::CommRequest<int> >
1269 ireceive<int, long> (const ArrayRCP<long>& recvBuffer,
1270  const int sourceRank,
1271  const int tag,
1272  const Comm<int>& comm)
1273 {
1274  TEUCHOS_COMM_TIME_MONITOR("ireceive<int, long>");
1275  return ireceiveImpl<long> (recvBuffer, sourceRank, tag, comm);
1276 }
1277 
1278 template<>
1279 TEUCHOSCOMM_LIB_DLL_EXPORT void
1280 send<int, long> (const Comm<int>& comm,
1281  const int count,
1282  const long sendBuffer[],
1283  const int destRank)
1284 {
1285  return sendImpl<long> (comm, count, sendBuffer, destRank);
1286 }
1287 
1288 template<>
1289 TEUCHOSCOMM_LIB_DLL_EXPORT void
1290 send<int, long> (const long sendBuffer[],
1291  const int count,
1292  const int destRank,
1293  const int tag,
1294  const Comm<int>& comm)
1295 {
1296  return sendImpl<long> (sendBuffer, count, destRank, tag, comm);
1297 }
1298 
1299 template<>
1300 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<Teuchos::CommRequest<int> >
1301 isend (const ArrayRCP<const long>& sendBuffer,
1302  const int destRank,
1303  const int tag,
1304  const Comm<int>& comm)
1305 {
1306  return isendImpl<long> (sendBuffer, destRank, tag, comm);
1307 }
1308 
1309 // Specialization for Ordinal=int and Packet=int.
1310 template<>
1311 void
1312 gather<int, int> (const int sendBuf[],
1313  const int sendCount,
1314  int recvBuf[],
1315  const int recvCount,
1316  const int root,
1317  const Comm<int>& comm)
1318 {
1319  gatherImpl<int> (sendBuf, sendCount, recvBuf, recvCount, root, comm);
1320 }
1321 
1322 template<>
1323 void
1324 gatherv<int, int> (const int sendBuf[],
1325  const int sendCount,
1326  int recvBuf[],
1327  const int recvCounts[],
1328  const int displs[],
1329  const int root,
1330  const Comm<int>& comm)
1331 {
1332  gathervImpl<int> (sendBuf, sendCount, recvBuf, recvCounts, displs, root, comm);
1333 }
1334 
1335 template<>
1336 void
1337 reduceAll<int, int> (const Comm<int>& comm,
1338  const EReductionType reductType,
1339  const int count,
1340  const int sendBuffer[],
1341  int globalReducts[])
1342 {
1343  TEUCHOS_COMM_TIME_MONITOR(
1344  "Teuchos::reduceAll<int, int> (" << count << ", "
1345  << toString (reductType) << ")"
1346  );
1347  reduceAllImpl<int> (comm, reductType, count, sendBuffer, globalReducts);
1348 }
1349 
1350 template<>
1351 void
1352 reduceAllAndScatter<int, int> (const Comm<int>& comm,
1353  const EReductionType reductType,
1354  const int sendCount,
1355  const int sendBuffer[],
1356  const int recvCounts[],
1357  int myGlobalReducts[])
1358 {
1359  reduceAllAndScatterImpl<int> (comm, reductType, sendCount, sendBuffer,
1360  recvCounts, myGlobalReducts);
1361 }
1362 
1363 template<>
1364 RCP<Teuchos::CommRequest<int> >
1365 ireceive<int, int> (const Comm<int>& comm,
1366  const ArrayRCP<int>& recvBuffer,
1367  const int sourceRank)
1368 {
1369  TEUCHOS_COMM_TIME_MONITOR("ireceive<int, int>");
1370  return ireceiveImpl<int> (comm, recvBuffer, sourceRank);
1371 }
1372 
1373 template<>
1374 RCP<Teuchos::CommRequest<int> >
1375 ireceive<int, int> (const ArrayRCP<int>& recvBuffer,
1376  const int sourceRank,
1377  const int tag,
1378  const Comm<int>& comm)
1379 {
1380  TEUCHOS_COMM_TIME_MONITOR("ireceive<int, int>");
1381  return ireceiveImpl<int> (recvBuffer, sourceRank, tag, comm);
1382 }
1383 
1384 template<>
1385 TEUCHOSCOMM_LIB_DLL_EXPORT void
1386 send<int, int> (const Comm<int>& comm,
1387  const int count,
1388  const int sendBuffer[],
1389  const int destRank)
1390 {
1391  return sendImpl<int> (comm, count, sendBuffer, destRank);
1392 }
1393 
1394 template<>
1395 TEUCHOSCOMM_LIB_DLL_EXPORT void
1396 send<int, int> (const int sendBuffer[],
1397  const int count,
1398  const int destRank,
1399  const int tag,
1400  const Comm<int>& comm)
1401 {
1402  return sendImpl<int> (sendBuffer, count, destRank, tag, comm);
1403 }
1404 
1405 template<>
1406 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<Teuchos::CommRequest<int> >
1407 isend (const ArrayRCP<const int>& sendBuffer,
1408  const int destRank,
1409  const int tag,
1410  const Comm<int>& comm)
1411 {
1412  return isendImpl<int> (sendBuffer, destRank, tag, comm);
1413 }
1414 
1415 // Specialization for Ordinal=int and Packet=short.
1416 template<>
1417 void
1418 gather<int, short> (const short sendBuf[],
1419  const int sendCount,
1420  short recvBuf[],
1421  const int recvCount,
1422  const int root,
1423  const Comm<int>& comm)
1424 {
1425  gatherImpl<short> (sendBuf, sendCount, recvBuf, recvCount, root, comm);
1426 }
1427 
1428 template<>
1429 void
1430 gatherv<int, short> (const short sendBuf[],
1431  const int sendCount,
1432  short recvBuf[],
1433  const int recvCounts[],
1434  const int displs[],
1435  const int root,
1436  const Comm<int>& comm)
1437 {
1438  gathervImpl<short> (sendBuf, sendCount, recvBuf, recvCounts, displs, root, comm);
1439 }
1440 
1441 template<>
1442 void
1443 reduceAll<int, short> (const Comm<int>& comm,
1444  const EReductionType reductType,
1445  const int count,
1446  const short sendBuffer[],
1447  short globalReducts[])
1448 {
1449  TEUCHOS_COMM_TIME_MONITOR(
1450  "Teuchos::reduceAll<int, short> (" << count << ", "
1451  << toString (reductType) << ")"
1452  );
1453  reduceAllImpl<short> (comm, reductType, count, sendBuffer, globalReducts);
1454 }
1455 
1456 template<>
1457 RCP<Teuchos::CommRequest<int> >
1458 ireceive<int, short> (const Comm<int>& comm,
1459  const ArrayRCP<short>& recvBuffer,
1460  const int sourceRank)
1461 {
1462  TEUCHOS_COMM_TIME_MONITOR("ireceive<int, short>");
1463  return ireceiveImpl<short> (comm, recvBuffer, sourceRank);
1464 }
1465 
1466 template<>
1467 RCP<Teuchos::CommRequest<int> >
1468 ireceive<int, short> (const ArrayRCP<short>& recvBuffer,
1469  const int sourceRank,
1470  const int tag,
1471  const Comm<int>& comm)
1472 {
1473  TEUCHOS_COMM_TIME_MONITOR("ireceive<int, short>");
1474  return ireceiveImpl<short> (recvBuffer, sourceRank, tag, comm);
1475 }
1476 
1477 template<>
1478 TEUCHOSCOMM_LIB_DLL_EXPORT void
1479 send<int, short> (const Comm<int>& comm,
1480  const int count,
1481  const short sendBuffer[],
1482  const int destRank)
1483 {
1484  return sendImpl<short> (comm, count, sendBuffer, destRank);
1485 }
1486 
1487 template<>
1488 TEUCHOSCOMM_LIB_DLL_EXPORT void
1489 send<int, short> (const short sendBuffer[],
1490  const int count,
1491  const int destRank,
1492  const int tag,
1493  const Comm<int>& comm)
1494 {
1495  return sendImpl<short> (sendBuffer, count, destRank, tag, comm);
1496 }
1497 
1498 template<>
1499 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<Teuchos::CommRequest<int> >
1500 isend (const ArrayRCP<const short>& sendBuffer,
1501  const int destRank,
1502  const int tag,
1503  const Comm<int>& comm)
1504 {
1505  return isendImpl<short> (sendBuffer, destRank, tag, comm);
1506 }
1507 
1508 // mfh 18 Oct 2012: The specialization for Packet=char seems to be
1509 // causing problems such as the following:
1510 //
1511 // http://testing.sandia.gov/cdash/testDetails.php?test=9909246&build=747699
1512 //
1513 // I am disabling it for now. This should revert back to the old
1514 // behavior for Packet=char. That should fix the Tpetra errors, since
1515 // many Tpetra objects inherit from DistObject<char, ...>.
1516 #if 0
1517 // Specialization for Ordinal=int and Packet=char.
1518 template<>
1519 void
1520 reduceAll<int, char> (const Comm<int>& comm,
1521  const EReductionType reductType,
1522  const int count,
1523  const char sendBuffer[],
1524  char globalReducts[])
1525 {
1526  TEUCHOS_COMM_TIME_MONITOR(
1527  "Teuchos::reduceAll<int, char> (" << count << ", "
1528  << toString (reductType) << ")"
1529  );
1530  reduceAllImpl<char> (comm, reductType, count, sendBuffer, globalReducts);
1531 }
1532 #endif // 0
1533 
1534 } // namespace Teuchos
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
TEUCHOS_DEPRECATED void reduceAll(const Comm< Ordinal > &comm, const EReductionType reductType, const Packet &send, Packet *globalReduct)
Deprecated .
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos, as well as a number of utility routines.