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
58 # include "Teuchos_DefaultMpiComm.hpp"
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 inline
87 const char* toString( const EReductionType reductType )
88 {
89  switch(reductType) {
90  case REDUCE_SUM: return "REDUCE_SUM";
91  case REDUCE_MIN: return "REDUCE_MIN";
92  case REDUCE_MAX: return "REDUCE_MAX";
93  case REDUCE_AND: return "REDUCE_AND";
94  default: TEUCHOS_TEST_FOR_EXCEPT(true);
95  }
96  return 0; // Will never be called
97 }
98 
103 template<typename Ordinal>
104 int rank(const Comm<Ordinal>& comm);
105 
110 template<typename Ordinal>
111 int size(const Comm<Ordinal>& comm);
112 
117 template<typename Ordinal>
118 void barrier(const Comm<Ordinal>& comm);
119 
124 template<typename Ordinal, typename Packet>
125 void broadcast(
126  const Comm<Ordinal>& comm,
127  const int rootRank,
128  const Ordinal count, Packet buffer[]
129  );
130 
135 template<typename Ordinal, typename Packet>
136 void broadcast(
137  const Comm<Ordinal>& comm,
138  const int rootRank,
139  const ArrayView<Packet> &buffer
140  );
141 
146 template<typename Ordinal, typename Packet>
147 void broadcast(
148  const Comm<Ordinal>& comm,
149  const int rootRank, Packet *object
150  );
151 
156 template<typename Ordinal, typename Packet>
157 void broadcast(
158  const Comm<Ordinal>& comm,
159  const int rootRank, const Ptr<Packet> &object
160  );
161 
166 template<typename Ordinal, typename Packet>
167 void broadcast(
168  const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
169  const int rootRank, const Ordinal count, Packet*const buffer[]
170  );
171 
176 template<typename Ordinal, typename Packet>
177 void broadcast(
178  const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
179  const int rootRank, const ArrayView<const Ptr<Packet> > &buffer
180  );
181 
187 template<typename Ordinal, typename Packet, typename Serializer>
188 void broadcast(
189  const Comm<Ordinal>& comm,
190  const Serializer& serializer,
191  const int rootRank,
192  const Ordinal count, Packet buffer[]
193  );
194 
199 template<typename Ordinal, typename Packet>
200 void
201 gather (const Packet sendBuf[],
202  const Ordinal sendCount,
203  Packet recvBuf[],
204  const Ordinal recvCount,
205  const int root,
206  const Comm<Ordinal>& comm);
207 
212 template<typename Ordinal, typename Packet>
213 void
214 gatherv (const Packet sendBuf[],
215  const Ordinal sendCount,
216  Packet recvBuf[],
217  const Ordinal recvCounts[],
218  const Ordinal displs[],
219  const int root,
220  const Comm<Ordinal>& comm);
221 
227 template<typename Ordinal, typename Packet>
228 void gatherAll(
229  const Comm<Ordinal>& comm,
230  const Ordinal sendCount, const Packet sendBuffer[],
231  const Ordinal recvCount, Packet recvBuffer[]
232  );
233 
239 template<typename Ordinal, typename Packet>
240 void gatherAll(
241  const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
242  const Ordinal sendCount, const Packet*const sendBuffer[],
243  const Ordinal recvCount, Packet*const recvBuffer[]
244  );
245 
251 template<typename Ordinal, typename Packet, typename Serializer>
252 void gatherAll(
253  const Comm<Ordinal>& comm,
254  const Serializer& serializer,
255  const Ordinal sendCount, const Packet sendBuffer[],
256  const Ordinal recvCount, Packet recvBuffer[]
257  );
258 
264 template<typename Ordinal, typename Packet>
265 void reduceAll(
266  const Comm<Ordinal>& comm, const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
267  const Ordinal count, const Packet sendBuffer[], Packet globalReducts[]
268  );
269 
275 template<typename Ordinal, typename Packet>
276 void reduceAll(
277  const Comm<Ordinal>& comm, const EReductionType reductType,
278  const Ordinal count, const Packet sendBuffer[], Packet globalReducts[]
279  );
280 
286 template<typename Ordinal, typename Packet>
287 void reduceAll(
288  const Comm<Ordinal>& comm, const EReductionType reductType,
289  const Packet &send, const Ptr<Packet> &globalReduct
290  );
291 
293 template<typename Ordinal, typename Packet>
294 TEUCHOS_DEPRECATED void reduceAll(
295  const Comm<Ordinal>& comm, const EReductionType reductType,
296  const Packet &send, Packet *globalReduct
297  )
298 {
299  reduceAll<Ordinal,Packet>(comm, reductType, send, ptr(globalReduct));
300 }
301 
307 template<typename Ordinal, typename Packet>
308 void reduceAll(
309  const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
311  const Ordinal count, const Packet*const sendBuffer[], Packet*const globalReducts[]
312  );
313 
319 template<typename Ordinal, typename Packet, typename Serializer>
320 void reduceAll(
321  const Comm<Ordinal>& comm,
322  const Serializer& serializer,
323  const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
324  const Ordinal count, const Packet sendBuffer[], Packet globalReducts[]
325  );
326 
332 template<typename Ordinal, typename Packet, typename Serializer>
333 void reduceAll(
334  const Comm<Ordinal>& comm,
335  const Serializer& serializer,
336  const EReductionType reductType,
337  const Ordinal count, const Packet sendBuffer[], Packet globalReducts[]
338  );
339 
345 template<typename Ordinal, typename Packet>
346 void reduceAllAndScatter(
347  const Comm<Ordinal>& comm, const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
348  const Ordinal sendCount, const Packet sendBuffer[] ,
349  const Ordinal recvCounts[], Packet myGlobalReducts[]
350  );
351 
357 template<typename Ordinal, typename Packet>
358 void reduceAllAndScatter(
359  const Comm<Ordinal>& comm, const EReductionType reductType,
360  const Ordinal sendCount, const Packet sendBuffer[] ,
361  const Ordinal recvCounts[], Packet myGlobalReducts[]
362  );
363 
369 template<typename Ordinal, typename Packet>
370 void reduceAllAndScatter(
371  const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
373  const Ordinal sendCount, const Packet*const sendBuffer[] ,
374  const Ordinal recvCounts[], Packet*const myGlobalReducts[]
375  );
376 
382 template<typename Ordinal, typename Packet>
383 void scan(
384  const Comm<Ordinal>& comm, const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
385  const Ordinal count, const Packet sendBuffer[], Packet scanReducts[]
386  );
387 
393 template<typename Ordinal, typename Packet, typename Serializer>
394 void reduceAllAndScatter(
395  const Comm<Ordinal>& comm,
396  const Serializer& serializer,
397  const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
398  const Ordinal sendCount, const Packet sendBuffer[],
399  const Ordinal recvCounts[], Packet myGlobalReducts[]
400  );
401 
407 template<typename Ordinal, typename Packet, typename Serializer>
408 void reduceAllAndScatter(
409  const Comm<Ordinal>& comm,
410  const Serializer& serializer,
411  const EReductionType reductType,
412  const Ordinal sendCount, const Packet sendBuffer[] ,
413  const Ordinal recvCounts[], Packet myGlobalReducts[]
414  );
415 
421 template<typename Ordinal, typename Packet>
422 void scan(
423  const Comm<Ordinal>& comm, const EReductionType reductType,
424  const Ordinal count, const Packet sendBuffer[], Packet scanReducts[]
425  );
426 
432 template<typename Ordinal, typename Packet>
433 void scan(
434  const Comm<Ordinal>& comm, const EReductionType reductType,
435  const Packet &send, const Ptr<Packet> &scanReduct
436  );
437 
439 template<typename Ordinal, typename Packet>
440 TEUCHOS_DEPRECATED void scan(
441  const Comm<Ordinal>& comm, const EReductionType reductType,
442  const Packet &send, Packet *scanReduct
443  )
444 {
445  scan(comm, reductType, send, ptr(scanReduct));
446 }
447 
453 template<typename Ordinal, typename Packet>
454 void scan(
455  const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
457  const Ordinal count, const Packet*const sendBuffer[], Packet*const scanReducts[]
458  );
459 
465 template<typename Ordinal, typename Packet, typename Serializer>
466 void scan(
467  const Comm<Ordinal>& comm,
468  const Serializer& serializer,
469  const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
470  const Ordinal count, const Packet sendBuffer[], Packet scanReducts[]
471  );
472 
478 template<typename Ordinal, typename Packet, typename Serializer>
479 void scan(
480  const Comm<Ordinal>& comm,
481  const Serializer& serializer,
482  const EReductionType reductType,
483  const Ordinal count, const Packet sendBuffer[], Packet scanReducts[]
484  );
485 
490 template<typename Ordinal, typename Packet>
491 void send(
492  const Comm<Ordinal>& comm,
493  const Ordinal count, const Packet sendBuffer[], const int destRank
494  );
495 
497 template<typename Ordinal, typename Packet>
498 void
499 send (const Packet sendBuffer[],
500  const Ordinal count,
501  const int destRank,
502  const int tag,
503  const Comm<Ordinal>& comm);
504 
509 template<typename Ordinal, typename Packet>
510 void ssend(
511  const Comm<Ordinal>& comm,
512  const Ordinal count, const Packet sendBuffer[], const int destRank
513  );
514 
516 template<typename Ordinal, typename Packet>
517 void
518 ssend (const Packet sendBuffer[],
519  const Ordinal count,
520  const int destRank,
521  const int tag,
522  const Comm<Ordinal>& comm);
523 
528 template<typename Ordinal, typename Packet>
529 void send(
530  const Comm<Ordinal>& comm,
531  const Packet &send, const int destRank
532  );
533 
538 template<typename Ordinal, typename Packet>
539 void ssend(
540  const Comm<Ordinal>& comm,
541  const Packet &send, const int destRank
542  );
543 
550 template<typename Ordinal, typename Packet>
551 void send(
552  const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
553  const Ordinal count, const Packet*const sendBuffer[], const int destRank
554  );
555 
561 template<typename Ordinal, typename Packet, typename Serializer>
562 void send(
563  const Comm<Ordinal>& comm,
564  const Serializer& serializer,
565  const Ordinal count, const Packet sendBuffer[], const int destRank
566  );
567 
572 template<typename Ordinal, typename Packet>
573 int receive(
574  const Comm<Ordinal>& comm,
575  const int sourceRank, const Ordinal count, Packet recvBuffer[]
576  );
577 
582 template<typename Ordinal, typename Packet>
583 int receive(
584  const Comm<Ordinal>& comm,
585  const int sourceRank, Packet *recv
586  );
587 
592 template<typename Ordinal, typename Packet>
593 int receive(
594  const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
595  const int sourceRank, const Ordinal count, Packet*const recvBuffer[]
596  );
597 
603 template<typename Ordinal, typename Packet, typename Serializer>
604 int receive(
605  const Comm<Ordinal>& comm,
606  const Serializer& serializer,
607  const int sourceRank, const Ordinal count, Packet recvBuffer[]
608  );
609 
615 template<typename Ordinal, typename Packet>
616 void readySend(
617  const Comm<Ordinal>& comm,
618  const ArrayView<const Packet> &sendBuffer,
619  const int destRank
620  );
621 
623 template<typename Ordinal, typename Packet>
624 void
625 readySend (const Packet sendBuffer[],
626  const Ordinal count,
627  const int destRank,
628  const int tag,
629  const Comm<Ordinal>& comm);
630 
635 template<typename Ordinal, typename Packet>
636 void readySend(
637  const Comm<Ordinal>& comm,
638  const Packet &send,
639  const int destRank
640  );
641 
647 template<typename Ordinal, typename Packet, typename Serializer>
648 void readySend(
649  const Comm<Ordinal>& comm,
650  const Serializer& serializer,
651  const ArrayView<const Packet> &sendBuffer,
652  const int destRank
653  );
654 
659 template<typename Ordinal, typename Packet>
661  const Comm<Ordinal>& comm,
662  const ArrayRCP<const Packet> &sendBuffer,
663  const int destRank
664  );
665 
667 template<typename Ordinal, typename Packet>
669 isend (const ArrayRCP<const Packet>& sendBuffer,
670  const int destRank,
671  const int tag,
672  const Comm<Ordinal>& comm);
673 
678 template<typename Ordinal, typename Packet>
680  const Comm<Ordinal>& comm,
681  const RCP<const Packet> &send,
682  const int destRank
683  );
684 
690 template<typename Ordinal, typename Packet, typename Serializer>
692  const Comm<Ordinal>& comm,
693  const Serializer& serializer,
694  const ArrayRCP<const Packet> &sendBuffer,
695  const int destRank
696  );
697 
698 
699 // 2008/07/29: rabartl: ToDo: Add reference semantics version of isend!
700 
701 
711 template<typename Ordinal, typename Packet>
713  const Comm<Ordinal>& comm,
714  const ArrayRCP<Packet> &recvBuffer,
715  const int sourceRank
716  );
717 
719 template<typename Ordinal, typename Packet>
721 ireceive (const ArrayRCP<Packet> &recvBuffer,
722  const int sourceRank,
723  const int tag,
724  const Comm<Ordinal>& comm);
725 
737 template<typename Ordinal, typename Packet>
739  const Comm<Ordinal>& comm,
740  const RCP<Packet> &recv,
741  const int sourceRank
742  );
743 
749 template<typename Ordinal, typename Packet, typename Serializer>
751  const Comm<Ordinal>& comm,
752  const Serializer& serializer,
753  const ArrayRCP<Packet> &recvBuffer,
754  const int sourceRank
755  );
756 
757 
758 // 2008/07/29: rabartl: ToDo: Add reference semantics version of ireceive!
759 
760 
768 template<typename Ordinal>
769 void waitAll(
770  const Comm<Ordinal>& comm,
771  const ArrayView<RCP<CommRequest<Ordinal> > > &requests
772  );
773 
803 template<typename Ordinal>
804 void
805 waitAll (const Comm<Ordinal>& comm,
806  const ArrayView<RCP<CommRequest<Ordinal> > >& requests,
807  const ArrayView<RCP<CommStatus<Ordinal> > >& statuses);
808 
832 template<typename Ordinal>
834 wait (const Comm<Ordinal>& comm, const Ptr<RCP<CommRequest<Ordinal> > >& request);
835 
836 //
837 // Standard reduction subclasses for objects that use value semantics
838 //
839 
840 
845 template<typename Ordinal, typename Packet>
846 class SumValueReductionOp : public ValueTypeReductionOp<Ordinal,Packet>
847 {
848 public:
850  void reduce(
851  const Ordinal count,
852  const Packet inBuffer[],
853  Packet inoutBuffer[]
854  ) const;
855 };
856 
857 
866 template<typename Ordinal, typename Packet>
867 class MinValueReductionOp : public ValueTypeReductionOp<Ordinal,Packet>
868 {
869 public:
871  void reduce(
872  const Ordinal count,
873  const Packet inBuffer[],
874  Packet inoutBuffer[]
875  ) const;
876 };
877 
878 
887 template<typename Ordinal, typename Packet>
888 class MaxValueReductionOp : public ValueTypeReductionOp<Ordinal,Packet>
889 {
890 public:
892  void reduce(
893  const Ordinal count,
894  const Packet inBuffer[],
895  Packet inoutBuffer[]
896  ) const;
897 };
898 
899 
904 template<typename Ordinal, typename Packet>
905 class ANDValueReductionOp : public ValueTypeReductionOp<Ordinal,Packet>
906 {
907 public:
909  void reduce(
910  const Ordinal count,
911  const Packet inBuffer[],
912  Packet inoutBuffer[]
913  ) const;
914 };
915 
916 
917 // ////////////////////////////////////////////////////////////
918 // Implementation details (not for geneal users to mess with)
919 
920 
921 //
922 // ReductionOp Utilities
923 //
924 
925 
926 namespace MixMaxUtilities {
927 
928 
929 template<bool isComparable, typename Ordinal, typename Packet>
930 class Min {};
931 
932 
933 template<typename Ordinal, typename Packet>
934 class Min<true,Ordinal,Packet> {
935 public:
936  static void min(
937  const Ordinal count,
938  const Packet inBuffer[],
939  Packet inoutBuffer[]
940  )
941  {
942  for( int i = 0; i < count; ++i )
943  inoutBuffer[i] = TEUCHOS_MIN(inoutBuffer[i],inBuffer[i]);
944  }
945 };
946 
947 
948 template<typename Ordinal, typename Packet>
949 class Min<false,Ordinal,Packet> {
950 public:
951  static void min(
952  const Ordinal,
953  const Packet[],
954  Packet[]
955  )
956  {
958  true,std::logic_error,
959  "Error, the type "<<TypeNameTraits<Packet>::name()
960  <<" does not support comparison operations!"
961  );
962  }
963 };
964 
965 
966 template<bool isComparable, typename Ordinal, typename Packet>
967 class Max {};
968 
969 
970 template<typename Ordinal, typename Packet>
971 class Max<true,Ordinal,Packet> {
972 public:
973  static void max(
974  const Ordinal count,
975  const Packet inBuffer[],
976  Packet inoutBuffer[]
977  )
978  {
979  for( int i = 0; i < count; ++i )
980  inoutBuffer[i] = TEUCHOS_MAX(inoutBuffer[i],inBuffer[i]);
981  }
982 };
983 
984 
985 template<typename Ordinal, typename Packet>
986 class Max<false,Ordinal,Packet> {
987 public:
988  static void max(
989  const Ordinal,
990  const Packet[],
991  Packet[]
992  )
993  {
995  true,std::logic_error,
996  "Error, the type "<<TypeNameTraits<Packet>::name()
997  <<" does not support comparison operations!"
998  );
999  }
1000 };
1001 
1002 
1003 template<bool isComparable, typename Ordinal, typename Packet>
1004 class AND {};
1005 
1006 
1007 template<typename Ordinal, typename Packet>
1008 class AND<true,Ordinal,Packet> {
1009 public:
1010  static void andOp(
1011  const Ordinal count,
1012  const Packet inBuffer[],
1013  Packet inoutBuffer[]
1014  )
1015  {
1016  for( int i = 0; i < count; ++i )
1017  inoutBuffer[i] = inoutBuffer[i] && inBuffer[i];
1018  }
1019 };
1020 
1021 
1022 template<typename Ordinal, typename Packet>
1023 class AND<false,Ordinal,Packet> {
1024 public:
1025  static void andOp(
1026  const Ordinal,
1027  const Packet[],
1028  Packet[]
1029  )
1030  {
1032  true,std::logic_error,
1033  "Error, the type "<<TypeNameTraits<Packet>::name()
1034  <<" does not support logical AND operations!"
1035  );
1036  }
1037 };
1038 
1039 
1040 } // namespace MixMaxUtilities
1041 
1042 
1043 template<typename Ordinal, typename Packet>
1045  const Ordinal count,
1046  const Packet inBuffer[],
1047  Packet inoutBuffer[]
1048  ) const
1049 {
1050  for( int i = 0; i < count; ++i )
1051  inoutBuffer[i] += inBuffer[i];
1052 }
1053 
1054 
1055 template<typename Ordinal, typename Packet>
1057  const Ordinal count,
1058  const Packet inBuffer[],
1059  Packet inoutBuffer[]
1060  ) const
1061 {
1062  typedef MixMaxUtilities::Min<ScalarTraits<Packet>::isComparable, Ordinal, Packet> min_type;
1063  min_type::min (count, inBuffer, inoutBuffer);
1064 }
1065 
1066 
1067 template<typename Ordinal, typename Packet>
1069  const Ordinal count,
1070  const Packet inBuffer[],
1071  Packet inoutBuffer[]
1072  ) const
1073 {
1074  typedef MixMaxUtilities::Max<ScalarTraits<Packet>::isComparable, Ordinal, Packet> max_type;
1075  max_type::max (count,inBuffer,inoutBuffer);
1076 }
1077 
1078 
1079 template<typename Ordinal, typename Packet>
1081  const Ordinal count,
1082  const Packet inBuffer[],
1083  Packet inoutBuffer[]
1084  ) const
1085 {
1086  typedef MixMaxUtilities::AND<ScalarTraits<Packet>::isComparable, Ordinal, Packet> and_type;
1087  and_type::andOp (count, inBuffer, inoutBuffer);
1088 }
1089 
1090 
1091 } // namespace Teuchos
1092 
1093 
1094 // //////////////////////////
1095 // Template implemenations
1096 
1097 
1098 //
1099 // ReductionOp utilities
1100 //
1101 
1102 
1103 namespace Teuchos {
1104 
1105 
1106 // Not for the general user to use! I am returning a raw ReducionOp* pointer
1107 // to avoid the overhead of using RCP. However, given the use case
1108 // this is just fine since I can just use std::auto_ptr to make sure things
1109 // are deleted correctly.
1110 template<typename Ordinal, typename Packet>
1112 createOp( const EReductionType reductType )
1113 {
1114  typedef ScalarTraits<Packet> ST;
1115  switch(reductType) {
1116  case REDUCE_SUM: {
1118  break;
1119  }
1120  case REDUCE_MIN: {
1121  TEUCHOS_TEST_FOR_EXCEPT(!ST::isComparable);
1123  break;
1124  }
1125  case REDUCE_MAX: {
1126  TEUCHOS_TEST_FOR_EXCEPT(!ST::isComparable);
1128  break;
1129  }
1130  case REDUCE_AND: {
1132  break;
1133  }
1134  default:
1136  }
1137  return 0; // Will never be called!
1138 }
1139 
1140 
1141 } // namespace Teuchos
1142 
1143 
1144 //
1145 // Teuchos::Comm wrapper functions
1146 //
1147 
1148 
1149 template<typename Ordinal>
1150 int Teuchos::rank(const Comm<Ordinal>& comm)
1151 {
1152  return comm.getRank();
1153 }
1154 
1155 
1156 template<typename Ordinal>
1157 int Teuchos::size(const Comm<Ordinal>& comm)
1158 {
1159  return comm.getSize();
1160 }
1161 
1162 
1163 template<typename Ordinal>
1164 void Teuchos::barrier(const Comm<Ordinal>& comm)
1165 {
1166  TEUCHOS_COMM_TIME_MONITOR(
1167  "Teuchos::CommHelpers: barrier<"
1169  <<">()"
1170  );
1171  comm.barrier();
1172 }
1173 
1174 
1175 template<typename Ordinal, typename Packet>
1176 void Teuchos::broadcast(
1177  const Comm<Ordinal>& comm,
1178  const int rootRank, const Ordinal count, Packet buffer[]
1179  )
1180 {
1181  TEUCHOS_COMM_TIME_MONITOR(
1182  "Teuchos::CommHelpers: broadcast<"
1184  <<">( value type )"
1185  );
1187  charBuffer(count,buffer);
1188  comm.broadcast(
1189  rootRank,charBuffer.getBytes(),charBuffer.getCharBuffer()
1190  );
1191 }
1192 
1193 
1194 template<typename Ordinal, typename Packet>
1195 void Teuchos::broadcast(
1196  const Comm<Ordinal>& comm,
1197  const int rootRank,
1198  const ArrayView<Packet> &buffer
1199  )
1200 {
1201  broadcast<Ordinal, Packet>(comm, rootRank, buffer.size(), buffer.getRawPtr() );
1202 }
1203 
1204 
1205 template<typename Ordinal, typename Packet>
1206 void Teuchos::broadcast(
1207  const Comm<Ordinal>& comm,
1208  const int rootRank, Packet *object
1209  )
1210 {
1211  broadcast<Ordinal,Packet>(comm,rootRank,1,object);
1212 }
1213 
1214 
1215 template<typename Ordinal, typename Packet>
1216 void Teuchos::broadcast(
1217  const Comm<Ordinal>& comm,
1218  const int rootRank, const Ptr<Packet> &object
1219  )
1220 {
1221  broadcast<Ordinal,Packet>(comm,rootRank,1,object.getRawPtr());
1222 }
1223 
1224 
1225 template<typename Ordinal, typename Packet>
1226 void Teuchos::broadcast(
1227  const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
1228  const int rootRank, const Ordinal count, Packet*const buffer[]
1229  )
1230 {
1231  TEUCHOS_COMM_TIME_MONITOR(
1232  "Teuchos::CommHelpers: broadcast<"
1234  <<">( reference type )"
1235  );
1237  charBuffer(serializer, count, buffer);
1238  comm.broadcast(
1239  rootRank,charBuffer.getBytes(),charBuffer.getCharBuffer()
1240  );
1241 }
1242 
1243 
1244 template<typename Ordinal, typename Packet>
1245 void Teuchos::broadcast(
1246  const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
1247  const int rootRank, const ArrayView<const Ptr<Packet> > &buffer
1248  )
1249 {
1250  Array<Packet*> bufferPtrArray;
1251  for (int i = 0; i < buffer.size(); ++i) {
1252  bufferPtrArray.push_back(buffer[i].getRawPtr());
1253  }
1254  broadcast<Ordinal,Packet>(comm, serializer, rootRank,
1255  buffer.size(), bufferPtrArray.getRawPtr());
1256 }
1257 
1258 template<typename Ordinal, typename Packet, typename Serializer>
1259 void Teuchos::broadcast(
1260  const Comm<Ordinal>& comm,
1261  const Serializer& serializer,
1262  const int rootRank, const Ordinal count, Packet buffer[]
1263  )
1264 {
1265  TEUCHOS_COMM_TIME_MONITOR(
1266  "Teuchos::CommHelpers: broadcast<"
1268  <<">( value type )"
1269  );
1271  charBuffer(count,buffer,rcp(&serializer,false));
1272  comm.broadcast(
1273  rootRank,charBuffer.getBytes(),charBuffer.getCharBuffer()
1274  );
1275 }
1276 
1277 
1278 template<typename Ordinal, typename Packet>
1279 void Teuchos::gatherAll(
1280  const Comm<Ordinal>& comm,
1281  const Ordinal sendCount, const Packet sendBuffer[],
1282  const Ordinal recvCount, Packet recvBuffer[]
1283  )
1284 {
1285  TEUCHOS_COMM_TIME_MONITOR(
1286  "Teuchos::CommHelpers: gatherAll<"
1288  <<">( value type )"
1289  );
1291  charSendBuffer(sendCount,sendBuffer);
1293  charRecvBuffer(recvCount,recvBuffer);
1294  comm.gatherAll(
1295  charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
1296  ,charRecvBuffer.getBytes(),charRecvBuffer.getCharBuffer()
1297  );
1298 }
1299 
1300 template<typename Ordinal, typename Packet>
1301 void
1302 Teuchos::gather (const Packet sendBuf[],
1303  const Ordinal sendCount,
1304  Packet recvBuf[],
1305  const Ordinal recvCount,
1306  const int root,
1307  const Comm<Ordinal>& comm)
1308 {
1309  TEUCHOS_COMM_TIME_MONITOR(
1310  "Teuchos::CommHelpers: gather<"
1312  <<">( value type )"
1313  );
1315  charSendBuffer (sendCount, sendBuf);
1317  charRecvBuffer (recvCount, recvBuf);
1318  comm.gather (charSendBuffer.getBytes (),
1319  charSendBuffer.getCharBuffer (),
1320  charRecvBuffer.getBytes (),
1321  charRecvBuffer.getCharBuffer (),
1322  root);
1323 }
1324 
1325 template<typename Ordinal, typename Packet>
1326 void
1327 Teuchos::gatherv (const Packet sendBuf[],
1328  const Ordinal sendCount,
1329  Packet recvBuf[],
1330  const Ordinal recvCounts[],
1331  const Ordinal displs[],
1332  const int root,
1333  const Comm<Ordinal>& comm)
1334 {
1335  // Ordinal totalRecvCount = 0;
1336 
1337  // // In order to get the right output buffer length, we have to sum
1338  // // the receive counts from all the processes in the communicator.
1339  // const Ordinal numProcs = as<Ordinal> (comm->getSize ());
1340  // for (Ordinal k = 0; k < as<Ordinal> (numProcs); ++k) {
1341  // totalRecvCount += recvCounts[k];
1342  // }
1343 
1344  // // FIXME (mfh 16 Apr 2013) We also have to redo the displacements.
1345 
1346  // ConstValueTypeSerializationBuffer<Ordinal,Packet>
1347  // charSendBuffer (sendCount, sendBuf);
1348  // ValueTypeSerializationBuffer<Ordinal,Packet>
1349  // charRecvBuffer (totalRecvCount, recvBuf);
1350  // comm.gatherv (charSendBuffer.getBytes (),
1351  // charSendBuffer.getCharBuffer (),
1352  // charRecvBuffer.getBytes (),
1353  // charRecvBuffer.getCharBuffer (),
1354  // root);
1355  TEUCHOS_TEST_FOR_EXCEPTION(true, std::logic_error,
1356  "Teuchos::gatherv: The general case is not implemented.");
1357 }
1358 
1359 template<typename Ordinal, typename Packet>
1360 void Teuchos::gatherAll(
1361  const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
1362  const Ordinal sendCount, const Packet*const sendBuffer[],
1363  const Ordinal recvCount, Packet*const recvBuffer[]
1364  )
1365 {
1366  TEUCHOS_TEST_FOR_EXCEPT(true); // ToDo: Implement and test when needed!
1367 }
1368 
1369 template<typename Ordinal, typename Packet, typename Serializer>
1370 void Teuchos::gatherAll(
1371  const Comm<Ordinal>& comm,
1372  const Serializer& serializer,
1373  const Ordinal sendCount, const Packet sendBuffer[],
1374  const Ordinal recvCount, Packet recvBuffer[]
1375  )
1376 {
1377  TEUCHOS_COMM_TIME_MONITOR(
1378  "Teuchos::CommHelpers: gatherAll<"
1380  <<">( value type )"
1381  );
1383  charSendBuffer(sendCount,sendBuffer,rcp(&serializer,false));
1385  charRecvBuffer(recvCount,recvBuffer,rcp(&serializer,false));
1386  comm.gatherAll(
1387  charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
1388  ,charRecvBuffer.getBytes(),charRecvBuffer.getCharBuffer()
1389  );
1390 }
1391 
1392 
1393 template<typename Ordinal, typename Packet>
1394 void Teuchos::reduceAll(
1395  const Comm<Ordinal>& comm, const ValueTypeReductionOp<Ordinal,Packet> &reductOp
1396  ,const Ordinal count, const Packet sendBuffer[], Packet globalReducts[]
1397  )
1398 {
1399  TEUCHOS_COMM_TIME_MONITOR(
1400  "Teuchos::CommHelpers: reduceAll<"
1402  <<">( value type, user-defined op )"
1403  );
1405  charSendBuffer(count,sendBuffer);
1407  charGlobalReducts(count,globalReducts);
1409  charReductOp(rcp(&reductOp,false));
1410  comm.reduceAll(
1411  charReductOp,charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
1412  ,charGlobalReducts.getCharBuffer()
1413  );
1414 }
1415 
1416 
1417 template<typename Ordinal, typename Packet>
1418 void Teuchos::reduceAll(
1419  const Comm<Ordinal>& comm, const EReductionType reductType,
1420  const Ordinal count, const Packet sendBuffer[], Packet globalReducts[]
1421  )
1422 {
1423  TEUCHOS_COMM_TIME_MONITOR(
1424  "Teuchos::CommHelpers: reduceAll<"
1426  <<">( value type, "<<toString(reductType)<<" )"
1427  );
1428  std::auto_ptr<ValueTypeReductionOp<Ordinal,Packet> >
1429  reductOp(createOp<Ordinal,Packet>(reductType));
1430  reduceAll(comm,*reductOp,count,sendBuffer,globalReducts);
1431 }
1432 
1433 
1434 namespace Teuchos {
1435 
1436 #ifdef TEUCHOS_HAVE_COMPLEX
1437 // Specialization for Ordinal=int and Packet=std::complex<double>.
1438 template<>
1439 TEUCHOSCOMM_LIB_DLL_EXPORT void
1440 reduceAll<int, std::complex<double> > (const Comm<int>& comm,
1441  const EReductionType reductType,
1442  const int count,
1443  const std::complex<double> sendBuffer[],
1444  std::complex<double> globalReducts[]);
1445 template<>
1446 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1447 ireceive<int, std::complex<double> > (const Comm<int>& comm,
1448  const ArrayRCP<std::complex<double> >& recvBuffer,
1449  const int sourceRank);
1450 template<>
1451 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1452 ireceive<int, std::complex<double> > (const ArrayRCP<std::complex<double> > &recvBuffer,
1453  const int sourceRank,
1454  const int tag,
1455  const Comm<int>& comm);
1456 template<>
1457 TEUCHOSCOMM_LIB_DLL_EXPORT void
1458 send<int, std::complex<double> > (const Comm<int>& comm,
1459  const int count,
1460  const std::complex<double> sendBuffer[],
1461  const int destRank);
1462 template<>
1463 TEUCHOSCOMM_LIB_DLL_EXPORT void
1464 send<int, std::complex<double> > (const std::complex<double> sendBuffer[],
1465  const int count,
1466  const int destRank,
1467  const int tag,
1468  const Comm<int>& comm);
1469 template<>
1470 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1471 isend<int, std::complex<double> > (const ArrayRCP<const std::complex<double> >& sendBuffer,
1472  const int destRank,
1473  const int tag,
1474  const Comm<int>& comm);
1475 
1476 // Specialization for Ordinal=int and Packet=std::complex<float>.
1477 template<>
1478 TEUCHOSCOMM_LIB_DLL_EXPORT void
1479 reduceAll<int, std::complex<float> > (const Comm<int>& comm,
1480  const EReductionType reductType,
1481  const int count,
1482  const std::complex<float> sendBuffer[],
1483  std::complex<float> globalReducts[]);
1484 template<>
1485 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1486 ireceive<int, std::complex<float> > (const Comm<int>& comm,
1487  const ArrayRCP<std::complex<float> >& recvBuffer,
1488  const int sourceRank);
1489 template<>
1490 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1491 ireceive<int, std::complex<float> > (const ArrayRCP<std::complex<float> > &recvBuffer,
1492  const int sourceRank,
1493  const int tag,
1494  const Comm<int>& comm);
1495 template<>
1496 TEUCHOSCOMM_LIB_DLL_EXPORT void
1497 send<int, std::complex<float> > (const Comm<int>& comm,
1498  const int count,
1499  const std::complex<float> sendBuffer[],
1500  const int destRank);
1501 template<>
1502 TEUCHOSCOMM_LIB_DLL_EXPORT void
1503 send<int, std::complex<float> > (const std::complex<float> sendBuffer[],
1504  const int count,
1505  const int destRank,
1506  const int tag,
1507  const Comm<int>& comm);
1508 template<>
1509 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1510 isend<int, std::complex<float> > (const ArrayRCP<const std::complex<float> >& sendBuffer,
1511  const int destRank,
1512  const int tag,
1513  const Comm<int>& comm);
1514 #endif // TEUCHOS_HAVE_COMPLEX
1515 
1516 // Specialization for Ordinal=int and Packet=double.
1517 template<>
1518 TEUCHOSCOMM_LIB_DLL_EXPORT void
1519 reduceAll<int, double> (const Comm<int>& comm,
1520  const EReductionType reductType,
1521  const int count,
1522  const double sendBuffer[],
1523  double globalReducts[]);
1524 template<>
1525 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1526 ireceive<int, double> (const Comm<int>& comm,
1527  const ArrayRCP<double>& recvBuffer,
1528  const int sourceRank);
1529 template<>
1530 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1531 ireceive<int, double> (const ArrayRCP<double> &recvBuffer,
1532  const int sourceRank,
1533  const int tag,
1534  const Comm<int>& comm);
1535 template<>
1536 TEUCHOSCOMM_LIB_DLL_EXPORT void
1537 send<int, double> (const Comm<int>& comm,
1538  const int count,
1539  const double sendBuffer[],
1540  const int destRank);
1541 template<>
1542 TEUCHOSCOMM_LIB_DLL_EXPORT void
1543 send<int, double> (const double sendBuffer[],
1544  const int count,
1545  const int destRank,
1546  const int tag,
1547  const Comm<int>& comm);
1548 template<>
1549 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1550 isend<int, double> (const ArrayRCP<const double>& sendBuffer,
1551  const int destRank,
1552  const int tag,
1553  const Comm<int>& comm);
1554 
1555 // Specialization for Ordinal=int and Packet=float.
1556 template<>
1557 TEUCHOSCOMM_LIB_DLL_EXPORT void
1558 reduceAll<int, float> (const Comm<int>& comm,
1559  const EReductionType reductType,
1560  const int count,
1561  const float sendBuffer[],
1562  float globalReducts[]);
1563 template<>
1564 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1565 ireceive<int, float> (const Comm<int>& comm,
1566  const ArrayRCP<float>& recvBuffer,
1567  const int sourceRank);
1568 template<>
1569 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1570 ireceive<int, float> (const ArrayRCP<float> &recvBuffer,
1571  const int sourceRank,
1572  const int tag,
1573  const Comm<int>& comm);
1574 template<>
1575 TEUCHOSCOMM_LIB_DLL_EXPORT void
1576 send<int, float> (const Comm<int>& comm,
1577  const int count,
1578  const float sendBuffer[],
1579  const int destRank);
1580 template<>
1581 TEUCHOSCOMM_LIB_DLL_EXPORT void
1582 send<int, float> (const float sendBuffer[],
1583  const int count,
1584  const int destRank,
1585  const int tag,
1586  const Comm<int>& comm);
1587 template<>
1588 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1589 isend<int, float> (const ArrayRCP<const float>& sendBuffer,
1590  const int destRank,
1591  const int tag,
1592  const Comm<int>& comm);
1593 
1594 #ifdef TEUCHOS_HAVE_LONG_LONG_INT
1595 // Specialization for Ordinal=int and Packet=long long.
1596 template<>
1597 TEUCHOSCOMM_LIB_DLL_EXPORT void
1598 gather<int, long long> (const long long sendBuf[],
1599  const int sendCount,
1600  long long recvBuf[],
1601  const int recvCount,
1602  const int root,
1603  const Comm<int>& comm);
1604 template<>
1605 TEUCHOSCOMM_LIB_DLL_EXPORT void
1606 gatherv<int, long long> (const long long sendBuf[],
1607  const int sendCount,
1608  long long recvBuf[],
1609  const int recvCounts[],
1610  const int displs[],
1611  const int root,
1612  const Comm<int>& comm);
1613 template<>
1614 TEUCHOSCOMM_LIB_DLL_EXPORT void
1615 reduceAll<int, long long> (const Comm<int>& comm,
1616  const EReductionType reductType,
1617  const int count,
1618  const long long sendBuffer[],
1619  long long globalReducts[]);
1620 template<>
1621 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1622 ireceive<int, long long> (const Comm<int>& comm,
1623  const ArrayRCP<long long>& recvBuffer,
1624  const int sourceRank);
1625 template<>
1626 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1627 ireceive<int, long long> (const ArrayRCP<long long> &recvBuffer,
1628  const int sourceRank,
1629  const int tag,
1630  const Comm<int>& comm);
1631 template<>
1632 TEUCHOSCOMM_LIB_DLL_EXPORT void
1633 send<int, long long> (const Comm<int>& comm,
1634  const int count,
1635  const long long sendBuffer[],
1636  const int destRank);
1637 template<>
1638 TEUCHOSCOMM_LIB_DLL_EXPORT void
1639 send<int, long long> (const long long sendBuffer[],
1640  const int count,
1641  const int destRank,
1642  const int tag,
1643  const Comm<int>& comm);
1644 template<>
1645 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1646 isend<int, long long> (const ArrayRCP<const long long>& sendBuffer,
1647  const int destRank,
1648  const int tag,
1649  const Comm<int>& comm);
1650 #endif // TEUCHOS_HAVE_LONG_LONG_INT
1651 
1652 // Specialization for Ordinal=int and Packet=long.
1653 template<>
1654 TEUCHOSCOMM_LIB_DLL_EXPORT void
1655 gather<int, long> (const long sendBuf[],
1656  const int sendCount,
1657  long recvBuf[],
1658  const int recvCount,
1659  const int root,
1660  const Comm<int>& comm);
1661 template<>
1662 TEUCHOSCOMM_LIB_DLL_EXPORT void
1663 gatherv<int, long> (const long sendBuf[],
1664  const int sendCount,
1665  long recvBuf[],
1666  const int recvCounts[],
1667  const int displs[],
1668  const int root,
1669  const Comm<int>& comm);
1670 template<>
1671 TEUCHOSCOMM_LIB_DLL_EXPORT void
1672 reduceAll<int, long> (const Comm<int>& comm,
1673  const EReductionType reductType,
1674  const int count,
1675  const long sendBuffer[],
1676  long globalReducts[]);
1677 template<>
1678 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1679 ireceive<int, long> (const Comm<int>& comm,
1680  const ArrayRCP<long>& recvBuffer,
1681  const int sourceRank);
1682 template<>
1683 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1684 ireceive<int, long> (const ArrayRCP<long> &recvBuffer,
1685  const int sourceRank,
1686  const int tag,
1687  const Comm<int>& comm);
1688 template<>
1689 TEUCHOSCOMM_LIB_DLL_EXPORT void
1690 send<int, long> (const Comm<int>& comm,
1691  const int count,
1692  const long sendBuffer[],
1693  const int destRank);
1694 template<>
1695 TEUCHOSCOMM_LIB_DLL_EXPORT void
1696 send<int, long> (const long sendBuffer[],
1697  const int count,
1698  const int destRank,
1699  const int tag,
1700  const Comm<int>& comm);
1701 template<>
1702 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1703 isend<int, long> (const ArrayRCP<const long>& sendBuffer,
1704  const int destRank,
1705  const int tag,
1706  const Comm<int>& comm);
1707 
1708 // Specialization for Ordinal=int and Packet=int.
1709 template<>
1710 TEUCHOSCOMM_LIB_DLL_EXPORT void
1711 gather<int, int> (const int sendBuf[],
1712  const int sendCount,
1713  int recvBuf[],
1714  const int recvCount,
1715  const int root,
1716  const Comm<int>& comm);
1717 template<>
1718 TEUCHOSCOMM_LIB_DLL_EXPORT void
1719 gatherv<int, int> (const int sendBuf[],
1720  const int sendCount,
1721  int recvBuf[],
1722  const int recvCounts[],
1723  const int displs[],
1724  const int root,
1725  const Comm<int>& comm);
1726 template<>
1727 TEUCHOSCOMM_LIB_DLL_EXPORT void
1728 reduceAll<int, int> (const Comm<int>& comm,
1729  const EReductionType reductType,
1730  const int count,
1731  const int sendBuffer[],
1732  int globalReducts[]);
1733 // mfh 09 Apr 2013: We provide a full specialization of
1734 // reduceAllAndScatter, because it is an important part of
1735 // Tpetra::Distributor initialization.
1736 template<>
1737 TEUCHOSCOMM_LIB_DLL_EXPORT void
1738 reduceAllAndScatter<int, int> (const Comm<int>& comm,
1739  const EReductionType reductType,
1740  const int sendCount,
1741  const int sendBuffer[],
1742  const int recvCounts[],
1743  int myGlobalReducts[]);
1744 template<>
1745 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1746 ireceive<int, int> (const Comm<int>& comm,
1747  const ArrayRCP<int>& recvBuffer,
1748  const int sourceRank);
1749 template<>
1750 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1751 ireceive<int, int> (const ArrayRCP<int> &recvBuffer,
1752  const int sourceRank,
1753  const int tag,
1754  const Comm<int>& comm);
1755 template<>
1756 TEUCHOSCOMM_LIB_DLL_EXPORT void
1757 send<int, int> (const Comm<int>& comm,
1758  const int count,
1759  const int sendBuffer[],
1760  const int destRank);
1761 template<>
1762 TEUCHOSCOMM_LIB_DLL_EXPORT void
1763 send<int, int> (const int sendBuffer[],
1764  const int count,
1765  const int destRank,
1766  const int tag,
1767  const Comm<int>& comm);
1768 template<>
1769 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1770 isend<int, int> (const ArrayRCP<const int>& sendBuffer,
1771  const int destRank,
1772  const int tag,
1773  const Comm<int>& comm);
1774 
1775 // Specialization for Ordinal=int and Packet=short.
1776 template<>
1777 TEUCHOSCOMM_LIB_DLL_EXPORT void
1778 gather<int, short> (const short sendBuf[],
1779  const int sendCount,
1780  short recvBuf[],
1781  const int recvCount,
1782  const int root,
1783  const Comm<int>& comm);
1784 template<>
1785 TEUCHOSCOMM_LIB_DLL_EXPORT void
1786 gatherv<int, short> (const short sendBuf[],
1787  const int sendCount,
1788  short recvBuf[],
1789  const int recvCounts[],
1790  const int displs[],
1791  const int root,
1792  const Comm<int>& comm);
1793 template<>
1794 TEUCHOSCOMM_LIB_DLL_EXPORT void
1795 reduceAll<int, short> (const Comm<int>& comm,
1796  const EReductionType reductType,
1797  const int count,
1798  const short sendBuffer[],
1799  short globalReducts[]);
1800 template<>
1801 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1802 ireceive<int, short> (const Comm<int>& comm,
1803  const ArrayRCP<short>& recvBuffer,
1804  const int sourceRank);
1805 template<>
1806 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1807 ireceive<int, short> (const ArrayRCP<short> &recvBuffer,
1808  const int sourceRank,
1809  const int tag,
1810  const Comm<int>& comm);
1811 template<>
1812 TEUCHOSCOMM_LIB_DLL_EXPORT void
1813 send<int, short> (const Comm<int>& comm,
1814  const int count,
1815  const short sendBuffer[],
1816  const int destRank);
1817 template<>
1818 TEUCHOSCOMM_LIB_DLL_EXPORT void
1819 send<int, short> (const short sendBuffer[],
1820  const int count,
1821  const int destRank,
1822  const int tag,
1823  const Comm<int>& comm);
1824 template<>
1825 TEUCHOSCOMM_LIB_DLL_EXPORT RCP<CommRequest<int> >
1826 isend<int, short> (const ArrayRCP<const short>& sendBuffer,
1827  const int destRank,
1828  const int tag,
1829  const Comm<int>& comm);
1830 
1831 // mfh 18 Oct 2012: The specialization for Packet=char seems to be
1832 // causing problems such as the following:
1833 //
1834 // http://testing.sandia.gov/cdash/testDetails.php?test=9909246&build=747699
1835 //
1836 // I am disabling it for now. This should revert back to the old
1837 // behavior for Packet=char. That should fix the Tpetra errors, since
1838 // many Tpetra objects inherit from DistObject<char, ...>.
1839 #if 0
1840 // Specialization for Ordinal=int and Packet=char.
1841 template<>
1842 TEUCHOSCOMM_LIB_DLL_EXPORT void
1843 reduceAll<int, char> (const Comm<int>& comm,
1844  const EReductionType reductType,
1845  const int count,
1846  const char sendBuffer[],
1847  char globalReducts[]);
1848 #endif // 0
1849 } // namespace Teuchos
1850 
1851 
1852 template<typename Ordinal, typename Packet>
1853 void Teuchos::reduceAll(
1854  const Comm<Ordinal>& comm, const EReductionType reductType
1855  ,const Packet &send, const Ptr<Packet> &globalReduct
1856  )
1857 {
1858  // mfh 17 Oct 2012: This will invoke the above specializations for
1859  // general count, so we don't need to specialize this function.
1860  reduceAll<Ordinal,Packet>(comm, reductType, 1, &send, &*globalReduct);
1861 }
1862 
1863 
1864 template<typename Ordinal, typename Packet>
1865 void Teuchos::reduceAll(
1866  const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
1868  const Ordinal count, const Packet*const sendBuffer[], Packet*const globalReducts[]
1869  )
1870 {
1871  TEUCHOS_COMM_TIME_MONITOR(
1872  "Teuchos::CommHelpers: reduceAll<"
1874  <<">( reference type )"
1875  );
1877  charSendBuffer(serializer,count,sendBuffer);
1879  charGlobalReducts(serializer,count,globalReducts);
1881  charReductOp(rcp(&serializer,false),rcp(&reductOp,false));
1882  comm.reduceAll(
1883  charReductOp,charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
1884  ,charGlobalReducts.getCharBuffer()
1885  );
1886 }
1887 
1888 template<typename Ordinal, typename Packet, typename Serializer>
1889 void Teuchos::reduceAll(
1890  const Comm<Ordinal>& comm,
1891  const Serializer& serializer,
1892  const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
1893  const Ordinal count, const Packet sendBuffer[], Packet globalReducts[]
1894  )
1895 {
1896  TEUCHOS_COMM_TIME_MONITOR(
1897  "Teuchos::CommHelpers: reduceAll<"
1899  <<">( value type, user-defined op )"
1900  );
1902  charSendBuffer(count,sendBuffer,rcp(&serializer,false));
1904  charGlobalReducts(count,globalReducts,rcp(&serializer,false));
1906  charReductOp(rcp(&reductOp,false),rcp(&serializer,false));
1907  comm.reduceAll(
1908  charReductOp,charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
1909  ,charGlobalReducts.getCharBuffer()
1910  );
1911 }
1912 
1913 
1914 template<typename Ordinal, typename Packet, typename Serializer>
1915 void Teuchos::reduceAll(
1916  const Comm<Ordinal>& comm,
1917  const Serializer& serializer,
1918  const EReductionType reductType,
1919  const Ordinal count, const Packet sendBuffer[], Packet globalReducts[]
1920  )
1921 {
1922  TEUCHOS_COMM_TIME_MONITOR(
1923  "Teuchos::CommHelpers: reduceAll<"
1925  <<">( value type, "<<toString(reductType)<<" )"
1926  );
1927  std::auto_ptr<ValueTypeReductionOp<Ordinal,Packet> >
1928  reductOp(createOp<Ordinal,Packet>(reductType));
1929  reduceAll(comm,serializer,*reductOp,count,sendBuffer,globalReducts);
1930 }
1931 
1932 
1933 template<typename Ordinal, typename Packet>
1934 void Teuchos::reduceAllAndScatter(
1935  const Comm<Ordinal>& comm, const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
1936  const Ordinal sendCount, const Packet sendBuffer[] ,
1937  const Ordinal recvCounts[], Packet myGlobalReducts[]
1938  )
1939 {
1940 
1941  TEUCHOS_COMM_TIME_MONITOR(
1942  "Teuchos::CommHelpers: reduceAllAndScatter<"
1944  <<">( value type, user-defined op )"
1945  );
1946 
1947  const Ordinal size = Teuchos::size(comm);
1948  const Ordinal rank = Teuchos::rank(comm);
1949 
1950 #ifdef TEUCHOS_DEBUG
1951  Ordinal sumRecvCounts = 0;
1952  for( Ordinal i = 0; i < size; ++i )
1953  sumRecvCounts += recvCounts[static_cast<ptrdiff_t>(i)];
1954  TEUCHOS_TEST_FOR_EXCEPT(!(sumRecvCounts==sendCount));
1955 #endif
1956 
1958  charSendBuffer(sendCount,sendBuffer);
1960  charMyGlobalReducts(recvCounts[static_cast<ptrdiff_t>(rank)], myGlobalReducts);
1962  charReductOp(rcp(&reductOp,false));
1963 
1964  const Ordinal
1965  packetSize = charSendBuffer.getBytes()/sendCount;
1966 
1968  Workspace<Ordinal> charRecvCounts(wss, size);
1969  for (Ordinal k = 0; k < size; ++k) {
1970  charRecvCounts[k] = as<Ordinal>(recvCounts[static_cast<ptrdiff_t>(k)] * packetSize);
1971  }
1972 
1973  comm.reduceAllAndScatter(
1974  charReductOp, charSendBuffer.getBytes(), charSendBuffer.getCharBuffer(),
1975  &charRecvCounts[0], charMyGlobalReducts.getCharBuffer()
1976  );
1977 
1978 }
1979 
1980 
1981 template<typename Ordinal, typename Packet>
1982 void Teuchos::reduceAllAndScatter(
1983  const Comm<Ordinal>& comm, const EReductionType reductType,
1984  const Ordinal sendCount, const Packet sendBuffer[] ,
1985  const Ordinal recvCounts[], Packet myGlobalReducts[]
1986  )
1987 {
1988  TEUCHOS_COMM_TIME_MONITOR(
1989  "Teuchos::CommHelpers: reduceAllAndScatter<"
1991  <<">( value type, "<<toString(reductType)<<" )"
1992  );
1993  std::auto_ptr<ValueTypeReductionOp<Ordinal,Packet> >
1994  reductOp(createOp<Ordinal,Packet>(reductType));
1995  reduceAllAndScatter(
1996  comm, *reductOp, sendCount, sendBuffer, recvCounts, myGlobalReducts
1997  );
1998 }
1999 
2000 
2001 template<typename Ordinal, typename Packet>
2002 void Teuchos::reduceAllAndScatter(
2003  const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
2005  const Ordinal sendCount, const Packet*const sendBuffer[],
2006  const Ordinal recvCounts[], Packet*const myGlobalReducts[]
2007  )
2008 {
2009  TEUCHOS_TEST_FOR_EXCEPT(true); // ToDo: Implement and test when needed!
2010 }
2011 
2012 template<typename Ordinal, typename Packet, typename Serializer>
2013 void Teuchos::reduceAllAndScatter(
2014  const Comm<Ordinal>& comm,
2015  const Serializer& serializer,
2016  const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
2017  const Ordinal sendCount, const Packet sendBuffer[] ,
2018  const Ordinal recvCounts[], Packet myGlobalReducts[]
2019  )
2020 {
2021 
2022  TEUCHOS_COMM_TIME_MONITOR(
2023  "Teuchos::CommHelpers: reduceAllAndScatter<"
2025  <<">( value type, user-defined op )"
2026  );
2027 
2028  const Ordinal size = Teuchos::size(comm);
2029  const Ordinal rank = Teuchos::rank(comm);
2030 
2031 #ifdef TEUCHOS_DEBUG
2032  Ordinal sumRecvCounts = 0;
2033  for( Ordinal i = 0; i < size; ++i )
2034  sumRecvCounts += recvCounts[static_cast<ptrdiff_t>(i)];
2035  TEUCHOS_TEST_FOR_EXCEPT(!(sumRecvCounts==sendCount));
2036 #endif
2037 
2039  charSendBuffer(sendCount,sendBuffer,rcp(&serializer,false));
2041  charMyGlobalReducts(recvCounts[static_cast<ptrdiff_t>(rank)], myGlobalReducts,rcp(&serializer,false));
2043  charReductOp(rcp(&reductOp,false),rcp(&serializer,false));
2044 
2045  const Ordinal
2046  packetSize = charSendBuffer.getBytes()/sendCount;
2047 
2049  Workspace<Ordinal> charRecvCounts(wss, size);
2050  for (Ordinal k = 0; k < size; ++k) {
2051  charRecvCounts[k] = as<Ordinal>(recvCounts[static_cast<ptrdiff_t>(k)] * packetSize);
2052  }
2053 
2054  comm.reduceAllAndScatter(
2055  charReductOp, charSendBuffer.getBytes(), charSendBuffer.getCharBuffer(),
2056  &charRecvCounts[0], charMyGlobalReducts.getCharBuffer()
2057  );
2058 
2059 }
2060 
2061 template<typename Ordinal, typename Packet, typename Serializer>
2062 void Teuchos::reduceAllAndScatter(
2063  const Comm<Ordinal>& comm,
2064  const Serializer& serializer,
2065  const EReductionType reductType,
2066  const Ordinal sendCount, const Packet sendBuffer[] ,
2067  const Ordinal recvCounts[], Packet myGlobalReducts[]
2068  )
2069 {
2070  TEUCHOS_COMM_TIME_MONITOR(
2071  "Teuchos::CommHelpers: reduceAllAndScatter<"
2073  <<">( value type, "<<toString(reductType)<<" )"
2074  );
2075  std::auto_ptr<ValueTypeReductionOp<Ordinal,Packet> >
2076  reductOp(createOp<Ordinal,Packet>(reductType));
2077  reduceAllAndScatter(
2078  comm, serializer, *reductOp, sendCount, sendBuffer, recvCounts,
2079  myGlobalReducts
2080  );
2081 }
2082 
2083 
2084 template<typename Ordinal, typename Packet>
2085 void Teuchos::scan(
2086  const Comm<Ordinal>& comm, const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
2087  const Ordinal count, const Packet sendBuffer[], Packet scanReducts[]
2088  )
2089 {
2090  TEUCHOS_COMM_TIME_MONITOR(
2091  "Teuchos::CommHelpers: scan<"
2093  <<">( value type, user-defined op )"
2094  );
2096  charSendBuffer(count,sendBuffer);
2098  charScanReducts(count,scanReducts);
2100  charReductOp(rcp(&reductOp,false));
2101  comm.scan(
2102  charReductOp,charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
2103  ,charScanReducts.getCharBuffer()
2104  );
2105 }
2106 
2107 
2108 template<typename Ordinal, typename Packet>
2109 void Teuchos::scan(
2110  const Comm<Ordinal>& comm, const EReductionType reductType,
2111  const Ordinal count, const Packet sendBuffer[], Packet scanReducts[]
2112  )
2113 {
2114  TEUCHOS_COMM_TIME_MONITOR(
2115  "Teuchos::CommHelpers: scan<"
2117  <<">( value type, "<<toString(reductType)<<" )"
2118  );
2119  std::auto_ptr<ValueTypeReductionOp<Ordinal,Packet> >
2120  reductOp(createOp<Ordinal,Packet>(reductType));
2121  scan(comm,*reductOp,count,sendBuffer,scanReducts);
2122 }
2123 
2124 
2125 template<typename Ordinal, typename Packet>
2126 void Teuchos::scan(
2127  const Comm<Ordinal>& comm, const EReductionType reductType,
2128  const Packet &send, const Ptr<Packet> &scanReduct
2129  )
2130 {
2131  scan<Ordinal,Packet>(comm, reductType, 1, &send, &*scanReduct);
2132 }
2133 
2134 
2135 template<typename Ordinal, typename Packet>
2136 void Teuchos::scan(
2137  const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
2139  const Ordinal count, const Packet*const sendBuffer[], Packet*const scanReducts[]
2140  )
2141 {
2142  TEUCHOS_TEST_FOR_EXCEPT(true); // ToDo: Implement and test when needed!
2143 }
2144 
2145 template<typename Ordinal, typename Packet, typename Serializer>
2146 void Teuchos::scan(
2147  const Comm<Ordinal>& comm,
2148  const Serializer& serializer,
2149  const ValueTypeReductionOp<Ordinal,Packet> &reductOp,
2150  const Ordinal count, const Packet sendBuffer[], Packet scanReducts[]
2151  )
2152 {
2153  TEUCHOS_COMM_TIME_MONITOR(
2154  "Teuchos::CommHelpers: scan<"
2156  <<">( value type, user-defined op )"
2157  );
2159  charSendBuffer(count,sendBuffer,rcp(&serializer,false));
2161  charScanReducts(count,scanReducts,rcp(&serializer,false));
2163  charReductOp(rcp(&reductOp,false),rcp(&serializer,false));
2164  comm.scan(
2165  charReductOp,charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
2166  ,charScanReducts.getCharBuffer()
2167  );
2168 }
2169 
2170 
2171 template<typename Ordinal, typename Packet, typename Serializer>
2172 void Teuchos::scan(
2173  const Comm<Ordinal>& comm,
2174  const Serializer& serializer,
2175  const EReductionType reductType,
2176  const Ordinal count, const Packet sendBuffer[], Packet scanReducts[]
2177  )
2178 {
2179  TEUCHOS_COMM_TIME_MONITOR(
2180  "Teuchos::CommHelpers: scan<"
2182  <<">( value type, "<<toString(reductType)<<" )"
2183  );
2184  std::auto_ptr<ValueTypeReductionOp<Ordinal,Packet> >
2185  reductOp(createOp<Ordinal,Packet>(reductType));
2186  scan(comm,serializer,*reductOp,count,sendBuffer,scanReducts);
2187 }
2188 
2189 template<typename Ordinal, typename Packet>
2190 void Teuchos::send(
2191  const Comm<Ordinal>& comm,
2192  const Ordinal count, const Packet sendBuffer[], const int destRank
2193  )
2194 {
2195  TEUCHOS_COMM_TIME_MONITOR(
2196  "Teuchos::CommHelpers: send<"
2198  <<">( value type )"
2199  );
2201  charSendBuffer(count,sendBuffer);
2202  comm.send(
2203  charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
2204  ,destRank
2205  );
2206 }
2207 
2208 template<typename Ordinal, typename Packet>
2209 void
2210 Teuchos::send (const Packet sendBuffer[],
2211  const Ordinal count,
2212  const int destRank,
2213  const int tag,
2214  const Comm<Ordinal>& comm)
2215 {
2216  TEUCHOS_COMM_TIME_MONITOR(
2217  "Teuchos::CommHelpers: send<"
2219  <<">( value type )"
2220  );
2221  ConstValueTypeSerializationBuffer<Ordinal,Packet> charSendBuffer (count, sendBuffer);
2222  comm.send (charSendBuffer.getBytes (), charSendBuffer.getCharBuffer (), destRank, tag);
2223 }
2224 
2225 template<typename Ordinal, typename Packet>
2226 void Teuchos::ssend(
2227  const Comm<Ordinal>& comm,
2228  const Ordinal count, const Packet sendBuffer[], const int destRank
2229  )
2230 {
2231  TEUCHOS_COMM_TIME_MONITOR(
2232  "Teuchos::CommHelpers: ssend<"
2234  <<">( value type )"
2235  );
2237  charSendBuffer(count,sendBuffer);
2238  comm.ssend(
2239  charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
2240  ,destRank
2241  );
2242 }
2243 
2244 template<typename Ordinal, typename Packet>
2245 void
2246 Teuchos::ssend (const Packet sendBuffer[],
2247  const Ordinal count,
2248  const int destRank,
2249  const int tag,
2250  const Comm<Ordinal>& comm)
2251 {
2252  TEUCHOS_COMM_TIME_MONITOR(
2253  "Teuchos::CommHelpers: ssend<"
2255  <<">( value type )"
2256  );
2258  buf_type charSendBuffer (count, sendBuffer);
2259  comm.ssend (charSendBuffer.getBytes (),
2260  charSendBuffer.getCharBuffer (),
2261  destRank, tag);
2262 }
2263 
2264 template<typename Ordinal, typename Packet>
2265 void Teuchos::send(
2266  const Comm<Ordinal>& comm,
2267  const Packet &send, const int destRank
2268  )
2269 {
2270  Teuchos::send<Ordinal,Packet>(comm,1,&send,destRank);
2271 }
2272 
2273 template<typename Ordinal, typename Packet>
2274 void Teuchos::ssend(
2275  const Comm<Ordinal>& comm,
2276  const Packet &send, const int destRank
2277  )
2278 {
2279  Teuchos::ssend<Ordinal,Packet>(comm,1,&send,destRank);
2280 }
2281 
2282 template<typename Ordinal, typename Packet>
2283 void Teuchos::send(
2284  const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
2285  const Ordinal count, const Packet*const sendBuffer[], const int destRank
2286  )
2287 {
2288  TEUCHOS_TEST_FOR_EXCEPT(true); // ToDo: Implement and test when needed!
2289 }
2290 
2291 template<typename Ordinal, typename Packet, typename Serializer>
2292 void Teuchos::send(
2293  const Comm<Ordinal>& comm,
2294  const Serializer& serializer,
2295  const Ordinal count, const Packet sendBuffer[], const int destRank
2296  )
2297 {
2298  TEUCHOS_COMM_TIME_MONITOR(
2299  "Teuchos::CommHelpers: send<"
2301  <<">( value type )"
2302  );
2304  charSendBuffer(count,sendBuffer,rcp(&serializer,false));
2305  comm.send(
2306  charSendBuffer.getBytes(),charSendBuffer.getCharBuffer()
2307  ,destRank
2308  );
2309 }
2310 
2311 template<typename Ordinal, typename Packet>
2312 int Teuchos::receive(
2313  const Comm<Ordinal>& comm,
2314  const int sourceRank, const Ordinal count, Packet recvBuffer[]
2315  )
2316 {
2317  TEUCHOS_COMM_TIME_MONITOR(
2318  "Teuchos::CommHelpers: receive<"
2320  <<">( value type )"
2321  );
2323  charRecvBuffer(count,recvBuffer);
2324  return comm.receive(
2325  sourceRank
2326  ,charRecvBuffer.getBytes(),charRecvBuffer.getCharBuffer()
2327  );
2328 }
2329 
2330 
2331 template<typename Ordinal, typename Packet>
2332 int Teuchos::receive(
2333  const Comm<Ordinal>& comm,
2334  const int sourceRank, Packet *recv
2335  )
2336 {
2337  return Teuchos::receive<Ordinal,Packet>(comm,sourceRank,1,recv);
2338 }
2339 
2340 
2341 template<typename Ordinal, typename Packet>
2342 int Teuchos::receive(
2343  const Comm<Ordinal>& comm, const Serializer<Ordinal,Packet> &serializer,
2344  const int sourceRank, const Ordinal count, Packet*const recvBuffer[]
2345  )
2346 {
2347  TEUCHOS_TEST_FOR_EXCEPT(true); // ToDo: Implement and test when needed!
2348 }
2349 
2350 template<typename Ordinal, typename Packet, typename Serializer>
2351 int Teuchos::receive(
2352  const Comm<Ordinal>& comm,
2353  const Serializer& serializer,
2354  const int sourceRank, const Ordinal count, Packet recvBuffer[]
2355  )
2356 {
2357  TEUCHOS_COMM_TIME_MONITOR(
2358  "Teuchos::CommHelpers: receive<"
2360  <<">( value type )"
2361  );
2363  charRecvBuffer(count,recvBuffer,rcp(&serializer,false));
2364  return comm.receive(
2365  sourceRank
2366  ,charRecvBuffer.getBytes(),charRecvBuffer.getCharBuffer()
2367  );
2368 }
2369 
2370 template<typename Ordinal, typename Packet>
2371 void Teuchos::readySend(
2372  const Comm<Ordinal>& comm,
2373  const ArrayView<const Packet> &sendBuffer,
2374  const int destRank
2375  )
2376 {
2377  TEUCHOS_COMM_TIME_MONITOR(
2378  "Teuchos::CommHelpers: readySend<"
2380  <<">( value type )"
2381  );
2383  charSendBuffer(sendBuffer.size(), sendBuffer.getRawPtr());
2384  comm.readySend( charSendBuffer.getCharBufferView(), destRank );
2385 }
2386 
2387 template<typename Ordinal, typename Packet>
2388 void
2389 Teuchos::readySend (const Packet sendBuffer[],
2390  const Ordinal count,
2391  const int destRank,
2392  const int tag,
2393  const Comm<Ordinal>& comm)
2394 {
2395  TEUCHOS_COMM_TIME_MONITOR(
2396  "Teuchos::CommHelpers: readySend<"
2398  <<">( value type )"
2399  );
2401  buf_type charSendBuffer (count, sendBuffer);
2402  comm.readySend (charSendBuffer.getBytes (),
2403  charSendBuffer.getCharBuffer (),
2404  destRank, tag);
2405 }
2406 
2407 template<typename Ordinal, typename Packet>
2408 void Teuchos::readySend(
2409  const Comm<Ordinal>& comm,
2410  const Packet &send,
2411  const int destRank
2412  )
2413 {
2414  readySend<Ordinal, Packet>( comm, arrayView(&send,1), destRank );
2415 }
2416 
2417 template<typename Ordinal, typename Packet, typename Serializer>
2418 void Teuchos::readySend(
2419  const Comm<Ordinal>& comm,
2420  const Serializer& serializer,
2421  const ArrayView<const Packet> &sendBuffer,
2422  const int destRank
2423  )
2424 {
2425  TEUCHOS_COMM_TIME_MONITOR(
2426  "Teuchos::CommHelpers: readySend<"
2428  <<">( value type )"
2429  );
2431  charSendBuffer(sendBuffer.size(), sendBuffer.getRawPtr(), serializer);
2432  comm.readySend( charSendBuffer.getCharBufferView(), destRank );
2433 }
2434 
2435 template<typename Ordinal, typename Packet>
2438  const Comm<Ordinal>& comm,
2439  const ArrayRCP<const Packet> &sendBuffer,
2440  const int destRank
2441  )
2442 {
2443  TEUCHOS_COMM_TIME_MONITOR(
2444  "Teuchos::CommHelpers: isend<"
2446  <<">( value type )"
2447  );
2449  charSendBuffer(sendBuffer.size(), sendBuffer.getRawPtr());
2450  RCP<CommRequest<Ordinal> > commRequest = comm.isend(
2451  charSendBuffer.getCharBufferView(), destRank );
2452  set_extra_data( sendBuffer, "buffer", inOutArg(commRequest) );
2453  return commRequest;
2454 }
2455 
2456 template<typename Ordinal, typename Packet>
2458 Teuchos::isend (const ArrayRCP<const Packet> &sendBuffer,
2459  const int destRank,
2460  const int tag,
2461  const Comm<Ordinal>& comm)
2462 {
2463  TEUCHOS_COMM_TIME_MONITOR(
2464  "Teuchos::isend<" << OrdinalTraits<Ordinal>::name () << ","
2465  << TypeNameTraits<Packet>::name () << ">");
2467  charSendBuffer (sendBuffer.size (), sendBuffer.getRawPtr ());
2468  RCP<CommRequest<Ordinal> > commRequest =
2469  comm.isend (charSendBuffer.getCharBufferView (), destRank, tag);
2470  set_extra_data (sendBuffer, "buffer", inOutArg (commRequest));
2471  return commRequest;
2472 }
2473 
2474 template<typename Ordinal, typename Packet>
2477  const Comm<Ordinal>& comm,
2478  const RCP<const Packet> &send,
2479  const int destRank
2480  )
2481 {
2482  const ArrayRCP<const Packet> sendBuffer =
2483  arcpWithEmbeddedObj( send.get(), 0, 1, send, false );
2484  // 2008/07/29: rabartl: Above: I need to write a helper function to create
2485  // new ArrayRCP object given a single object to copy.
2486  return isend<Ordinal, Packet>( comm, sendBuffer, destRank );
2487 }
2488 
2489 template<typename Ordinal, typename Packet, typename Serializer>
2492  const Comm<Ordinal>& comm,
2493  const Serializer& serializer,
2494  const ArrayRCP<const Packet> &sendBuffer,
2495  const int destRank
2496  )
2497 {
2498  TEUCHOS_COMM_TIME_MONITOR(
2499  "Teuchos::CommHelpers: isend<"
2501  <<">( value type )"
2502  );
2504  charSendBuffer(sendBuffer.size(), sendBuffer.getRawPtr(), serializer);
2505  RCP<CommRequest<Ordinal> > commRequest = comm.isend(
2506  charSendBuffer.getCharBufferView(), destRank );
2507  set_extra_data( sendBuffer, "buffer", inOutArg(commRequest) );
2508  return commRequest;
2509 }
2510 
2511 template<typename Ordinal, typename Packet>
2514  const Comm<Ordinal>& comm,
2515  const ArrayRCP<Packet> &recvBuffer,
2516  const int sourceRank
2517  )
2518 {
2519  TEUCHOS_COMM_TIME_MONITOR(
2520  "Teuchos::ireceive<int, " << "," << TypeNameTraits<Packet>::name () << ">");
2522  charRecvBuffer(recvBuffer.size(), recvBuffer.getRawPtr());
2523  RCP<CommRequest<Ordinal> > commRequest = comm.ireceive(
2524  charRecvBuffer.getCharBufferView(), sourceRank );
2525  set_extra_data( recvBuffer, "buffer", inOutArg(commRequest) );
2526  return commRequest;
2527 }
2528 
2529 template<typename Ordinal, typename Packet>
2531 Teuchos::ireceive (const Teuchos::ArrayRCP<Packet> &recvBuffer,
2532  const int sourceRank,
2533  const int tag,
2534  const Teuchos::Comm<Ordinal>& comm)
2535 {
2536  TEUCHOS_COMM_TIME_MONITOR(
2537  "Teuchos::ireceive<int, " << "," << TypeNameTraits<Packet>::name () << ">");
2539  charRecvBuffer (recvBuffer.size (), recvBuffer.getRawPtr ());
2540  RCP<CommRequest<int> > commRequest =
2541  comm.ireceive (charRecvBuffer.getCharBufferView (), sourceRank, tag);
2542  set_extra_data (recvBuffer, "buffer", inOutArg (commRequest));
2543  return commRequest;
2544 }
2545 
2546 template<typename Ordinal, typename Packet>
2549  const Comm<Ordinal>& comm,
2550  const RCP<Packet> &recv,
2551  const int sourceRank
2552  )
2553 {
2554  const ArrayRCP<Packet> recvBuffer =
2555  arcpWithEmbeddedObj( recv.get(), 0, 1, recv, false );
2556  // 2008/07/29: rabartl: Above: I need to write a helper function to create
2557  // new ArrayRCP object given a single object to copy.
2558  return ireceive<Ordinal, Packet>( comm, recvBuffer, sourceRank );
2559 }
2560 
2561 template<typename Ordinal, typename Packet, typename Serializer>
2564  const Comm<Ordinal>& comm,
2565  const Serializer& serializer,
2566  const ArrayRCP<Packet> &recvBuffer,
2567  const int sourceRank
2568  )
2569 {
2570  TEUCHOS_COMM_TIME_MONITOR(
2571  "Teuchos::CommHelpers: ireceive<"
2573  <<">( value type )"
2574  );
2576  charRecvBuffer(recvBuffer.size(), recvBuffer.getRawPtr(), serializer);
2577  RCP<CommRequest<Ordinal> > commRequest = comm.ireceive(
2578  charRecvBuffer.getCharBufferView(), sourceRank );
2579  set_extra_data( recvBuffer, "buffer", inOutArg(commRequest) );
2580  return commRequest;
2581 }
2582 
2583 template<typename Ordinal>
2584 void Teuchos::waitAll(
2585  const Comm<Ordinal>& comm,
2586  const ArrayView<RCP<CommRequest<Ordinal> > > &requests
2587  )
2588 {
2589  comm.waitAll(requests);
2590 }
2591 
2592 
2593 template<typename Ordinal>
2594 void
2595 Teuchos::waitAll (const Comm<Ordinal>& comm,
2596  const ArrayView<RCP<CommRequest<Ordinal> > >& requests,
2597  const ArrayView<RCP<CommStatus<Ordinal> > >& statuses)
2598 {
2599  comm.waitAll (requests, statuses);
2600 }
2601 
2602 
2603 template<typename Ordinal>
2605 Teuchos::wait (const Comm<Ordinal>& comm,
2606  const Ptr<RCP<CommRequest<Ordinal> > > &request)
2607 {
2608  return comm.wait (request);
2609 }
2610 
2611 
2612 #endif // TEUCHOS_COMM_HELPERS_HPP
Strategy interface for the indirect serializing and deserializing objects of a given type handled usi...
void reduce(const Ordinal count, const Packet inBuffer[], Packet inoutBuffer[]) const
Encapsulate how an array of non-const objects with reference sematics is serialized into a char[] arr...
EReductionType
Enumeration for selecting from a set of pre-defined reduction operations.
Templated class for workspace creation.
virtual void reduceAllAndScatter(const ValueTypeReductionOp< Ordinal, char > &reductOp, const Ordinal sendBytes, const char sendBuffer[], const Ordinal recvCounts[], char myGlobalReducts[]) const =0
Global reduction combined with a scatter.
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.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
virtual void broadcast(const int rootRank, const Ordinal bytes, char buffer[]) const =0
Broadcast values from the root process to the slave processes.
T * getRawPtr() const
Get the raw C++ pointer to the underlying object.
size_type size() const
The total number of items in the managed array.
T * get() const
Get the raw C++ pointer to the underlying object.
virtual int getRank() const =0
Returns the rank of this process.
T * getRawPtr()
Return a raw pointer to beginning of array or NULL if unsized.
virtual RCP< CommRequest< Ordinal > > ireceive(const ArrayView< char > &recvBuffer, const int sourceRank) const =0
Non-blocking receive.
virtual void gather(const Ordinal sendBytes, const char sendBuffer[], const Ordinal recvBytes, char recvBuffer[], const int root) const =0
Gather values from all processes to the root process.
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.
Standard min operator for types with value semantics.
This structure defines some basic traits for a scalar field type.
Decorator class that uses traits to convert to and from char[] to typed buffers for objects that use ...
Standard summation operator for types with value semantics.
void reduce(const Ordinal count, const Packet inBuffer[], Packet inoutBuffer[]) const
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 RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
virtual void readySend(const ArrayView< const char > &sendBuffer, const int destRank) const =0
Ready send of data from this process to another process.
virtual void reduceAll(const ValueTypeReductionOp< Ordinal, char > &reductOp, const Ordinal bytes, const char sendBuffer[], char globalReducts[]) const =0
Global reduction.
TEUCHOS_DEPRECATED void scan(const Comm< Ordinal > &comm, const EReductionType reductType, const Packet &send, Packet *scanReduct)
Deprecated.
Encapsulate how an array of const objects with reference sematics is serialized into a char[] array...
Standard Max operator for types with value semantics.
Decorator class that uses a strategy object to convert to and from char[] to typed buffers for object...
virtual void waitAll(const ArrayView< RCP< CommRequest< Ordinal > > > &requests) const =0
Wait on a set of communication requests.
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 .
Encapsulation of the result of a receive (blocking or nonblocking).
void send(const Packet sendBuffer[], const Ordinal count, const int destRank, const int tag, const Comm< Ordinal > &comm)
Variant of send() that takes a tag (and restores the correct order of arguments). ...
Workspace encapsulation class.
T * getRawPtr() const
Return a raw pointer to beginning of array or NULL if unsized.
Encapsulate how an array of non-const objects with value sematics is serialized into a char[] array...
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.
virtual RCP< CommRequest< Ordinal > > isend(const ArrayView< const char > &sendBuffer, const int destRank) const =0
Non-blocking send.
void push_back(const value_type &x)
Nonowning array view.
Defines basic traits for the ordinal field type.
Default traits class that just returns typeid(T).name().
virtual void scan(const ValueTypeReductionOp< Ordinal, char > &reductOp, const Ordinal bytes, const char sendBuffer[], char scanReducts[]) const =0
Scan reduction.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos, as well as a number of utility routines.
virtual int getSize() const =0
Returns the number of processes that make up this communicator.
virtual int receive(const int sourceRank, const Ordinal bytes, char recvBuffer[]) const =0
Blocking receive of data from this process to another process.
void reduce(const Ordinal count, const Packet inBuffer[], Packet inoutBuffer[]) const
Base interface class for user-defined reduction operations for objects that use reference semantics...
virtual void gatherAll(const Ordinal sendBytes, const char sendBuffer[], const Ordinal recvBytes, char recvBuffer[]) const =0
Gather values from each process to collect on all processes.
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.
virtual RCP< CommStatus< Ordinal > > wait(const Ptr< RCP< CommRequest< Ordinal > > > &request) const =0
Wait on a single communication request, and return its status.
virtual void barrier() const =0
Pause every process in *this communicator until all the processes reach this point.
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).
Encapsulation of a pending nonblocking communication operation.
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...
TEUCHOSCORE_LIB_DLL_EXPORT Teuchos::RCP< WorkspaceStore > get_default_workspace_store()
Get the global workspace object set by set_default_workspace_store().
Encapsulate how an array of const objects with value sematics is serialized into a const char[] array...
Reference-counted smart pointer for managing arrays.
Memory-safe templated array class that encapsulates std::vector.