Zoltan2  Version of the Day
CoordinateModel.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 CoordinateModel
47 //
48 
51 #include <Zoltan2_TestHelpers.hpp>
52 
53 #include <set>
54 #include <bitset>
55 
56 #include <Teuchos_Comm.hpp>
57 #include <Teuchos_CommHelpers.hpp>
58 #include <Teuchos_DefaultComm.hpp>
59 #include <Teuchos_ArrayView.hpp>
60 #include <Teuchos_OrdinalTraits.hpp>
61 
62 #include <Tpetra_CrsMatrix.hpp>
63 
64 using namespace std;
65 using Teuchos::RCP;
66 using Teuchos::Comm;
67 using Teuchos::DefaultComm;
68 using std::cout;
69 using std::endl;
70 
71 void testCoordinateModel(std::string &fname, int weightDim,
72  const RCP<const Comm<int> > &comm, bool consecutiveIds,
73  bool nodeZeroHasAll, bool printInfo)
74 {
75  int fail = 0, gfail = 0;
76 
77  if (printInfo){
78  cout << "Test: " << fname << endl;
79  cout << "Weight dim: " << weightDim;
80  cout << " want consec ids: " << consecutiveIds;
81  cout << " proc 0 has all: " << nodeZeroHasAll;
82  cout << endl;
83  }
84 
86  // Input data
88 
89  typedef Tpetra::MultiVector<scalar_t, lno_t, gno_t, node_t> mv_t;
90 
91  RCP<UserInputForTests> uinput;
92 
93  try{
94  uinput = rcp(new UserInputForTests(testDataFilePath, fname, comm, true));
95  }
96  catch(std::exception &e){
97  fail=1;
98  }
99 
100  TEST_FAIL_AND_EXIT(*comm, !fail, "input constructor", 1);
101 
102  RCP<mv_t> coords;
103 
104  try{
105  coords = uinput->getCoordinates();
106  }
107  catch(std::exception &e){
108  fail=2;
109  }
110 
111  TEST_FAIL_AND_EXIT(*comm, !fail, "getting coordinates", 1);
112 
113  int coordDim = coords->getNumVectors();
114 
115  TEST_FAIL_AND_EXIT(*comm, coordDim <= 3, "dim 3 at most", 1);
116 
117  const scalar_t *x=NULL, *y=NULL, *z=NULL;
118 
119  x = coords->getData(0).getRawPtr();
120  if (coordDim > 1){
121  y = coords->getData(1).getRawPtr();
122  if (coordDim > 2)
123  z = coords->getData(2).getRawPtr();
124  }
125 
126  // Are these coordinates correct
127 
128  int nLocalIds = coords->getLocalLength();
129  ArrayView<const gno_t> idList = coords->getMap()->getNodeElementList();
130 
131  int nGlobalIds = 0;
132  if (nodeZeroHasAll){
133  if (comm->getRank() > 0){
134  x = y = z = NULL;
135  nLocalIds = 0;
136  }
137  else{
138  nGlobalIds = nLocalIds;
139  }
140  Teuchos::broadcast<int, int>(*comm, 0, &nGlobalIds);
141  }
142  else{
143  nGlobalIds = coords->getGlobalLength();
144  }
145 
146  Array<ArrayRCP<const scalar_t> > coordWeights(weightDim);
147 
148  if (nLocalIds > 0){
149  for (int wdim=0; wdim < weightDim; wdim++){
150  scalar_t *w = new scalar_t [nLocalIds];
151  for (int i=0; i < nLocalIds; i++){
152  w[i] = ((i%2) + 1) + wdim;
153  }
154  coordWeights[wdim] = Teuchos::arcp(w, 0, nLocalIds);
155  }
156  }
157 
158 
160  // Create a BasicCoordinateInput adapter object.
162 
164  typedef Zoltan2::CoordinateInput<mv_t> base_ia_t;
165 
166  RCP<ia_t> ia;
167 
168  if (weightDim == 0){ // use the simpler constructor
169  try{
170  ia = rcp(new ia_t(nLocalIds, idList.getRawPtr(), x, y, z));
171  }
172  catch(std::exception &e){
173  fail=3;
174  }
175  }
176  else{
177  std::vector<const scalar_t *> values, weights;
178  std::vector<int> valueStrides, weightStrides; // default is 1
179  values.push_back(x);
180  if (y) {
181  values.push_back(y);
182  if (z)
183  values.push_back(z);
184  }
185  for (int wdim=0; wdim < weightDim; wdim++){
186  weights.push_back(coordWeights[wdim].getRawPtr());
187  }
188 
189  try{
190  ia = rcp(new ia_t(nLocalIds, idList.getRawPtr(),
191  values, valueStrides, weights, weightStrides));
192  }
193  catch(std::exception &e){
194  fail=4;
195  }
196  }
197 
198  RCP<base_ia_t> base_ia = Teuchos::rcp_implicit_cast<base_ia_t>(ia);
199 
200  TEST_FAIL_AND_EXIT(*comm, !fail, "making input adapter", 1);
201 
203  // Create an CoordinateModel with this input
205 
207  typedef std::bitset<Zoltan2::NUM_MODEL_FLAGS> modelFlags_t;
208  typedef Zoltan2::CoordinateModel<base_ia_t> model_t;
209  modelFlags_t modelFlags;
210 
211  if (consecutiveIds)
213 
214  RCP<const Zoltan2::Environment> env = rcp(new Zoltan2::Environment);
215  RCP<model_t> model;
216 
217 
218  try{
219  model = rcp(new model_t(base_ia.getRawPtr(), env, comm, modelFlags));
220  }
221  catch (std::exception &e){
222  fail = 5;
223  }
224 
225  TEST_FAIL_AND_EXIT(*comm, !fail, "making model", 1);
226 
227  // Test the CoordinateModel interface
228 
229  if (model->getCoordinateDim() != coordDim)
230  fail = 6;
231 
232  if (!fail && model->getLocalNumCoordinates() != size_t(nLocalIds))
233  fail = 7;
234 
235  if (!fail && model->getGlobalNumCoordinates() != size_t(nGlobalIds))
236  fail = 8;
237 
238  if (!fail && model->getCoordinateWeightDim() != weightDim)
239  fail = 9;
240 
241  gfail = globalFail(comm, fail);
242 
243  if (gfail)
244  printFailureCode(comm, fail);
245 
246  ArrayView<const gno_t> gids;
247  ArrayView<input_t> xyz;
248  ArrayView<input_t> wgts;
249 
250  model->getCoordinates(gids, xyz, wgts);
251 
252  if (!fail && gids.size() != nLocalIds)
253  fail = 10;
254 
255  for (int i=0; !fail && i < nLocalIds; i++){
256  if (gids[i] != idList[i])
257  fail = 11;
258  }
259 
260  if (!fail && wgts.size() != weightDim)
261  fail = 12;
262 
263  const scalar_t *vals[3] = {x, y, z};
264 
265  for (int dim=0; !fail && dim < coordDim; dim++){
266  for (int i=0; !fail && i < nLocalIds; i++){
267  if (xyz[dim][i] != vals[dim][i])
268  fail = 13;
269  }
270  }
271 
272  for (int wdim=0; !fail && wdim < weightDim; wdim++){
273  for (int i=0; !fail && i < nLocalIds; i++){
274  if (wgts[wdim][i] != coordWeights[wdim][i])
275  fail = 14;
276  }
277  }
278 
279  if (!fail && consecutiveIds){
281  gids.getRawPtr(), nLocalIds);
282 
283  if (!inARow)
284  fail = 15;
285  }
286 
287  gfail = globalFail(comm, fail);
288 
289  if (gfail)
290  printFailureCode(comm, fail);
291 }
292 
293 int main(int argc, char *argv[])
294 {
295  Teuchos::GlobalMPISession session(&argc, &argv);
296  Teuchos::RCP<const Teuchos::Comm<int> > comm =
297  Teuchos::DefaultComm<int>::getComm();
298 
299  int rank = comm->getRank();
300  string fname("simple"); // reader will seek coord file
301  bool wishConsecutiveIds = true;
302 
303  testCoordinateModel(fname, 0, comm, !wishConsecutiveIds, false, rank==0);
304 
305  testCoordinateModel(fname, 0, comm, wishConsecutiveIds, false, rank==0);
306 
307  testCoordinateModel(fname, 1, comm, !wishConsecutiveIds, false, rank==0);
308 
309  testCoordinateModel(fname, 2, comm, wishConsecutiveIds, false, rank==0);
310 
311  testCoordinateModel(fname, 0, comm, !wishConsecutiveIds, true, rank==0);
312 
313  testCoordinateModel(fname, 0, comm, wishConsecutiveIds, true, rank==0);
314 
315  testCoordinateModel(fname, 1, comm, !wishConsecutiveIds, true, rank==0);
316 
317  testCoordinateModel(fname, 2, comm, wishConsecutiveIds, true, rank==0);
318 
319  if (rank==0) cout << "PASS" << endl;
320 
321  return 0;
322 }
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.
#define TEST_FAIL_AND_EXIT(comm, ok, s, code)
Defines the BasicCoordinateInput class.
common code used by tests
void testCoordinateModel(std::string &fname, int weightDim, const RCP< const Comm< int > > &comm, bool consecutiveIds, bool nodeZeroHasAll, bool printInfo)
list idList
Match up parameters to validators.
This class provides geometric coordinates with optional weights to the Zoltan2 algorithm.
dictionary vals
Definition: xml2dox.py:186
The StridedData class manages lists of weights or coordinates.
The user parameters, debug, timing and memory profiling output objects, and error checking methods...
static bool areConsecutive(const T *val, size_t n)
Determine if the values are locally increasing consecutive.
double scalar_t
void printFailureCode(const RCP< const Comm< int > > &comm, int fail)
int main(int argc, char *argv[])
Defines the CoordinateModel classes.
BasicCoordinateInput represents geometric coordinates that are supplied by the user as pointers to st...
CoordinateInput defines the interface for input geometric coordinates.
std::string testDataFilePath(".")