Zoltan2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
IdentifierModel.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 // Testing of IdentifierModel
47 //
48 // TODO test with BasicIdentifierAdapter with weights
49 
53 #include <Zoltan2_TestHelpers.hpp>
54 
55 #include <set>
56 #include <bitset>
57 
58 #include <Teuchos_Comm.hpp>
59 #include <Teuchos_DefaultComm.hpp>
60 #include <Teuchos_ArrayView.hpp>
61 #include <Teuchos_OrdinalTraits.hpp>
62 
63 #include <Tpetra_CrsMatrix.hpp>
64 
65 using Teuchos::RCP;
66 using Teuchos::rcp;
67 using Teuchos::Comm;
68 
69 void testIdentifierModel(std::string fname, zgno_t xdim, zgno_t ydim, zgno_t zdim,
70  const RCP<const Comm<int> > &comm)
71 {
72  int rank = comm->getRank();
73  int fail = 0, gfail = 0;
74 
75  std::bitset<Zoltan2::NUM_MODEL_FLAGS> modelFlags = 0;
76 
77  RCP<const Zoltan2::Environment> env = rcp(new Zoltan2::Environment(comm));
78 
80  // Use an Tpetra::CrsMatrix for the user data.
82  typedef Tpetra::CrsMatrix<zscalar_t, zlno_t, zgno_t> tcrsMatrix_t;
83 
84  UserInputForTests *uinput;
85  if (fname.size() > 0)
86  uinput = new UserInputForTests(testDataFilePath, fname, comm, true);
87  else
88  uinput = new UserInputForTests(xdim,ydim,zdim,string(""),comm, true, true);
89 
90  RCP<tcrsMatrix_t > M = uinput->getUITpetraCrsMatrix();
91  zlno_t nLocalIds = M->getNodeNumRows();
92  zgno_t nGlobalIds = M->getGlobalNumRows();
93 
94  ArrayView<const zgno_t> idList = M->getRowMap()->getNodeElementList();
95  std::set<zgno_t> idSet(idList.begin(), idList.end());
96 
98  // Create an IdentifierModel with this input
100 
104 
105  RCP<const adapter_t> ia = Teuchos::rcp(new adapter_t(M));
106 
108  RCP<const base_adapter_t> base_ia =
109  Teuchos::rcp_dynamic_cast<const base_adapter_t>(ia);
110 
111  try{
113  base_ia, env, comm, modelFlags);
114  }
115  catch (std::exception &e){
116  std::cerr << rank << ") " << e.what() << std::endl;
117  fail = 1;
118  }
119 
120  gfail = globalFail(*comm, fail);
121 
122  if (gfail)
123  printFailureCode(*comm, fail);
124 
125  // Test the IdentifierModel interface
126 
127  if (model->getLocalNumIdentifiers() != size_t(nLocalIds)) {
128  std::cerr << rank << ") getLocalNumIdentifiers "
129  << model->getLocalNumIdentifiers() << " "
130  << nLocalIds << std::endl;
131  fail = 2;
132  }
133 
134  if (!fail && model->getGlobalNumIdentifiers() != size_t(nGlobalIds)) {
135  std::cerr << rank << ") getGlobalNumIdentifiers "
136  << model->getGlobalNumIdentifiers() << " "
137  << nGlobalIds << std::endl;
138  fail = 3;
139  }
140 
141  gfail = globalFail(*comm, fail);
142 
143  if (gfail)
144  printFailureCode(*comm, fail);
145 
146  ArrayView<const zgno_t> gids;
147  ArrayView<input_t> wgts;
148 
149  model->getIdentifierList(gids, wgts);
150 
151  if (!fail && gids.size() != nLocalIds) {
152  std::cerr << rank << ") getIdentifierList IDs "
153  << gids.size() << " "
154  << nLocalIds << std::endl;
155  fail = 5;
156  }
157 
158  if (!fail && wgts.size() != 0) {
159  std::cerr << rank << ") getIdentifierList Weights "
160  << wgts.size() << " "
161  << 0 << std::endl;
162  fail = 6;
163  }
164 
165  for (zlno_t i=0; !fail && i < nLocalIds; i++){
166  std::set<zgno_t>::iterator next = idSet.find(gids[i]);
167  if (next == idSet.end()) {
168  std::cerr << rank << ") getIdentifierList gid not found "
169  << gids[i] << std::endl;
170  fail = 7;
171  }
172  }
173 
174  gfail = globalFail(*comm, fail);
175 
176  if (gfail)
177  printFailureCode(*comm, fail);
178 
179  delete model;
180  delete uinput;
181 }
182 
183 int main(int narg, char *arg[])
184 {
185  Tpetra::ScopeGuard tscope(&narg, &arg);
186  Teuchos::RCP<const Teuchos::Comm<int> > comm = Tpetra::getDefaultComm();
187 
188  int rank = comm->getRank();
189 
190  string fname("simple");
191 
192 
193  if (rank == 0){
194  std::cout << fname;
195  }
196  testIdentifierModel(fname, 0,0,0,comm);
197 
198  if (rank==0) std::cout << "PASS" << std::endl;
199 
200  return 0;
201 }
void printFailureCode(const Comm< int > &comm, int fail)
Zoltan2::BaseAdapter< userTypes_t > base_adapter_t
Tpetra::CrsMatrix< zscalar_t, zlno_t, zgno_t, znode_t > tcrsMatrix_t
Definition: GraphModel.cpp:82
MatrixAdapter defines the adapter interface for matrices.
RCP< tcrsMatrix_t > getUITpetraCrsMatrix()
Provides access for Zoltan2 to Xpetra::CrsMatrix data.
int main(int narg, char *arg[])
int zlno_t
common code used by tests
list idList
Match up parameters to validators.
Defines the XpetraCrsMatrixAdapter class.
Defines the IdentifierModel interface.
void testIdentifierModel(std::string fname, zgno_t xdim, zgno_t ydim, zgno_t zdim, const RCP< const Comm< int > > &comm)
The StridedData class manages lists of weights or coordinates.
The user parameters, debug, timing and memory profiling output objects, and error checking methods...
static const std::string fail
int zgno_t
Defines the BasicIdentifierAdapter class.
int globalFail(const Comm< int > &comm, int fail)
IdentifierModel defines the interface for all identifier models.
global_size_t getGlobalNumIdentifiers() const
size_t getIdentifierList(ArrayView< const gno_t > &Ids, ArrayView< input_t > &wgts) const
std::string testDataFilePath(".")