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