Zoltan2
Loading...
Searching...
No Matches
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
51#include <Zoltan2_AlltoAll.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
65int main(int narg, char *arg[])
66{
67 Tpetra::ScopeGuard tscope(&narg, &arg);
68 Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
69
70 int rank = comm->getRank();
71 int nprocs = comm->getSize();
72
73 Teuchos::RCP<const Zoltan2::Environment> envPtr =
74 Teuchos::rcp(new Zoltan2::Environment(comm));
75
76 int errcode = 0;
77
78 if (!errcode){
79
80 // test of Zoltan2::AlltoAllv (different sized messages) using a Scalar type
81
82 int myMsgSizeBase=rank*nprocs + 1;
83 Array<int> sendCount(nprocs, 0);
84 Array<int> recvCount(nprocs, 0);
85 long totalOut = 0;
86
87 for (int p=0; p < nprocs; p++){
88 sendCount[p] = myMsgSizeBase + p;
89 totalOut += sendCount[p];
90 }
91
92 Array<int> sendBuf(totalOut, 0);
93
94 int *out = &(sendBuf[0]);
95 for (int p=0; p < nprocs; p++){
96 for (int i=0; i < sendCount[p]; i++){
97 *out++ = p+rank;
98 }
99 }
100
101 Teuchos::ArrayRCP<int> recvBuf;
102
103 Zoltan2::AlltoAllv<int>(*comm, *envPtr,
104 sendBuf(),
105 sendCount(),
106 recvBuf,
107 recvCount());
108
109 int *inBuf = recvBuf.get();
110
111 for (int p=0; p < nprocs; p++){
112 for (int i=0; i < recvCount[p]; i++){
113 if (*inBuf++ != rank+p){
114 errcode = 4;
115 break;
116 }
117 }
118 }
119 }
120
121 TEST_FAIL_AND_EXIT(*comm, errcode==0, "int", errcode);
122
123 if (!errcode){
124
125 // test of Zoltan2::AlltoAllv using strings - which can not
126 // be serialized by Teuchos.
127 // Rank p sends p messages to each process.
128
129 int nstrings = nprocs * rank;
130 string *sendStrings = NULL;
131
132 if (nstrings > 0)
133 sendStrings = new string [nstrings];
134
135 std::ostringstream myMessage;
136 myMessage << "message from process " << rank;
137
138 for (int i=0; i < nstrings; i++)
139 sendStrings[i] = myMessage.str();
140
141 int *counts = new int [nprocs];
142 for (int i=0; i < nprocs ; i++)
143 counts[i] = rank;
144
145 Teuchos::ArrayView<const string> sendBuf(sendStrings, nstrings);
146 Teuchos::ArrayView<const int> sendCount(counts, nprocs);
147 Teuchos::Array<int> recvCounts(nprocs, 0);
148
149 Teuchos::ArrayRCP<string> recvBuf;
150
151 Zoltan2::AlltoAllv<string>(*comm, *envPtr,
152 sendBuf,
153 sendCount,
154 recvBuf,
155 recvCounts());
156
157 delete [] sendStrings;
158 delete [] counts;
159
160 int next = 0;
161 for (int i=0; i < nprocs; i++){
162 if (recvCounts[i] != i){
163 errcode = 5;
164 break;
165 }
166 std::ostringstream msg;
167 msg << "message from process " << i;
168 for (int j=0; j < recvCounts[i]; j++){
169 if (recvBuf[next++] != msg.str()){
170 errcode = 6;
171 break;
172 }
173 }
174 }
175 }
176
177 TEST_FAIL_AND_EXIT(*comm, errcode==0, "strings", errcode);
178
179 if (rank == 0){
180 if (errcode)
181 std::cout << "FAIL" << std::endl;
182 else
183 std::cout << "PASS" << std::endl;
184 }
185
186 return errcode;
187}
#define TEST_FAIL_AND_EXIT(comm, ok, s, code)
AlltoAll communication methods.
Defines the Environment class.
common code used by tests
int main()
The user parameters, debug, timing and memory profiling output objects, and error checking methods.
void AlltoAllv(const Comm< int > &comm, const Environment &env, const ArrayView< const std::string > &sendBuf, const ArrayView< const int > &sendCount, ArrayRCP< std::string > &recvBuf, const ArrayView< int > &recvCount)