Zoltan2
AlltoAll.cpp
Go to the documentation of this file.
1 // @HEADER
2 //
3 // ***********************************************************************
4 //
5 // Zoltan2: A package of combinatorial algorithms for scientific computing
6 // Copyright 2012 Sandia Corporation
7 //
8 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Karen Devine (kddevin@sandia.gov)
39 // Erik Boman (egboman@sandia.gov)
40 // Siva Rajamanickam (srajama@sandia.gov)
41 //
42 // ***********************************************************************
43 //
44 // @HEADER
45 
46 // TODO: doxygen comments
47 // make this a real unit test that gives helpful information if it fails
48 // and uses different template values
49 
50 #include <Zoltan2_Environment.hpp>
51 #include <Zoltan2_AlltoAll.hpp>
52 #include <Zoltan2_TestHelpers.hpp>
53 
54 #include <iostream>
55 #include <algorithm>
56 #include <vector>
57 #include <string>
58 
59 #include <Teuchos_RCP.hpp>
60 #include <Teuchos_ArrayRCP.hpp>
61 #include <Teuchos_Comm.hpp>
62 #include <Teuchos_DefaultComm.hpp>
63 
64 using namespace std;
65 
66 int main(int argc, char *argv[])
67 {
68  Teuchos::GlobalMPISession session(&argc, &argv);
69  Teuchos::RCP<const Teuchos::Comm<int> > comm =
70  Teuchos::DefaultComm<int>::getComm();
71 
72  int rank = comm->getRank();
73  int nprocs = comm->getSize();
74 
75  Teuchos::RCP<const Zoltan2::Environment> envPtr =
76  Teuchos::rcp(new Zoltan2::Environment);
77 
78  int errcode = 0;
79 
80  if (!errcode){
81 
82  // test of Zoltan2::AlltoAllv (different sized messages) using a Scalar type
83 
84  int myMsgSizeBase=rank*nprocs + 1;
85  Array<int> sendCount(nprocs, 0);
86  Array<int> recvCount(nprocs, 0);
87  long totalOut = 0;
88 
89  for (int p=0; p < nprocs; p++){
90  sendCount[p] = myMsgSizeBase + p;
91  totalOut += sendCount[p];
92  }
93 
94  Array<int> sendBuf(totalOut, 0);
95 
96  int *out = &(sendBuf[0]);
97  for (int p=0; p < nprocs; p++){
98  for (int i=0; i < sendCount[p]; i++){
99  *out++ = p+rank;
100  }
101  }
102 
103  Teuchos::ArrayRCP<int> recvBuf;
104 
105  Zoltan2::AlltoAllv<int>(*comm, *envPtr,
106  sendBuf(),
107  sendCount(),
108  recvBuf,
109  recvCount());
110 
111  int *inBuf = recvBuf.get();
112 
113  for (int p=0; p < nprocs; p++){
114  for (int i=0; i < recvCount[p]; i++){
115  if (*inBuf++ != rank+p){
116  errcode = 4;
117  break;
118  }
119  }
120  }
121  }
122 
123  TEST_FAIL_AND_EXIT(*comm, errcode==0, "int", errcode);
124 
125  if (!errcode){
126 
127  // test of Zoltan2::AlltoAllv using strings - which can not
128  // be serialized by Teuchos.
129  // Rank p sends p messages to each process.
130 
131  int nstrings = nprocs * rank;
132  string *sendStrings = NULL;
133 
134  if (nstrings > 0)
135  sendStrings = new string [nstrings];
136 
137  ostringstream myMessage;
138  myMessage << "message from process " << rank;
139 
140  for (int i=0; i < nstrings; i++)
141  sendStrings[i] = myMessage.str();
142 
143  int *counts = new int [nprocs];
144  for (int i=0; i < nprocs ; i++)
145  counts[i] = rank;
146 
147  Teuchos::ArrayView<const string> sendBuf(sendStrings, nstrings);
148  Teuchos::ArrayView<const int> sendCount(counts, nprocs);
149  Teuchos::Array<int> recvCounts(nprocs, 0);
150 
151  Teuchos::ArrayRCP<string> recvBuf;
152 
153  Zoltan2::AlltoAllv<string>(*comm, *envPtr,
154  sendBuf,
155  sendCount,
156  recvBuf,
157  recvCounts());
158 
159  delete [] sendStrings;
160  delete [] counts;
161 
162  int next = 0;
163  for (int i=0; i < nprocs; i++){
164  if (recvCounts[i] != i){
165  errcode = 5;
166  break;
167  }
168  ostringstream msg;
169  msg << "message from process " << i;
170  for (int j=0; j < recvCounts[i]; j++){
171  if (recvBuf[next++] != msg.str()){
172  errcode = 6;
173  break;
174  }
175  }
176  }
177  }
178 
179  TEST_FAIL_AND_EXIT(*comm, errcode==0, "strings", errcode);
180 
181  if (rank == 0){
182  if (errcode)
183  std::cout << "FAIL" << std::endl;
184  else
185  std::cout << "PASS" << std::endl;
186  }
187 
188  return errcode;
189 }
#define TEST_FAIL_AND_EXIT(comm, ok, s, code)
common code used by tests
The user parameters, debug, timing and memory profiling output objects, and error checking methods...
Defines the Environment class.
int main(int argc, char *argv[])
Definition: AlltoAll.cpp:66
AlltoAll communication methods.