Zoltan2  Version of the Day
BasicVectorInput.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::BasicVectorInput
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 
56 using Teuchos::RCP;
57 using Teuchos::Comm;
58 using Teuchos::DefaultComm;
59 
61 
63  Zoltan2::BasicVectorInput<userTypes_t> *ia, int len, int glen,
64  gno_t *ids, int mvdim, const scalar_t **values, int *valueStrides,
65  int wdim, const scalar_t **weights, int *weightStrides)
66 {
67  int fail = 0;
68  bool strideOne = false;
69 
70  if (valueStrides == NULL) strideOne = true;
71 
72  if (ia->getNumberOfVectors() != mvdim)
73  fail = 100;
74 
75  if (!fail && ia->getNumberOfWeights() != wdim)
76  fail = 101;
77 
78  if (!fail && ia->getLocalLength() != size_t(len))
79  fail = 102;
80 
81  if (!fail && ia->getGlobalLength() != size_t(glen))
82  fail = 103;
83 
84  for (int v=0; !fail && v < mvdim; v++){
85  const gno_t *idList;
86  const scalar_t *vals;
87  int correctStride = (strideOne ? 1 : valueStrides[v]);
88  int stride;
89 
90  size_t nvals = ia->getVector(v, idList, vals, stride);
91 
92  if (nvals != size_t(len*stride))
93  fail = 104;
94 
95  if (!fail && stride != correctStride)
96  fail = 105;
97 
98  for (int i=0; !fail && i < len; i++){
99 // TODO fix values check
100 // if (vals[stride*i] != values[v][correctStride*i])
101 // fail = 106;
102 
103  if (!fail && idList[i] != ids[i])
104  fail = 107;
105  }
106  }
107 
108  for (int w=0; !fail && w < wdim; w++){
109  const scalar_t *wgts;
110  int stride;
111 
112  size_t nvals = ia->getVectorWeights(w, wgts, stride);
113 
114  if (nvals != size_t(len*stride))
115  fail = 108;
116 
117  if (!fail && stride != weightStrides[w])
118  fail = 109;
119 
120  for (int i=0; !fail && i < len; i++){
121  if (wgts[stride*i] != weights[w][weightStrides[w]*i])
122  fail = 110;
123  }
124  }
125 
126  return fail;
127 }
128 
129 
130 int main(int argc, char *argv[])
131 {
132  Teuchos::GlobalMPISession session(&argc, &argv);
133  RCP<const Comm<int> > comm = DefaultComm<int>::getComm();
134  int rank = comm->getRank();
135  int nprocs = comm->getSize();
136  int fail = 0;
137 
138  // Create a single vector and a strided multi-vector with
139  // strided multi-weights.
140 
141  lno_t numLocalIds = 10;
142  gno_t *myIds = new gno_t [numLocalIds];
143  gno_t base = rank * numLocalIds;
144 
145  int wdim = 2;
146  scalar_t *weights = new scalar_t [numLocalIds*wdim];
147  int *weightStrides = new int [wdim];
148  const scalar_t **weightPtrs = new const scalar_t * [wdim];
149 
150  int mvdim = 3;
151  scalar_t *v_values= new scalar_t [numLocalIds];
152  scalar_t *mv_values= new scalar_t [mvdim * numLocalIds];
153  int *valueStrides = new int [mvdim];
154  const scalar_t **valuePtrs = new const scalar_t * [mvdim];
155 
156  for (lno_t i=0; i < numLocalIds; i++){
157  myIds[i] = base+i;
158 
159  for (int w=0; w < wdim; w++)
160  weights[w*numLocalIds + i] = w + 1 + nprocs - rank;
161 
162  v_values[i] = numLocalIds-i;
163 
164  for (int v=0; v < mvdim; v++)
165  mv_values[i*mvdim + v] = (v+1) * (nprocs-rank) / (i+1);
166  }
167 
168  for (int w=0; w < wdim; w++){
169  weightStrides[w] = 1;
170  weightPtrs[w] = weights + numLocalIds*w;
171  }
172 
173  for (int v=0; v < mvdim; v++){
174  valueStrides[v] = mvdim;
175  valuePtrs[v] = mv_values + v;
176  }
177 
179 
180  {
181  // A Zoltan2::BasicVectorInput object with one vector and no weights
182 
183  std::vector<const scalar_t *> weightValues;
184  std::vector<int> strides;
185 
186  try{
187  ia = new Zoltan2::BasicVectorInput<userTypes_t>(numLocalIds, myIds,
188  v_values, 1, weightValues, strides);
189  }
190  catch (std::exception &e){
191  fail = 1;
192  }
193 
194  TEST_FAIL_AND_RETURN_VALUE(*comm, fail==0, "constructor 1", fail);
195 
196  fail = checkBasicVector(ia, numLocalIds, numLocalIds*nprocs,
197  myIds, 1, valuePtrs, NULL, 0, NULL, NULL);
198 
199  delete ia;
200 
201  TEST_FAIL_AND_RETURN_VALUE(*comm, fail==0, "check vector 1", fail);
202  }
203 
204  {
205  // A Zoltan2::BasicVectorInput object with one vector and weights
206 
207  std::vector<const scalar_t *> weightValues;
208  std::vector<int> strides;
209 
210  weightValues.push_back(weightPtrs[0]);
211  weightValues.push_back(weightPtrs[1]);
212  strides.push_back(1);
213  strides.push_back(1);
214 
215  try{
216  ia = new Zoltan2::BasicVectorInput<userTypes_t>(numLocalIds, myIds,
217  v_values, 1, weightValues, strides);
218  }
219  catch (std::exception &e){
220  fail = 1;
221  }
222 
223  TEST_FAIL_AND_RETURN_VALUE(*comm, fail==0, "constructor 2", fail);
224 
225  fail = checkBasicVector(ia, numLocalIds, numLocalIds*nprocs,
226  myIds, 1, valuePtrs, NULL, wdim, weightPtrs, weightStrides);
227 
228  delete ia;
229 
230  TEST_FAIL_AND_RETURN_VALUE(*comm, fail==0, "check vector 2", fail);
231  }
232 
233  {
234  // A Zoltan2::BasicVectorInput object with a multivector and no weights
235 
236  std::vector<const scalar_t *> weightValues, values;
237  std::vector<int> wstrides, vstrides;
238 
239  for (int dim=0; dim < mvdim; dim++){
240  values.push_back(valuePtrs[dim]);
241  vstrides.push_back(mvdim);
242  }
243 
244 
245  try{
247  numLocalIds, myIds, values, vstrides, weightValues, wstrides);
248  }
249  catch (std::exception &e){
250  fail = 1;
251  }
252 
253  TEST_FAIL_AND_RETURN_VALUE(*comm, fail==0, "constructor 3", fail);
254 
255  fail = checkBasicVector(ia, numLocalIds, numLocalIds*nprocs,
256  myIds, mvdim, valuePtrs, valueStrides, 0, NULL, NULL);
257 
258  delete ia;
259 
260  TEST_FAIL_AND_RETURN_VALUE(*comm, fail==0, "check vector 3", fail);
261  }
262 
263  {
264  // A Zoltan2::BasicVectorInput object with a multivector with weights
265 
266  std::vector<const scalar_t *> weightValues, values;
267  std::vector<int> wstrides, vstrides;
268 
269  for (int dim=0; dim < wdim; dim++){
270  weightValues.push_back(weightPtrs[dim]);
271  wstrides.push_back(1);
272  }
273 
274  for (int dim=0; dim < mvdim; dim++){
275  values.push_back(valuePtrs[dim]);
276  vstrides.push_back(mvdim);
277  }
278 
279  try{
281  numLocalIds, myIds, values, vstrides, weightValues, wstrides);
282 
283  }
284  catch (std::exception &e){
285  fail = 1;
286  }
287 
288  TEST_FAIL_AND_RETURN_VALUE(*comm, fail==0, "constructor 4", fail);
289 
290  fail = checkBasicVector(ia, numLocalIds, numLocalIds*nprocs,
291  myIds, mvdim, valuePtrs, valueStrides,
292  wdim, weightPtrs, weightStrides);
293 
294  delete ia;
295 
296  TEST_FAIL_AND_RETURN_VALUE(*comm, fail==0, "check vector 4", fail);
297  }
298 
299  if (rank == 0)
300  std::cout << "PASS" << std::endl;
301 
302  delete [] myIds;
303  delete [] weights;
304  delete [] weightStrides;
305  delete [] weightPtrs;
306  delete [] v_values;
307  delete [] mv_values;
308  delete [] valueStrides;
309  delete [] valuePtrs;
310 
311  return fail;
312 }
313 
size_t getVector(const gid_t *&ids, const scalar_t *&element, int &stride) const
Provide a pointer to the vertex elements. If the VectorInput represents more than one vector...
A simple class that can be the User template argument for an InputAdapter.
common code used by tests
int main(int argc, char *argv[])
BasicVectorInput represents a vector (plus optional weights) supplied by the user as pointers to stri...
list idList
Match up parameters to validators.
int checkBasicVector(Zoltan2::BasicVectorInput< userTypes_t > *ia, int len, int glen, gno_t *ids, int mvdim, const scalar_t **values, int *valueStrides, int wdim, const scalar_t **weights, int *weightStrides)
dictionary vals
Definition: xml2dox.py:186
int getNumberOfVectors() const
Return the number of vectors (typically one).
#define TEST_FAIL_AND_RETURN_VALUE(comm, ok, s, rc)
double scalar_t
size_t getLocalLength() const
Return the length of the portion of the vector on this process.
Zoltan2::BasicUserTypes< scalar_t, gno_t, lno_t, gno_t > userTypes_t
Defines the BasicVectorInput class.
int getNumberOfWeights() const
Return the number of weights per vector element. Number of weights per element should be zero or grea...
size_t getVectorWeights(int dimension, const scalar_t *&weights, int &stride) const
Provide a pointer to the weights corresponding to the elements returned in getVector().