Zoltan2  Version of the Day
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 BasicIdentifierInput 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, gno_t xdim, gno_t ydim, gno_t zdim,
72  const RCP<const Comm<int> > &comm, bool consecutiveIds)
73 {
74  int rank = comm->getRank();
75  int fail = 0, gfail = 0;
76 
77  std::bitset<Zoltan2::NUM_MODEL_FLAGS> modelFlags = 0;
78  if (consecutiveIds)
80 
81  RCP<const Zoltan2::Environment> env = rcp(new Zoltan2::Environment);
82 
84  // Use an Tpetra::CrsMatrix for the user data.
86  typedef Tpetra::CrsMatrix<scalar_t, lno_t, gno_t> tcrsMatrix_t;
87 
88  UserInputForTests *input;
89  if (fname.size() > 0)
90  input = new UserInputForTests(testDataFilePath, fname, comm, true);
91  else
92  input = new UserInputForTests(xdim,ydim,zdim,string(""),comm, true);
93 
94  RCP<tcrsMatrix_t > M = input->getTpetraCrsMatrix();
95  lno_t nLocalIds = M->getNodeNumRows();
96  gno_t nGlobalIds = M->getGlobalNumRows();
97 
98  ArrayView<const gno_t> idList = M->getRowMap()->getNodeElementList();
99  std::set<gno_t> idSet(idList.begin(), idList.end());
100 
102  // Create an IdentifierModel with this input
104 
106  typedef Zoltan2::MatrixInput<tcrsMatrix_t> base_adapter_t;
108 
109  RCP<const adapter_t> ia = Teuchos::rcp(new adapter_t(M));
110 
112  const base_adapter_t *base_ia = ia.get();
113 
114  try{
116  base_ia, env, comm, modelFlags);
117  }
118  catch (std::exception &e){
119  std::cerr << rank << ") " << e.what() << std::endl;
120  fail = 1;
121  }
122 
123  gfail = globalFail(comm, fail);
124 
125  if (gfail)
126  printFailureCode(comm, fail);
127 
128  // Test the IdentifierModel interface
129 
130  if (model->getLocalNumIdentifiers() != size_t(nLocalIds))
131  fail = 2;
132 
133  if (!fail && model->getGlobalNumIdentifiers() != size_t(nGlobalIds))
134  fail = 3;
135 
136  // For now, MatrixInput does not implement weights
137  if (!fail && model->getIdentifierWeightDim() != 0)
138  fail = 4;
139 
140  gfail = globalFail(comm, fail);
141 
142  if (gfail)
143  printFailureCode(comm, fail);
144 
145  ArrayView<const gno_t> gids;
146  ArrayView<input_t> wgts;
147 
148  model->getIdentifierList(gids, wgts);
149 
150  if (!fail && gids.size() != nLocalIds)
151  fail = 5;
152 
153  if (!fail && wgts.size() != 0)
154  fail = 6;
155 
156  for (lno_t i=0; !fail && i < nLocalIds; i++){
157  std::set<gno_t>::iterator next = idSet.find(gids[i]);
158  if (next == idSet.end())
159  fail = 7;
160  }
161 
162  if (!fail && consecutiveIds){
164  gids.getRawPtr(), nLocalIds);
165 
166  if (!inARow)
167  fail = 8;
168  }
169 
170  gfail = globalFail(comm, fail);
171 
172  if (gfail)
173  printFailureCode(comm, fail);
174 
175  delete model;
176  delete input;
177 }
178 
179 int main(int argc, char *argv[])
180 {
181  Teuchos::GlobalMPISession session(&argc, &argv);
182  Teuchos::RCP<const Teuchos::Comm<int> > comm =
183  Teuchos::DefaultComm<int>::getComm();
184 
185  int rank = comm->getRank();
186 
187  string fname("simple");
188 
189  bool wishConsecutiveIds = true;
190 
191  if (rank == 0){
192  std::cout << fname;
193  std::cout << ", consecutive IDs not requested" << std::endl;
194  }
195  testIdentifierModel(fname, 0,0,0,comm, !wishConsecutiveIds);
196 
197  if (rank == 0){
198  std::cout << fname;
199  std::cout << ", consecutive IDs are requested" << std::endl;
200  }
201  testIdentifierModel(fname, 0,0,0,comm, wishConsecutiveIds);
202 
203  if (rank==0) std::cout << "PASS" << std::endl;
204 
205  return 0;
206 }
Defines the XpetraCrsMatrixInput adapter class.
Provides access for Zoltan2 to Xpetra::CrsMatrix data.
int globalFail(const RCP< const Comm< int > > &comm, int fail)
Zoltan2::StridedData< lno_t, scalar_t > input_t
Definition: GraphModel.cpp:80
algorithm requires consecutive ids
A class that generates typical user input for testing.
common code used by tests
Tpetra::CrsMatrix< scalar_t, lno_t, gno_t, node_t > tcrsMatrix_t
Definition: GraphModel.cpp:78
int main(int argc, char *argv[])
list idList
Match up parameters to validators.
int getIdentifierWeightDim() const
Returns the dimension (0 or greater) of identifier weights.
Defines the IdentifierModel interface.
RCP< tcrsMatrix_t > getTpetraCrsMatrix()
The StridedData class manages lists of weights or coordinates.
The user parameters, debug, timing and memory profiling output objects, and error checking methods...
void testIdentifierModel(std::string fname, gno_t xdim, gno_t ydim, gno_t zdim, const RCP< const Comm< int > > &comm, bool consecutiveIds)
MatrixInput defines the interface for input adapters for matrices.
static bool areConsecutive(const T *val, size_t n)
Determine if the values are locally increasing consecutive.
void printFailureCode(const RCP< const Comm< int > > &comm, int fail)
size_t getLocalNumIdentifiers() const
Returns the number identifiers on this process.
IdentifierModel defines the interface for all identifier models.
global_size_t getGlobalNumIdentifiers() const
Returns the global number identifiers.
size_t getIdentifierList(ArrayView< const gno_t > &Ids, ArrayView< input_t > &wgts) const
Sets pointers to this process&#39; identifier Ids and their weights.
Defines the BasicIdentifierInput class.
std::string testDataFilePath(".")