Zoltan2  Version of the Day
BasicCoordinateInput.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 // Test for Zoltan2::BasicCoordinateInput
47 
49 #include <Zoltan2_TestHelpers.hpp>
50 
51 #include <Teuchos_GlobalMPISession.hpp>
52 #include <Teuchos_DefaultComm.hpp>
53 #include <Teuchos_RCP.hpp>
54 #include <Teuchos_CommHelpers.hpp>
55 #include <Teuchos_SerialDenseVector.hpp>
56 
57 using Teuchos::RCP;
58 using Teuchos::Comm;
59 using Teuchos::DefaultComm;
60 using Teuchos::Array;
61 
62 typedef Teuchos::SerialDenseVector<lno_t, scalar_t> tvec_t;
63 
65 
68  int len, int glen, gno_t *ids,
69  scalar_t *xyz,
70  scalar_t *weights,
71  int nCoords, int nWeights)
72 {
73  int fail = 0;
74 
75  if (ia->getCoordinateDimension() != nCoords)
76  fail = 100;
77 
78  if (!fail && ia->getNumberOfWeights() != nWeights)
79  fail = 101;
80 
81  if (!fail && ia->getLocalNumberOfCoordinates() != size_t(len))
82  fail = 102;
83 
84  for (int x=0; !fail && x < nCoords; x++){
85  const gno_t *idList;
86  const scalar_t *vals;
87  int stride;
88 
89  size_t nvals = ia->getCoordinates(x, idList, vals, stride);
90 
91  if (nvals != size_t(len*stride))
92  fail = 104;
93 
94  scalar_t *coordVal = xyz + x;
95  for (int i=0; !fail && i < len; i++, coordVal += 3){
96 
97  if (idList[i] != ids[i])
98  fail = 105;
99 
100  if (!fail && vals[stride*i] != *coordVal)
101  fail = 106;
102  }
103  }
104 
105  for (int w=0; !fail && w < nWeights; w++){
106  const scalar_t *wgts;
107  int stride;
108 
109  size_t nvals = ia->getCoordinateWeights(w, wgts, stride);
110 
111  if (nvals != size_t(len))
112  fail = 108;
113 
114  scalar_t *weightVal = weights + len*w;
115  for (int i=0; !fail && i < len; i++, weightVal++){
116  if (wgts[stride*i] != *weightVal)
117  fail = 110;
118  }
119  }
120 
121  return fail;
122 }
123 
124 
125 int main(int argc, char *argv[])
126 {
127  Teuchos::GlobalMPISession session(&argc, &argv);
128  RCP<const Comm<int> > comm = DefaultComm<int>::getComm();
129  int rank = comm->getRank();
130  int fail = 0;
131 
132  // Get some coordinates
133 
134  typedef Tpetra::MultiVector<scalar_t, lno_t, gno_t, node_t> mv_t;
135  RCP<UserInputForTests> uinput;
136  std::string fname("simple");
137 
138  try{
139  uinput = rcp(new UserInputForTests(testDataFilePath, fname, comm, true));
140  }
141  catch(std::exception &e){
142  fail=1;
143  }
144 
145  TEST_FAIL_AND_EXIT(*comm, !fail, "input constructor", 1);
146 
147  RCP<mv_t> coords;
148 
149  try{
150  coords = uinput->getCoordinates();
151  }
152  catch(std::exception &e){
153  fail=1;
154  }
155 
156  TEST_FAIL_AND_EXIT(*comm, !fail, "getting coordinates", 1);
157 
158  int numLocalIds = coords->getLocalLength();
159  int numGlobalIds = coords->getGlobalLength();
160  int coordDim = coords->getNumVectors();
161  ArrayView<const gno_t> idList = coords->getMap()->getNodeElementList();
162 
163  // Create global Ids, x-, y- and z-coordinates, and also arrays of weights.
164 
165  Array<gno_t> myIds(numLocalIds);
166  gno_t base = rank * numLocalIds;
167 
168  int wdim = 2;
169  Array<scalar_t> weights(numLocalIds*wdim);
170 
171  scalar_t *x_values= coords->getDataNonConst(0).getRawPtr();
172  scalar_t *y_values= x_values; // fake 3 dimensions if needed
173  scalar_t *z_values= x_values;
174 
175  if (coordDim > 1){
176  y_values= coords->getDataNonConst(1).getRawPtr();
177  if (coordDim > 2)
178  z_values= coords->getDataNonConst(2).getRawPtr();
179  }
180 
181  Array<scalar_t> xyz_values(3*numLocalIds);
182 
183  for (lno_t i=0; i < numLocalIds; i++) // global Ids
184  myIds[i] = base+i;
185 
186  scalar_t *x = xyz_values.getRawPtr(); // a stride-3 coordinate array
187  scalar_t *y = x+1;
188  scalar_t *z = y+1;
189 
190  for (int i=0, ii=0; i < numLocalIds; i++, ii += 3){
191  x[ii] = x_values[i];
192  y[ii] = y_values[i];
193  z[ii] = z_values[i];
194  }
195 
196  RCP<Zoltan2::BasicCoordinateInput<userTypes_t> > ia;
197 
198  {
200  // 3-dimensional coordinates with stride one and no weights,
201  // using simpler constructor
202 
203  int ncoords = 3;
204  int nweights = 0;
205 
206  try{
208  numLocalIds, myIds.getRawPtr(), x_values, y_values, z_values));
209  }
210  catch (std::exception &e){
211  fail = 1;
212  }
213 
214  TEST_FAIL_AND_RETURN_VALUE(*comm, fail==0, "constructor 0", fail);
215 
216  fail = checkBasicCoordinate(ia.getRawPtr(), numLocalIds, numGlobalIds,
217  myIds.getRawPtr(), xyz_values.getRawPtr(),
218  weights.getRawPtr(), ncoords, nweights);
219 
220  TEST_FAIL_AND_RETURN_VALUE(*comm, fail==0, "check adapter 0", fail);
221  }
222 
223  {
225  // 3-dimensional coordinates with stride one and no weights
226 
227  int ncoords = 3;
228  int nweights = 0;
229 
230  std::vector<const scalar_t *> values, weightValues;
231  std::vector<int> valueStrides, weightStrides;
232 
233  values.push_back(x_values);
234  values.push_back(y_values);
235  values.push_back(z_values);
236  valueStrides.push_back(1);
237  valueStrides.push_back(1);
238  valueStrides.push_back(1);
239 
240  try{
242  numLocalIds, myIds.getRawPtr(), values, valueStrides,
243  weightValues, weightStrides));
244  }
245  catch (std::exception &e){
246  fail = 1;
247  }
248 
249  TEST_FAIL_AND_RETURN_VALUE(*comm, fail==0, "constructor 1", fail);
250 
251  fail = checkBasicCoordinate(ia.getRawPtr(), numLocalIds, numGlobalIds,
252  myIds.getRawPtr(), xyz_values.getRawPtr(),
253  weights.getRawPtr(), ncoords, nweights);
254 
255  TEST_FAIL_AND_RETURN_VALUE(*comm, fail==0, "check adapter 1", fail);
256 
257  // Try using the default: no strides supplied means strides are one.
258 
259  std::vector<int> emptyStrides;
260 
261  try{
263  numLocalIds, myIds.getRawPtr(), values, emptyStrides,
264  weightValues, emptyStrides));
265  }
266  catch (std::exception &e){
267  fail = 1;
268  }
269 
270  TEST_FAIL_AND_RETURN_VALUE(*comm, fail==0, "constructor 2", fail);
271 
272  fail = checkBasicCoordinate(ia.getRawPtr(), numLocalIds, numGlobalIds,
273  myIds.getRawPtr(), xyz_values.getRawPtr(),
274  weights.getRawPtr(), ncoords, nweights);
275 
276  TEST_FAIL_AND_RETURN_VALUE(*comm, fail==0, "check adapter 2", fail);
277  }
278 
279  {
281  // 2-dimensional coordinates with stride three and two weights
282 
283  int ncoords = 2;
284  int nweights = 2;
285 
286  std::vector<const scalar_t *> values, weightValues;
287  std::vector<int> valueStrides, weightStrides;
288 
289  values.push_back(xyz_values.getRawPtr());
290  values.push_back(xyz_values.getRawPtr() + 1);
291  valueStrides.push_back(3);
292  valueStrides.push_back(3);
293 
294  weightValues.push_back(weights.getRawPtr());
295  weightValues.push_back(weights.getRawPtr() + numLocalIds);
296  weightStrides.push_back(1);
297  weightStrides.push_back(1);
298 
299  try{
301  numLocalIds, myIds.getRawPtr(), values, valueStrides,
302  weightValues, weightStrides));
303  }
304  catch (std::exception &e){
305  fail = 1;
306  }
307 
308  TEST_FAIL_AND_RETURN_VALUE(*comm, fail==0, "constructor 3", fail);
309 
310  fail = checkBasicCoordinate(ia.getRawPtr(), numLocalIds, numGlobalIds,
311  myIds.getRawPtr(), xyz_values.getRawPtr(),
312  weights.getRawPtr(), ncoords, nweights);
313 
314  TEST_FAIL_AND_RETURN_VALUE(*comm, fail==0, "check adapter 3", fail);
315 
316  // Try using default weight strides
317 
318  std::vector<int> emptyStrides;
319 
320  try{
322  numLocalIds, myIds.getRawPtr(), values, valueStrides,
323  weightValues, emptyStrides));
324  }
325  catch (std::exception &e){
326  fail = 1;
327  }
328 
329  TEST_FAIL_AND_RETURN_VALUE(*comm, fail==0, "constructor 4", fail);
330 
331  fail = checkBasicCoordinate(ia.getRawPtr(), numLocalIds, numGlobalIds,
332  myIds.getRawPtr(), xyz_values.getRawPtr(),
333  weights.getRawPtr(), ncoords, nweights);
334 
335  TEST_FAIL_AND_RETURN_VALUE(*comm, fail==0, "check adapter 4", fail);
336  }
337 
338  {
340  // 1-dimensional coordinates with stride one and two weights
341 
342  int ncoords = 1;
343  int nweights = 2;
344 
345  std::vector<const scalar_t *> values, weightValues;
346  std::vector<int> valueStrides, weightStrides;
347 
348  values.push_back(x_values);
349  valueStrides.push_back(1);
350 
351  weightValues.push_back(weights.getRawPtr());
352  weightValues.push_back(weights.getRawPtr() + numLocalIds);
353  weightStrides.push_back(1);
354  weightStrides.push_back(1);
355 
356  try{
358  numLocalIds, myIds.getRawPtr(), values, valueStrides,
359  weightValues, weightStrides));
360  }
361  catch (std::exception &e){
362  fail = 1;
363  }
364 
365  TEST_FAIL_AND_RETURN_VALUE(*comm, fail==0, "constructor 4", fail);
366 
367  fail = checkBasicCoordinate(ia.getRawPtr(), numLocalIds, numGlobalIds,
368  myIds.getRawPtr(), xyz_values.getRawPtr(),
369  weights.getRawPtr(), ncoords, nweights);
370 
371  TEST_FAIL_AND_RETURN_VALUE(*comm, fail==0, "check adapter 4", fail);
372  }
373 
374  if (rank == 0)
375  std::cout << "PASS" << std::endl;
376 
377  return fail;
378 }
379 
size_t getLocalNumberOfCoordinates() const
Return the number of coordinates on this process.
size_t getCoordinateWeights(int dim, const scalar_t *&weights, int &stride) const
Provide a pointer to the weights, if any, corresponding to the coordinates returned in getCoordinates...
A simple class that can be the User template argument for an InputAdapter.
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
Teuchos::SerialDenseVector< lno_t, scalar_t > tvec_t
int main(int argc, char *argv[])
list idList
Match up parameters to validators.
dictionary vals
Definition: xml2dox.py:186
int getNumberOfWeights() const
Return the number of weights per coordinate.
int getCoordinateDimension() const
Return dimension of the coordinates.
size_t getCoordinates(int dim, const gid_t *&gids, const scalar_t *&coords, int &stride) const
Provide a pointer to one dimension of this process&#39; coordinates.
int checkBasicCoordinate(Zoltan2::BasicCoordinateInput< userTypes_t > *ia, int len, int glen, gno_t *ids, scalar_t *xyz, scalar_t *weights, int nCoords, int nWeights)
#define TEST_FAIL_AND_RETURN_VALUE(comm, ok, s, rc)
double scalar_t
Zoltan2::BasicUserTypes< scalar_t, gno_t, lno_t, gno_t > userTypes_t
BasicCoordinateInput represents geometric coordinates that are supplied by the user as pointers to st...
std::string testDataFilePath(".")