Zoltan2  Version of the Day
XpetraMultiVectorInput.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 // @HEADER
46 // ***********************************************************************
47 // Zoltan2: Sandia Partitioning Ordering & Coloring Library
48 // ***********************************************************************
49 
55 #include <string>
56 
58 #include <Zoltan2_InputTraits.hpp>
59 #include <Zoltan2_TestHelpers.hpp>
60 
61 #include <Teuchos_GlobalMPISession.hpp>
62 #include <Teuchos_DefaultComm.hpp>
63 #include <Teuchos_RCP.hpp>
64 #include <Teuchos_Comm.hpp>
65 #include <Teuchos_CommHelpers.hpp>
66 
67 using namespace std;
68 using Teuchos::RCP;
69 using Teuchos::rcp;
70 using Teuchos::rcp_const_cast;
71 using Teuchos::Comm;
72 using Teuchos::DefaultComm;
73 
75 typedef Tpetra::MultiVector<scalar_t, lno_t, gno_t, node_t> tvector_t;
76 typedef Xpetra::MultiVector<scalar_t, lno_t, gno_t, node_t> xvector_t;
77 typedef Epetra_MultiVector evector_t;
78 
79 template <typename User>
81  Zoltan2::XpetraMultiVectorInput<User> &ia, tvector_t &vector, int nvec,
82  int wdim, scalar_t **weights, int *strides)
83 {
84  RCP<const Comm<int> > comm = vector.getMap()->getComm();
85  int fail = 0, gfail=0;
86 
87  if (!fail && ia.getNumberOfVectors() !=nvec)
88  fail = 42;
89 
90  if (!fail && ia.getNumberOfWeights() !=wdim)
91  fail = 41;
92 
93  size_t length = vector.getLocalLength();
94 
95  if (!fail && ia.getLocalLength() != length)
96  fail = 4;
97 
98  gfail = globalFail(comm, fail);
99 
100  if (!gfail){
101  const gno_t *vtxIds=NULL;
102  const scalar_t *vals=NULL;
103  int stride;
104 
105  for (int v=0; v < nvec; v++){
106  size_t nvals = ia.getVector( v, vtxIds, vals, stride);
107 
108  if (nvals != vector.getLocalLength())
109  fail = 8;
110  if (!fail && stride != 1)
111  fail = 10;
112 
113  // TODO check the values returned
114  }
115 
116  gfail = globalFail(comm, fail);
117  }
118 
119  if (!gfail && wdim){
120  const scalar_t *wgt =NULL;
121  int stride;
122 
123  for (int w=0; !fail && w < wdim; w++){
124  size_t nvals = ia.getVectorWeights(w, wgt, stride);
125 
126  if (nvals != vector.getLocalLength())
127  fail = 100;
128  if (!fail && stride != strides[w])
129  fail = 101;
130 
131  for (size_t v=0; !fail && v < nvals; v++){
132  if (wgt[v*stride] != weights[w][v*stride])
133  fail=102;
134  }
135  }
136 
137  gfail = globalFail(comm, fail);
138  }
139 
140  return gfail;
141 }
142 
143 int main(int argc, char *argv[])
144 {
145  Teuchos::GlobalMPISession session(&argc, &argv);
146  RCP<const Comm<int> > comm = DefaultComm<int>::getComm();
147  int rank = comm->getRank();
148  int fail = 0, gfail=0;
149 
150  // Create object that can give us test Tpetra, Xpetra
151  // and Epetra vectors for testing.
152 
153  RCP<uinput_t> uinput;
154 
155  try{
156  uinput =
157  rcp(new uinput_t(testDataFilePath,std::string("simple"), comm, true));
158  }
159  catch(std::exception &e){
160  TEST_FAIL_AND_EXIT(*comm, 0, string("input ")+e.what(), 1);
161  }
162 
163  RCP<tvector_t> tV; // original vector (for checking)
164  RCP<tvector_t> newV; // migrated vector
165 
166  int numVectors = 2;
167 
168  tV = uinput->getTpetraMultiVector(numVectors);
169  size_t vlen = tV->getLocalLength();
170  Teuchos::ArrayView<const gno_t> rowGids = tV->getMap()->getNodeElementList();
171 
172  // To test migration in the input adapter we need a Solution
173  // object. The Solution needs an IdentifierMap.
174 
175  typedef Zoltan2::IdentifierMap<tvector_t> idmap_t;
176 
177  RCP<const Zoltan2::Environment> env = rcp(new Zoltan2::Environment);
178 
179  ArrayRCP<const gno_t> gidArray = arcpFromArrayView(rowGids);
180  RCP<const idmap_t> idMap = rcp(new idmap_t(env, comm, gidArray));
181 
182  int weightDim = 1;
183 
184  zoltan2_partId_t *p = new zoltan2_partId_t [vlen];
185  memset(p, 0, sizeof(zoltan2_partId_t) * vlen);
186  ArrayRCP<zoltan2_partId_t> solnParts(p, 0, vlen, true);
187 
190  soln_t solution(env, comm, idMap, weightDim);
191  solution.setParts(gidArray, solnParts, true);
192 
193  std::vector<const scalar_t *> emptyWeights;
194  std::vector<int> emptyStrides;
195 
197  // User object is Tpetra::MultiVector, no weights
198  if (!gfail){
199  RCP<const tvector_t> ctV = rcp_const_cast<const tvector_t>(tV);
200  RCP<Zoltan2::XpetraMultiVectorInput<tvector_t> > tVInput;
201 
202  try {
203  tVInput =
205  emptyWeights, emptyStrides));
206  }
207  catch (std::exception &e){
208  TEST_FAIL_AND_EXIT(*comm, 0,
209  string("XpetraMultiVectorInput ")+e.what(), 1);
210  }
211 
212  if (rank==0){
213  std::cout << tVInput->inputAdapterName() << ", constructed with ";
214  std::cout << "Tpetra::MultiVector" << std::endl;
215  }
216 
217  fail = verifyInputAdapter<tvector_t>(*tVInput, *tV, numVectors, 0, NULL, NULL);
218 
219  gfail = globalFail(comm, fail);
220 
221  if (!gfail){
222  tvector_t *vMigrate = NULL;
223  try{
224  tVInput->applyPartitioningSolution(*tV, vMigrate, solution);
225  newV = rcp(vMigrate);
226  }
227  catch (std::exception &e){
228  fail = 11;
229  }
230 
231  gfail = globalFail(comm, fail);
232 
233  if (!gfail){
234  RCP<const tvector_t> cnewV = rcp_const_cast<const tvector_t>(newV);
235  RCP<Zoltan2::XpetraMultiVectorInput<tvector_t> > newInput;
236  try{
238  cnewV, emptyWeights, emptyStrides));
239  }
240  catch (std::exception &e){
241  TEST_FAIL_AND_EXIT(*comm, 0,
242  string("XpetraMultiVectorInput 2 ")+e.what(), 1);
243  }
244 
245  if (rank==0){
246  std::cout << tVInput->inputAdapterName() << ", constructed with ";
247  std::cout << "Tpetra::MultiVector migrated to proc 0" << std::endl;
248  }
249  fail = verifyInputAdapter<tvector_t>(*newInput, *newV, numVectors, 0, NULL, NULL);
250  if (fail) fail += 100;
251  gfail = globalFail(comm, fail);
252  }
253  }
254  if (gfail){
255  printFailureCode(comm, fail);
256  }
257  }
258 
260  // User object is Xpetra::MultiVector
261  if (!gfail){
262  RCP<xvector_t> xV = uinput->getXpetraMultiVector(numVectors);
263  RCP<const xvector_t> cxV = rcp_const_cast<const xvector_t>(xV);
264  RCP<Zoltan2::XpetraMultiVectorInput<xvector_t> > xVInput;
265 
266  try {
267  xVInput =
269  emptyWeights, emptyStrides));
270  }
271  catch (std::exception &e){
272  TEST_FAIL_AND_EXIT(*comm, 0,
273  string("XpetraMultiVectorInput 3 ")+e.what(), 1);
274  }
275 
276  if (rank==0){
277  std::cout << xVInput->inputAdapterName() << ", constructed with ";
278  std::cout << "Xpetra::MultiVector" << std::endl;
279  }
280  fail = verifyInputAdapter<xvector_t>(*xVInput, *tV, numVectors, 0, NULL, NULL);
281 
282  gfail = globalFail(comm, fail);
283 
284  if (!gfail){
285  xvector_t *vMigrate =NULL;
286  try{
287  xVInput->applyPartitioningSolution(*xV, vMigrate, solution);
288  }
289  catch (std::exception &e){
290  fail = 11;
291  }
292 
293  gfail = globalFail(comm, fail);
294 
295  if (!gfail){
296  RCP<const xvector_t> cnewV(vMigrate);
297  RCP<Zoltan2::XpetraMultiVectorInput<xvector_t> > newInput;
298  try{
299  newInput =
301  cnewV, emptyWeights, emptyStrides));
302  }
303  catch (std::exception &e){
304  TEST_FAIL_AND_EXIT(*comm, 0,
305  string("XpetraMultiVectorInput 4 ")+e.what(), 1);
306  }
307 
308  if (rank==0){
309  std::cout << xVInput->inputAdapterName() << ", constructed with ";
310  std::cout << "Xpetra::MultiVector migrated to proc 0" << std::endl;
311  }
312  fail = verifyInputAdapter<xvector_t>(*newInput, *newV, numVectors, 0, NULL, NULL);
313  if (fail) fail += 100;
314  gfail = globalFail(comm, fail);
315  }
316  }
317  if (gfail){
318  printFailureCode(comm, fail);
319  }
320  }
321 
322 #ifdef HAVE_EPETRA_DATA_TYPES
323  // User object is Epetra_MultiVector
325  if (!gfail){
326  RCP<evector_t> eV = uinput->getEpetraMultiVector(numVectors);
327  RCP<const evector_t> ceV = rcp_const_cast<const evector_t>(eV);
328  RCP<Zoltan2::XpetraMultiVectorInput<evector_t> > eVInput;
329 
330  try {
331  eVInput =
333  emptyWeights, emptyStrides));
334  }
335  catch (std::exception &e){
336  TEST_FAIL_AND_EXIT(*comm, 0,
337  string("XpetraMultiVectorInput 5 ")+e.what(), 1);
338  }
339 
340  if (rank==0){
341  std::cout << eVInput->inputAdapterName() << ", constructed with ";
342  std::cout << "Epetra_MultiVector" << std::endl;
343  }
344  fail = verifyInputAdapter<evector_t>(*eVInput, *tV, numVectors, 0, NULL, NULL);
345 
346  gfail = globalFail(comm, fail);
347 
348  if (!gfail){
349  evector_t *vMigrate =NULL;
350  try{
351  eVInput->applyPartitioningSolution(*eV, vMigrate, solution);
352  }
353  catch (std::exception &e){
354  fail = 11;
355  }
356 
357  gfail = globalFail(comm, fail);
358 
359  if (!gfail){
360  RCP<const evector_t> cnewV(vMigrate, true);
361  RCP<Zoltan2::XpetraMultiVectorInput<evector_t> > newInput;
362  try{
363  newInput =
365  emptyWeights, emptyStrides));
366  }
367  catch (std::exception &e){
368  TEST_FAIL_AND_EXIT(*comm, 0,
369  string("XpetraMultiVectorInput 6 ")+e.what(), 1);
370  }
371 
372  if (rank==0){
373  std::cout << eVInput->inputAdapterName() << ", constructed with ";
374  std::cout << "Epetra_MultiVector migrated to proc 0" << std::endl;
375  }
376  fail = verifyInputAdapter<evector_t>(*newInput, *newV, numVectors, 0, NULL, NULL);
377  if (fail) fail += 100;
378  gfail = globalFail(comm, fail);
379  }
380  }
381  if (gfail){
382  printFailureCode(comm, fail);
383  }
384  }
385 #endif
386 
388  // DONE
389 
390  if (rank==0)
391  std::cout << "PASS" << std::endl;
392 }
An IdentifierMap manages a global space of object identifiers.
An input adapter for Xpetra::MultiVector.
int getNumberOfVectors() const
Return the number of vectors (typically one).
int globalFail(const RCP< const Comm< int > > &comm, int fail)
A class that generates typical user input for testing.
#define TEST_FAIL_AND_EXIT(comm, ok, s, code)
size_t getVectorWeights(int dim, const scalar_t *&weights, int &stride) const
Provide a pointer to the weights corresponding to the elements returned in getVector().
common code used by tests
const RCP< const x_mvector_t > & getVector() const
Access to xpetra wrapper multivector.
Defines the XpetraMultiVectorInput adapter.
int zoltan2_partId_t
a data type for part IDs
dictionary vals
Definition: xml2dox.py:186
A PartitioningSolution is a solution to a partitioning problem.
Epetra_MultiVector evector_t
int verifyInputAdapter(Zoltan2::XpetraMultiVectorInput< User > &ia, tvector_t &vector, int nvec, int wdim, scalar_t **weights, int *strides)
int getNumberOfWeights() const
Return the number of weights per vector element. Number of weights per element should be zero or grea...
Xpetra::MultiVector< scalar_t, lno_t, gno_t, node_t > xvector_t
Traits for application input objects.
The user parameters, debug, timing and memory profiling output objects, and error checking methods...
double scalar_t
void printFailureCode(const RCP< const Comm< int > > &comm, int fail)
UserInputForTests uinput_t
int main(int argc, char *argv[])
size_t getLocalLength() const
Return the length of the portion of the vector on this process.
Tpetra::MultiVector< scalar_t, lno_t, gno_t, node_t > tvector_t
std::string testDataFilePath(".")