Zoltan2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
AlltoAll.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Zoltan2: A package of combinatorial algorithms for scientific computing
4 //
5 // Copyright 2012 NTESS and the Zoltan2 contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 // TODO: doxygen comments
11 // make this a real unit test that gives helpful information if it fails
12 // and uses different template values
13 
14 #include <Zoltan2_Environment.hpp>
15 #include <Zoltan2_AlltoAll.hpp>
16 #include <Zoltan2_TestHelpers.hpp>
17 
18 #include <iostream>
19 #include <algorithm>
20 #include <vector>
21 #include <string>
22 
23 #include <Teuchos_RCP.hpp>
24 #include <Teuchos_ArrayRCP.hpp>
25 #include <Teuchos_Comm.hpp>
26 #include <Teuchos_DefaultComm.hpp>
27 
28 
29 int main(int narg, char *arg[])
30 {
31  Tpetra::ScopeGuard tscope(&narg, &arg);
32  Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
33 
34  int rank = comm->getRank();
35  int nprocs = comm->getSize();
36 
37  Teuchos::RCP<const Zoltan2::Environment> envPtr =
38  Teuchos::rcp(new Zoltan2::Environment(comm));
39 
40  int errcode = 0;
41 
42  if (!errcode){
43 
44  // test of Zoltan2::AlltoAllv (different sized messages) using a Scalar type
45 
46  int myMsgSizeBase=rank*nprocs + 1;
47  Array<int> sendCount(nprocs, 0);
48  Array<int> recvCount(nprocs, 0);
49  long totalOut = 0;
50 
51  for (int p=0; p < nprocs; p++){
52  sendCount[p] = myMsgSizeBase + p;
53  totalOut += sendCount[p];
54  }
55 
56  Array<int> sendBuf(totalOut, 0);
57 
58  int *out = &(sendBuf[0]);
59  for (int p=0; p < nprocs; p++){
60  for (int i=0; i < sendCount[p]; i++){
61  *out++ = p+rank;
62  }
63  }
64 
65  Teuchos::ArrayRCP<int> recvBuf;
66 
67  Zoltan2::AlltoAllv<int>(*comm, *envPtr,
68  sendBuf(),
69  sendCount(),
70  recvBuf,
71  recvCount());
72 
73  int *inBuf = recvBuf.get();
74 
75  for (int p=0; p < nprocs; p++){
76  for (int i=0; i < recvCount[p]; i++){
77  if (*inBuf++ != rank+p){
78  errcode = 4;
79  break;
80  }
81  }
82  }
83  }
84 
85  TEST_FAIL_AND_EXIT(*comm, errcode==0, "int", errcode);
86 
87  if (!errcode){
88 
89  // test of Zoltan2::AlltoAllv using strings - which can not
90  // be serialized by Teuchos.
91  // Rank p sends p messages to each process.
92 
93  int nstrings = nprocs * rank;
94  string *sendStrings = NULL;
95 
96  if (nstrings > 0)
97  sendStrings = new string [nstrings];
98 
99  std::ostringstream myMessage;
100  myMessage << "message from process " << rank;
101 
102  for (int i=0; i < nstrings; i++)
103  sendStrings[i] = myMessage.str();
104 
105  int *counts = new int [nprocs];
106  for (int i=0; i < nprocs ; i++)
107  counts[i] = rank;
108 
109  Teuchos::ArrayView<const string> sendBuf(sendStrings, nstrings);
110  Teuchos::ArrayView<const int> sendCount(counts, nprocs);
111  Teuchos::Array<int> recvCounts(nprocs, 0);
112 
113  Teuchos::ArrayRCP<string> recvBuf;
114 
115  Zoltan2::AlltoAllv<string>(*comm, *envPtr,
116  sendBuf,
117  sendCount,
118  recvBuf,
119  recvCounts());
120 
121  delete [] sendStrings;
122  delete [] counts;
123 
124  int next = 0;
125  for (int i=0; i < nprocs; i++){
126  if (recvCounts[i] != i){
127  errcode = 5;
128  break;
129  }
130  std::ostringstream msg;
131  msg << "message from process " << i;
132  for (int j=0; j < recvCounts[i]; j++){
133  if (recvBuf[next++] != msg.str()){
134  errcode = 6;
135  break;
136  }
137  }
138  }
139  }
140 
141  TEST_FAIL_AND_EXIT(*comm, errcode==0, "strings", errcode);
142 
143  if (rank == 0){
144  if (errcode)
145  std::cout << "FAIL" << std::endl;
146  else
147  std::cout << "PASS" << std::endl;
148  }
149 
150  return errcode;
151 }
#define TEST_FAIL_AND_EXIT(comm, ok, s, code)
int main(int narg, char **arg)
Definition: coloring1.cpp:164
common code used by tests
The user parameters, debug, timing and memory profiling output objects, and error checking methods...
Defines the Environment class.
AlltoAll communication methods.