Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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_TEUCHOS_MPI
59 #endif // HAVE_TEUCHOS_MPI
60 #include "Teuchos_DefaultSerialComm.hpp"
62 
63 namespace Teuchos {
64 
65 //
66 // Teuchos::Comm Helper Functions
67 //
68 
69 #ifdef HAVE_TEUCHOS_MPI
70 namespace Details {
71 
78 std::string getMpiErrorString (const int errCode);
79 
80 } // namespace Details
81 #endif // HAVE_TEUCHOS_MPI
82 
87 template<typename Ordinal>
88 int rank(const Comm<Ordinal>& comm);
89 
94 template<typename Ordinal>
95 int size(const Comm<Ordinal>& comm);
96 
101 template<typename Ordinal>
102 void barrier(const Comm<Ordinal>& comm);
103 
108 template<typename Ordinal, typename Packet>
109 void broadcast(
110  const Comm<Ordinal>& comm,
111  const int rootRank,
112  const Ordinal count, Packet buffer[]
113  );
114 
119 template<typename Ordinal, typename Packet>
120 void broadcast(
121  const Comm<Ordinal>& comm,
122  const int rootRank,
123  const ArrayView<Packet> &buffer
124  );
125 
130 template<typename Ordinal, typename Packet>
131 void broadcast(
132  const Comm<Ordinal>& comm,
133  const int rootRank, Packet *object
134  );
135 
140 template<typename Ordinal, typename Packet>
141 void broadcast(
142  const Comm<Ordinal>& comm,
143  const int rootRank, const Ptr<Packet> &object
144  );
145 
150 template<typename Ordinal, typename Packet>
151 void broadcast(
152  const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
153  const int rootRank, const Ordinal count, Packet*const buffer[]
154  );
155 
160 template<typename Ordinal, typename Packet>
161 void broadcast(
162  const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
163  const int rootRank, const ArrayView<const Ptr<Packet> > &buffer
164  );
165 
171 template<typename Ordinal, typename Packet, typename Serializer>
172 void broadcast(
173  const Comm<Ordinal>& comm,
174  const Serializer& serializer,
175  const int rootRank,
176  const Ordinal count, Packet buffer[]
177  );
178 
183 template<typename Ordinal, typename Packet>
184 void
185 gather (const Packet sendBuf[],
186  const Ordinal sendCount,
187  Packet recvBuf[],
188  const Ordinal recvCount,
189  const int root,
190  const Comm<Ordinal>& comm);
191 
196 template<typename Ordinal, typename Packet>
197 void
198 gatherv (const Packet sendBuf[],
199  const Ordinal sendCount,
200  Packet recvBuf[],
201  const Ordinal recvCounts[],
202  const Ordinal displs[],
203  const int root,
204  const Comm<Ordinal>& comm);
205 
211 template<typename Ordinal, typename Packet>
212 void gatherAll(
213  const Comm<Ordinal>& comm,
214  const Ordinal sendCount, const Packet sendBuffer[],
215  const Ordinal recvCount, Packet recvBuffer[]
216  );
217 
223 template<typename Ordinal, typename Packet>
224 void gatherAll(
225  const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
226  const Ordinal sendCount, const Packet*const sendBuffer[],
227  const Ordinal recvCount, Packet*const recvBuffer[]
228  );
229 
235 template<typename Ordinal, typename Packet, typename Serializer>
236 void gatherAll(
237  const Comm<Ordinal>& comm,
238  const Serializer& serializer,
239  const Ordinal sendCount, const Packet sendBuffer[],
240  const Ordinal recvCount, Packet recvBuffer[]
241  );
242 
269 template<typename Ordinal, typename Packet>
270 void
271 scatter (const Packet sendBuf[],
272  const Ordinal sendCount,
273  Packet recvBuf[],
274  const Ordinal recvCount,
275  const Ordinal root,
276  const Comm<Ordinal>& comm)
277 {
278  // See Bug 6375; Tpetra does not actually need any specializations
279  // other than Ordinal = int and Packet = int. We may add them later
280  // if there is interest.
282  (true, std::logic_error, "Teuchos::scatter<" <<
284  << ">: Generic version is not yet implemented. This function currently "
285  "only has an implementtion for Ordinal = int and Packet = int. "
286  "See Bug 6375 and Bug 6336.");
287 }
288 
316 template<typename Ordinal, typename Packet>
317 void
318 reduce (const Packet sendBuf[],
319  Packet recvBuf[],
320  const Ordinal count,
321  const EReductionType reductType,
322  const Ordinal root,
323  const Comm<Ordinal>& comm);
324 
347 template<typename Ordinal, typename Packet>
348 void reduceAll(
349  const Comm<Ordinal>& comm, const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
350  const Ordinal count, const Packet sendBuffer[], Packet globalReducts[]
351  );
352 
358 template<typename Ordinal, typename Packet>
359 void reduceAll(
360  const Comm<Ordinal>& comm, const EReductionType reductType,
361  const Ordinal count, const Packet sendBuffer[], Packet globalReducts[]
362  );
363 
369 template<typename Ordinal, typename Packet>
370 void reduceAll(
371  const Comm<Ordinal>& comm, const EReductionType reductType,
372  const Packet &send, const Ptr<Packet> &globalReduct
373  );
374 
376 template<typename Ordinal, typename Packet>
377 TEUCHOS_DEPRECATED void reduceAll(
378  const Comm<Ordinal>& comm, const EReductionType reductType,
379  const Packet &send, Packet *globalReduct
380  )
381 {
382  reduceAll<Ordinal,Packet>(comm, reductType, send, ptr(globalReduct));
383 }
384 
390 template<typename Ordinal, typename Packet>
391 void reduceAll(
392  const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
393  const ReferenceTypeReductionOp<Ordinal,Packet> &reductOp,
394  const Ordinal count, const Packet*const sendBuffer[], Packet*const globalReducts[]
395  );
396 
402 template<typename Ordinal, typename Packet, typename Serializer>
403 void reduceAll(
404  const Comm<Ordinal>& comm,
405  const Serializer& serializer,
406  const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
407  const Ordinal count, const Packet sendBuffer[], Packet globalReducts[]
408  );
409 
415 template<typename Ordinal, typename Packet, typename Serializer>
416 void reduceAll(
417  const Comm<Ordinal>& comm,
418  const Serializer& serializer,
419  const EReductionType reductType,
420  const Ordinal count, const Packet sendBuffer[], Packet globalReducts[]
421  );
422 
428 template<typename Ordinal, typename Packet>
429 void scan(
430  const Comm<Ordinal>& comm, const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
431  const Ordinal count, const Packet sendBuffer[], Packet scanReducts[]
432  );
433 
439 template<typename Ordinal, typename Packet>
440 void scan(
441  const Comm<Ordinal>& comm, const EReductionType reductType,
442  const Ordinal count, const Packet sendBuffer[], Packet scanReducts[]
443  );
444 
450 template<typename Ordinal, typename Packet>
451 void scan(
452  const Comm<Ordinal>& comm, const EReductionType reductType,
453  const Packet &send, const Ptr<Packet> &scanReduct
454  );
455 
457 template<typename Ordinal, typename Packet>
458 TEUCHOS_DEPRECATED void scan(
459  const Comm<Ordinal>& comm, const EReductionType reductType,
460  const Packet &send, Packet *scanReduct
461  )
462 {
463  scan(comm, reductType, send, ptr(scanReduct));
464 }
465 
471 template<typename Ordinal, typename Packet>
472 void scan(
473  const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
474  const ReferenceTypeReductionOp<Ordinal,Packet> &reductOp,
475  const Ordinal count, const Packet*const sendBuffer[], Packet*const scanReducts[]
476  );
477 
483 template<typename Ordinal, typename Packet, typename Serializer>
484 void scan(
485  const Comm<Ordinal>& comm,
486  const Serializer& serializer,
487  const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
488  const Ordinal count, const Packet sendBuffer[], Packet scanReducts[]
489  );
490 
496 template<typename Ordinal, typename Packet, typename Serializer>
497 void scan(
498  const Comm<Ordinal>& comm,
499  const Serializer& serializer,
500  const EReductionType reductType,
501  const Ordinal count, const Packet sendBuffer[], Packet scanReducts[]
502  );
503 
508 template<typename Ordinal, typename Packet>
509 void send(
510  const Comm<Ordinal>& comm,
511  const Ordinal count, const Packet sendBuffer[], const int destRank
512  );
513 
515 template<typename Ordinal, typename Packet>
516 void
517 send (const Packet sendBuffer[],
518  const Ordinal count,
519  const int destRank,
520  const int tag,
521  const Comm<Ordinal>& comm);
522 
527 template<typename Ordinal, typename Packet>
528 void ssend(
529  const Comm<Ordinal>& comm,
530  const Ordinal count, const Packet sendBuffer[], const int destRank
531  );
532 
534 template<typename Ordinal, typename Packet>
535 void
536 ssend (const Packet sendBuffer[],
537  const Ordinal count,
538  const int destRank,
539  const int tag,
540  const Comm<Ordinal>& comm);
541 
546 template<typename Ordinal, typename Packet>
547 void send(
548  const Comm<Ordinal>& comm,
549  const Packet &send, const int destRank
550  );
551 
556 template<typename Ordinal, typename Packet>
557 void ssend(
558  const Comm<Ordinal>& comm,
559  const Packet &send, const int destRank
560  );
561 
568 template<typename Ordinal, typename Packet>
569 void send(
570  const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
571  const Ordinal count, const Packet*const sendBuffer[], const int destRank
572  );
573 
579 template<typename Ordinal, typename Packet, typename Serializer>
580 void send(
581  const Comm<Ordinal>& comm,
582  const Serializer& serializer,
583  const Ordinal count, const Packet sendBuffer[], const int destRank
584  );
585 
590 template<typename Ordinal, typename Packet>
591 int receive(
592  const Comm<Ordinal>& comm,
593  const int sourceRank, const Ordinal count, Packet recvBuffer[]
594  );
595 
600 template<typename Ordinal, typename Packet>
601 int receive(
602  const Comm<Ordinal>& comm,
603  const int sourceRank, Packet *recv
604  );
605 
610 template<typename Ordinal, typename Packet>
611 int receive(
612  const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
613  const int sourceRank, const Ordinal count, Packet*const recvBuffer[]
614  );
615 
621 template<typename Ordinal, typename Packet, typename Serializer>
622 int receive(
623  const Comm<Ordinal>& comm,
624  const Serializer& serializer,
625  const int sourceRank, const Ordinal count, Packet recvBuffer[]
626  );
627 
633 template<typename Ordinal, typename Packet>
634 void readySend(
635  const Comm<Ordinal>& comm,
636  const ArrayView<const Packet> &sendBuffer,
637  const int destRank
638  );
639 
641 template<typename Ordinal, typename Packet>
642 void
643 readySend (const Packet sendBuffer[],
644  const Ordinal count,
645  const int destRank,
646  const int tag,
647  const Comm<Ordinal>& comm);
648 
653 template<typename Ordinal, typename Packet>
654 void readySend(
655  const Comm<Ordinal>& comm,
656  const Packet &send,
657  const int destRank
658  );
659 
665 template<typename Ordinal, typename Packet, typename Serializer>
666 void readySend(
667  const Comm<Ordinal>& comm,
668  const Serializer& serializer,
669  const ArrayView<const Packet> &sendBuffer,
670  const int destRank
671  );
672 
677 template<typename Ordinal, typename Packet>
678 RCP<CommRequest<Ordinal> > isend(
679  const Comm<Ordinal>& comm,
680  const ArrayRCP<const Packet> &sendBuffer,
681  const int destRank
682  );
683 
685 template<typename Ordinal, typename Packet>
686 RCP<CommRequest<Ordinal> >
687 isend (const ArrayRCP<const Packet>& sendBuffer,
688  const int destRank,
689  const int tag,
690  const Comm<Ordinal>& comm);
691 
696 template<typename Ordinal, typename Packet>
697 RCP<CommRequest<Ordinal> > isend(
698  const Comm<Ordinal>& comm,
699  const RCP<const Packet> &send,
700  const int destRank
701  );
702 
708 template<typename Ordinal, typename Packet, typename Serializer>
709 RCP<CommRequest<Ordinal> > isend(
710  const Comm<Ordinal>& comm,
711  const Serializer& serializer,
712  const ArrayRCP<const Packet> &sendBuffer,
713  const int destRank
714  );
715 
716 
717 // 2008/07/29: rabartl: ToDo: Add reference semantics version of isend!
718 
719 
729 template<typename Ordinal, typename Packet>
730 RCP<CommRequest<Ordinal> > ireceive(
731  const Comm<Ordinal>& comm,
732  const ArrayRCP<Packet> &recvBuffer,
733  const int sourceRank
734  );
735 
737 template<typename Ordinal, typename Packet>
738 RCP<CommRequest<Ordinal> >
739 ireceive (const ArrayRCP<Packet> &recvBuffer,
740  const int sourceRank,
741  const int tag,
742  const Comm<Ordinal>& comm);
743 
755 template<typename Ordinal, typename Packet>
756 RCP<CommRequest<Ordinal> > ireceive(
757  const Comm<Ordinal>& comm,
758  const RCP<Packet> &recv,
759  const int sourceRank
760  );
761 
767 template<typename Ordinal, typename Packet, typename Serializer>
768 RCP<CommRequest<Ordinal> > ireceive(
769  const Comm<Ordinal>& comm,
770  const Serializer& serializer,
771  const ArrayRCP<Packet> &recvBuffer,
772  const int sourceRank
773  );
774 
775 
776 // 2008/07/29: rabartl: ToDo: Add reference semantics version of ireceive!
777 
778 
786 template<typename Ordinal>
787 void waitAll(
788  const Comm<Ordinal>& comm,
789  const ArrayView<RCP<CommRequest<Ordinal> > > &requests
790  );
791 
821 template<typename Ordinal>
822 void
823 waitAll (const Comm<Ordinal>& comm,
824  const ArrayView<RCP<CommRequest<Ordinal> > >& requests,
825  const ArrayView<RCP<CommStatus<Ordinal> > >& statuses);
826 
850 template<typename Ordinal>
851 RCP<CommStatus<Ordinal> >
852 wait (const Comm<Ordinal>& comm, const Ptr<RCP<CommRequest<Ordinal> > >& request);
853 
854 //
855 // Standard reduction subclasses for objects that use value semantics
856 //
857 
858 
863 template<typename Ordinal, typename Packet>
864 class SumValueReductionOp : public ValueTypeReductionOp<Ordinal,Packet>
865 {
866 public:
868  void reduce(
869  const Ordinal count,
870  const Packet inBuffer[],
871  Packet inoutBuffer[]
872  ) const;
873 };
874 
875 
884 template<typename Ordinal, typename Packet>
885 class MinValueReductionOp : public ValueTypeReductionOp<Ordinal,Packet>
886 {
887 public:
889  void reduce(
890  const Ordinal count,
891  const Packet inBuffer[],
892  Packet inoutBuffer[]
893  ) const;
894 };
895 
896 
905 template<typename Ordinal, typename Packet>
906 class MaxValueReductionOp : public ValueTypeReductionOp<Ordinal,Packet>
907 {
908 public:
910  void reduce(
911  const Ordinal count,
912  const Packet inBuffer[],
913  Packet inoutBuffer[]
914  ) const;
915 };
916 
917 
922 template<typename Ordinal, typename Packet>
923 class ANDValueReductionOp : public ValueTypeReductionOp<Ordinal,Packet>
924 {
925 public:
927  void reduce(
928  const Ordinal count,
929  const Packet inBuffer[],
930  Packet inoutBuffer[]
931  ) const;
932 };
933 
934 
935 // ////////////////////////////////////////////////////////////
936 // Implementation details (not for geneal users to mess with)
937 
938 
939 //
940 // ReductionOp Utilities
941 //
942 
943 
944 namespace MixMaxUtilities {
945 
946 
947 template<bool isComparable, typename Ordinal, typename Packet>
948 class Min {};
949 
950 
951 template<typename Ordinal, typename Packet>
952 class Min<true,Ordinal,Packet> {
953 public:
954  static void min(
955  const Ordinal count,
956  const Packet inBuffer[],
957  Packet inoutBuffer[]
958  )
959  {
960  for( int i = 0; i < count; ++i )
961  inoutBuffer[i] = TEUCHOS_MIN(inoutBuffer[i],inBuffer[i]);
962  }
963 };
964 
965 
966 template<typename Ordinal, typename Packet>
967 class Min<false,Ordinal,Packet> {
968 public:
969  static void min(
970  const Ordinal,
971  const Packet[],
972  Packet[]
973  )
974  {
976  true,std::logic_error,
977  "Error, the type "<<TypeNameTraits<Packet>::name()
978  <<" does not support comparison operations!"
979  );
980  }
981 };
982 
983 
984 template<bool isComparable, typename Ordinal, typename Packet>
985 class Max {};
986 
987 
988 template<typename Ordinal, typename Packet>
989 class Max<true,Ordinal,Packet> {
990 public:
991  static void max(
992  const Ordinal count,
993  const Packet inBuffer[],
994  Packet inoutBuffer[]
995  )
996  {
997  for( int i = 0; i < count; ++i )
998  inoutBuffer[i] = TEUCHOS_MAX(inoutBuffer[i],inBuffer[i]);
999  }
1000 };
1001 
1002 
1003 template<typename Ordinal, typename Packet>
1004 class Max<false,Ordinal,Packet> {
1005 public:
1006  static void max(
1007  const Ordinal,
1008  const Packet[],
1009  Packet[]
1010  )
1011  {
1013  true,std::logic_error,
1014  "Error, the type "<<TypeNameTraits<Packet>::name()
1015  <<" does not support comparison operations!"
1016  );
1017  }
1018 };
1019 
1020 
1021 template<bool isComparable, typename Ordinal, typename Packet>
1022 class AND {};
1023 
1024 
1025 template<typename Ordinal, typename Packet>
1026 class AND<true,Ordinal,Packet> {
1027 public:
1028  static void andOp(
1029  const Ordinal count,
1030  const Packet inBuffer[],
1031  Packet inoutBuffer[]
1032  )
1033  {
1034  for( int i = 0; i < count; ++i )
1035  inoutBuffer[i] = inoutBuffer[i] && inBuffer[i];
1036  }
1037 };
1038 
1039 
1040 template<typename Ordinal, typename Packet>
1041 class AND<false,Ordinal,Packet> {
1042 public:
1043  static void andOp(
1044  const Ordinal,
1045  const Packet[],
1046  Packet[]
1047  )
1048  {
1050  true,std::logic_error,
1051  "Error, the type "<<TypeNameTraits<Packet>::name()
1052  <<" does not support logical AND operations!"
1053  );
1054  }
1055 };
1056 
1057 
1058 } // namespace MixMaxUtilities
1059 
1060 
1061 template<typename Ordinal, typename Packet>
1063  const Ordinal count,
1064  const Packet inBuffer[],
1065  Packet inoutBuffer[]
1066  ) const
1067 {
1068  for( int i = 0; i < count; ++i )
1069  inoutBuffer[i] += inBuffer[i];
1070 }
1071 
1072 
1073 template<typename Ordinal, typename Packet>
1075  const Ordinal count,
1076  const Packet inBuffer[],
1077  Packet inoutBuffer[]
1078  ) const
1079 {
1080  typedef MixMaxUtilities::Min<ScalarTraits<Packet>::isComparable, Ordinal, Packet> min_type;
1081  min_type::min (count, inBuffer, inoutBuffer);
1082 }
1083 
1084 
1085 template<typename Ordinal, typename Packet>
1087  const Ordinal count,
1088  const Packet inBuffer[],
1089  Packet inoutBuffer[]
1090  ) const
1091 {
1092  typedef MixMaxUtilities::Max<ScalarTraits<Packet>::isComparable, Ordinal, Packet> max_type;
1093  max_type::max (count,inBuffer,inoutBuffer);
1094 }
1095 
1096 
1097 template<typename Ordinal, typename Packet>
1099  const Ordinal count,
1100  const Packet inBuffer[],
1101  Packet inoutBuffer[]
1102  ) const
1103 {
1104  typedef MixMaxUtilities::AND<ScalarTraits<Packet>::isComparable, Ordinal, Packet> and_type;
1105  and_type::andOp (count, inBuffer, inoutBuffer);
1106 }
1107 
1108 
1109 } // namespace Teuchos
1110 
1111 
1112 // //////////////////////////
1113 // Template implemenations
1114 
1115 
1116 //
1117 // ReductionOp utilities
1118 //
1119 
1120 
1121 namespace Teuchos {
1122 
1123 
1124 // Not for the general user to use! I am returning a raw ReductionOp* pointer
1125 // to avoid the overhead of using RCP. However, given the use case
1126 // this is just fine since I can just use std::auto_ptr to make sure things
1127 // are deleted correctly.
1128 //
1129 // NOTE (mfh 08 Feb 2015) std::auto_ptr has been deprecated in C++11.
1130 // I could either replace it with std::unique_ptr, or just call 'new'
1131 // and 'delete' manually. The former is less error prone, but
1132 // requires checking a macro for whether C++11 is actually enabled.
1133 // Thus, I've chosen (for now) to rewrite all the code that uses
1134 // std::auto_ptr, so that it allocates and deletes manually.
1135 template<typename Ordinal, typename Packet>
1136 ValueTypeReductionOp<Ordinal,Packet>*
1137 createOp (const EReductionType reductType)
1138 {
1139  typedef ScalarTraits<Packet> ST;
1140  switch (reductType) {
1141  case REDUCE_SUM: {
1142  return new SumValueReductionOp<Ordinal,Packet> ();
1143  }
1144  case REDUCE_MIN: {
1145  if (ST::isComparable) {
1146  return new MinValueReductionOp<Ordinal,Packet> ();
1147  }
1148  else {
1150  (! ST::isComparable, std::invalid_argument, "Teuchos::createOp"
1151  "(EReductionType): The Packet type " << TypeNameTraits<Packet>::name ()
1152  << " is not less-than comparable, so it does not make sense to do a "
1153  "MIN reduction with it.");
1154  }
1155  }
1156  case REDUCE_MAX: {
1157  if (ST::isComparable) {
1158  return new MaxValueReductionOp<Ordinal,Packet> ();
1159  }
1160  else {
1162  (! ST::isComparable, std::invalid_argument, "Teuchos::createOp"
1163  "(EReductionType): The Packet type " << TypeNameTraits<Packet>::name ()
1164  << " is not less-than comparable, so it does not make sense to do a "
1165  "MAX reduction with it.");
1166  }
1167  }
1168  case REDUCE_AND: {
1169  return new ANDValueReductionOp<Ordinal, Packet> ();
1170  }
1171  default:
1173  true, std::invalid_argument, "Teuchos::createOp(EReductionType): "
1174  "Invalid EReductionType value " << reductType << ". Valid values "
1175  "include REDUCE_SUM, REDUCE_MIN, REDUCE_MAX, and REDUCE_AND.");
1176  }
1177 }
1178 
1179 
1180 } // namespace Teuchos
1181 
1182 
1183 //
1184 // Teuchos::Comm wrapper functions
1185 //
1186 
1187 
1188 template<typename Ordinal>
1189 int Teuchos::rank(const Comm<Ordinal>& comm)
1190 {
1191  return comm.getRank();
1192 }
1193 
1194 
1195 template<typename Ordinal>
1196 int Teuchos::size(const Comm<Ordinal>& comm)
1197 {
1198  return comm.getSize();
1199 }
1200 
1201 
1202 template<typename Ordinal>
1203 void Teuchos::barrier(const Comm<Ordinal>& comm)
1204 {
1205  TEUCHOS_COMM_TIME_MONITOR(
1206  "Teuchos::CommHelpers: barrier<"
1207  <<OrdinalTraits<Ordinal>::name()
1208  <<">()"
1209  );
1210  comm.barrier();
1211 }
1212 
1213 
1214 template<typename Ordinal, typename Packet>
1215 void Teuchos::broadcast(
1216  const Comm<Ordinal>& comm,
1217  const int rootRank, const Ordinal count, Packet buffer[]
1218  )
1219 {
1220  TEUCHOS_COMM_TIME_MONITOR(
1221  "Teuchos::CommHelpers: broadcast<"
1222  <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
1223  <<">( value type )"
1224  );
1225  ValueTypeSerializationBuffer<Ordinal,Packet>
1226  charBuffer(count,buffer);
1227  comm.broadcast(
1228  rootRank,charBuffer.getBytes(),charBuffer.getCharBuffer()
1229  );
1230 }
1231 
1232 
1233 template<typename Ordinal, typename Packet>
1234 void Teuchos::broadcast(
1235  const Comm<Ordinal>& comm,
1236  const int rootRank,
1237  const ArrayView<Packet> &buffer
1238  )
1239 {
1240  broadcast<Ordinal, Packet>(comm, rootRank, buffer.size(), buffer.getRawPtr() );
1241 }
1242 
1243 
1244 template<typename Ordinal, typename Packet>
1245 void Teuchos::broadcast(
1246  const Comm<Ordinal>& comm,
1247  const int rootRank, Packet *object
1248  )
1249 {
1250  broadcast<Ordinal,Packet>(comm,rootRank,1,object);
1251 }
1252 
1253 
1254 template<typename Ordinal, typename Packet>
1255 void Teuchos::broadcast(
1256  const Comm<Ordinal>& comm,
1257  const int rootRank, const Ptr<Packet> &object
1258  )
1259 {
1260  broadcast<Ordinal,Packet>(comm,rootRank,1,object.getRawPtr());
1261 }
1262 
1263 
1264 template<typename Ordinal, typename Packet>
1265 void Teuchos::broadcast(
1266  const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
1267  const int rootRank, const Ordinal count, Packet*const buffer[]
1268  )
1269 {
1270  TEUCHOS_COMM_TIME_MONITOR(
1271  "Teuchos::CommHelpers: broadcast<"
1272  <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
1273  <<">( reference type )"
1274  );
1275  ReferenceTypeSerializationBuffer<Ordinal,Packet>
1276  charBuffer(serializer, count, buffer);
1277  comm.broadcast(
1278  rootRank,charBuffer.getBytes(),charBuffer.getCharBuffer()
1279  );
1280 }
1281 
1282 
1283 template<typename Ordinal, typename Packet>
1284 void Teuchos::broadcast(
1285  const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
1286  const int rootRank, const ArrayView<const Ptr<Packet> > &buffer
1287  )
1288 {
1289  Array<Packet*> bufferPtrArray;
1290  for (int i = 0; i < buffer.size(); ++i) {
1291  bufferPtrArray.push_back(buffer[i].getRawPtr());
1292  }
1293  broadcast<Ordinal,Packet>(comm, serializer, rootRank,
1294  buffer.size(), bufferPtrArray.getRawPtr());
1295 }
1296 
1297 template<typename Ordinal, typename Packet, typename Serializer>
1298 void Teuchos::broadcast(
1299  const Comm<Ordinal>& comm,
1300  const Serializer& serializer,
1301  const int rootRank, const Ordinal count, Packet buffer[]
1302  )
1303 {
1304  TEUCHOS_COMM_TIME_MONITOR(
1305  "Teuchos::CommHelpers: broadcast<"
1306  <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
1307  <<">( value type )"
1308  );
1309  ValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
1310  charBuffer(count,buffer,rcp(&serializer,false));
1311  comm.broadcast(
1312  rootRank,charBuffer.getBytes(),charBuffer.getCharBuffer()
1313  );
1314 }
1315 
1316 
1317 template<typename Ordinal, typename Packet>
1318 void Teuchos::gatherAll(
1319  const Comm<Ordinal>& comm,
1320  const Ordinal sendCount, const Packet sendBuffer[],
1321  const Ordinal recvCount, Packet recvBuffer[]
1322  )
1323 {
1324  TEUCHOS_COMM_TIME_MONITOR(
1325  "Teuchos::CommHelpers: gatherAll<"
1326  <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
1327  <<">( value type )"
1328  );
1329  ConstValueTypeSerializationBuffer<Ordinal,Packet>
1330  charSendBuffer(sendCount,sendBuffer);
1331  ValueTypeSerializationBuffer<Ordinal,Packet>
1332  charRecvBuffer(recvCount,recvBuffer);
1333  comm.gatherAll(
1334  charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
1335  ,charRecvBuffer.getBytes(),charRecvBuffer.getCharBuffer()
1336  );
1337 }
1338 
1339 template<typename Ordinal, typename Packet>
1340 void
1341 Teuchos::gather (const Packet sendBuf[],
1342  const Ordinal sendCount,
1343  Packet recvBuf[],
1344  const Ordinal recvCount,
1345  const int root,
1346  const Comm<Ordinal>& comm)
1347 {
1348  TEUCHOS_COMM_TIME_MONITOR(
1349  "Teuchos::CommHelpers: gather<"
1350  <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
1351  <<">( value type )"
1352  );
1353  ConstValueTypeSerializationBuffer<Ordinal,Packet>
1354  charSendBuffer (sendCount, sendBuf);
1355  ValueTypeSerializationBuffer<Ordinal,Packet>
1356  charRecvBuffer (recvCount, recvBuf);
1357  comm.gather (charSendBuffer.getBytes (),
1358  charSendBuffer.getCharBuffer (),
1359  charRecvBuffer.getBytes (),
1360  charRecvBuffer.getCharBuffer (),
1361  root);
1362 }
1363 
1364 template<typename Ordinal, typename Packet>
1365 void
1366 Teuchos::gatherv (const Packet sendBuf[],
1367  const Ordinal sendCount,
1368  Packet recvBuf[],
1369  const Ordinal recvCounts[],
1370  const Ordinal displs[],
1371  const int root,
1372  const Comm<Ordinal>& comm)
1373 {
1374  // Ordinal totalRecvCount = 0;
1375 
1376  // // In order to get the right output buffer length, we have to sum
1377  // // the receive counts from all the processes in the communicator.
1378  // const Ordinal numProcs = as<Ordinal> (comm->getSize ());
1379  // for (Ordinal k = 0; k < as<Ordinal> (numProcs); ++k) {
1380  // totalRecvCount += recvCounts[k];
1381  // }
1382 
1383  // // FIXME (mfh 16 Apr 2013) We also have to redo the displacements.
1384 
1385  // ConstValueTypeSerializationBuffer<Ordinal,Packet>
1386  // charSendBuffer (sendCount, sendBuf);
1387  // ValueTypeSerializationBuffer<Ordinal,Packet>
1388  // charRecvBuffer (totalRecvCount, recvBuf);
1389  // comm.gatherv (charSendBuffer.getBytes (),
1390  // charSendBuffer.getCharBuffer (),
1391  // charRecvBuffer.getBytes (),
1392  // charRecvBuffer.getCharBuffer (),
1393  // root);
1394  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
1395  "Teuchos::gatherv: The general case is not implemented.");
1396 }
1397 
1398 template<typename Ordinal, typename Packet>
1399 void Teuchos::gatherAll(
1400  const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
1401  const Ordinal sendCount, const Packet*const sendBuffer[],
1402  const Ordinal recvCount, Packet*const recvBuffer[]
1403  )
1404 {
1405  TEUCHOS_TEST_FOR_EXCEPT(true); // ToDo: Implement and test when needed!
1406 }
1407 
1408 template<typename Ordinal, typename Packet, typename Serializer>
1409 void Teuchos::gatherAll(
1410  const Comm<Ordinal>& comm,
1411  const Serializer& serializer,
1412  const Ordinal sendCount, const Packet sendBuffer[],
1413  const Ordinal recvCount, Packet recvBuffer[]
1414  )
1415 {
1416  TEUCHOS_COMM_TIME_MONITOR(
1417  "Teuchos::CommHelpers: gatherAll<"
1418  <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
1419  <<">( value type )"
1420  );
1421  ConstValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
1422  charSendBuffer(sendCount,sendBuffer,rcp(&serializer,false));
1423  ValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
1424  charRecvBuffer(recvCount,recvBuffer,rcp(&serializer,false));
1425  comm.gatherAll(
1426  charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
1427  ,charRecvBuffer.getBytes(),charRecvBuffer.getCharBuffer()
1428  );
1429 }
1430 
1431 
1432 template<typename Ordinal, typename Packet>
1433 void
1434 Teuchos::reduce (const Packet sendBuf[],
1435  Packet recvBuf[],
1436  const Ordinal count,
1437  const EReductionType reductType,
1438  const Ordinal root,
1439  const Comm<Ordinal>& comm)
1440 {
1441  // See Bug 6375; Tpetra does not actually need any specializations
1442  // other than Ordinal = int and Packet = int. We may add them later
1443  // if there is interest.
1445  (true, std::logic_error, "Teuchos::reduce<" <<
1446  TypeNameTraits<Ordinal>::name () << "," << TypeNameTraits<Packet>::name ()
1447  << ">: Generic version not implemented. We only implement this function "
1448  "for Ordinal = int and Packet = specific types.");
1449 }
1450 
1451 
1452 template<typename Ordinal, typename Packet>
1453 void Teuchos::reduceAll(
1454  const Comm<Ordinal>& comm, const ValueTypeReductionOp<Ordinal,Packet> &reductOp
1455  ,const Ordinal count, const Packet sendBuffer[], Packet globalReducts[]
1456  )
1457 {
1458  TEUCHOS_COMM_TIME_MONITOR(
1459  "Teuchos::CommHelpers: reduceAll<"
1460  <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
1461  <<">( value type, user-defined op )"
1462  );
1463  ConstValueTypeSerializationBuffer<Ordinal,Packet>
1464  charSendBuffer(count,sendBuffer);
1465  ValueTypeSerializationBuffer<Ordinal,Packet>
1466  charGlobalReducts(count,globalReducts);
1467  CharToValueTypeReductionOp<Ordinal,Packet>
1468  charReductOp(rcp(&reductOp,false));
1469  comm.reduceAll(
1470  charReductOp,charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
1471  ,charGlobalReducts.getCharBuffer()
1472  );
1473 }
1474 
1475 
1476 template<typename Ordinal, typename Packet>
1477 void Teuchos::reduceAll(
1478  const Comm<Ordinal>& comm, const EReductionType reductType,
1479  const Ordinal count, const Packet sendBuffer[], Packet globalReducts[]
1480  )
1481 {
1482  TEUCHOS_COMM_TIME_MONITOR(
1483  "Teuchos::CommHelpers: reduceAll<"
1484  <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
1485  <<">( value type, "<<toString(reductType)<<" )"
1486  );
1487 
1488  ValueTypeReductionOp<Ordinal,Packet>* reductOp =
1489  createOp<Ordinal, Packet> (reductType);
1490  try {
1491  reduceAll(comm,*reductOp,count,sendBuffer,globalReducts);
1492  }
1493  catch (std::exception& e) {
1494  delete reductOp;
1495  throw e;
1496  }
1497  delete reductOp;
1498 }
1499 
1500 
1501 namespace Teuchos {
1502 
1503 // amb 11 Nov 2014. I am disabling these specializations for
1504 // now. MPI_C_DOUBLE_COMPLEX is causing a problem in some builds. This code was
1505 // effectively turned on only yesterday (10 Nov 2014) when TEUCHOS_HAVE_COMPLEX
1506 // was corrected to be HAVE_TEUCHOS_COMPLEX, so evidently there are no users of
1507 // these specializations.
1508 #if 0
1509 #ifdef HAVE_TEUCHOS_COMPLEX
1510 // Specialization for Ordinal=int and Packet=std::complex<double>.
1511 template<>
1512 TEUCHOSCOMM_LIB_DLL_EXPORT void
1513 reduceAll<int, std::complex<double> > (const Comm<int>& comm,
1514  const EReductionType reductType,
1515  const int count,
1516  const std::complex<double> sendBuffer[],
1517  std::complex<double> globalReducts[]);
1518 template<>
1519 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1520 ireceive<int, std::complex<double> > (const Comm<int>& comm,
1521  const ArrayRCP<std::complex<double> >& recvBuffer,
1522  const int sourceRank);
1523 template<>
1524 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1525 ireceive<int, std::complex<double> > (const ArrayRCP<std::complex<double> > &recvBuffer,
1526  const int sourceRank,
1527  const int tag,
1528  const Comm<int>& comm);
1529 template<>
1530 TEUCHOSCOMM_LIB_DLL_EXPORT void
1531 send<int, std::complex<double> > (const Comm<int>& comm,
1532  const int count,
1533  const std::complex<double> sendBuffer[],
1534  const int destRank);
1535 template<>
1536 TEUCHOSCOMM_LIB_DLL_EXPORT void
1537 send<int, std::complex<double> > (const std::complex<double> sendBuffer[],
1538  const int count,
1539  const int destRank,
1540  const int tag,
1541  const Comm<int>& comm);
1542 template<>
1543 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1544 isend<int, std::complex<double> > (const ArrayRCP<const std::complex<double> >& sendBuffer,
1545  const int destRank,
1546  const int tag,
1547  const Comm<int>& comm);
1548 
1549 // Specialization for Ordinal=int and Packet=std::complex<float>.
1550 template<>
1551 TEUCHOSCOMM_LIB_DLL_EXPORT void
1552 reduceAll<int, std::complex<float> > (const Comm<int>& comm,
1553  const EReductionType reductType,
1554  const int count,
1555  const std::complex<float> sendBuffer[],
1556  std::complex<float> globalReducts[]);
1557 template<>
1558 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1559 ireceive<int, std::complex<float> > (const Comm<int>& comm,
1560  const ArrayRCP<std::complex<float> >& recvBuffer,
1561  const int sourceRank);
1562 template<>
1563 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1564 ireceive<int, std::complex<float> > (const ArrayRCP<std::complex<float> > &recvBuffer,
1565  const int sourceRank,
1566  const int tag,
1567  const Comm<int>& comm);
1568 template<>
1569 TEUCHOSCOMM_LIB_DLL_EXPORT void
1570 send<int, std::complex<float> > (const Comm<int>& comm,
1571  const int count,
1572  const std::complex<float> sendBuffer[],
1573  const int destRank);
1574 template<>
1575 TEUCHOSCOMM_LIB_DLL_EXPORT void
1576 send<int, std::complex<float> > (const std::complex<float> sendBuffer[],
1577  const int count,
1578  const int destRank,
1579  const int tag,
1580  const Comm<int>& comm);
1581 template<>
1582 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1583 isend<int, std::complex<float> > (const ArrayRCP<const std::complex<float> >& sendBuffer,
1584  const int destRank,
1585  const int tag,
1586  const Comm<int>& comm);
1587 #endif // HAVE_TEUCHOS_COMPLEX
1588 #endif // if 0
1589 
1590 // Specialization for Ordinal=int and Packet=double.
1591 template<>
1592 TEUCHOSCOMM_LIB_DLL_EXPORT void
1593 reduceAll<int, double> (const Comm<int>& comm,
1594  const EReductionType reductType,
1595  const int count,
1596  const double sendBuffer[],
1597  double globalReducts[]);
1598 template<>
1599 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1600 ireceive<int, double> (const Comm<int>& comm,
1601  const ArrayRCP<double>& recvBuffer,
1602  const int sourceRank);
1603 template<>
1604 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1605 ireceive<int, double> (const ArrayRCP<double> &recvBuffer,
1606  const int sourceRank,
1607  const int tag,
1608  const Comm<int>& comm);
1609 template<>
1610 TEUCHOSCOMM_LIB_DLL_EXPORT void
1611 send<int, double> (const Comm<int>& comm,
1612  const int count,
1613  const double sendBuffer[],
1614  const int destRank);
1615 template<>
1616 TEUCHOSCOMM_LIB_DLL_EXPORT void
1617 send<int, double> (const double sendBuffer[],
1618  const int count,
1619  const int destRank,
1620  const int tag,
1621  const Comm<int>& comm);
1622 template<>
1623 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1624 isend<int, double> (const ArrayRCP<const double>& sendBuffer,
1625  const int destRank,
1626  const int tag,
1627  const Comm<int>& comm);
1628 
1629 // Specialization for Ordinal=int and Packet=float.
1630 template<>
1631 TEUCHOSCOMM_LIB_DLL_EXPORT void
1632 reduceAll<int, float> (const Comm<int>& comm,
1633  const EReductionType reductType,
1634  const int count,
1635  const float sendBuffer[],
1636  float globalReducts[]);
1637 template<>
1638 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1639 ireceive<int, float> (const Comm<int>& comm,
1640  const ArrayRCP<float>& recvBuffer,
1641  const int sourceRank);
1642 template<>
1643 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1644 ireceive<int, float> (const ArrayRCP<float> &recvBuffer,
1645  const int sourceRank,
1646  const int tag,
1647  const Comm<int>& comm);
1648 template<>
1649 TEUCHOSCOMM_LIB_DLL_EXPORT void
1650 send<int, float> (const Comm<int>& comm,
1651  const int count,
1652  const float sendBuffer[],
1653  const int destRank);
1654 template<>
1655 TEUCHOSCOMM_LIB_DLL_EXPORT void
1656 send<int, float> (const float sendBuffer[],
1657  const int count,
1658  const int destRank,
1659  const int tag,
1660  const Comm<int>& comm);
1661 template<>
1662 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1663 isend<int, float> (const ArrayRCP<const float>& sendBuffer,
1664  const int destRank,
1665  const int tag,
1666  const Comm<int>& comm);
1667 
1668 // Specialization for Ordinal=int and Packet=long long.
1669 template<>
1670 TEUCHOSCOMM_LIB_DLL_EXPORT void
1671 gather<int, long long> (const long long sendBuf[],
1672  const int sendCount,
1673  long long recvBuf[],
1674  const int recvCount,
1675  const int root,
1676  const Comm<int>& comm);
1677 template<>
1678 TEUCHOSCOMM_LIB_DLL_EXPORT void
1679 gatherv<int, long long> (const long long sendBuf[],
1680  const int sendCount,
1681  long long recvBuf[],
1682  const int recvCounts[],
1683  const int displs[],
1684  const int root,
1685  const Comm<int>& comm);
1686 template<>
1687 TEUCHOSCOMM_LIB_DLL_EXPORT void
1688 reduceAll<int, long long> (const Comm<int>& comm,
1689  const EReductionType reductType,
1690  const int count,
1691  const long long sendBuffer[],
1692  long long globalReducts[]);
1693 template<>
1694 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1695 ireceive<int, long long> (const Comm<int>& comm,
1696  const ArrayRCP<long long>& recvBuffer,
1697  const int sourceRank);
1698 template<>
1699 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1700 ireceive<int, long long> (const ArrayRCP<long long> &recvBuffer,
1701  const int sourceRank,
1702  const int tag,
1703  const Comm<int>& comm);
1704 template<>
1705 TEUCHOSCOMM_LIB_DLL_EXPORT void
1706 send<int, long long> (const Comm<int>& comm,
1707  const int count,
1708  const long long sendBuffer[],
1709  const int destRank);
1710 template<>
1711 TEUCHOSCOMM_LIB_DLL_EXPORT void
1712 send<int, long long> (const long long sendBuffer[],
1713  const int count,
1714  const int destRank,
1715  const int tag,
1716  const Comm<int>& comm);
1717 template<>
1718 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1719 isend<int, long long> (const ArrayRCP<const long long>& sendBuffer,
1720  const int destRank,
1721  const int tag,
1722  const Comm<int>& comm);
1723 
1724 // Specialization for Ordinal=int and Packet=unsigned long long.
1725 template<>
1726 TEUCHOSCOMM_LIB_DLL_EXPORT void
1727 gather<int, unsigned long long> (const unsigned long long sendBuf[],
1728  const int sendCount,
1729  unsigned long long recvBuf[],
1730  const int recvCount,
1731  const int root,
1732  const Comm<int>& comm);
1733 template<>
1734 TEUCHOSCOMM_LIB_DLL_EXPORT void
1735 gatherv<int, unsigned long long> (const unsigned long long sendBuf[],
1736  const int sendCount,
1737  unsigned long long recvBuf[],
1738  const int recvCounts[],
1739  const int displs[],
1740  const int root,
1741  const Comm<int>& comm);
1742 template<>
1743 TEUCHOSCOMM_LIB_DLL_EXPORT void
1744 reduceAll<int, unsigned long long> (const Comm<int>& comm,
1745  const EReductionType reductType,
1746  const int count,
1747  const unsigned long long sendBuffer[],
1748  unsigned long long globalReducts[]);
1749 template<>
1750 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1751 ireceive<int, unsigned long long> (const Comm<int>& comm,
1752  const ArrayRCP<unsigned long long>& recvBuffer,
1753  const int sourceRank);
1754 template<>
1755 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1756 ireceive<int, unsigned long long> (const ArrayRCP<unsigned long long> &recvBuffer,
1757  const int sourceRank,
1758  const int tag,
1759  const Comm<int>& comm);
1760 template<>
1761 TEUCHOSCOMM_LIB_DLL_EXPORT void
1762 send<int, unsigned long long> (const Comm<int>& comm,
1763  const int count,
1764  const unsigned long long sendBuffer[],
1765  const int destRank);
1766 template<>
1767 TEUCHOSCOMM_LIB_DLL_EXPORT void
1768 send<int, unsigned long long> (const unsigned long long sendBuffer[],
1769  const int count,
1770  const int destRank,
1771  const int tag,
1772  const Comm<int>& comm);
1773 template<>
1774 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1775 isend<int, unsigned long long> (const ArrayRCP<const unsigned long long>& sendBuffer,
1776  const int destRank,
1777  const int tag,
1778  const Comm<int>& comm);
1779 
1780 // Specialization for Ordinal=int and Packet=long.
1781 template<>
1782 TEUCHOSCOMM_LIB_DLL_EXPORT void
1783 gather<int, long> (const long sendBuf[],
1784  const int sendCount,
1785  long recvBuf[],
1786  const int recvCount,
1787  const int root,
1788  const Comm<int>& comm);
1789 template<>
1790 TEUCHOSCOMM_LIB_DLL_EXPORT void
1791 gatherv<int, long> (const long sendBuf[],
1792  const int sendCount,
1793  long recvBuf[],
1794  const int recvCounts[],
1795  const int displs[],
1796  const int root,
1797  const Comm<int>& comm);
1798 template<>
1799 TEUCHOSCOMM_LIB_DLL_EXPORT void
1800 reduceAll<int, long> (const Comm<int>& comm,
1801  const EReductionType reductType,
1802  const int count,
1803  const long sendBuffer[],
1804  long globalReducts[]);
1805 template<>
1806 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1807 ireceive<int, long> (const Comm<int>& comm,
1808  const ArrayRCP<long>& recvBuffer,
1809  const int sourceRank);
1810 template<>
1811 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1812 ireceive<int, long> (const ArrayRCP<long> &recvBuffer,
1813  const int sourceRank,
1814  const int tag,
1815  const Comm<int>& comm);
1816 template<>
1817 TEUCHOSCOMM_LIB_DLL_EXPORT void
1818 send<int, long> (const Comm<int>& comm,
1819  const int count,
1820  const long sendBuffer[],
1821  const int destRank);
1822 template<>
1823 TEUCHOSCOMM_LIB_DLL_EXPORT void
1824 send<int, long> (const long sendBuffer[],
1825  const int count,
1826  const int destRank,
1827  const int tag,
1828  const Comm<int>& comm);
1829 template<>
1830 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1831 isend<int, long> (const ArrayRCP<const long>& sendBuffer,
1832  const int destRank,
1833  const int tag,
1834  const Comm<int>& comm);
1835 
1836 // Specialization for Ordinal=int and Packet=unsigned long.
1837 template<>
1838 TEUCHOSCOMM_LIB_DLL_EXPORT void
1839 gather<int, unsigned long> (const unsigned long sendBuf[],
1840  const int sendCount,
1841  unsigned long recvBuf[],
1842  const int recvCount,
1843  const int root,
1844  const Comm<int>& comm);
1845 template<>
1846 TEUCHOSCOMM_LIB_DLL_EXPORT void
1847 gatherv<int, unsigned long> (const unsigned long sendBuf[],
1848  const int sendCount,
1849  unsigned long recvBuf[],
1850  const int recvCounts[],
1851  const int displs[],
1852  const int root,
1853  const Comm<int>& comm);
1854 template<>
1855 TEUCHOSCOMM_LIB_DLL_EXPORT void
1856 reduceAll<int, unsigned long> (const Comm<int>& comm,
1857  const EReductionType reductType,
1858  const int count,
1859  const unsigned long sendBuffer[],
1860  unsigned long globalReducts[]);
1861 template<>
1862 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1863 ireceive<int, unsigned long> (const Comm<int>& comm,
1864  const ArrayRCP<unsigned long>& recvBuffer,
1865  const int sourceRank);
1866 template<>
1867 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1868 ireceive<int, unsigned long> (const ArrayRCP<unsigned long> &recvBuffer,
1869  const int sourceRank,
1870  const int tag,
1871  const Comm<int>& comm);
1872 template<>
1873 TEUCHOSCOMM_LIB_DLL_EXPORT void
1874 send<int, unsigned long> (const Comm<int>& comm,
1875  const int count,
1876  const unsigned long sendBuffer[],
1877  const int destRank);
1878 template<>
1879 TEUCHOSCOMM_LIB_DLL_EXPORT void
1880 send<int, unsigned long> (const unsigned long sendBuffer[],
1881  const int count,
1882  const int destRank,
1883  const int tag,
1884  const Comm<int>& comm);
1885 template<>
1886 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1887 isend<int, unsigned long> (const ArrayRCP<const unsigned long>& sendBuffer,
1888  const int destRank,
1889  const int tag,
1890  const Comm<int>& comm);
1891 
1892 // Specialization for Ordinal=int and Packet=int.
1893 template<>
1894 TEUCHOSCOMM_LIB_DLL_EXPORT void
1895 gather<int, int> (const int sendBuf[],
1896  const int sendCount,
1897  int recvBuf[],
1898  const int recvCount,
1899  const int root,
1900  const Comm<int>& comm);
1901 template<>
1902 TEUCHOSCOMM_LIB_DLL_EXPORT void
1903 gatherv<int, int> (const int sendBuf[],
1904  const int sendCount,
1905  int recvBuf[],
1906  const int recvCounts[],
1907  const int displs[],
1908  const int root,
1909  const Comm<int>& comm);
1910 template<>
1911 TEUCHOSCOMM_LIB_DLL_EXPORT void
1912 scatter (const int sendBuf[],
1913  const int sendCount,
1914  int recvBuf[],
1915  const int recvCount,
1916  const int root,
1917  const Comm<int>& comm);
1918 template<>
1919 TEUCHOSCOMM_LIB_DLL_EXPORT void
1920 reduce<int, int> (const int sendBuf[],
1921  int recvBuf[],
1922  const int count,
1923  const EReductionType reductType,
1924  const int root,
1925  const Comm<int>& comm);
1926 template<>
1927 TEUCHOSCOMM_LIB_DLL_EXPORT void
1928 reduce<int, long> (const long sendBuf[],
1929  long recvBuf[],
1930  const int count,
1931  const EReductionType reductType,
1932  const int root,
1933  const Comm<int>& comm);
1934 template<>
1935 TEUCHOSCOMM_LIB_DLL_EXPORT void
1936 reduce<int, unsigned long> (const unsigned long sendBuf[],
1937  unsigned long recvBuf[],
1938  const int count,
1939  const EReductionType reductType,
1940  const int root,
1941  const Comm<int>& comm);
1942 template<>
1943 TEUCHOSCOMM_LIB_DLL_EXPORT void
1944 reduce<int, unsigned long long > (const unsigned long long sendBuf[],
1945  unsigned long long recvBuf[],
1946  const int count,
1947  const EReductionType reductType,
1948  const int root,
1949  const Comm<int>& comm);
1950 template<>
1951 TEUCHOSCOMM_LIB_DLL_EXPORT void
1952 reduce<int, double> (const double sendBuf[],
1953  double recvBuf[],
1954  const int count,
1955  const EReductionType reductType,
1956  const int root,
1957  const Comm<int>& comm);
1958 template<>
1959 TEUCHOSCOMM_LIB_DLL_EXPORT void
1960 reduceAll<int, int> (const Comm<int>& comm,
1961  const EReductionType reductType,
1962  const int count,
1963  const int sendBuffer[],
1964  int globalReducts[]);
1965 
1966 template<>
1967 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1968 ireceive<int, int> (const Comm<int>& comm,
1969  const ArrayRCP<int>& recvBuffer,
1970  const int sourceRank);
1971 template<>
1972 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1973 ireceive<int, int> (const ArrayRCP<int> &recvBuffer,
1974  const int sourceRank,
1975  const int tag,
1976  const Comm<int>& comm);
1977 template<>
1978 TEUCHOSCOMM_LIB_DLL_EXPORT void
1979 send<int, int> (const Comm<int>& comm,
1980  const int count,
1981  const int sendBuffer[],
1982  const int destRank);
1983 template<>
1984 TEUCHOSCOMM_LIB_DLL_EXPORT void
1985 send<int, int> (const int sendBuffer[],
1986  const int count,
1987  const int destRank,
1988  const int tag,
1989  const Comm<int>& comm);
1990 template<>
1991 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1992 isend<int, int> (const ArrayRCP<const int>& sendBuffer,
1993  const int destRank,
1994  const int tag,
1995  const Comm<int>& comm);
1996 
1997 // Specialization for Ordinal=int and Packet=unsigned int.
1998 template<>
1999 TEUCHOSCOMM_LIB_DLL_EXPORT void
2000 gather<int, unsigned int> (const unsigned int sendBuf[],
2001  const int sendCount,
2002  unsigned int recvBuf[],
2003  const int recvCount,
2004  const int root,
2005  const Comm<int>& comm);
2006 template<>
2007 TEUCHOSCOMM_LIB_DLL_EXPORT void
2008 gatherv<int, unsigned int> (const unsigned int sendBuf[],
2009  const int sendCount,
2010  unsigned int recvBuf[],
2011  const int recvCounts[],
2012  const int displs[],
2013  const int root,
2014  const Comm<int>& comm);
2015 template<>
2016 TEUCHOSCOMM_LIB_DLL_EXPORT void
2017 reduceAll<int, unsigned int> (const Comm<int>& comm,
2018  const EReductionType reductType,
2019  const int count,
2020  const unsigned int sendBuffer[],
2021  unsigned int globalReducts[]);
2022 template<>
2023 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
2024 ireceive<int, unsigned int> (const Comm<int>& comm,
2025  const ArrayRCP<unsigned int>& recvBuffer,
2026  const int sourceRank);
2027 template<>
2028 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
2029 ireceive<int, unsigned int> (const ArrayRCP<unsigned int> &recvBuffer,
2030  const int sourceRank,
2031  const int tag,
2032  const Comm<int>& comm);
2033 template<>
2034 TEUCHOSCOMM_LIB_DLL_EXPORT void
2035 send<int, unsigned int> (const Comm<int>& comm,
2036  const int count,
2037  const unsigned int sendBuffer[],
2038  const int destRank);
2039 template<>
2040 TEUCHOSCOMM_LIB_DLL_EXPORT void
2041 send<int, unsigned int> (const unsigned int sendBuffer[],
2042  const int count,
2043  const int destRank,
2044  const int tag,
2045  const Comm<int>& comm);
2046 template<>
2047 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
2048 isend<int, unsigned int> (const ArrayRCP<const unsigned int>& sendBuffer,
2049  const int destRank,
2050  const int tag,
2051  const Comm<int>& comm);
2052 
2053 // Specialization for Ordinal=int and Packet=short.
2054 template<>
2055 TEUCHOSCOMM_LIB_DLL_EXPORT void
2056 gather<int, short> (const short sendBuf[],
2057  const int sendCount,
2058  short recvBuf[],
2059  const int recvCount,
2060  const int root,
2061  const Comm<int>& comm);
2062 template<>
2063 TEUCHOSCOMM_LIB_DLL_EXPORT void
2064 gatherv<int, short> (const short sendBuf[],
2065  const int sendCount,
2066  short recvBuf[],
2067  const int recvCounts[],
2068  const int displs[],
2069  const int root,
2070  const Comm<int>& comm);
2071 template<>
2072 TEUCHOSCOMM_LIB_DLL_EXPORT void
2073 reduceAll<int, short> (const Comm<int>& comm,
2074  const EReductionType reductType,
2075  const int count,
2076  const short sendBuffer[],
2077  short globalReducts[]);
2078 template<>
2079 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
2080 ireceive<int, short> (const Comm<int>& comm,
2081  const ArrayRCP<short>& recvBuffer,
2082  const int sourceRank);
2083 template<>
2084 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
2085 ireceive<int, short> (const ArrayRCP<short> &recvBuffer,
2086  const int sourceRank,
2087  const int tag,
2088  const Comm<int>& comm);
2089 template<>
2090 TEUCHOSCOMM_LIB_DLL_EXPORT void
2091 send<int, short> (const Comm<int>& comm,
2092  const int count,
2093  const short sendBuffer[],
2094  const int destRank);
2095 template<>
2096 TEUCHOSCOMM_LIB_DLL_EXPORT void
2097 send<int, short> (const short sendBuffer[],
2098  const int count,
2099  const int destRank,
2100  const int tag,
2101  const Comm<int>& comm);
2102 template<>
2103 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
2104 isend<int, short> (const ArrayRCP<const short>& sendBuffer,
2105  const int destRank,
2106  const int tag,
2107  const Comm<int>& comm);
2108 
2109 // mfh 18 Oct 2012: The specialization for Packet=char seems to be
2110 // causing problems such as the following:
2111 //
2112 // http://testing.sandia.gov/cdash/testDetails.php?test=9909246&build=747699
2113 //
2114 // I am disabling it for now. This should revert back to the old
2115 // behavior for Packet=char. That should fix the Tpetra errors, since
2116 // many Tpetra objects inherit from DistObject<char, ...>.
2117 #if 0
2118 // Specialization for Ordinal=int and Packet=char.
2119 template<>
2120 TEUCHOSCOMM_LIB_DLL_EXPORT void
2121 reduceAll<int, char> (const Comm<int>& comm,
2122  const EReductionType reductType,
2123  const int count,
2124  const char sendBuffer[],
2125  char globalReducts[]);
2126 #endif // 0
2127 } // namespace Teuchos
2128 
2129 
2130 template<typename Ordinal, typename Packet>
2131 void Teuchos::reduceAll(
2132  const Comm<Ordinal>& comm, const EReductionType reductType
2133  ,const Packet &send, const Ptr<Packet> &globalReduct
2134  )
2135 {
2136  // mfh 17 Oct 2012: This will invoke the above specializations for
2137  // general count, so we don't need to specialize this function.
2138  reduceAll<Ordinal,Packet>(comm, reductType, 1, &send, &*globalReduct);
2139 }
2140 
2141 
2142 template<typename Ordinal, typename Packet>
2143 void Teuchos::reduceAll(
2144  const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
2145  const ReferenceTypeReductionOp<Ordinal,Packet> &reductOp,
2146  const Ordinal count, const Packet*const sendBuffer[], Packet*const globalReducts[]
2147  )
2148 {
2149  TEUCHOS_COMM_TIME_MONITOR(
2150  "Teuchos::CommHelpers: reduceAll<"
2151  <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
2152  <<">( reference type )"
2153  );
2154  ConstReferenceTypeSerializationBuffer<Ordinal,Packet>
2155  charSendBuffer(serializer,count,sendBuffer);
2156  ReferenceTypeSerializationBuffer<Ordinal,Packet>
2157  charGlobalReducts(serializer,count,globalReducts);
2158  CharToReferenceTypeReductionOp<Ordinal,Packet>
2159  charReductOp(rcp(&serializer,false),rcp(&reductOp,false));
2160  comm.reduceAll(
2161  charReductOp,charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
2162  ,charGlobalReducts.getCharBuffer()
2163  );
2164 }
2165 
2166 template<typename Ordinal, typename Packet, typename Serializer>
2167 void Teuchos::reduceAll(
2168  const Comm<Ordinal>& comm,
2169  const Serializer& serializer,
2170  const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
2171  const Ordinal count, const Packet sendBuffer[], Packet globalReducts[]
2172  )
2173 {
2174  TEUCHOS_COMM_TIME_MONITOR(
2175  "Teuchos::CommHelpers: reduceAll<"
2176  <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
2177  <<">( value type, user-defined op )"
2178  );
2179  ConstValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
2180  charSendBuffer(count,sendBuffer,rcp(&serializer,false));
2181  ValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
2182  charGlobalReducts(count,globalReducts,rcp(&serializer,false));
2183  CharToValueTypeReductionOp<Ordinal,Packet,Serializer>
2184  charReductOp(rcp(&reductOp,false),rcp(&serializer,false));
2185  comm.reduceAll(
2186  charReductOp,charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
2187  ,charGlobalReducts.getCharBuffer()
2188  );
2189 }
2190 
2191 
2192 template<typename Ordinal, typename Packet, typename Serializer>
2193 void Teuchos::reduceAll(
2194  const Comm<Ordinal>& comm,
2195  const Serializer& serializer,
2196  const EReductionType reductType,
2197  const Ordinal count, const Packet sendBuffer[], Packet globalReducts[]
2198  )
2199 {
2200  TEUCHOS_COMM_TIME_MONITOR(
2201  "Teuchos::CommHelpers: reduceAll<"
2202  <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
2203  <<">( value type, "<<toString(reductType)<<" )"
2204  );
2205 
2206  ValueTypeReductionOp<Ordinal,Packet>* reductOp =
2207  createOp<Ordinal, Packet> (reductType);
2208  try {
2209  reduceAll(comm,serializer,*reductOp,count,sendBuffer,globalReducts);
2210  }
2211  catch (std::exception& e) {
2212  delete reductOp;
2213  throw e;
2214  }
2215  delete reductOp;
2216 }
2217 
2218 
2219 template<typename Ordinal, typename Packet>
2220 void Teuchos::scan(
2221  const Comm<Ordinal>& comm, const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
2222  const Ordinal count, const Packet sendBuffer[], Packet scanReducts[]
2223  )
2224 {
2225  TEUCHOS_COMM_TIME_MONITOR(
2226  "Teuchos::CommHelpers: scan<"
2227  <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
2228  <<">( value type, user-defined op )"
2229  );
2230  ConstValueTypeSerializationBuffer<Ordinal,Packet>
2231  charSendBuffer(count,sendBuffer);
2232  ValueTypeSerializationBuffer<Ordinal,Packet>
2233  charScanReducts(count,scanReducts);
2234  CharToValueTypeReductionOp<Ordinal,Packet>
2235  charReductOp(rcp(&reductOp,false));
2236  comm.scan(
2237  charReductOp,charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
2238  ,charScanReducts.getCharBuffer()
2239  );
2240 }
2241 
2242 
2243 template<typename Ordinal, typename Packet>
2244 void Teuchos::scan(
2245  const Comm<Ordinal>& comm, const EReductionType reductType,
2246  const Ordinal count, const Packet sendBuffer[], Packet scanReducts[]
2247  )
2248 {
2249  TEUCHOS_COMM_TIME_MONITOR(
2250  "Teuchos::CommHelpers: scan<"
2251  <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
2252  <<">( value type, "<<toString(reductType)<<" )"
2253  );
2254 
2255  ValueTypeReductionOp<Ordinal,Packet>* reductOp =
2256  createOp<Ordinal, Packet> (reductType);
2257  try {
2258  scan(comm,*reductOp,count,sendBuffer,scanReducts);
2259  }
2260  catch (std::exception& e) {
2261  delete reductOp;
2262  throw e;
2263  }
2264  delete reductOp;
2265 }
2266 
2267 
2268 template<typename Ordinal, typename Packet>
2269 void Teuchos::scan(
2270  const Comm<Ordinal>& comm, const EReductionType reductType,
2271  const Packet &send, const Ptr<Packet> &scanReduct
2272  )
2273 {
2274  scan<Ordinal,Packet>(comm, reductType, 1, &send, &*scanReduct);
2275 }
2276 
2277 
2278 template<typename Ordinal, typename Packet>
2279 void Teuchos::scan(
2280  const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
2281  const ReferenceTypeReductionOp<Ordinal,Packet> &reductOp,
2282  const Ordinal count, const Packet*const sendBuffer[], Packet*const scanReducts[]
2283  )
2284 {
2285  TEUCHOS_TEST_FOR_EXCEPT(true); // ToDo: Implement and test when needed!
2286 }
2287 
2288 template<typename Ordinal, typename Packet, typename Serializer>
2289 void Teuchos::scan(
2290  const Comm<Ordinal>& comm,
2291  const Serializer& serializer,
2292  const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
2293  const Ordinal count, const Packet sendBuffer[], Packet scanReducts[]
2294  )
2295 {
2296  TEUCHOS_COMM_TIME_MONITOR(
2297  "Teuchos::CommHelpers: scan<"
2298  <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
2299  <<">( value type, user-defined op )"
2300  );
2301  ConstValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
2302  charSendBuffer(count,sendBuffer,rcp(&serializer,false));
2303  ValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
2304  charScanReducts(count,scanReducts,rcp(&serializer,false));
2305  CharToValueTypeReductionOp<Ordinal,Packet,Serializer>
2306  charReductOp(rcp(&reductOp,false),rcp(&serializer,false));
2307  comm.scan(
2308  charReductOp,charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
2309  ,charScanReducts.getCharBuffer()
2310  );
2311 }
2312 
2313 
2314 template<typename Ordinal, typename Packet, typename Serializer>
2315 void Teuchos::scan(
2316  const Comm<Ordinal>& comm,
2317  const Serializer& serializer,
2318  const EReductionType reductType,
2319  const Ordinal count, const Packet sendBuffer[], Packet scanReducts[]
2320  )
2321 {
2322  TEUCHOS_COMM_TIME_MONITOR(
2323  "Teuchos::CommHelpers: scan<"
2324  <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
2325  <<">( value type, "<<toString(reductType)<<" )"
2326  );
2327 
2328  ValueTypeReductionOp<Ordinal,Packet>* reductOp =
2329  createOp<Ordinal, Packet> (reductType);
2330  try {
2331  scan(comm,serializer,*reductOp,count,sendBuffer,scanReducts);
2332  }
2333  catch (std::exception& e) {
2334  delete reductOp;
2335  throw e;
2336  }
2337  delete reductOp;
2338 }
2339 
2340 template<typename Ordinal, typename Packet>
2341 void Teuchos::send(
2342  const Comm<Ordinal>& comm,
2343  const Ordinal count, const Packet sendBuffer[], const int destRank
2344  )
2345 {
2346  TEUCHOS_COMM_TIME_MONITOR(
2347  "Teuchos::CommHelpers: send<"
2348  <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
2349  <<">( value type )"
2350  );
2351  ConstValueTypeSerializationBuffer<Ordinal,Packet>
2352  charSendBuffer(count,sendBuffer);
2353  comm.send(
2354  charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
2355  ,destRank
2356  );
2357 }
2358 
2359 template<typename Ordinal, typename Packet>
2360 void
2361 Teuchos::send (const Packet sendBuffer[],
2362  const Ordinal count,
2363  const int destRank,
2364  const int tag,
2365  const Comm<Ordinal>& comm)
2366 {
2367  TEUCHOS_COMM_TIME_MONITOR(
2368  "Teuchos::CommHelpers: send<"
2370  <<">( value type )"
2371  );
2372  ConstValueTypeSerializationBuffer<Ordinal,Packet> charSendBuffer (count, sendBuffer);
2373  comm.send (charSendBuffer.getBytes (), charSendBuffer.getCharBuffer (), destRank, tag);
2374 }
2375 
2376 template<typename Ordinal, typename Packet>
2377 void Teuchos::ssend(
2378  const Comm<Ordinal>& comm,
2379  const Ordinal count, const Packet sendBuffer[], const int destRank
2380  )
2381 {
2382  TEUCHOS_COMM_TIME_MONITOR(
2383  "Teuchos::CommHelpers: ssend<"
2384  <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
2385  <<">( value type )"
2386  );
2387  ConstValueTypeSerializationBuffer<Ordinal,Packet>
2388  charSendBuffer(count,sendBuffer);
2389  comm.ssend(
2390  charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
2391  ,destRank
2392  );
2393 }
2394 
2395 template<typename Ordinal, typename Packet>
2396 void
2397 Teuchos::ssend (const Packet sendBuffer[],
2398  const Ordinal count,
2399  const int destRank,
2400  const int tag,
2401  const Comm<Ordinal>& comm)
2402 {
2403  TEUCHOS_COMM_TIME_MONITOR(
2404  "Teuchos::CommHelpers: ssend<"
2406  <<">( value type )"
2407  );
2409  buf_type charSendBuffer (count, sendBuffer);
2410  comm.ssend (charSendBuffer.getBytes (),
2411  charSendBuffer.getCharBuffer (),
2412  destRank, tag);
2413 }
2414 
2415 template<typename Ordinal, typename Packet>
2416 void Teuchos::send(
2417  const Comm<Ordinal>& comm,
2418  const Packet &send, const int destRank
2419  )
2420 {
2421  Teuchos::send<Ordinal,Packet>(comm,1,&send,destRank);
2422 }
2423 
2424 template<typename Ordinal, typename Packet>
2425 void Teuchos::ssend(
2426  const Comm<Ordinal>& comm,
2427  const Packet &send, const int destRank
2428  )
2429 {
2430  Teuchos::ssend<Ordinal,Packet>(comm,1,&send,destRank);
2431 }
2432 
2433 template<typename Ordinal, typename Packet>
2434 void Teuchos::send(
2435  const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
2436  const Ordinal count, const Packet*const sendBuffer[], const int destRank
2437  )
2438 {
2439  TEUCHOS_TEST_FOR_EXCEPT(true); // ToDo: Implement and test when needed!
2440 }
2441 
2442 template<typename Ordinal, typename Packet, typename Serializer>
2443 void Teuchos::send(
2444  const Comm<Ordinal>& comm,
2445  const Serializer& serializer,
2446  const Ordinal count, const Packet sendBuffer[], const int destRank
2447  )
2448 {
2449  TEUCHOS_COMM_TIME_MONITOR(
2450  "Teuchos::CommHelpers: send<"
2451  <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
2452  <<">( value type )"
2453  );
2454  ConstValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
2455  charSendBuffer(count,sendBuffer,rcp(&serializer,false));
2456  comm.send(
2457  charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
2458  ,destRank
2459  );
2460 }
2461 
2462 template<typename Ordinal, typename Packet>
2463 int Teuchos::receive(
2464  const Comm<Ordinal>& comm,
2465  const int sourceRank, const Ordinal count, Packet recvBuffer[]
2466  )
2467 {
2468  TEUCHOS_COMM_TIME_MONITOR(
2469  "Teuchos::CommHelpers: receive<"
2470  <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
2471  <<">( value type )"
2472  );
2473  ValueTypeSerializationBuffer<Ordinal,Packet>
2474  charRecvBuffer(count,recvBuffer);
2475  return comm.receive(
2476  sourceRank
2477  ,charRecvBuffer.getBytes(),charRecvBuffer.getCharBuffer()
2478  );
2479 }
2480 
2481 
2482 template<typename Ordinal, typename Packet>
2483 int Teuchos::receive(
2484  const Comm<Ordinal>& comm,
2485  const int sourceRank, Packet *recv
2486  )
2487 {
2488  return Teuchos::receive<Ordinal,Packet>(comm,sourceRank,1,recv);
2489 }
2490 
2491 
2492 template<typename Ordinal, typename Packet>
2493 int Teuchos::receive(
2494  const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
2495  const int sourceRank, const Ordinal count, Packet*const recvBuffer[]
2496  )
2497 {
2498  TEUCHOS_TEST_FOR_EXCEPT(true); // ToDo: Implement and test when needed!
2499 }
2500 
2501 template<typename Ordinal, typename Packet, typename Serializer>
2502 int Teuchos::receive(
2503  const Comm<Ordinal>& comm,
2504  const Serializer& serializer,
2505  const int sourceRank, const Ordinal count, Packet recvBuffer[]
2506  )
2507 {
2508  TEUCHOS_COMM_TIME_MONITOR(
2509  "Teuchos::CommHelpers: receive<"
2510  <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
2511  <<">( value type )"
2512  );
2513  ValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
2514  charRecvBuffer(count,recvBuffer,rcp(&serializer,false));
2515  return comm.receive(
2516  sourceRank
2517  ,charRecvBuffer.getBytes(),charRecvBuffer.getCharBuffer()
2518  );
2519 }
2520 
2521 template<typename Ordinal, typename Packet>
2522 void Teuchos::readySend(
2523  const Comm<Ordinal>& comm,
2524  const ArrayView<const Packet> &sendBuffer,
2525  const int destRank
2526  )
2527 {
2528  TEUCHOS_COMM_TIME_MONITOR(
2529  "Teuchos::CommHelpers: readySend<"
2530  <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
2531  <<">( value type )"
2532  );
2533  ConstValueTypeSerializationBuffer<Ordinal,Packet>
2534  charSendBuffer(sendBuffer.size(), sendBuffer.getRawPtr());
2535  comm.readySend( charSendBuffer.getCharBufferView(), destRank );
2536 }
2537 
2538 template<typename Ordinal, typename Packet>
2539 void
2540 Teuchos::readySend (const Packet sendBuffer[],
2541  const Ordinal count,
2542  const int destRank,
2543  const int tag,
2544  const Comm<Ordinal>& comm)
2545 {
2546  TEUCHOS_COMM_TIME_MONITOR(
2547  "Teuchos::CommHelpers: readySend<"
2549  <<">( value type )"
2550  );
2552  buf_type charSendBuffer (count, sendBuffer);
2553  comm.readySend (charSendBuffer.getBytes (),
2554  charSendBuffer.getCharBuffer (),
2555  destRank, tag);
2556 }
2557 
2558 template<typename Ordinal, typename Packet>
2559 void Teuchos::readySend(
2560  const Comm<Ordinal>& comm,
2561  const Packet &send,
2562  const int destRank
2563  )
2564 {
2565  readySend<Ordinal, Packet>( comm, arrayView(&send,1), destRank );
2566 }
2567 
2568 template<typename Ordinal, typename Packet, typename Serializer>
2569 void Teuchos::readySend(
2570  const Comm<Ordinal>& comm,
2571  const Serializer& serializer,
2572  const ArrayView<const Packet> &sendBuffer,
2573  const int destRank
2574  )
2575 {
2576  TEUCHOS_COMM_TIME_MONITOR(
2577  "Teuchos::CommHelpers: readySend<"
2578  <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
2579  <<">( value type )"
2580  );
2581  ConstValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
2582  charSendBuffer(sendBuffer.size(), sendBuffer.getRawPtr(), serializer);
2583  comm.readySend( charSendBuffer.getCharBufferView(), destRank );
2584 }
2585 
2586 template<typename Ordinal, typename Packet>
2589  const Comm<Ordinal>& comm,
2590  const ArrayRCP<const Packet> &sendBuffer,
2591  const int destRank
2592  )
2593 {
2594  TEUCHOS_COMM_TIME_MONITOR(
2595  "Teuchos::CommHelpers: isend<"
2596  <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
2597  <<">( value type )"
2598  );
2599  ConstValueTypeSerializationBuffer<Ordinal,Packet>
2600  charSendBuffer(sendBuffer.size(), sendBuffer.getRawPtr());
2601  RCP<CommRequest<Ordinal> > commRequest = comm.isend(
2602  charSendBuffer.getCharBufferView(), destRank );
2603  set_extra_data( sendBuffer, "buffer", inOutArg(commRequest) );
2604  return commRequest;
2605 }
2606 
2607 template<typename Ordinal, typename Packet>
2609 Teuchos::isend (const ArrayRCP<const Packet> &sendBuffer,
2610  const int destRank,
2611  const int tag,
2612  const Comm<Ordinal>& comm)
2613 {
2614  TEUCHOS_COMM_TIME_MONITOR(
2615  "Teuchos::isend<" << OrdinalTraits<Ordinal>::name () << ","
2616  << TypeNameTraits<Packet>::name () << ">");
2617  ConstValueTypeSerializationBuffer<Ordinal,Packet>
2618  charSendBuffer (sendBuffer.size (), sendBuffer.getRawPtr ());
2619  RCP<CommRequest<Ordinal> > commRequest =
2620  comm.isend (charSendBuffer.getCharBufferView (), destRank, tag);
2621  set_extra_data (sendBuffer, "buffer", inOutArg (commRequest));
2622  return commRequest;
2623 }
2624 
2625 template<typename Ordinal, typename Packet>
2628  const Comm<Ordinal>& comm,
2629  const RCP<const Packet> &send,
2630  const int destRank
2631  )
2632 {
2633  const ArrayRCP<const Packet> sendBuffer =
2634  arcpWithEmbeddedObj( send.get(), 0, 1, send, false );
2635  // 2008/07/29: rabartl: Above: I need to write a helper function to create
2636  // new ArrayRCP object given a single object to copy.
2637  return isend<Ordinal, Packet>( comm, sendBuffer, destRank );
2638 }
2639 
2640 template<typename Ordinal, typename Packet, typename Serializer>
2643  const Comm<Ordinal>& comm,
2644  const Serializer& serializer,
2645  const ArrayRCP<const Packet> &sendBuffer,
2646  const int destRank
2647  )
2648 {
2649  TEUCHOS_COMM_TIME_MONITOR(
2650  "Teuchos::CommHelpers: isend<"
2651  <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
2652  <<">( value type )"
2653  );
2654  ConstValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
2655  charSendBuffer(sendBuffer.size(), sendBuffer.getRawPtr(), serializer);
2656  RCP<CommRequest<Ordinal> > commRequest = comm.isend(
2657  charSendBuffer.getCharBufferView(), destRank );
2658  set_extra_data( sendBuffer, "buffer", inOutArg(commRequest) );
2659  return commRequest;
2660 }
2661 
2662 template<typename Ordinal, typename Packet>
2665  const Comm<Ordinal>& comm,
2666  const ArrayRCP<Packet> &recvBuffer,
2667  const int sourceRank
2668  )
2669 {
2670  TEUCHOS_COMM_TIME_MONITOR(
2671  "Teuchos::ireceive<int, " << "," << TypeNameTraits<Packet>::name () << ">");
2672  ValueTypeSerializationBuffer<Ordinal,Packet>
2673  charRecvBuffer(recvBuffer.size(), recvBuffer.getRawPtr());
2674  RCP<CommRequest<Ordinal> > commRequest = comm.ireceive(
2675  charRecvBuffer.getCharBufferView(), sourceRank );
2676  set_extra_data( recvBuffer, "buffer", inOutArg(commRequest) );
2677  return commRequest;
2678 }
2679 
2680 template<typename Ordinal, typename Packet>
2682 Teuchos::ireceive (const Teuchos::ArrayRCP<Packet> &recvBuffer,
2683  const int sourceRank,
2684  const int tag,
2685  const Teuchos::Comm<Ordinal>& comm)
2686 {
2687  TEUCHOS_COMM_TIME_MONITOR(
2688  "Teuchos::ireceive<int, " << "," << TypeNameTraits<Packet>::name () << ">");
2689  ValueTypeSerializationBuffer<int, Packet>
2690  charRecvBuffer (recvBuffer.size (), recvBuffer.getRawPtr ());
2691  RCP<CommRequest<int> > commRequest =
2692  comm.ireceive (charRecvBuffer.getCharBufferView (), sourceRank, tag);
2693  set_extra_data (recvBuffer, "buffer", inOutArg (commRequest));
2694  return commRequest;
2695 }
2696 
2697 template<typename Ordinal, typename Packet>
2700  const Comm<Ordinal>& comm,
2701  const RCP<Packet> &recv,
2702  const int sourceRank
2703  )
2704 {
2705  const ArrayRCP<Packet> recvBuffer =
2706  arcpWithEmbeddedObj( recv.get(), 0, 1, recv, false );
2707  // 2008/07/29: rabartl: Above: I need to write a helper function to create
2708  // new ArrayRCP object given a single object to copy.
2709  return ireceive<Ordinal, Packet>( comm, recvBuffer, sourceRank );
2710 }
2711 
2712 template<typename Ordinal, typename Packet, typename Serializer>
2715  const Comm<Ordinal>& comm,
2716  const Serializer& serializer,
2717  const ArrayRCP<Packet> &recvBuffer,
2718  const int sourceRank
2719  )
2720 {
2721  TEUCHOS_COMM_TIME_MONITOR(
2722  "Teuchos::CommHelpers: ireceive<"
2723  <<OrdinalTraits<Ordinal>::name()<<","<<TypeNameTraits<Packet>::name()
2724  <<">( value type )"
2725  );
2726  ValueTypeSerializationBuffer<Ordinal,Packet,Serializer>
2727  charRecvBuffer(recvBuffer.size(), recvBuffer.getRawPtr(), serializer);
2728  RCP<CommRequest<Ordinal> > commRequest = comm.ireceive(
2729  charRecvBuffer.getCharBufferView(), sourceRank );
2730  set_extra_data( recvBuffer, "buffer", inOutArg(commRequest) );
2731  return commRequest;
2732 }
2733 
2734 template<typename Ordinal>
2735 void Teuchos::waitAll(
2736  const Comm<Ordinal>& comm,
2737  const ArrayView<RCP<CommRequest<Ordinal> > > &requests
2738  )
2739 {
2740  comm.waitAll(requests);
2741 }
2742 
2743 
2744 template<typename Ordinal>
2745 void
2746 Teuchos::waitAll (const Comm<Ordinal>& comm,
2747  const ArrayView<RCP<CommRequest<Ordinal> > >& requests,
2748  const ArrayView<RCP<CommStatus<Ordinal> > >& statuses)
2749 {
2750  comm.waitAll (requests, statuses);
2751 }
2752 
2753 
2754 template<typename Ordinal>
2756 Teuchos::wait (const Comm<Ordinal>& comm,
2757  const Ptr<RCP<CommRequest<Ordinal> > > &request)
2758 {
2759  return comm.wait (request);
2760 }
2761 
2762 
2763 #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...
void reduce(const Ordinal count, const Packet inBuffer[], Packet inoutBuffer[]) const
EReductionType
Predefined reduction operations that Teuchos::Comm understands.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
T * getRawPtr() const
Get the raw C++ pointer to the underlying object.
void reduce(const Ordinal count, const Packet inBuffer[], Packet inoutBuffer[]) const
size_type size() const
The total number of entries in the array.
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.
Standard summation operator for types with value semantics.
void reduce(const Ordinal count, const Packet inBuffer[], Packet inoutBuffer[]) const
virtual RCP< CommRequest< Ordinal > > ireceive(const ArrayView< char > &recvBuffer, const int sourceRank) const =0
Non-blocking receive.
void send(const Comm< Ordinal > &comm, const Ordinal count, const Packet sendBuffer[], const int destRank)
Send objects that use values semantics 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.
Declaration of Teuchos::EReductionType enum, and related functions.
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). ...
virtual void readySend(const ArrayView< const char > &sendBuffer, const int destRank) const =0
Ready send of data from this process to another process.
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.
Defines basic traits for the ordinal field type.
Default traits class that just returns typeid(T).name().
void reduce(const Ordinal count, const Packet inBuffer[], Packet inoutBuffer[]) const
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.
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.
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).
Defines basic traits returning the name of a type in a portable and readable way. ...
Definition of Teuchos::as, for conversions between types.
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.