Zoltan2
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 namespace std;
66 using Teuchos::RCP;
67 using Teuchos::rcp;
68 using Teuchos::Comm;
69 using Teuchos::DefaultComm;
70 
71 void testIdentifierModel(std::string fname, zgno_t xdim, zgno_t ydim, zgno_t zdim,
72  const RCP<const Comm<int> > &comm)
73 {
74  int rank = comm->getRank();
75  int fail = 0, gfail = 0;
76 
77  std::bitset<Zoltan2::NUM_MODEL_FLAGS> modelFlags = 0;
78 
79  RCP<const Zoltan2::Environment> env = rcp(new Zoltan2::Environment);
80 
82  // Use an Tpetra::CrsMatrix for the user data.
84  typedef Tpetra::CrsMatrix<zscalar_t, zlno_t, zgno_t> tcrsMatrix_t;
85 
86  UserInputForTests *uinput;
87  if (fname.size() > 0)
88  uinput = new UserInputForTests(testDataFilePath, fname, comm, true);
89  else
90  uinput = new UserInputForTests(xdim,ydim,zdim,string(""),comm, true, true);
91 
92  RCP<tcrsMatrix_t > M = uinput->getUITpetraCrsMatrix();
93  zlno_t nLocalIds = M->getNodeNumRows();
94  zgno_t nGlobalIds = M->getGlobalNumRows();
95 
96  ArrayView<const zgno_t> idList = M->getRowMap()->getNodeElementList();
97  std::set<zgno_t> idSet(idList.begin(), idList.end());
98 
100  // Create an IdentifierModel with this input
102 
106 
107  RCP<const adapter_t> ia = Teuchos::rcp(new adapter_t(M));
108 
110  RCP<const base_adapter_t> base_ia =
111  Teuchos::rcp_dynamic_cast<const base_adapter_t>(ia);
112 
113  try{
115  base_ia, env, comm, modelFlags);
116  }
117  catch (std::exception &e){
118  std::cerr << rank << ") " << e.what() << std::endl;
119  fail = 1;
120  }
121 
122  gfail = globalFail(comm, fail);
123 
124  if (gfail)
125  printFailureCode(comm, fail);
126 
127  // Test the IdentifierModel interface
128 
129  if (model->getLocalNumIdentifiers() != size_t(nLocalIds)) {
130  std::cerr << rank << ") getLocalNumIdentifiers "
131  << model->getLocalNumIdentifiers() << " "
132  << nLocalIds << std::endl;
133  fail = 2;
134  }
135 
136  if (!fail && model->getGlobalNumIdentifiers() != size_t(nGlobalIds)) {
137  std::cerr << rank << ") getGlobalNumIdentifiers "
138  << model->getGlobalNumIdentifiers() << " "
139  << nGlobalIds << std::endl;
140  fail = 3;
141  }
142 
143  gfail = globalFail(comm, fail);
144 
145  if (gfail)
146  printFailureCode(comm, fail);
147 
148  ArrayView<const zgno_t> gids;
149  ArrayView<input_t> wgts;
150 
151  model->getIdentifierList(gids, wgts);
152 
153  if (!fail && gids.size() != nLocalIds) {
154  std::cerr << rank << ") getIdentifierList IDs "
155  << gids.size() << " "
156  << nLocalIds << std::endl;
157  fail = 5;
158  }
159 
160  if (!fail && wgts.size() != 0) {
161  std::cerr << rank << ") getIdentifierList Weights "
162  << wgts.size() << " "
163  << 0 << std::endl;
164  fail = 6;
165  }
166 
167  for (zlno_t i=0; !fail && i < nLocalIds; i++){
168  std::set<zgno_t>::iterator next = idSet.find(gids[i]);
169  if (next == idSet.end()) {
170  std::cerr << rank << ") getIdentifierList gid not found "
171  << gids[i] << std::endl;
172  fail = 7;
173  }
174  }
175 
176  gfail = globalFail(comm, fail);
177 
178  if (gfail)
179  printFailureCode(comm, fail);
180 
181  delete model;
182  delete uinput;
183 }
184 
185 int main(int argc, char *argv[])
186 {
187  Teuchos::GlobalMPISession session(&argc, &argv);
188  Teuchos::RCP<const Teuchos::Comm<int> > comm =
189  Teuchos::DefaultComm<int>::getComm();
190 
191  int rank = comm->getRank();
192 
193  string fname("simple");
194 
195 
196  if (rank == 0){
197  std::cout << fname;
198  }
199  testIdentifierModel(fname, 0,0,0,comm);
200 
201  if (rank==0) std::cout << "PASS" << std::endl;
202 
203  return 0;
204 }
Zoltan2::BaseAdapter< userTypes_t > base_adapter_t
Tpetra::CrsMatrix< zscalar_t, zlno_t, zgno_t, znode_t > tcrsMatrix_t
Definition: GraphModel.cpp:84
int main(int argc, char *argv[])
int globalFail(const RCP< const Comm< int > > &comm, int fail)
MatrixAdapter defines the adapter interface for matrices.
RCP< tcrsMatrix_t > getUITpetraCrsMatrix()
Provides access for Zoltan2 to Xpetra::CrsMatrix data.
global_size_t getGlobalNumIdentifiers() const
int zlno_t
common code used by tests
size_t getIdentifierList(ArrayView< const gno_t > &Ids, ArrayView< input_t > &wgts) const
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.
void printFailureCode(const RCP< const Comm< int > > &comm, int fail)
BaseAdapter defines methods required by all Adapters.
IdentifierModel defines the interface for all identifier models.
std::string testDataFilePath(".")