Teuchos - Trilinos Tools Package  Version of the Day
Teuchos_CommHelpers.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_COMM_HELPERS_HPP
43 #define TEUCHOS_COMM_HELPERS_HPP
44 
45 #include "Teuchos_Comm.hpp"
46 #include "Teuchos_CommUtilities.hpp"
47 #include "Teuchos_SerializationTraitsHelpers.hpp"
48 #include "Teuchos_ReductionOpHelpers.hpp"
49 #include "Teuchos_SerializerHelpers.hpp"
50 #include "Teuchos_ScalarTraits.hpp"
52 #include "Teuchos_Array.hpp"
54 #include "Teuchos_Workspace.hpp"
55 #include "Teuchos_as.hpp"
56 
57 #ifdef HAVE_MPI
59 #endif // HAVE_MPI
60 #include "Teuchos_DefaultSerialComm.hpp"
61 
62 
63 namespace Teuchos {
64 
65 
66 //
67 // Teuchos::Comm Helper Functions
68 //
69 
80 };
81 
86 const char* toString (const EReductionType reductType);
87 
92 template<typename Ordinal>
93 int rank(const Comm<Ordinal>& comm);
94 
99 template<typename Ordinal>
100 int size(const Comm<Ordinal>& comm);
101 
106 template<typename Ordinal>
107 void barrier(const Comm<Ordinal>& comm);
108 
113 template<typename Ordinal, typename Packet>
114 void broadcast(
115  const Comm<Ordinal>& comm,
116  const int rootRank,
117  const Ordinal count, Packet buffer[]
118  );
119 
124 template<typename Ordinal, typename Packet>
125 void broadcast(
126  const Comm<Ordinal>& comm,
127  const int rootRank,
128  const ArrayView<Packet> &buffer
129  );
130 
135 template<typename Ordinal, typename Packet>
136 void broadcast(
137  const Comm<Ordinal>& comm,
138  const int rootRank, Packet *object
139  );
140 
145 template<typename Ordinal, typename Packet>
146 void broadcast(
147  const Comm<Ordinal>& comm,
148  const int rootRank, const Ptr<Packet> &object
149  );
150 
155 template<typename Ordinal, typename Packet>
156 void broadcast(
157  const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
158  const int rootRank, const Ordinal count, Packet*const buffer[]
159  );
160 
165 template<typename Ordinal, typename Packet>
166 void broadcast(
167  const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
168  const int rootRank, const ArrayView<const Ptr<Packet> > &buffer
169  );
170 
176 template<typename Ordinal, typename Packet, typename Serializer>
177 void broadcast(
178  const Comm<Ordinal>& comm,
179  const Serializer& serializer,
180  const int rootRank,
181  const Ordinal count, Packet buffer[]
182  );
183 
188 template<typename Ordinal, typename Packet>
189 void
190 gather (const Packet sendBuf[],
191  const Ordinal sendCount,
192  Packet recvBuf[],
193  const Ordinal recvCount,
194  const int root,
195  const Comm<Ordinal>& comm);
196 
201 template<typename Ordinal, typename Packet>
202 void
203 gatherv (const Packet sendBuf[],
204  const Ordinal sendCount,
205  Packet recvBuf[],
206  const Ordinal recvCounts[],
207  const Ordinal displs[],
208  const int root,
209  const Comm<Ordinal>& comm);
210 
216 template<typename Ordinal, typename Packet>
217 void gatherAll(
218  const Comm<Ordinal>& comm,
219  const Ordinal sendCount, const Packet sendBuffer[],
220  const Ordinal recvCount, Packet recvBuffer[]
221  );
222 
228 template<typename Ordinal, typename Packet>
229 void gatherAll(
230  const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
231  const Ordinal sendCount, const Packet*const sendBuffer[],
232  const Ordinal recvCount, Packet*const recvBuffer[]
233  );
234 
240 template<typename Ordinal, typename Packet, typename Serializer>
241 void gatherAll(
242  const Comm<Ordinal>& comm,
243  const Serializer& serializer,
244  const Ordinal sendCount, const Packet sendBuffer[],
245  const Ordinal recvCount, Packet recvBuffer[]
246  );
247 
274 template<typename Ordinal, typename Packet>
275 void
276 scatter (const Packet sendBuf[],
277  const Ordinal sendCount,
278  Packet recvBuf[],
279  const Ordinal recvCount,
280  const Ordinal root,
281  const Comm<Ordinal>& comm)
282 {
283  // See Bug 6375; Tpetra does not actually need any specializations
284  // other than Ordinal = int and Packet = int. We may add them later
285  // if there is interest.
287  (true, std::logic_error, "Teuchos::scatter<" <<
289  << ">: Generic version is not yet implemented. This function currently "
290  "only has an implementtion for Ordinal = int and Packet = int. "
291  "See Bug 6375 and Bug 6336.");
292 }
293 
321 template<typename Ordinal, typename Packet>
322 void
323 reduce (const Packet sendBuf[],
324  Packet recvBuf[],
325  const Ordinal count,
326  const EReductionType reductType,
327  const Ordinal root,
328  const Comm<Ordinal>& comm);
329 
352 template<typename Ordinal, typename Packet>
353 void reduceAll(
354  const Comm<Ordinal>& comm, const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
355  const Ordinal count, const Packet sendBuffer[], Packet globalReducts[]
356  );
357 
363 template<typename Ordinal, typename Packet>
364 void reduceAll(
365  const Comm<Ordinal>& comm, const EReductionType reductType,
366  const Ordinal count, const Packet sendBuffer[], Packet globalReducts[]
367  );
368 
374 template<typename Ordinal, typename Packet>
375 void reduceAll(
376  const Comm<Ordinal>& comm, const EReductionType reductType,
377  const Packet &send, const Ptr<Packet> &globalReduct
378  );
379 
381 template<typename Ordinal, typename Packet>
382 TEUCHOS_DEPRECATED void reduceAll(
383  const Comm<Ordinal>& comm, const EReductionType reductType,
384  const Packet &send, Packet *globalReduct
385  )
386 {
387  reduceAll<Ordinal,Packet>(comm, reductType, send, ptr(globalReduct));
388 }
389 
395 template<typename Ordinal, typename Packet>
396 void reduceAll(
397  const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
398  const ReferenceTypeReductionOp<Ordinal,Packet> &reductOp,
399  const Ordinal count, const Packet*const sendBuffer[], Packet*const globalReducts[]
400  );
401 
407 template<typename Ordinal, typename Packet, typename Serializer>
408 void reduceAll(
409  const Comm<Ordinal>& comm,
410  const Serializer& serializer,
411  const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
412  const Ordinal count, const Packet sendBuffer[], Packet globalReducts[]
413  );
414 
420 template<typename Ordinal, typename Packet, typename Serializer>
421 void reduceAll(
422  const Comm<Ordinal>& comm,
423  const Serializer& serializer,
424  const EReductionType reductType,
425  const Ordinal count, const Packet sendBuffer[], Packet globalReducts[]
426  );
427 
433 template<typename Ordinal, typename Packet>
434 void scan(
435  const Comm<Ordinal>& comm, const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
436  const Ordinal count, const Packet sendBuffer[], Packet scanReducts[]
437  );
438 
444 template<typename Ordinal, typename Packet>
445 void scan(
446  const Comm<Ordinal>& comm, const EReductionType reductType,
447  const Ordinal count, const Packet sendBuffer[], Packet scanReducts[]
448  );
449 
455 template<typename Ordinal, typename Packet>
456 void scan(
457  const Comm<Ordinal>& comm, const EReductionType reductType,
458  const Packet &send, const Ptr<Packet> &scanReduct
459  );
460 
462 template<typename Ordinal, typename Packet>
463 TEUCHOS_DEPRECATED void scan(
464  const Comm<Ordinal>& comm, const EReductionType reductType,
465  const Packet &send, Packet *scanReduct
466  )
467 {
468  scan(comm, reductType, send, ptr(scanReduct));
469 }
470 
476 template<typename Ordinal, typename Packet>
477 void scan(
478  const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
479  const ReferenceTypeReductionOp<Ordinal,Packet> &reductOp,
480  const Ordinal count, const Packet*const sendBuffer[], Packet*const scanReducts[]
481  );
482 
488 template<typename Ordinal, typename Packet, typename Serializer>
489 void scan(
490  const Comm<Ordinal>& comm,
491  const Serializer& serializer,
492  const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
493  const Ordinal count, const Packet sendBuffer[], Packet scanReducts[]
494  );
495 
501 template<typename Ordinal, typename Packet, typename Serializer>
502 void scan(
503  const Comm<Ordinal>& comm,
504  const Serializer& serializer,
505  const EReductionType reductType,
506  const Ordinal count, const Packet sendBuffer[], Packet scanReducts[]
507  );
508 
513 template<typename Ordinal, typename Packet>
514 void send(
515  const Comm<Ordinal>& comm,
516  const Ordinal count, const Packet sendBuffer[], const int destRank
517  );
518 
520 template<typename Ordinal, typename Packet>
521 void
522 send (const Packet sendBuffer[],
523  const Ordinal count,
524  const int destRank,
525  const int tag,
526  const Comm<Ordinal>& comm);
527 
532 template<typename Ordinal, typename Packet>
533 void ssend(
534  const Comm<Ordinal>& comm,
535  const Ordinal count, const Packet sendBuffer[], const int destRank
536  );
537 
539 template<typename Ordinal, typename Packet>
540 void
541 ssend (const Packet sendBuffer[],
542  const Ordinal count,
543  const int destRank,
544  const int tag,
545  const Comm<Ordinal>& comm);
546 
551 template<typename Ordinal, typename Packet>
552 void send(
553  const Comm<Ordinal>& comm,
554  const Packet &send, const int destRank
555  );
556 
561 template<typename Ordinal, typename Packet>
562 void ssend(
563  const Comm<Ordinal>& comm,
564  const Packet &send, const int destRank
565  );
566 
573 template<typename Ordinal, typename Packet>
574 void send(
575  const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
576  const Ordinal count, const Packet*const sendBuffer[], const int destRank
577  );
578 
584 template<typename Ordinal, typename Packet, typename Serializer>
585 void send(
586  const Comm<Ordinal>& comm,
587  const Serializer& serializer,
588  const Ordinal count, const Packet sendBuffer[], const int destRank
589  );
590 
595 template<typename Ordinal, typename Packet>
596 int receive(
597  const Comm<Ordinal>& comm,
598  const int sourceRank, const Ordinal count, Packet recvBuffer[]
599  );
600 
605 template<typename Ordinal, typename Packet>
606 int receive(
607  const Comm<Ordinal>& comm,
608  const int sourceRank, Packet *recv
609  );
610 
615 template<typename Ordinal, typename Packet>
616 int receive(
617  const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
618  const int sourceRank, const Ordinal count, Packet*const recvBuffer[]
619  );
620 
626 template<typename Ordinal, typename Packet, typename Serializer>
627 int receive(
628  const Comm<Ordinal>& comm,
629  const Serializer& serializer,
630  const int sourceRank, const Ordinal count, Packet recvBuffer[]
631  );
632 
638 template<typename Ordinal, typename Packet>
639 void readySend(
640  const Comm<Ordinal>& comm,
641  const ArrayView<const Packet> &sendBuffer,
642  const int destRank
643  );
644 
646 template<typename Ordinal, typename Packet>
647 void
648 readySend (const Packet sendBuffer[],
649  const Ordinal count,
650  const int destRank,
651  const int tag,
652  const Comm<Ordinal>& comm);
653 
658 template<typename Ordinal, typename Packet>
659 void readySend(
660  const Comm<Ordinal>& comm,
661  const Packet &send,
662  const int destRank
663  );
664 
670 template<typename Ordinal, typename Packet, typename Serializer>
671 void readySend(
672  const Comm<Ordinal>& comm,
673  const Serializer& serializer,
674  const ArrayView<const Packet> &sendBuffer,
675  const int destRank
676  );
677 
682 template<typename Ordinal, typename Packet>
683 RCP<CommRequest<Ordinal> > isend(
684  const Comm<Ordinal>& comm,
685  const ArrayRCP<const Packet> &sendBuffer,
686  const int destRank
687  );
688 
690 template<typename Ordinal, typename Packet>
691 RCP<CommRequest<Ordinal> >
692 isend (const ArrayRCP<const Packet>& sendBuffer,
693  const int destRank,
694  const int tag,
695  const Comm<Ordinal>& comm);
696 
701 template<typename Ordinal, typename Packet>
702 RCP<CommRequest<Ordinal> > isend(
703  const Comm<Ordinal>& comm,
704  const RCP<const Packet> &send,
705  const int destRank
706  );
707 
713 template<typename Ordinal, typename Packet, typename Serializer>
714 RCP<CommRequest<Ordinal> > isend(
715  const Comm<Ordinal>& comm,
716  const Serializer& serializer,
717  const ArrayRCP<const Packet> &sendBuffer,
718  const int destRank
719  );
720 
721 
722 // 2008/07/29: rabartl: ToDo: Add reference semantics version of isend!
723 
724 
734 template<typename Ordinal, typename Packet>
735 RCP<CommRequest<Ordinal> > ireceive(
736  const Comm<Ordinal>& comm,
737  const ArrayRCP<Packet> &recvBuffer,
738  const int sourceRank
739  );
740 
742 template<typename Ordinal, typename Packet>
743 RCP<CommRequest<Ordinal> >
744 ireceive (const ArrayRCP<Packet> &recvBuffer,
745  const int sourceRank,
746  const int tag,
747  const Comm<Ordinal>& comm);
748 
760 template<typename Ordinal, typename Packet>
761 RCP<CommRequest<Ordinal> > ireceive(
762  const Comm<Ordinal>& comm,
763  const RCP<Packet> &recv,
764  const int sourceRank
765  );
766 
772 template<typename Ordinal, typename Packet, typename Serializer>
773 RCP<CommRequest<Ordinal> > ireceive(
774  const Comm<Ordinal>& comm,
775  const Serializer& serializer,
776  const ArrayRCP<Packet> &recvBuffer,
777  const int sourceRank
778  );
779 
780 
781 // 2008/07/29: rabartl: ToDo: Add reference semantics version of ireceive!
782 
783 
791 template<typename Ordinal>
792 void waitAll(
793  const Comm<Ordinal>& comm,
794  const ArrayView<RCP<CommRequest<Ordinal> > > &requests
795  );
796 
826 template<typename Ordinal>
827 void
828 waitAll (const Comm<Ordinal>& comm,
829  const ArrayView<RCP<CommRequest<Ordinal> > >& requests,
830  const ArrayView<RCP<CommStatus<Ordinal> > >& statuses);
831 
855 template<typename Ordinal>
856 RCP<CommStatus<Ordinal> >
857 wait (const Comm<Ordinal>& comm, const Ptr<RCP<CommRequest<Ordinal> > >& request);
858 
859 //
860 // Standard reduction subclasses for objects that use value semantics
861 //
862 
863 
868 template<typename Ordinal, typename Packet>
869 class SumValueReductionOp : public ValueTypeReductionOp<Ordinal,Packet>
870 {
871 public:
873  void reduce(
874  const Ordinal count,
875  const Packet inBuffer[],
876  Packet inoutBuffer[]
877  ) const;
878 };
879 
880 
889 template<typename Ordinal, typename Packet>
890 class MinValueReductionOp : public ValueTypeReductionOp<Ordinal,Packet>
891 {
892 public:
894  void reduce(
895  const Ordinal count,
896  const Packet inBuffer[],
897  Packet inoutBuffer[]
898  ) const;
899 };
900 
901 
910 template<typename Ordinal, typename Packet>
911 class MaxValueReductionOp : public ValueTypeReductionOp<Ordinal,Packet>
912 {
913 public:
915  void reduce(
916  const Ordinal count,
917  const Packet inBuffer[],
918  Packet inoutBuffer[]
919  ) const;
920 };
921 
922 
927 template<typename Ordinal, typename Packet>
928 class ANDValueReductionOp : public ValueTypeReductionOp<Ordinal,Packet>
929 {
930 public:
932  void reduce(
933  const Ordinal count,
934  const Packet inBuffer[],
935  Packet inoutBuffer[]
936  ) const;
937 };
938 
939 
940 // ////////////////////////////////////////////////////////////
941 // Implementation details (not for geneal users to mess with)
942 
943 
944 //
945 // ReductionOp Utilities
946 //
947 
948 
949 namespace MixMaxUtilities {
950 
951 
952 template<bool isComparable, typename Ordinal, typename Packet>
953 class Min {};
954 
955 
956 template<typename Ordinal, typename Packet>
957 class Min<true,Ordinal,Packet> {
958 public:
959  static void min(
960  const Ordinal count,
961  const Packet inBuffer[],
962  Packet inoutBuffer[]
963  )
964  {
965  for( int i = 0; i < count; ++i )
966  inoutBuffer[i] = TEUCHOS_MIN(inoutBuffer[i],inBuffer[i]);
967  }
968 };
969 
970 
971 template<typename Ordinal, typename Packet>
972 class Min<false,Ordinal,Packet> {
973 public:
974  static void min(
975  const Ordinal,
976  const Packet[],
977  Packet[]
978  )
979  {
981  true,std::logic_error,
982  "Error, the type "<<TypeNameTraits<Packet>::name()
983  <<" does not support comparison operations!"
984  );
985  }
986 };
987 
988 
989 template<bool isComparable, typename Ordinal, typename Packet>
990 class Max {};
991 
992 
993 template<typename Ordinal, typename Packet>
994 class Max<true,Ordinal,Packet> {
995 public:
996  static void max(
997  const Ordinal count,
998  const Packet inBuffer[],
999  Packet inoutBuffer[]
1000  )
1001  {
1002  for( int i = 0; i < count; ++i )
1003  inoutBuffer[i] = TEUCHOS_MAX(inoutBuffer[i],inBuffer[i]);
1004  }
1005 };
1006 
1007 
1008 template<typename Ordinal, typename Packet>
1009 class Max<false,Ordinal,Packet> {
1010 public:
1011  static void max(
1012  const Ordinal,
1013  const Packet[],
1014  Packet[]
1015  )
1016  {
1018  true,std::logic_error,
1019  "Error, the type "<<TypeNameTraits<Packet>::name()
1020  <<" does not support comparison operations!"
1021  );
1022  }
1023 };
1024 
1025 
1026 template<bool isComparable, typename Ordinal, typename Packet>
1027 class AND {};
1028 
1029 
1030 template<typename Ordinal, typename Packet>
1031 class AND<true,Ordinal,Packet> {
1032 public:
1033  static void andOp(
1034  const Ordinal count,
1035  const Packet inBuffer[],
1036  Packet inoutBuffer[]
1037  )
1038  {
1039  for( int i = 0; i < count; ++i )
1040  inoutBuffer[i] = inoutBuffer[i] && inBuffer[i];
1041  }
1042 };
1043 
1044 
1045 template<typename Ordinal, typename Packet>
1046 class AND<false,Ordinal,Packet> {
1047 public:
1048  static void andOp(
1049  const Ordinal,
1050  const Packet[],
1051  Packet[]
1052  )
1053  {
1055  true,std::logic_error,
1056  "Error, the type "<<TypeNameTraits<Packet>::name()
1057  <<" does not support logical AND operations!"
1058  );
1059  }
1060 };
1061 
1062 
1063 } // namespace MixMaxUtilities
1064 
1065 
1066 template<typename Ordinal, typename Packet>
1068  const Ordinal count,
1069  const Packet inBuffer[],
1070  Packet inoutBuffer[]
1071  ) const
1072 {
1073  for( int i = 0; i < count; ++i )
1074  inoutBuffer[i] += inBuffer[i];
1075 }
1076 
1077 
1078 template<typename Ordinal, typename Packet>
1080  const Ordinal count,
1081  const Packet inBuffer[],
1082  Packet inoutBuffer[]
1083  ) const
1084 {
1085  typedef MixMaxUtilities::Min<ScalarTraits<Packet>::isComparable, Ordinal, Packet> min_type;
1086  min_type::min (count, inBuffer, inoutBuffer);
1087 }
1088 
1089 
1090 template<typename Ordinal, typename Packet>
1092  const Ordinal count,
1093  const Packet inBuffer[],
1094  Packet inoutBuffer[]
1095  ) const
1096 {
1097  typedef MixMaxUtilities::Max<ScalarTraits<Packet>::isComparable, Ordinal, Packet> max_type;
1098  max_type::max (count,inBuffer,inoutBuffer);
1099 }
1100 
1101 
1102 template<typename Ordinal, typename Packet>
1104  const Ordinal count,
1105  const Packet inBuffer[],
1106  Packet inoutBuffer[]
1107  ) const
1108 {
1109  typedef MixMaxUtilities::AND<ScalarTraits<Packet>::isComparable, Ordinal, Packet> and_type;
1110  and_type::andOp (count, inBuffer, inoutBuffer);
1111 }
1112 
1113 
1114 } // namespace Teuchos
1115 
1116 
1117 // //////////////////////////
1118 // Template implemenations
1119 
1120 
1121 //
1122 // ReductionOp utilities
1123 //
1124 
1125 
1126 namespace Teuchos {
1127 
1128 
1129 // Not for the general user to use! I am returning a raw ReductionOp* pointer
1130 // to avoid the overhead of using RCP. However, given the use case
1131 // this is just fine since I can just use std::auto_ptr to make sure things
1132 // are deleted correctly.
1133 //
1134 // NOTE (mfh 08 Feb 2015) std::auto_ptr has been deprecated in C++11.
1135 // I could either replace it with std::unique_ptr, or just call 'new'
1136 // and 'delete' manually. The former is less error prone, but
1137 // requires checking a macro for whether C++11 is actually enabled.
1138 // Thus, I've chosen (for now) to rewrite all the code that uses
1139 // std::auto_ptr, so that it allocates and deletes manually.
1140 template<typename Ordinal, typename Packet>
1141 ValueTypeReductionOp<Ordinal,Packet>*
1142 createOp (const EReductionType reductType)
1143 {
1144  typedef ScalarTraits<Packet> ST;
1145  switch (reductType) {
1146  case REDUCE_SUM: {
1147  return new SumValueReductionOp<Ordinal,Packet> ();
1148  }
1149  case REDUCE_MIN: {
1151  ! ST::isComparable, std::invalid_argument, "Teuchos::createOp"
1152  "(EReductionType): The Packet type " << TypeNameTraits<Packet>::name ()
1153  << " is not less-than comparable, so it does not make sense to do a "
1154  "MIN reduction with it.");
1155  return new MinValueReductionOp<Ordinal,Packet> ();
1156  }
1157  case REDUCE_MAX: {
1159  ! ST::isComparable, std::invalid_argument, "Teuchos::createOp"
1160  "(EReductionType): The Packet type " << TypeNameTraits<Packet>::name ()
1161  << " is not less-than comparable, so it does not make sense to do a "
1162  "MAX reduction with it.");
1163  return new MaxValueReductionOp<Ordinal,Packet> ();
1164  }
1165  case REDUCE_AND: {
1166  return new ANDValueReductionOp<Ordinal, Packet> ();
1167  }
1168  default:
1170  true, std::invalid_argument, "Teuchos::createOp(EReductionType): "
1171  "Invalid EReductionType value " << reductType << ". Valid values "
1172  "include REDUCE_SUM, REDUCE_MIN, REDUCE_MAX, and REDUCE_AND.");
1173  }
1174 }
1175 
1176 
1177 } // namespace Teuchos
1178 
1179 
1180 //
1181 // Teuchos::Comm wrapper functions
1182 //
1183 
1184 
1185 template<typename Ordinal>
1186 int Teuchos::rank(const Comm<Ordinal>& comm)
1187 {
1188  return comm.getRank();
1189 }
1190 
1191 
1192 template<typename Ordinal>
1193 int Teuchos::size(const Comm<Ordinal>& comm)
1194 {
1195  return comm.getSize();
1196 }
1197 
1198 
1199 template<typename Ordinal>
1200 void Teuchos::barrier(const Comm<Ordinal>& comm)
1201 {
1202  TEUCHOS_COMM_TIME_MONITOR(
1203  "Teuchos::CommHelpers: barrier<"
1204  <<OrdinalTraits<Ordinal>::name()
1205  <<">()"
1206  );
1207  comm.barrier();
1208 }
1209 
1210 
1211 template<typename Ordinal, typename Packet>
1212 void Teuchos::broadcast(
1213  const Comm<Ordinal>& comm,
1214  const int rootRank, const Ordinal count, Packet buffer[]
1215  )
1216 {
1217  TEUCHOS_COMM_TIME_MONITOR(
1218  "Teuchos::CommHelpers: broadcast<"
1219  <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
1220  <<">( value type )"
1221  );
1222  ValueTypeSerializationBuffer<Ordinal,Packet>
1223  charBuffer(count,buffer);
1224  comm.broadcast(
1225  rootRank,charBuffer.getBytes(),charBuffer.getCharBuffer()
1226  );
1227 }
1228 
1229 
1230 template<typename Ordinal, typename Packet>
1231 void Teuchos::broadcast(
1232  const Comm<Ordinal>& comm,
1233  const int rootRank,
1234  const ArrayView<Packet> &buffer
1235  )
1236 {
1237  broadcast<Ordinal, Packet>(comm, rootRank, buffer.size(), buffer.getRawPtr() );
1238 }
1239 
1240 
1241 template<typename Ordinal, typename Packet>
1242 void Teuchos::broadcast(
1243  const Comm<Ordinal>& comm,
1244  const int rootRank, Packet *object
1245  )
1246 {
1247  broadcast<Ordinal,Packet>(comm,rootRank,1,object);
1248 }
1249 
1250 
1251 template<typename Ordinal, typename Packet>
1252 void Teuchos::broadcast(
1253  const Comm<Ordinal>& comm,
1254  const int rootRank, const Ptr<Packet> &object
1255  )
1256 {
1257  broadcast<Ordinal,Packet>(comm,rootRank,1,object.getRawPtr());
1258 }
1259 
1260 
1261 template<typename Ordinal, typename Packet>
1262 void Teuchos::broadcast(
1263  const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
1264  const int rootRank, const Ordinal count, Packet*const buffer[]
1265  )
1266 {
1267  TEUCHOS_COMM_TIME_MONITOR(
1268  "Teuchos::CommHelpers: broadcast<"
1269  <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
1270  <<">( reference type )"
1271  );
1272  ReferenceTypeSerializationBuffer<Ordinal,Packet>
1273  charBuffer(serializer, count, buffer);
1274  comm.broadcast(
1275  rootRank,charBuffer.getBytes(),charBuffer.getCharBuffer()
1276  );
1277 }
1278 
1279 
1280 template<typename Ordinal, typename Packet>
1281 void Teuchos::broadcast(
1282  const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
1283  const int rootRank, const ArrayView<const Ptr<Packet> > &buffer
1284  )
1285 {
1286  Array<Packet*> bufferPtrArray;
1287  for (int i = 0; i < buffer.size(); ++i) {
1288  bufferPtrArray.push_back(buffer[i].getRawPtr());
1289  }
1290  broadcast<Ordinal,Packet>(comm, serializer, rootRank,
1291  buffer.size(), bufferPtrArray.getRawPtr());
1292 }
1293 
1294 template<typename Ordinal, typename Packet, typename Serializer>
1295 void Teuchos::broadcast(
1296  const Comm<Ordinal>& comm,
1297  const Serializer& serializer,
1298  const int rootRank, const Ordinal count, Packet buffer[]
1299  )
1300 {
1301  TEUCHOS_COMM_TIME_MONITOR(
1302  "Teuchos::CommHelpers: broadcast<"
1303  <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
1304  <<">( value type )"
1305  );
1306  ValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
1307  charBuffer(count,buffer,rcp(&serializer,false));
1308  comm.broadcast(
1309  rootRank,charBuffer.getBytes(),charBuffer.getCharBuffer()
1310  );
1311 }
1312 
1313 
1314 template<typename Ordinal, typename Packet>
1315 void Teuchos::gatherAll(
1316  const Comm<Ordinal>& comm,
1317  const Ordinal sendCount, const Packet sendBuffer[],
1318  const Ordinal recvCount, Packet recvBuffer[]
1319  )
1320 {
1321  TEUCHOS_COMM_TIME_MONITOR(
1322  "Teuchos::CommHelpers: gatherAll<"
1323  <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
1324  <<">( value type )"
1325  );
1326  ConstValueTypeSerializationBuffer<Ordinal,Packet>
1327  charSendBuffer(sendCount,sendBuffer);
1328  ValueTypeSerializationBuffer<Ordinal,Packet>
1329  charRecvBuffer(recvCount,recvBuffer);
1330  comm.gatherAll(
1331  charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
1332  ,charRecvBuffer.getBytes(),charRecvBuffer.getCharBuffer()
1333  );
1334 }
1335 
1336 template<typename Ordinal, typename Packet>
1337 void
1338 Teuchos::gather (const Packet sendBuf[],
1339  const Ordinal sendCount,
1340  Packet recvBuf[],
1341  const Ordinal recvCount,
1342  const int root,
1343  const Comm<Ordinal>& comm)
1344 {
1345  TEUCHOS_COMM_TIME_MONITOR(
1346  "Teuchos::CommHelpers: gather<"
1347  <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
1348  <<">( value type )"
1349  );
1350  ConstValueTypeSerializationBuffer<Ordinal,Packet>
1351  charSendBuffer (sendCount, sendBuf);
1352  ValueTypeSerializationBuffer<Ordinal,Packet>
1353  charRecvBuffer (recvCount, recvBuf);
1354  comm.gather (charSendBuffer.getBytes (),
1355  charSendBuffer.getCharBuffer (),
1356  charRecvBuffer.getBytes (),
1357  charRecvBuffer.getCharBuffer (),
1358  root);
1359 }
1360 
1361 template<typename Ordinal, typename Packet>
1362 void
1363 Teuchos::gatherv (const Packet sendBuf[],
1364  const Ordinal sendCount,
1365  Packet recvBuf[],
1366  const Ordinal recvCounts[],
1367  const Ordinal displs[],
1368  const int root,
1369  const Comm<Ordinal>& comm)
1370 {
1371  // Ordinal totalRecvCount = 0;
1372 
1373  // // In order to get the right output buffer length, we have to sum
1374  // // the receive counts from all the processes in the communicator.
1375  // const Ordinal numProcs = as<Ordinal> (comm->getSize ());
1376  // for (Ordinal k = 0; k < as<Ordinal> (numProcs); ++k) {
1377  // totalRecvCount += recvCounts[k];
1378  // }
1379 
1380  // // FIXME (mfh 16 Apr 2013) We also have to redo the displacements.
1381 
1382  // ConstValueTypeSerializationBuffer<Ordinal,Packet>
1383  // charSendBuffer (sendCount, sendBuf);
1384  // ValueTypeSerializationBuffer<Ordinal,Packet>
1385  // charRecvBuffer (totalRecvCount, recvBuf);
1386  // comm.gatherv (charSendBuffer.getBytes (),
1387  // charSendBuffer.getCharBuffer (),
1388  // charRecvBuffer.getBytes (),
1389  // charRecvBuffer.getCharBuffer (),
1390  // root);
1391  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
1392  "Teuchos::gatherv: The general case is not implemented.");
1393 }
1394 
1395 template<typename Ordinal, typename Packet>
1396 void Teuchos::gatherAll(
1397  const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
1398  const Ordinal sendCount, const Packet*const sendBuffer[],
1399  const Ordinal recvCount, Packet*const recvBuffer[]
1400  )
1401 {
1402  TEUCHOS_TEST_FOR_EXCEPT(true); // ToDo: Implement and test when needed!
1403 }
1404 
1405 template<typename Ordinal, typename Packet, typename Serializer>
1406 void Teuchos::gatherAll(
1407  const Comm<Ordinal>& comm,
1408  const Serializer& serializer,
1409  const Ordinal sendCount, const Packet sendBuffer[],
1410  const Ordinal recvCount, Packet recvBuffer[]
1411  )
1412 {
1413  TEUCHOS_COMM_TIME_MONITOR(
1414  "Teuchos::CommHelpers: gatherAll<"
1415  <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
1416  <<">( value type )"
1417  );
1418  ConstValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
1419  charSendBuffer(sendCount,sendBuffer,rcp(&serializer,false));
1420  ValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
1421  charRecvBuffer(recvCount,recvBuffer,rcp(&serializer,false));
1422  comm.gatherAll(
1423  charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
1424  ,charRecvBuffer.getBytes(),charRecvBuffer.getCharBuffer()
1425  );
1426 }
1427 
1428 
1429 template<typename Ordinal, typename Packet>
1430 void
1431 Teuchos::reduce (const Packet sendBuf[],
1432  Packet recvBuf[],
1433  const Ordinal count,
1434  const EReductionType reductType,
1435  const Ordinal root,
1436  const Comm<Ordinal>& comm)
1437 {
1438  // See Bug 6375; Tpetra does not actually need any specializations
1439  // other than Ordinal = int and Packet = int. We may add them later
1440  // if there is interest.
1442  (true, std::logic_error, "Teuchos::reduce<" <<
1443  TypeNameTraits<Ordinal>::name () << "," << TypeNameTraits<Packet>::name ()
1444  << ">: Generic version not implemented. We only implement this function "
1445  "for Ordinal = int and Packet = specific types.");
1446 }
1447 
1448 
1449 template<typename Ordinal, typename Packet>
1450 void Teuchos::reduceAll(
1451  const Comm<Ordinal>& comm, const ValueTypeReductionOp<Ordinal,Packet> &reductOp
1452  ,const Ordinal count, const Packet sendBuffer[], Packet globalReducts[]
1453  )
1454 {
1455  TEUCHOS_COMM_TIME_MONITOR(
1456  "Teuchos::CommHelpers: reduceAll<"
1457  <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
1458  <<">( value type, user-defined op )"
1459  );
1460  ConstValueTypeSerializationBuffer<Ordinal,Packet>
1461  charSendBuffer(count,sendBuffer);
1462  ValueTypeSerializationBuffer<Ordinal,Packet>
1463  charGlobalReducts(count,globalReducts);
1464  CharToValueTypeReductionOp<Ordinal,Packet>
1465  charReductOp(rcp(&reductOp,false));
1466  comm.reduceAll(
1467  charReductOp,charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
1468  ,charGlobalReducts.getCharBuffer()
1469  );
1470 }
1471 
1472 
1473 template<typename Ordinal, typename Packet>
1474 void Teuchos::reduceAll(
1475  const Comm<Ordinal>& comm, const EReductionType reductType,
1476  const Ordinal count, const Packet sendBuffer[], Packet globalReducts[]
1477  )
1478 {
1479  TEUCHOS_COMM_TIME_MONITOR(
1480  "Teuchos::CommHelpers: reduceAll<"
1481  <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
1482  <<">( value type, "<<toString(reductType)<<" )"
1483  );
1484 
1485  ValueTypeReductionOp<Ordinal,Packet>* reductOp =
1486  createOp<Ordinal, Packet> (reductType);
1487  try {
1488  reduceAll(comm,*reductOp,count,sendBuffer,globalReducts);
1489  }
1490  catch (std::exception& e) {
1491  delete reductOp;
1492  throw e;
1493  }
1494  delete reductOp;
1495 }
1496 
1497 
1498 namespace Teuchos {
1499 
1500 // amb 11 Nov 2014. I am disabling these specializations for
1501 // now. MPI_C_DOUBLE_COMPLEX is causing a problem in some builds. This code was
1502 // effectively turned on only yesterday (10 Nov 2014) when TEUCHOS_HAVE_COMPLEX
1503 // was corrected to be HAVE_TEUCHOS_COMPLEX, so evidently there are no users of
1504 // these specializations.
1505 #if 0
1506 #ifdef HAVE_TEUCHOS_COMPLEX
1507 // Specialization for Ordinal=int and Packet=std::complex<double>.
1508 template<>
1509 TEUCHOSCOMM_LIB_DLL_EXPORT void
1510 reduceAll<int, std::complex<double> > (const Comm<int>& comm,
1511  const EReductionType reductType,
1512  const int count,
1513  const std::complex<double> sendBuffer[],
1514  std::complex<double> globalReducts[]);
1515 template<>
1516 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1517 ireceive<int, std::complex<double> > (const Comm<int>& comm,
1518  const ArrayRCP<std::complex<double> >& recvBuffer,
1519  const int sourceRank);
1520 template<>
1521 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1522 ireceive<int, std::complex<double> > (const ArrayRCP<std::complex<double> > &recvBuffer,
1523  const int sourceRank,
1524  const int tag,
1525  const Comm<int>& comm);
1526 template<>
1527 TEUCHOSCOMM_LIB_DLL_EXPORT void
1528 send<int, std::complex<double> > (const Comm<int>& comm,
1529  const int count,
1530  const std::complex<double> sendBuffer[],
1531  const int destRank);
1532 template<>
1533 TEUCHOSCOMM_LIB_DLL_EXPORT void
1534 send<int, std::complex<double> > (const std::complex<double> sendBuffer[],
1535  const int count,
1536  const int destRank,
1537  const int tag,
1538  const Comm<int>& comm);
1539 template<>
1540 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1541 isend<int, std::complex<double> > (const ArrayRCP<const std::complex<double> >& sendBuffer,
1542  const int destRank,
1543  const int tag,
1544  const Comm<int>& comm);
1545 
1546 // Specialization for Ordinal=int and Packet=std::complex<float>.
1547 template<>
1548 TEUCHOSCOMM_LIB_DLL_EXPORT void
1549 reduceAll<int, std::complex<float> > (const Comm<int>& comm,
1550  const EReductionType reductType,
1551  const int count,
1552  const std::complex<float> sendBuffer[],
1553  std::complex<float> globalReducts[]);
1554 template<>
1555 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1556 ireceive<int, std::complex<float> > (const Comm<int>& comm,
1557  const ArrayRCP<std::complex<float> >& recvBuffer,
1558  const int sourceRank);
1559 template<>
1560 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1561 ireceive<int, std::complex<float> > (const ArrayRCP<std::complex<float> > &recvBuffer,
1562  const int sourceRank,
1563  const int tag,
1564  const Comm<int>& comm);
1565 template<>
1566 TEUCHOSCOMM_LIB_DLL_EXPORT void
1567 send<int, std::complex<float> > (const Comm<int>& comm,
1568  const int count,
1569  const std::complex<float> sendBuffer[],
1570  const int destRank);
1571 template<>
1572 TEUCHOSCOMM_LIB_DLL_EXPORT void
1573 send<int, std::complex<float> > (const std::complex<float> sendBuffer[],
1574  const int count,
1575  const int destRank,
1576  const int tag,
1577  const Comm<int>& comm);
1578 template<>
1579 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1580 isend<int, std::complex<float> > (const ArrayRCP<const std::complex<float> >& sendBuffer,
1581  const int destRank,
1582  const int tag,
1583  const Comm<int>& comm);
1584 #endif // HAVE_TEUCHOS_COMPLEX
1585 #endif // if 0
1586 
1587 // Specialization for Ordinal=int and Packet=double.
1588 template<>
1589 TEUCHOSCOMM_LIB_DLL_EXPORT void
1590 reduceAll<int, double> (const Comm<int>& comm,
1591  const EReductionType reductType,
1592  const int count,
1593  const double sendBuffer[],
1594  double globalReducts[]);
1595 template<>
1596 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1597 ireceive<int, double> (const Comm<int>& comm,
1598  const ArrayRCP<double>& recvBuffer,
1599  const int sourceRank);
1600 template<>
1601 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1602 ireceive<int, double> (const ArrayRCP<double> &recvBuffer,
1603  const int sourceRank,
1604  const int tag,
1605  const Comm<int>& comm);
1606 template<>
1607 TEUCHOSCOMM_LIB_DLL_EXPORT void
1608 send<int, double> (const Comm<int>& comm,
1609  const int count,
1610  const double sendBuffer[],
1611  const int destRank);
1612 template<>
1613 TEUCHOSCOMM_LIB_DLL_EXPORT void
1614 send<int, double> (const double sendBuffer[],
1615  const int count,
1616  const int destRank,
1617  const int tag,
1618  const Comm<int>& comm);
1619 template<>
1620 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1621 isend<int, double> (const ArrayRCP<const double>& sendBuffer,
1622  const int destRank,
1623  const int tag,
1624  const Comm<int>& comm);
1625 
1626 // Specialization for Ordinal=int and Packet=float.
1627 template<>
1628 TEUCHOSCOMM_LIB_DLL_EXPORT void
1629 reduceAll<int, float> (const Comm<int>& comm,
1630  const EReductionType reductType,
1631  const int count,
1632  const float sendBuffer[],
1633  float globalReducts[]);
1634 template<>
1635 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1636 ireceive<int, float> (const Comm<int>& comm,
1637  const ArrayRCP<float>& recvBuffer,
1638  const int sourceRank);
1639 template<>
1640 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1641 ireceive<int, float> (const ArrayRCP<float> &recvBuffer,
1642  const int sourceRank,
1643  const int tag,
1644  const Comm<int>& comm);
1645 template<>
1646 TEUCHOSCOMM_LIB_DLL_EXPORT void
1647 send<int, float> (const Comm<int>& comm,
1648  const int count,
1649  const float sendBuffer[],
1650  const int destRank);
1651 template<>
1652 TEUCHOSCOMM_LIB_DLL_EXPORT void
1653 send<int, float> (const float sendBuffer[],
1654  const int count,
1655  const int destRank,
1656  const int tag,
1657  const Comm<int>& comm);
1658 template<>
1659 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1660 isend<int, float> (const ArrayRCP<const float>& sendBuffer,
1661  const int destRank,
1662  const int tag,
1663  const Comm<int>& comm);
1664 
1665 #ifdef HAVE_TEUCHOS_LONG_LONG_INT
1666 // Specialization for Ordinal=int and Packet=long long.
1667 template<>
1668 TEUCHOSCOMM_LIB_DLL_EXPORT void
1669 gather<int, long long> (const long long sendBuf[],
1670  const int sendCount,
1671  long long recvBuf[],
1672  const int recvCount,
1673  const int root,
1674  const Comm<int>& comm);
1675 template<>
1676 TEUCHOSCOMM_LIB_DLL_EXPORT void
1677 gatherv<int, long long> (const long long sendBuf[],
1678  const int sendCount,
1679  long long recvBuf[],
1680  const int recvCounts[],
1681  const int displs[],
1682  const int root,
1683  const Comm<int>& comm);
1684 template<>
1685 TEUCHOSCOMM_LIB_DLL_EXPORT void
1686 reduceAll<int, long long> (const Comm<int>& comm,
1687  const EReductionType reductType,
1688  const int count,
1689  const long long sendBuffer[],
1690  long long globalReducts[]);
1691 template<>
1692 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1693 ireceive<int, long long> (const Comm<int>& comm,
1694  const ArrayRCP<long long>& recvBuffer,
1695  const int sourceRank);
1696 template<>
1697 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1698 ireceive<int, long long> (const ArrayRCP<long long> &recvBuffer,
1699  const int sourceRank,
1700  const int tag,
1701  const Comm<int>& comm);
1702 template<>
1703 TEUCHOSCOMM_LIB_DLL_EXPORT void
1704 send<int, long long> (const Comm<int>& comm,
1705  const int count,
1706  const long long sendBuffer[],
1707  const int destRank);
1708 template<>
1709 TEUCHOSCOMM_LIB_DLL_EXPORT void
1710 send<int, long long> (const long long sendBuffer[],
1711  const int count,
1712  const int destRank,
1713  const int tag,
1714  const Comm<int>& comm);
1715 template<>
1716 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1717 isend<int, long long> (const ArrayRCP<const long long>& sendBuffer,
1718  const int destRank,
1719  const int tag,
1720  const Comm<int>& comm);
1721 
1722 // Specialization for Ordinal=int and Packet=unsigned long long.
1723 template<>
1724 TEUCHOSCOMM_LIB_DLL_EXPORT void
1725 gather<int, unsigned long long> (const unsigned long long sendBuf[],
1726  const int sendCount,
1727  unsigned long long recvBuf[],
1728  const int recvCount,
1729  const int root,
1730  const Comm<int>& comm);
1731 template<>
1732 TEUCHOSCOMM_LIB_DLL_EXPORT void
1733 gatherv<int, unsigned long long> (const unsigned long long sendBuf[],
1734  const int sendCount,
1735  unsigned long long recvBuf[],
1736  const int recvCounts[],
1737  const int displs[],
1738  const int root,
1739  const Comm<int>& comm);
1740 template<>
1741 TEUCHOSCOMM_LIB_DLL_EXPORT void
1742 reduceAll<int, unsigned long long> (const Comm<int>& comm,
1743  const EReductionType reductType,
1744  const int count,
1745  const unsigned long long sendBuffer[],
1746  unsigned long long globalReducts[]);
1747 template<>
1748 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1749 ireceive<int, unsigned long long> (const Comm<int>& comm,
1750  const ArrayRCP<unsigned long long>& recvBuffer,
1751  const int sourceRank);
1752 template<>
1753 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1754 ireceive<int, unsigned long long> (const ArrayRCP<unsigned long long> &recvBuffer,
1755  const int sourceRank,
1756  const int tag,
1757  const Comm<int>& comm);
1758 template<>
1759 TEUCHOSCOMM_LIB_DLL_EXPORT void
1760 send<int, unsigned long long> (const Comm<int>& comm,
1761  const int count,
1762  const unsigned long long sendBuffer[],
1763  const int destRank);
1764 template<>
1765 TEUCHOSCOMM_LIB_DLL_EXPORT void
1766 send<int, unsigned long long> (const unsigned long long sendBuffer[],
1767  const int count,
1768  const int destRank,
1769  const int tag,
1770  const Comm<int>& comm);
1771 template<>
1772 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1773 isend<int, unsigned long long> (const ArrayRCP<const unsigned long long>& sendBuffer,
1774  const int destRank,
1775  const int tag,
1776  const Comm<int>& comm);
1777 #endif // HAVE_TEUCHOS_LONG_LONG_INT
1778 
1779 // Specialization for Ordinal=int and Packet=long.
1780 template<>
1781 TEUCHOSCOMM_LIB_DLL_EXPORT void
1782 gather<int, long> (const long sendBuf[],
1783  const int sendCount,
1784  long recvBuf[],
1785  const int recvCount,
1786  const int root,
1787  const Comm<int>& comm);
1788 template<>
1789 TEUCHOSCOMM_LIB_DLL_EXPORT void
1790 gatherv<int, long> (const long sendBuf[],
1791  const int sendCount,
1792  long recvBuf[],
1793  const int recvCounts[],
1794  const int displs[],
1795  const int root,
1796  const Comm<int>& comm);
1797 template<>
1798 TEUCHOSCOMM_LIB_DLL_EXPORT void
1799 reduceAll<int, long> (const Comm<int>& comm,
1800  const EReductionType reductType,
1801  const int count,
1802  const long sendBuffer[],
1803  long globalReducts[]);
1804 template<>
1805 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1806 ireceive<int, long> (const Comm<int>& comm,
1807  const ArrayRCP<long>& recvBuffer,
1808  const int sourceRank);
1809 template<>
1810 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1811 ireceive<int, long> (const ArrayRCP<long> &recvBuffer,
1812  const int sourceRank,
1813  const int tag,
1814  const Comm<int>& comm);
1815 template<>
1816 TEUCHOSCOMM_LIB_DLL_EXPORT void
1817 send<int, long> (const Comm<int>& comm,
1818  const int count,
1819  const long sendBuffer[],
1820  const int destRank);
1821 template<>
1822 TEUCHOSCOMM_LIB_DLL_EXPORT void
1823 send<int, long> (const long sendBuffer[],
1824  const int count,
1825  const int destRank,
1826  const int tag,
1827  const Comm<int>& comm);
1828 template<>
1829 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1830 isend<int, long> (const ArrayRCP<const long>& sendBuffer,
1831  const int destRank,
1832  const int tag,
1833  const Comm<int>& comm);
1834 
1835 // Specialization for Ordinal=int and Packet=unsigned long.
1836 template<>
1837 TEUCHOSCOMM_LIB_DLL_EXPORT void
1838 gather<int, unsigned long> (const unsigned long sendBuf[],
1839  const int sendCount,
1840  unsigned long recvBuf[],
1841  const int recvCount,
1842  const int root,
1843  const Comm<int>& comm);
1844 template<>
1845 TEUCHOSCOMM_LIB_DLL_EXPORT void
1846 gatherv<int, unsigned long> (const unsigned long sendBuf[],
1847  const int sendCount,
1848  unsigned long recvBuf[],
1849  const int recvCounts[],
1850  const int displs[],
1851  const int root,
1852  const Comm<int>& comm);
1853 template<>
1854 TEUCHOSCOMM_LIB_DLL_EXPORT void
1855 reduceAll<int, unsigned long> (const Comm<int>& comm,
1856  const EReductionType reductType,
1857  const int count,
1858  const unsigned long sendBuffer[],
1859  unsigned long globalReducts[]);
1860 template<>
1861 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1862 ireceive<int, unsigned long> (const Comm<int>& comm,
1863  const ArrayRCP<unsigned long>& recvBuffer,
1864  const int sourceRank);
1865 template<>
1866 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1867 ireceive<int, unsigned long> (const ArrayRCP<unsigned long> &recvBuffer,
1868  const int sourceRank,
1869  const int tag,
1870  const Comm<int>& comm);
1871 template<>
1872 TEUCHOSCOMM_LIB_DLL_EXPORT void
1873 send<int, unsigned long> (const Comm<int>& comm,
1874  const int count,
1875  const unsigned long sendBuffer[],
1876  const int destRank);
1877 template<>
1878 TEUCHOSCOMM_LIB_DLL_EXPORT void
1879 send<int, unsigned long> (const unsigned long sendBuffer[],
1880  const int count,
1881  const int destRank,
1882  const int tag,
1883  const Comm<int>& comm);
1884 template<>
1885 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1886 isend<int, unsigned long> (const ArrayRCP<const unsigned long>& sendBuffer,
1887  const int destRank,
1888  const int tag,
1889  const Comm<int>& comm);
1890 
1891 // Specialization for Ordinal=int and Packet=int.
1892 template<>
1893 TEUCHOSCOMM_LIB_DLL_EXPORT void
1894 gather<int, int> (const int sendBuf[],
1895  const int sendCount,
1896  int recvBuf[],
1897  const int recvCount,
1898  const int root,
1899  const Comm<int>& comm);
1900 template<>
1901 TEUCHOSCOMM_LIB_DLL_EXPORT void
1902 gatherv<int, int> (const int sendBuf[],
1903  const int sendCount,
1904  int recvBuf[],
1905  const int recvCounts[],
1906  const int displs[],
1907  const int root,
1908  const Comm<int>& comm);
1909 template<>
1910 TEUCHOSCOMM_LIB_DLL_EXPORT void
1911 scatter (const int sendBuf[],
1912  const int sendCount,
1913  int recvBuf[],
1914  const int recvCount,
1915  const int root,
1916  const Comm<int>& comm);
1917 template<>
1918 TEUCHOSCOMM_LIB_DLL_EXPORT void
1919 reduce<int, int> (const int sendBuf[],
1920  int recvBuf[],
1921  const int count,
1922  const EReductionType reductType,
1923  const int root,
1924  const Comm<int>& comm);
1925 template<>
1926 TEUCHOSCOMM_LIB_DLL_EXPORT void
1927 reduceAll<int, int> (const Comm<int>& comm,
1928  const EReductionType reductType,
1929  const int count,
1930  const int sendBuffer[],
1931  int globalReducts[]);
1932 
1933 template<>
1934 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1935 ireceive<int, int> (const Comm<int>& comm,
1936  const ArrayRCP<int>& recvBuffer,
1937  const int sourceRank);
1938 template<>
1939 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1940 ireceive<int, int> (const ArrayRCP<int> &recvBuffer,
1941  const int sourceRank,
1942  const int tag,
1943  const Comm<int>& comm);
1944 template<>
1945 TEUCHOSCOMM_LIB_DLL_EXPORT void
1946 send<int, int> (const Comm<int>& comm,
1947  const int count,
1948  const int sendBuffer[],
1949  const int destRank);
1950 template<>
1951 TEUCHOSCOMM_LIB_DLL_EXPORT void
1952 send<int, int> (const int sendBuffer[],
1953  const int count,
1954  const int destRank,
1955  const int tag,
1956  const Comm<int>& comm);
1957 template<>
1958 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1959 isend<int, int> (const ArrayRCP<const int>& sendBuffer,
1960  const int destRank,
1961  const int tag,
1962  const Comm<int>& comm);
1963 
1964 // Specialization for Ordinal=int and Packet=unsigned int.
1965 template<>
1966 TEUCHOSCOMM_LIB_DLL_EXPORT void
1967 gather<int, unsigned int> (const unsigned int sendBuf[],
1968  const int sendCount,
1969  unsigned int recvBuf[],
1970  const int recvCount,
1971  const int root,
1972  const Comm<int>& comm);
1973 template<>
1974 TEUCHOSCOMM_LIB_DLL_EXPORT void
1975 gatherv<int, unsigned int> (const unsigned int sendBuf[],
1976  const int sendCount,
1977  unsigned int recvBuf[],
1978  const int recvCounts[],
1979  const int displs[],
1980  const int root,
1981  const Comm<int>& comm);
1982 template<>
1983 TEUCHOSCOMM_LIB_DLL_EXPORT void
1984 reduceAll<int, unsigned int> (const Comm<int>& comm,
1985  const EReductionType reductType,
1986  const int count,
1987  const unsigned int sendBuffer[],
1988  unsigned int globalReducts[]);
1989 template<>
1990 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1991 ireceive<int, unsigned int> (const Comm<int>& comm,
1992  const ArrayRCP<unsigned int>& recvBuffer,
1993  const int sourceRank);
1994 template<>
1995 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1996 ireceive<int, unsigned int> (const ArrayRCP<unsigned int> &recvBuffer,
1997  const int sourceRank,
1998  const int tag,
1999  const Comm<int>& comm);
2000 template<>
2001 TEUCHOSCOMM_LIB_DLL_EXPORT void
2002 send<int, unsigned int> (const Comm<int>& comm,
2003  const int count,
2004  const unsigned int sendBuffer[],
2005  const int destRank);
2006 template<>
2007 TEUCHOSCOMM_LIB_DLL_EXPORT void
2008 send<int, unsigned int> (const unsigned int sendBuffer[],
2009  const int count,
2010  const int destRank,
2011  const int tag,
2012  const Comm<int>& comm);
2013 template<>
2014 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
2015 isend<int, unsigned int> (const ArrayRCP<const unsigned int>& sendBuffer,
2016  const int destRank,
2017  const int tag,
2018  const Comm<int>& comm);
2019 
2020 // Specialization for Ordinal=int and Packet=short.
2021 template<>
2022 TEUCHOSCOMM_LIB_DLL_EXPORT void
2023 gather<int, short> (const short sendBuf[],
2024  const int sendCount,
2025  short recvBuf[],
2026  const int recvCount,
2027  const int root,
2028  const Comm<int>& comm);
2029 template<>
2030 TEUCHOSCOMM_LIB_DLL_EXPORT void
2031 gatherv<int, short> (const short sendBuf[],
2032  const int sendCount,
2033  short recvBuf[],
2034  const int recvCounts[],
2035  const int displs[],
2036  const int root,
2037  const Comm<int>& comm);
2038 template<>
2039 TEUCHOSCOMM_LIB_DLL_EXPORT void
2040 reduceAll<int, short> (const Comm<int>& comm,
2041  const EReductionType reductType,
2042  const int count,
2043  const short sendBuffer[],
2044  short globalReducts[]);
2045 template<>
2046 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
2047 ireceive<int, short> (const Comm<int>& comm,
2048  const ArrayRCP<short>& recvBuffer,
2049  const int sourceRank);
2050 template<>
2051 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
2052 ireceive<int, short> (const ArrayRCP<short> &recvBuffer,
2053  const int sourceRank,
2054  const int tag,
2055  const Comm<int>& comm);
2056 template<>
2057 TEUCHOSCOMM_LIB_DLL_EXPORT void
2058 send<int, short> (const Comm<int>& comm,
2059  const int count,
2060  const short sendBuffer[],
2061  const int destRank);
2062 template<>
2063 TEUCHOSCOMM_LIB_DLL_EXPORT void
2064 send<int, short> (const short sendBuffer[],
2065  const int count,
2066  const int destRank,
2067  const int tag,
2068  const Comm<int>& comm);
2069 template<>
2070 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
2071 isend<int, short> (const ArrayRCP<const short>& sendBuffer,
2072  const int destRank,
2073  const int tag,
2074  const Comm<int>& comm);
2075 
2076 // mfh 18 Oct 2012: The specialization for Packet=char seems to be
2077 // causing problems such as the following:
2078 //
2079 // http://testing.sandia.gov/cdash/testDetails.php?test=9909246&build=747699
2080 //
2081 // I am disabling it for now. This should revert back to the old
2082 // behavior for Packet=char. That should fix the Tpetra errors, since
2083 // many Tpetra objects inherit from DistObject<char, ...>.
2084 #if 0
2085 // Specialization for Ordinal=int and Packet=char.
2086 template<>
2087 TEUCHOSCOMM_LIB_DLL_EXPORT void
2088 reduceAll<int, char> (const Comm<int>& comm,
2089  const EReductionType reductType,
2090  const int count,
2091  const char sendBuffer[],
2092  char globalReducts[]);
2093 #endif // 0
2094 } // namespace Teuchos
2095 
2096 
2097 template<typename Ordinal, typename Packet>
2098 void Teuchos::reduceAll(
2099  const Comm<Ordinal>& comm, const EReductionType reductType
2100  ,const Packet &send, const Ptr<Packet> &globalReduct
2101  )
2102 {
2103  // mfh 17 Oct 2012: This will invoke the above specializations for
2104  // general count, so we don't need to specialize this function.
2105  reduceAll<Ordinal,Packet>(comm, reductType, 1, &send, &*globalReduct);
2106 }
2107 
2108 
2109 template<typename Ordinal, typename Packet>
2110 void Teuchos::reduceAll(
2111  const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
2112  const ReferenceTypeReductionOp<Ordinal,Packet> &reductOp,
2113  const Ordinal count, const Packet*const sendBuffer[], Packet*const globalReducts[]
2114  )
2115 {
2116  TEUCHOS_COMM_TIME_MONITOR(
2117  "Teuchos::CommHelpers: reduceAll<"
2118  <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
2119  <<">( reference type )"
2120  );
2121  ConstReferenceTypeSerializationBuffer<Ordinal,Packet>
2122  charSendBuffer(serializer,count,sendBuffer);
2123  ReferenceTypeSerializationBuffer<Ordinal,Packet>
2124  charGlobalReducts(serializer,count,globalReducts);
2125  CharToReferenceTypeReductionOp<Ordinal,Packet>
2126  charReductOp(rcp(&serializer,false),rcp(&reductOp,false));
2127  comm.reduceAll(
2128  charReductOp,charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
2129  ,charGlobalReducts.getCharBuffer()
2130  );
2131 }
2132 
2133 template<typename Ordinal, typename Packet, typename Serializer>
2134 void Teuchos::reduceAll(
2135  const Comm<Ordinal>& comm,
2136  const Serializer& serializer,
2137  const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
2138  const Ordinal count, const Packet sendBuffer[], Packet globalReducts[]
2139  )
2140 {
2141  TEUCHOS_COMM_TIME_MONITOR(
2142  "Teuchos::CommHelpers: reduceAll<"
2143  <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
2144  <<">( value type, user-defined op )"
2145  );
2146  ConstValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
2147  charSendBuffer(count,sendBuffer,rcp(&serializer,false));
2148  ValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
2149  charGlobalReducts(count,globalReducts,rcp(&serializer,false));
2150  CharToValueTypeReductionOp<Ordinal,Packet,Serializer>
2151  charReductOp(rcp(&reductOp,false),rcp(&serializer,false));
2152  comm.reduceAll(
2153  charReductOp,charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
2154  ,charGlobalReducts.getCharBuffer()
2155  );
2156 }
2157 
2158 
2159 template<typename Ordinal, typename Packet, typename Serializer>
2160 void Teuchos::reduceAll(
2161  const Comm<Ordinal>& comm,
2162  const Serializer& serializer,
2163  const EReductionType reductType,
2164  const Ordinal count, const Packet sendBuffer[], Packet globalReducts[]
2165  )
2166 {
2167  TEUCHOS_COMM_TIME_MONITOR(
2168  "Teuchos::CommHelpers: reduceAll<"
2169  <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
2170  <<">( value type, "<<toString(reductType)<<" )"
2171  );
2172 
2173  ValueTypeReductionOp<Ordinal,Packet>* reductOp =
2174  createOp<Ordinal, Packet> (reductType);
2175  try {
2176  reduceAll(comm,serializer,*reductOp,count,sendBuffer,globalReducts);
2177  }
2178  catch (std::exception& e) {
2179  delete reductOp;
2180  throw e;
2181  }
2182  delete reductOp;
2183 }
2184 
2185 
2186 template<typename Ordinal, typename Packet>
2187 void Teuchos::scan(
2188  const Comm<Ordinal>& comm, const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
2189  const Ordinal count, const Packet sendBuffer[], Packet scanReducts[]
2190  )
2191 {
2192  TEUCHOS_COMM_TIME_MONITOR(
2193  "Teuchos::CommHelpers: scan<"
2194  <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
2195  <<">( value type, user-defined op )"
2196  );
2197  ConstValueTypeSerializationBuffer<Ordinal,Packet>
2198  charSendBuffer(count,sendBuffer);
2199  ValueTypeSerializationBuffer<Ordinal,Packet>
2200  charScanReducts(count,scanReducts);
2201  CharToValueTypeReductionOp<Ordinal,Packet>
2202  charReductOp(rcp(&reductOp,false));
2203  comm.scan(
2204  charReductOp,charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
2205  ,charScanReducts.getCharBuffer()
2206  );
2207 }
2208 
2209 
2210 template<typename Ordinal, typename Packet>
2211 void Teuchos::scan(
2212  const Comm<Ordinal>& comm, const EReductionType reductType,
2213  const Ordinal count, const Packet sendBuffer[], Packet scanReducts[]
2214  )
2215 {
2216  TEUCHOS_COMM_TIME_MONITOR(
2217  "Teuchos::CommHelpers: scan<"
2218  <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
2219  <<">( value type, "<<toString(reductType)<<" )"
2220  );
2221 
2222  ValueTypeReductionOp<Ordinal,Packet>* reductOp =
2223  createOp<Ordinal, Packet> (reductType);
2224  try {
2225  scan(comm,*reductOp,count,sendBuffer,scanReducts);
2226  }
2227  catch (std::exception& e) {
2228  delete reductOp;
2229  throw e;
2230  }
2231  delete reductOp;
2232 }
2233 
2234 
2235 template<typename Ordinal, typename Packet>
2236 void Teuchos::scan(
2237  const Comm<Ordinal>& comm, const EReductionType reductType,
2238  const Packet &send, const Ptr<Packet> &scanReduct
2239  )
2240 {
2241  scan<Ordinal,Packet>(comm, reductType, 1, &send, &*scanReduct);
2242 }
2243 
2244 
2245 template<typename Ordinal, typename Packet>
2246 void Teuchos::scan(
2247  const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
2248  const ReferenceTypeReductionOp<Ordinal,Packet> &reductOp,
2249  const Ordinal count, const Packet*const sendBuffer[], Packet*const scanReducts[]
2250  )
2251 {
2252  TEUCHOS_TEST_FOR_EXCEPT(true); // ToDo: Implement and test when needed!
2253 }
2254 
2255 template<typename Ordinal, typename Packet, typename Serializer>
2256 void Teuchos::scan(
2257  const Comm<Ordinal>& comm,
2258  const Serializer& serializer,
2259  const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
2260  const Ordinal count, const Packet sendBuffer[], Packet scanReducts[]
2261  )
2262 {
2263  TEUCHOS_COMM_TIME_MONITOR(
2264  "Teuchos::CommHelpers: scan<"
2265  <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
2266  <<">( value type, user-defined op )"
2267  );
2268  ConstValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
2269  charSendBuffer(count,sendBuffer,rcp(&serializer,false));
2270  ValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
2271  charScanReducts(count,scanReducts,rcp(&serializer,false));
2272  CharToValueTypeReductionOp<Ordinal,Packet,Serializer>
2273  charReductOp(rcp(&reductOp,false),rcp(&serializer,false));
2274  comm.scan(
2275  charReductOp,charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
2276  ,charScanReducts.getCharBuffer()
2277  );
2278 }
2279 
2280 
2281 template<typename Ordinal, typename Packet, typename Serializer>
2282 void Teuchos::scan(
2283  const Comm<Ordinal>& comm,
2284  const Serializer& serializer,
2285  const EReductionType reductType,
2286  const Ordinal count, const Packet sendBuffer[], Packet scanReducts[]
2287  )
2288 {
2289  TEUCHOS_COMM_TIME_MONITOR(
2290  "Teuchos::CommHelpers: scan<"
2291  <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
2292  <<">( value type, "<<toString(reductType)<<" )"
2293  );
2294 
2295  ValueTypeReductionOp<Ordinal,Packet>* reductOp =
2296  createOp<Ordinal, Packet> (reductType);
2297  try {
2298  scan(comm,serializer,*reductOp,count,sendBuffer,scanReducts);
2299  }
2300  catch (std::exception& e) {
2301  delete reductOp;
2302  throw e;
2303  }
2304  delete reductOp;
2305 }
2306 
2307 template<typename Ordinal, typename Packet>
2308 void Teuchos::send(
2309  const Comm<Ordinal>& comm,
2310  const Ordinal count, const Packet sendBuffer[], const int destRank
2311  )
2312 {
2313  TEUCHOS_COMM_TIME_MONITOR(
2314  "Teuchos::CommHelpers: send<"
2315  <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
2316  <<">( value type )"
2317  );
2318  ConstValueTypeSerializationBuffer<Ordinal,Packet>
2319  charSendBuffer(count,sendBuffer);
2320  comm.send(
2321  charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
2322  ,destRank
2323  );
2324 }
2325 
2326 template<typename Ordinal, typename Packet>
2327 void
2328 Teuchos::send (const Packet sendBuffer[],
2329  const Ordinal count,
2330  const int destRank,
2331  const int tag,
2332  const Comm<Ordinal>& comm)
2333 {
2334  TEUCHOS_COMM_TIME_MONITOR(
2335  "Teuchos::CommHelpers: send<"
2337  <<">( value type )"
2338  );
2339  ConstValueTypeSerializationBuffer<Ordinal,Packet> charSendBuffer (count, sendBuffer);
2340  comm.send (charSendBuffer.getBytes (), charSendBuffer.getCharBuffer (), destRank, tag);
2341 }
2342 
2343 template<typename Ordinal, typename Packet>
2344 void Teuchos::ssend(
2345  const Comm<Ordinal>& comm,
2346  const Ordinal count, const Packet sendBuffer[], const int destRank
2347  )
2348 {
2349  TEUCHOS_COMM_TIME_MONITOR(
2350  "Teuchos::CommHelpers: ssend<"
2351  <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
2352  <<">( value type )"
2353  );
2354  ConstValueTypeSerializationBuffer<Ordinal,Packet>
2355  charSendBuffer(count,sendBuffer);
2356  comm.ssend(
2357  charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
2358  ,destRank
2359  );
2360 }
2361 
2362 template<typename Ordinal, typename Packet>
2363 void
2364 Teuchos::ssend (const Packet sendBuffer[],
2365  const Ordinal count,
2366  const int destRank,
2367  const int tag,
2368  const Comm<Ordinal>& comm)
2369 {
2370  TEUCHOS_COMM_TIME_MONITOR(
2371  "Teuchos::CommHelpers: ssend<"
2373  <<">( value type )"
2374  );
2376  buf_type charSendBuffer (count, sendBuffer);
2377  comm.ssend (charSendBuffer.getBytes (),
2378  charSendBuffer.getCharBuffer (),
2379  destRank, tag);
2380 }
2381 
2382 template<typename Ordinal, typename Packet>
2383 void Teuchos::send(
2384  const Comm<Ordinal>& comm,
2385  const Packet &send, const int destRank
2386  )
2387 {
2388  Teuchos::send<Ordinal,Packet>(comm,1,&send,destRank);
2389 }
2390 
2391 template<typename Ordinal, typename Packet>
2392 void Teuchos::ssend(
2393  const Comm<Ordinal>& comm,
2394  const Packet &send, const int destRank
2395  )
2396 {
2397  Teuchos::ssend<Ordinal,Packet>(comm,1,&send,destRank);
2398 }
2399 
2400 template<typename Ordinal, typename Packet>
2401 void Teuchos::send(
2402  const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
2403  const Ordinal count, const Packet*const sendBuffer[], const int destRank
2404  )
2405 {
2406  TEUCHOS_TEST_FOR_EXCEPT(true); // ToDo: Implement and test when needed!
2407 }
2408 
2409 template<typename Ordinal, typename Packet, typename Serializer>
2410 void Teuchos::send(
2411  const Comm<Ordinal>& comm,
2412  const Serializer& serializer,
2413  const Ordinal count, const Packet sendBuffer[], const int destRank
2414  )
2415 {
2416  TEUCHOS_COMM_TIME_MONITOR(
2417  "Teuchos::CommHelpers: send<"
2418  <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
2419  <<">( value type )"
2420  );
2421  ConstValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
2422  charSendBuffer(count,sendBuffer,rcp(&serializer,false));
2423  comm.send(
2424  charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
2425  ,destRank
2426  );
2427 }
2428 
2429 template<typename Ordinal, typename Packet>
2430 int Teuchos::receive(
2431  const Comm<Ordinal>& comm,
2432  const int sourceRank, const Ordinal count, Packet recvBuffer[]
2433  )
2434 {
2435  TEUCHOS_COMM_TIME_MONITOR(
2436  "Teuchos::CommHelpers: receive<"
2437  <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
2438  <<">( value type )"
2439  );
2440  ValueTypeSerializationBuffer<Ordinal,Packet>
2441  charRecvBuffer(count,recvBuffer);
2442  return comm.receive(
2443  sourceRank
2444  ,charRecvBuffer.getBytes(),charRecvBuffer.getCharBuffer()
2445  );
2446 }
2447 
2448 
2449 template<typename Ordinal, typename Packet>
2450 int Teuchos::receive(
2451  const Comm<Ordinal>& comm,
2452  const int sourceRank, Packet *recv
2453  )
2454 {
2455  return Teuchos::receive<Ordinal,Packet>(comm,sourceRank,1,recv);
2456 }
2457 
2458 
2459 template<typename Ordinal, typename Packet>
2460 int Teuchos::receive(
2461  const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
2462  const int sourceRank, const Ordinal count, Packet*const recvBuffer[]
2463  )
2464 {
2465  TEUCHOS_TEST_FOR_EXCEPT(true); // ToDo: Implement and test when needed!
2466 }
2467 
2468 template<typename Ordinal, typename Packet, typename Serializer>
2469 int Teuchos::receive(
2470  const Comm<Ordinal>& comm,
2471  const Serializer& serializer,
2472  const int sourceRank, const Ordinal count, Packet recvBuffer[]
2473  )
2474 {
2475  TEUCHOS_COMM_TIME_MONITOR(
2476  "Teuchos::CommHelpers: receive<"
2477  <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
2478  <<">( value type )"
2479  );
2480  ValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
2481  charRecvBuffer(count,recvBuffer,rcp(&serializer,false));
2482  return comm.receive(
2483  sourceRank
2484  ,charRecvBuffer.getBytes(),charRecvBuffer.getCharBuffer()
2485  );
2486 }
2487 
2488 template<typename Ordinal, typename Packet>
2489 void Teuchos::readySend(
2490  const Comm<Ordinal>& comm,
2491  const ArrayView<const Packet> &sendBuffer,
2492  const int destRank
2493  )
2494 {
2495  TEUCHOS_COMM_TIME_MONITOR(
2496  "Teuchos::CommHelpers: readySend<"
2497  <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
2498  <<">( value type )"
2499  );
2500  ConstValueTypeSerializationBuffer<Ordinal,Packet>
2501  charSendBuffer(sendBuffer.size(), sendBuffer.getRawPtr());
2502  comm.readySend( charSendBuffer.getCharBufferView(), destRank );
2503 }
2504 
2505 template<typename Ordinal, typename Packet>
2506 void
2507 Teuchos::readySend (const Packet sendBuffer[],
2508  const Ordinal count,
2509  const int destRank,
2510  const int tag,
2511  const Comm<Ordinal>& comm)
2512 {
2513  TEUCHOS_COMM_TIME_MONITOR(
2514  "Teuchos::CommHelpers: readySend<"
2516  <<">( value type )"
2517  );
2519  buf_type charSendBuffer (count, sendBuffer);
2520  comm.readySend (charSendBuffer.getBytes (),
2521  charSendBuffer.getCharBuffer (),
2522  destRank, tag);
2523 }
2524 
2525 template<typename Ordinal, typename Packet>
2526 void Teuchos::readySend(
2527  const Comm<Ordinal>& comm,
2528  const Packet &send,
2529  const int destRank
2530  )
2531 {
2532  readySend<Ordinal, Packet>( comm, arrayView(&send,1), destRank );
2533 }
2534 
2535 template<typename Ordinal, typename Packet, typename Serializer>
2536 void Teuchos::readySend(
2537  const Comm<Ordinal>& comm,
2538  const Serializer& serializer,
2539  const ArrayView<const Packet> &sendBuffer,
2540  const int destRank
2541  )
2542 {
2543  TEUCHOS_COMM_TIME_MONITOR(
2544  "Teuchos::CommHelpers: readySend<"
2545  <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
2546  <<">( value type )"
2547  );
2548  ConstValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
2549  charSendBuffer(sendBuffer.size(), sendBuffer.getRawPtr(), serializer);
2550  comm.readySend( charSendBuffer.getCharBufferView(), destRank );
2551 }
2552 
2553 template<typename Ordinal, typename Packet>
2556  const Comm<Ordinal>& comm,
2557  const ArrayRCP<const Packet> &sendBuffer,
2558  const int destRank
2559  )
2560 {
2561  TEUCHOS_COMM_TIME_MONITOR(
2562  "Teuchos::CommHelpers: isend<"
2563  <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
2564  <<">( value type )"
2565  );
2566  ConstValueTypeSerializationBuffer<Ordinal,Packet>
2567  charSendBuffer(sendBuffer.size(), sendBuffer.getRawPtr());
2568  RCP<CommRequest<Ordinal> > commRequest = comm.isend(
2569  charSendBuffer.getCharBufferView(), destRank );
2570  set_extra_data( sendBuffer, "buffer", inOutArg(commRequest) );
2571  return commRequest;
2572 }
2573 
2574 template<typename Ordinal, typename Packet>
2576 Teuchos::isend (const ArrayRCP<const Packet> &sendBuffer,
2577  const int destRank,
2578  const int tag,
2579  const Comm<Ordinal>& comm)
2580 {
2581  TEUCHOS_COMM_TIME_MONITOR(
2582  "Teuchos::isend<" << OrdinalTraits<Ordinal>::name () << ","
2583  << TypeNameTraits<Packet>::name () << ">");
2584  ConstValueTypeSerializationBuffer<Ordinal,Packet>
2585  charSendBuffer (sendBuffer.size (), sendBuffer.getRawPtr ());
2586  RCP<CommRequest<Ordinal> > commRequest =
2587  comm.isend (charSendBuffer.getCharBufferView (), destRank, tag);
2588  set_extra_data (sendBuffer, "buffer", inOutArg (commRequest));
2589  return commRequest;
2590 }
2591 
2592 template<typename Ordinal, typename Packet>
2595  const Comm<Ordinal>& comm,
2596  const RCP<const Packet> &send,
2597  const int destRank
2598  )
2599 {
2600  const ArrayRCP<const Packet> sendBuffer =
2601  arcpWithEmbeddedObj( send.get(), 0, 1, send, false );
2602  // 2008/07/29: rabartl: Above: I need to write a helper function to create
2603  // new ArrayRCP object given a single object to copy.
2604  return isend<Ordinal, Packet>( comm, sendBuffer, destRank );
2605 }
2606 
2607 template<typename Ordinal, typename Packet, typename Serializer>
2610  const Comm<Ordinal>& comm,
2611  const Serializer& serializer,
2612  const ArrayRCP<const Packet> &sendBuffer,
2613  const int destRank
2614  )
2615 {
2616  TEUCHOS_COMM_TIME_MONITOR(
2617  "Teuchos::CommHelpers: isend<"
2618  <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
2619  <<">( value type )"
2620  );
2621  ConstValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
2622  charSendBuffer(sendBuffer.size(), sendBuffer.getRawPtr(), serializer);
2623  RCP<CommRequest<Ordinal> > commRequest = comm.isend(
2624  charSendBuffer.getCharBufferView(), destRank );
2625  set_extra_data( sendBuffer, "buffer", inOutArg(commRequest) );
2626  return commRequest;
2627 }
2628 
2629 template<typename Ordinal, typename Packet>
2632  const Comm<Ordinal>& comm,
2633  const ArrayRCP<Packet> &recvBuffer,
2634  const int sourceRank
2635  )
2636 {
2637  TEUCHOS_COMM_TIME_MONITOR(
2638  "Teuchos::ireceive<int, " << "," << TypeNameTraits<Packet>::name () << ">");
2639  ValueTypeSerializationBuffer<Ordinal,Packet>
2640  charRecvBuffer(recvBuffer.size(), recvBuffer.getRawPtr());
2641  RCP<CommRequest<Ordinal> > commRequest = comm.ireceive(
2642  charRecvBuffer.getCharBufferView(), sourceRank );
2643  set_extra_data( recvBuffer, "buffer", inOutArg(commRequest) );
2644  return commRequest;
2645 }
2646 
2647 template<typename Ordinal, typename Packet>
2649 Teuchos::ireceive (const Teuchos::ArrayRCP<Packet> &recvBuffer,
2650  const int sourceRank,
2651  const int tag,
2652  const Teuchos::Comm<Ordinal>& comm)
2653 {
2654  TEUCHOS_COMM_TIME_MONITOR(
2655  "Teuchos::ireceive<int, " << "," << TypeNameTraits<Packet>::name () << ">");
2656  ValueTypeSerializationBuffer<int, Packet>
2657  charRecvBuffer (recvBuffer.size (), recvBuffer.getRawPtr ());
2658  RCP<CommRequest<int> > commRequest =
2659  comm.ireceive (charRecvBuffer.getCharBufferView (), sourceRank, tag);
2660  set_extra_data (recvBuffer, "buffer", inOutArg (commRequest));
2661  return commRequest;
2662 }
2663 
2664 template<typename Ordinal, typename Packet>
2667  const Comm<Ordinal>& comm,
2668  const RCP<Packet> &recv,
2669  const int sourceRank
2670  )
2671 {
2672  const ArrayRCP<Packet> recvBuffer =
2673  arcpWithEmbeddedObj( recv.get(), 0, 1, recv, false );
2674  // 2008/07/29: rabartl: Above: I need to write a helper function to create
2675  // new ArrayRCP object given a single object to copy.
2676  return ireceive<Ordinal, Packet>( comm, recvBuffer, sourceRank );
2677 }
2678 
2679 template<typename Ordinal, typename Packet, typename Serializer>
2682  const Comm<Ordinal>& comm,
2683  const Serializer& serializer,
2684  const ArrayRCP<Packet> &recvBuffer,
2685  const int sourceRank
2686  )
2687 {
2688  TEUCHOS_COMM_TIME_MONITOR(
2689  "Teuchos::CommHelpers: ireceive<"
2690  <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
2691  <<">( value type )"
2692  );
2693  ValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
2694  charRecvBuffer(recvBuffer.size(), recvBuffer.getRawPtr(), serializer);
2695  RCP<CommRequest<Ordinal> > commRequest = comm.ireceive(
2696  charRecvBuffer.getCharBufferView(), sourceRank );
2697  set_extra_data( recvBuffer, "buffer", inOutArg(commRequest) );
2698  return commRequest;
2699 }
2700 
2701 template<typename Ordinal>
2702 void Teuchos::waitAll(
2703  const Comm<Ordinal>& comm,
2704  const ArrayView<RCP<CommRequest<Ordinal> > > &requests
2705  )
2706 {
2707  comm.waitAll(requests);
2708 }
2709 
2710 
2711 template<typename Ordinal>
2712 void
2713 Teuchos::waitAll (const Comm<Ordinal>& comm,
2714  const ArrayView<RCP<CommRequest<Ordinal> > >& requests,
2715  const ArrayView<RCP<CommStatus<Ordinal> > >& statuses)
2716 {
2717  comm.waitAll (requests, statuses);
2718 }
2719 
2720 
2721 template<typename Ordinal>
2723 Teuchos::wait (const Comm<Ordinal>& comm,
2724  const Ptr<RCP<CommRequest<Ordinal> > > &request)
2725 {
2726  return comm.wait (request);
2727 }
2728 
2729 
2730 #endif // TEUCHOS_COMM_HELPERS_HPP
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...
ArrayView< T > arrayView(T *p, typename ArrayView< T >::size_type size)
Construct a const or non-const view to const or non-const data.
void set_extra_data(const T1 &extra_data, const std::string &name, const Ptr< ArrayRCP< T2 > > &p, EPrePostDestruction destroy_when=POST_DESTROY, bool force_unique=true)
Set extra data associated with a ArrayRCP object.
Ptr< T > inOutArg(T &arg)
create a non-persisting (required or optional) input/output argument for a function call...
virtual void readySend(const ArrayView< const char > &sendBuffer, const int destRank) const =0
Ready send of data from this process to another process.
EReductionType
Enumeration for selecting from a set of pre-defined reduction operations.
size_type size() const
The total number of entries in the array.
virtual RCP< CommRequest< Ordinal > > ireceive(const ArrayView< char > &recvBuffer, const int sourceRank) const =0
Non-blocking receive.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
virtual void send(const Ordinal bytes, const char sendBuffer[], const int destRank) const =0
Possibly blocking send of data from this process to another process.
Standard logical AND operator for booleans.
void reduceAll(const Comm< Ordinal > &comm, const ValueTypeReductionOp< Ordinal, Packet > &reductOp, const Ordinal count, const Packet sendBuffer[], Packet globalReducts[])
Wrapper for MPI_Allreduce that takes a custom reduction operator.
Standard min operator for types with value semantics.
void scatter(const Packet sendBuf[], const Ordinal sendCount, Packet recvBuf[], const Ordinal recvCount, const Ordinal root, const Comm< Ordinal > &comm)
Wrapper for MPI_Scatter; scatter collective.
std::string toString(const any &rhs)
Converts the value in any to a std::string.
void reduce(const Ordinal count, const Packet inBuffer[], Packet inoutBuffer[]) const
Standard summation operator for types with value semantics.
void send(const Comm< Ordinal > &comm, const Ordinal count, const Packet sendBuffer[], const int destRank)
Send objects that use values semantics to another process.
virtual void ssend(const Ordinal bytes, const char sendBuffer[], const int destRank) const =0
Always blocking send of data from this process to another process.
TEUCHOS_DEPRECATED void scan(const Comm< Ordinal > &comm, const EReductionType reductType, const Packet &send, Packet *scanReduct)
Deprecated.
Standard Max operator for types with value semantics.
Implementation of Teuchos wrappers for MPI.
This structure defines some basic traits for the ordinal field type.
TEUCHOS_DEPRECATED void reduceAll(const Comm< Ordinal > &comm, const EReductionType reductType, const Packet &send, Packet *globalReduct)
Deprecated .
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). ...
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).
Templated array class derived from the STL std::vector.
Abstract interface for distributed-memory communication.
Defines basic traits for the ordinal field type.
Default traits class that just returns typeid(T).name().
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos, as well as a number of utility routines.
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).
Defines basic traits for the scalar field type.
ArrayRCP< T > arcpWithEmbeddedObj(T *p, typename ArrayRCP< T >::size_type lowerOffset, typename ArrayRCP< T >::size_type size, const Embedded &embedded, bool owns_mem=true)
Create an ArrayRCP with and also put in an embedded object.
Smart reference counting pointer class for automatic garbage collection.
RCP< CommRequest< Ordinal > > isend(const ArrayRCP< const Packet > &sendBuffer, const int destRank, const int tag, const Comm< Ordinal > &comm)
Variant of isend() that takes a tag (and restores the correct order of arguments).
T * getRawPtr() const
Get the raw C++ pointer to the underlying object.
void reduce(const Ordinal count, const Packet inBuffer[], Packet inoutBuffer[]) const
void reduce(const Ordinal count, const Packet inBuffer[], Packet inoutBuffer[]) const
Defines basic traits returning the name of a type in a portable and readable way. ...
Definition of Teuchos::as, for conversions between types.
void reduce(const Ordinal count, const Packet inBuffer[], Packet inoutBuffer[]) 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.
#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...
void scan(const Comm< Ordinal > &comm, const ValueTypeReductionOp< Ordinal, Packet > &reductOp, const Ordinal count, const Packet sendBuffer[], Packet scanReducts[])
Scan/Reduce array of objects that use value semantics using a user-defined reduction operator...
Encapsulate how an array of const objects with value sematics is serialized into a const char[] array...
Reference-counted smart pointer for managing arrays.